autogits/workflow-pr/pr_sync.go

57 lines
1.6 KiB
Go

package main
import (
"fmt"
"log"
"src.opensuse.org/autogits/common"
)
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
// req := h.Data.(*common.PullRequestAction)
return nil
}
type PullRequestSynced struct {
gitea common.Gitea
}
func (o *PullRequestSynced) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
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
prjPr, err := o.gitea.GetAssociatedPrjGitPR(req)
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)
}