workflow-pr: refactor

This commit is contained in:
Adam Majer 2024-11-29 12:49:11 +01:00
parent 60c0a118c9
commit 7342dc42e9
4 changed files with 18 additions and 24 deletions

View File

@ -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() {

View File

@ -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 {

View File

@ -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),

View File

@ -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
}