Skip to content

Commit 7284327

Browse files
guillep2kzeripath
andauthored
Prevent panic on merge to PR (go-gitea#10403) (go-gitea#10408)
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. Co-authored-by: zeripath <[email protected]>
1 parent 919f3f1 commit 7284327

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
@@ -231,6 +231,10 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) {
231231
}
232232
}()
233233

234+
if err := pr.LoadHeadRepo(); err != nil {
235+
log.Error("Unable to load head repository for PR[%d] Error: %v", pr.ID, err)
236+
return err
237+
}
234238
headRepoPath := pr.HeadRepo.RepoPath()
235239

236240
if err := git.Clone(headRepoPath, tmpBasePath, git.CloneRepoOptions{
@@ -247,6 +251,10 @@ func PushToBaseRepo(pr *models.PullRequest) (err error) {
247251
return fmt.Errorf("OpenRepository: %v", err)
248252
}
249253

254+
if err := pr.LoadBaseRepo(); err != nil {
255+
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
256+
return err
257+
}
250258
if err := gitRepo.AddRemote("base", pr.BaseRepo.RepoPath(), false); err != nil {
251259
return fmt.Errorf("tmpGitRepo.AddRemote: %v", err)
252260
}

0 commit comments

Comments
 (0)