workflow-pr: don't recreate branches on every check
Check if branch exists and if it matches the PR already created. If yes, then nothing needs to be updated.
This commit is contained in:
parent
fbaeddfcd8
commit
245181ad28
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@ -145,7 +146,9 @@ func TestUpdatePrBranch(t *testing.T) {
|
||||
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
|
||||
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
|
||||
|
||||
updateOrCreatePRBranch(req, git, "created commit", "testing")
|
||||
updateSubmoduleToPR(req, git)
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "created commit"))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:+testing"))
|
||||
git.GitExecOrPanic("prj", "reset", "--hard", "testing")
|
||||
rev := strings.TrimSpace(git.GitExecWithOutputOrPanic(filepath.Join(common.DefaultGitPrj, "testRepo"), "rev-list", "-1", "HEAD"))
|
||||
if rev != req.Pull_Request.Head.Sha {
|
||||
@ -181,7 +184,9 @@ func TestCreatePrBranch(t *testing.T) {
|
||||
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
|
||||
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
|
||||
|
||||
updateOrCreatePRBranch(req, git, "created commit", "testingCreated")
|
||||
updateSubmoduleToPR(req, git)
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", "created commit"))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:testingCreated"))
|
||||
|
||||
rev := strings.TrimSpace(git.GitExecWithOutputOrPanic(filepath.Join(common.DefaultGitPrj, "testRepo"), "rev-list", "-1", "HEAD"))
|
||||
if rev != req.Pull_Request.Head.Sha {
|
||||
@ -189,11 +194,11 @@ func TestCreatePrBranch(t *testing.T) {
|
||||
t.Error(buf.String())
|
||||
}
|
||||
|
||||
os.CopyFS("/tmp/test", os.DirFS(git.GitPath))
|
||||
git.GitExecOrPanic("prj", "reset", "--hard", "testingCreated")
|
||||
rev = strings.TrimSpace(git.GitExecWithOutputOrPanic("prj", "submodule", "status", "testRepo"))[1 : len(req.Pull_Request.Head.Sha)+1]
|
||||
if rev != req.Pull_Request.Head.Sha {
|
||||
t.Error("prj/testRepo not updated to", req.Pull_Request.Head.Sha, "but is at", rev)
|
||||
t.Error(buf.String())
|
||||
// os.CopyFS("/tmp/test", os.DirFS(git.GitPath))
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ func (o *PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git com
|
||||
This commit was autocreated by %s
|
||||
referencing
|
||||
|
||||
PullRequest: %s/%s#%d`,
|
||||
`+common.PrPattern,
|
||||
req.Repository.Owner.Username,
|
||||
req.Repository.Name,
|
||||
GitAuthor,
|
||||
@ -37,8 +37,20 @@ PullRequest: %s/%s#%d`,
|
||||
}
|
||||
|
||||
common.PanicOnError(git.GitExec("", "clone", "--depth", "1", prjGit.SSHURL, common.DefaultGitPrj))
|
||||
err = git.GitExec(common.DefaultGitPrj, "fetch", "origin", branchName+":"+branchName)
|
||||
if err != nil {
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch))
|
||||
updateOrCreatePRBranch(req, git, commitMsg, branchName)
|
||||
} else {
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "checkout", branchName))
|
||||
}
|
||||
subList, err := git.GitSubmoduleList(common.DefaultGitPrj, "HEAD")
|
||||
common.PanicOnError(err)
|
||||
|
||||
if id := subList[req.Repository.Name]; id != req.Pull_Request.Head.Sha {
|
||||
updateSubmoduleToPR(req, git)
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:"+branchName))
|
||||
}
|
||||
|
||||
PR, err := o.gitea.CreatePullRequestIfNotExist(prjGit, branchName, prjGit.DefaultBranch,
|
||||
fmt.Sprintf("Forwarded PR: %s", req.Repository.Name),
|
||||
|
@ -14,12 +14,10 @@ func prGitBranchNameForPR(req *common.PullRequestWebhookEvent) string {
|
||||
return fmt.Sprintf("PR_%s#%d", req.Repository.Name, req.Pull_Request.Number)
|
||||
}
|
||||
|
||||
func updateOrCreatePRBranch(req *common.PullRequestWebhookEvent, git common.Git, commitMsg, branchName string) {
|
||||
func updateSubmoduleToPR(req *common.PullRequestWebhookEvent, git common.Git) {
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--checkout", "--depth", "1", req.Repository.Name))
|
||||
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", "origin", req.Pull_Request.Head.Sha))
|
||||
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:"+branchName))
|
||||
}
|
||||
|
||||
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
||||
@ -78,6 +76,8 @@ Update to %s`, req.Pull_Request.Head.Sha)
|
||||
|
||||
// we need to update prjgit PR with the new head hash
|
||||
branchName := prGitBranchNameForPR(req)
|
||||
updateOrCreatePRBranch(req, git, commitMsg, branchName)
|
||||
updateSubmoduleToPR(req, git)
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg))
|
||||
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:"+branchName))
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user