From 7342dc42e9b99f38c3e19986f77da608b7e5e52cd1b1317fe62653b4ed737938 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Fri, 29 Nov 2024 12:49:11 +0100 Subject: [PATCH] workflow-pr: refactor --- workflow-pr/main.go | 15 --------------- workflow-pr/main_test.go | 9 ++------- workflow-pr/pr_opened.go | 2 +- workflow-pr/pr_sync.go | 16 +++++++++++++++- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/workflow-pr/main.go b/workflow-pr/main.go index f7de7b1..3115a80 100644 --- a/workflow-pr/main.go +++ b/workflow-pr/main.go @@ -20,9 +20,7 @@ package main import ( "flag" - "fmt" "log" - "path" "slices" "time" @@ -51,19 +49,6 @@ func fetchPrGit(h *common.RequestHandler, pr *models.PullRequest) error { return h.Error }*/ -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.GitHandler, commitMsg, branchName string) error { - 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)) - return nil -} - var DebugMode bool func main() { diff --git a/workflow-pr/main_test.go b/workflow-pr/main_test.go index 3755622..0e6476e 100644 --- a/workflow-pr/main_test.go +++ b/workflow-pr/main_test.go @@ -145,10 +145,7 @@ func TestUpdatePrBranch(t *testing.T) { req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1]) req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0]) - if err := updateOrCreatePRBranch(req, git, "created commit", "testing"); err != nil { - t.Error(err) - } - + updateOrCreatePRBranch(req, git, "created commit", "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 { @@ -184,9 +181,7 @@ func TestCreatePrBranch(t *testing.T) { req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1]) req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0]) - if err := updateOrCreatePRBranch(req, git, "created commit", "testingCreated"); err != nil { - t.Error(err) - } + updateOrCreatePRBranch(req, git, "created commit", "testingCreated") rev := strings.TrimSpace(git.GitExecWithOutputOrPanic(filepath.Join(common.DefaultGitPrj, "testRepo"), "rev-list", "-1", "HEAD")) if rev != req.Pull_Request.Head.Sha { diff --git a/workflow-pr/pr_opened.go b/workflow-pr/pr_opened.go index 3d0c8c9..b953ba4 100644 --- a/workflow-pr/pr_opened.go +++ b/workflow-pr/pr_opened.go @@ -38,7 +38,7 @@ 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)) - common.PanicOnError(updateOrCreatePRBranch(req, git, commitMsg, branchName)) + updateOrCreatePRBranch(req, git, commitMsg, branchName) PR, err := o.gitea.CreatePullRequestIfNotExist(prjGit, branchName, prjGit.DefaultBranch, fmt.Sprintf("Forwarded PR: %s", req.Repository.Name), diff --git a/workflow-pr/pr_sync.go b/workflow-pr/pr_sync.go index 1c7922d..0636fb0 100644 --- a/workflow-pr/pr_sync.go +++ b/workflow-pr/pr_sync.go @@ -3,10 +3,23 @@ package main import ( "fmt" "log" + "path" "src.opensuse.org/autogits/common" ) +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.GitHandler, commitMsg, branchName string) { + 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 { // req := h.Data.(*common.PullRequestAction) @@ -52,5 +65,6 @@ Update to %s`, req.Pull_Request.Head.Sha) // we need to update prjgit PR with the new head hash branchName := prGitBranchNameForPR(req) - return updateOrCreatePRBranch(req, git, commitMsg, branchName) + updateOrCreatePRBranch(req, git, commitMsg, branchName) + return nil }