diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index 26e82a3..8f9134e 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -41,7 +41,7 @@ import ( // maintainer list file in ProjectGit const ( MaintainershipFile = "_maitnainership.json" - MaintainershipDir = "maintaineirship" + MaintainershipDir = "maintaineirship" ) const ( diff --git a/workflow-pr/main_test.go b/workflow-pr/main_test.go index d6c188c..3bfcd3e 100644 --- a/workflow-pr/main_test.go +++ b/workflow-pr/main_test.go @@ -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)) } } diff --git a/workflow-pr/pr_processor_opened.go b/workflow-pr/pr_processor_opened.go index bcc3f79..c5a4441 100644 --- a/workflow-pr/pr_processor_opened.go +++ b/workflow-pr/pr_processor_opened.go @@ -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)) - common.PanicOnError(git.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch)) - updateOrCreatePRBranch(req, git, commitMsg, branchName) + err = git.GitExec(common.DefaultGitPrj, "fetch", "origin", branchName+":"+branchName) + if err != nil { + common.PanicOnError(git.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch)) + } 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), diff --git a/workflow-pr/pr_processor_sync.go b/workflow-pr/pr_processor_sync.go index 2d051e3..4db2d08 100644 --- a/workflow-pr/pr_processor_sync.go +++ b/workflow-pr/pr_processor_sync.go @@ -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 }