Skip to content

Commit cadec9b

Browse files
authored
Prevent panic on merge to PR (#10403)
If you attempt to merge to a branch which on a PR there will be a nil pointer error in the pull request checker. This panic is uncaught and will bring down the gitea server. This PR adds protection to prevent this.
1 parent cfcd8e4 commit cadec9b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

integrations/git_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,17 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
351351
pr, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, "protected", "unprotected")(t)
352352
assert.NoError(t, err)
353353
})
354+
t.Run("GenerateCommit", func(t *testing.T) {
355+
_, err := generateCommitWithNewData(littleSize, dstPath, "[email protected]", "User Two", "branch-data-file-")
356+
assert.NoError(t, err)
357+
})
358+
t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "protected:unprotected-2"))
359+
var pr2 api.PullRequest
360+
t.Run("CreatePullRequest", func(t *testing.T) {
361+
pr2, err = doAPICreatePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, "unprotected", "unprotected-2")(t)
362+
assert.NoError(t, err)
363+
})
364+
t.Run("MergePR2", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr2.Index))
354365
t.Run("MergePR", doAPIMergePullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr.Index))
355366
t.Run("PullProtected", doGitPull(dstPath, "origin", "protected"))
356367
t.Run("ProtectProtectedBranchWhitelist", doProtectBranch(ctx, "protected", baseCtx.Username))

services/pull/pull.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) {
324324
}
325325
}()
326326

327+
if err := pr.LoadHeadRepo(); err != nil {
328+
log.Error("Unable to load head repository for PR[%d] Error: %v", pr.ID, err)
329+
return err
330+
}
327331
headRepoPath := pr.HeadRepo.RepoPath()
328332

329333
if err := git.Clone(headRepoPath, tmpBasePath, git.CloneRepoOptions{
@@ -340,6 +344,10 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) {
340344
return fmt.Errorf("OpenRepository: %v", err)
341345
}
342346

347+
if err := pr.LoadBaseRepo(); err != nil {
348+
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
349+
return err
350+
}
343351
if err := gitRepo.AddRemote("base", pr.BaseRepo.RepoPath(), false); err != nil {
344352
return fmt.Errorf("tmpGitRepo.AddRemote: %v", err)
345353
}

0 commit comments

Comments
 (0)