pr: sanity check

make sure that the checked out PR matches what Gitea is sending
us, otherwise pause for a few seconds and retry.
This commit is contained in:
2025-10-11 18:10:15 +02:00
parent 0c47ca4d32
commit ca7966f3e0

View File

@@ -4,11 +4,13 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"path" "path"
"runtime/debug" "runtime/debug"
"slices" "slices"
"strings" "strings"
"time"
"github.com/opentracing/opentracing-go/log" "github.com/opentracing/opentracing-go/log"
"src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common"
@@ -266,6 +268,7 @@ func (pr *PRProcessor) RebaseAndSkipSubmoduleCommits(prset *common.PRSet, branch
return nil return nil
} }
var updatePrjGitError_requeue error = errors.New("Commits do not match. Requeing after 5 seconds.")
func (pr *PRProcessor) UpdatePrjGitPR(prset *common.PRSet) error { func (pr *PRProcessor) UpdatePrjGitPR(prset *common.PRSet) error {
_, _, PrjGitBranch := prset.Config.GetPrjGit() _, _, PrjGitBranch := prset.Config.GetPrjGit()
PrjGitPR, err := prset.GetPrjGitPR() PrjGitPR, err := prset.GetPrjGitPR()
@@ -328,6 +331,10 @@ func (pr *PRProcessor) UpdatePrjGitPR(prset *common.PRSet) error {
} }
if !common.IsDryRun { if !common.IsDryRun {
if headCommit != PrjGitPR.PR.Head.Sha {
common.LogError("HeadCommit:", headCommit, "is not what's expected from the PR:", PrjGitPR.PR.Head.Ref, " Requeing.")
return updatePrjGitError_requeue
}
if headCommit != newHeadCommit { if headCommit != newHeadCommit {
params := []string{"push", PrjGitPR.RemoteName, "+HEAD:" + prjGitPRbranch} params := []string{"push", PrjGitPR.RemoteName, "+HEAD:" + prjGitPRbranch}
if forcePush { if forcePush {
@@ -565,6 +572,7 @@ func (pr *PRProcessor) Process(req *models.PullRequest) error {
type RequestProcessor struct { type RequestProcessor struct {
configuredRepos map[string][]*common.AutogitConfig configuredRepos map[string][]*common.AutogitConfig
recursive int
} }
func ProcesPullRequest(pr *models.PullRequest, configs []*common.AutogitConfig) error { func ProcesPullRequest(pr *models.PullRequest, configs []*common.AutogitConfig) error {
@@ -583,16 +591,22 @@ func ProcesPullRequest(pr *models.PullRequest, configs []*common.AutogitConfig)
return PRProcessor.Process(pr) return PRProcessor.Process(pr)
} }
func (w *RequestProcessor) ProcessFunc(request *common.Request) error { func (w *RequestProcessor) ProcessFunc(request *common.Request) (err error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
common.LogInfo("panic cought --- recovered") common.LogInfo("panic cought --- recovered")
common.LogError(string(debug.Stack())) common.LogError(string(debug.Stack()))
} }
w.recursive--
}() }()
w.recursive++
if w.recursive > 3 {
common.LogError("Recursion limit reached... something is wrong with this PR?")
return nil
}
var pr *models.PullRequest var pr *models.PullRequest
var err error
if req, ok := request.Data.(*common.PullRequestWebhookEvent); ok { if req, ok := request.Data.(*common.PullRequestWebhookEvent); ok {
pr, err = Gitea.GetPullRequest(req.Pull_Request.Base.Repo.Owner.Username, req.Pull_Request.Base.Repo.Name, req.Pull_Request.Number) pr, err = Gitea.GetPullRequest(req.Pull_Request.Base.Repo.Owner.Username, req.Pull_Request.Base.Repo.Name, req.Pull_Request.Number)
if err != nil { if err != nil {
@@ -614,5 +628,9 @@ func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
if !ok { if !ok {
common.LogError("*** Cannot find config for org:", pr.Base.Repo.Owner.UserName) common.LogError("*** Cannot find config for org:", pr.Base.Repo.Owner.UserName)
} }
return ProcesPullRequest(pr, configs) if err = ProcesPullRequest(pr, configs); err == updatePrjGitError_requeue {
time.Sleep(time.Second * 5)
return w.ProcessFunc(request)
}
return err
} }