autogits/workflow-pr/pr_sync.go

57 lines
1.6 KiB
Go
Raw Normal View History

2024-11-07 18:25:35 +01:00
package main
import (
"fmt"
"log"
"src.opensuse.org/autogits/common"
)
2024-11-08 15:05:09 +01:00
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
// req := h.Data.(*common.PullRequestAction)
return nil
}
2024-11-07 18:25:35 +01:00
type PullRequestSynced struct {
2024-11-08 16:08:53 +01:00
gitea common.Gitea
2024-11-07 18:25:35 +01:00
}
2024-11-08 16:08:53 +01:00
func (o *PullRequestSynced) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
2024-11-07 18:25:35 +01:00
if req.Repository.Name == config.GitProjectName {
return processPrjGitPullRequestSync(req)
}
// need to verify that submodule in the PR for prjgit
// is still pointing to the HEAD of the PR
2024-11-08 16:08:53 +01:00
prjPr, err := o.gitea.GetAssociatedPrjGitPR(req)
2024-11-07 18:25:35 +01:00
if err != nil {
return fmt.Errorf("Cannot get associated PrjGit PR in processPullRequestSync. Err: %w", err)
}
common.PanicOnError(git.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj))
commitId, ok := git.GitSubmoduleCommitId(common.DefaultGitPrj, req.Repository.Name, prjPr.Head.Sha)
if !ok {
return fmt.Errorf("Cannot fetch submodule commit id in prjgit for '%s'", req.Repository.Name)
}
// nothing changed, still in sync
if commitId == req.Pull_Request.Head.Sha {
log.Println("commitID already match - nothing to do")
return nil
}
log.Printf("different ids: '%s' vs. '%s'\n", req.Pull_Request.Head.Sha, commitId)
commitMsg := fmt.Sprintf(`Sync PR
Update to %s`, req.Pull_Request.Head.Sha)
log.Println("will create new commit msg:", commitMsg)
// we need to update prjgit PR with the new head hash
branchName := prGitBranchNameForPR(req)
return updateOrCreatePRBranch(req, git, commitMsg, branchName)
}