forked from adamm/autogits
84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"src.opensuse.org/autogits/common"
|
|
)
|
|
|
|
type PullRequestOpened struct {
|
|
gitea common.Gitea
|
|
}
|
|
|
|
func (o *PullRequestOpened) CreateOrUpdatePrjGitPR(req *common.PullRequestWebhookEvent, git common.Git, config *common.AutogitConfig) error {
|
|
// create PrjGit branch for buidling the pull request
|
|
branchName := prGitBranchNameForPR(req)
|
|
// TODO: fix this for config.Organization
|
|
org, prj, _ := config.GetPrjGit()
|
|
prjGit, err := o.gitea.CreateRepositoryIfNotExist(git, org, prj)
|
|
common.PanicOnErrorWithMsg(err, "Error creating a prjgitrepo: "+err.Error())
|
|
|
|
remoteName, err := git.GitClone(common.DefaultGitPrj, config.Branch, prjGit.SSHURL)
|
|
common.PanicOnError(err)
|
|
|
|
commitMsg := fmt.Sprintf(`auto-created for %s
|
|
|
|
This commit was autocreated by %s
|
|
referencing
|
|
|
|
`+common.PrPattern,
|
|
req.Repository.Owner.Username,
|
|
req.Repository.Name,
|
|
GitAuthor,
|
|
req.Repository.Name,
|
|
req.Pull_Request.Number,
|
|
)
|
|
|
|
subList, err := git.GitSubmoduleList(common.DefaultGitPrj, "HEAD")
|
|
common.PanicOnError(err)
|
|
|
|
if id := subList[req.Repository.Name]; id != req.Pull_Request.Head.Sha {
|
|
updateSubmoduleInPR(req, git)
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg))
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", remoteName, "+HEAD:"+branchName))
|
|
}
|
|
|
|
_, err = o.gitea.CreatePullRequestIfNotExist(prjGit, branchName, prjGit.DefaultBranch,
|
|
fmt.Sprintf("Forwarded PR: %s", req.Repository.Name),
|
|
fmt.Sprintf(`This is a forwarded pull request by %s
|
|
referencing the following pull request:
|
|
|
|
`+common.PrPattern,
|
|
GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number),
|
|
)
|
|
|
|
return err
|
|
}
|
|
|
|
func (o *PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git common.Git, config *common.AutogitConfig) error {
|
|
// requests against project are not handled here
|
|
common.LogInfo("processing opened PR:", req.Pull_Request.Url)
|
|
if org, repo, _ := config.GetPrjGit(); req.Repository.Owner.Username != org || req.Repository.Name != repo {
|
|
if err := o.CreateOrUpdatePrjGitPR(req, git, config); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
prset, err := common.FetchPRSet(o.gitea, req.Repository.Owner.Username, req.Repository.Name, req.Number, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// request build review
|
|
PR, err := prset.GetPrjGitPR()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
common.LogDebug(" num of reviewers:", len(PR.RequestedReviewers))
|
|
maintainers, err := common.FetchProjectMaintainershipData(o.gitea, config.Organization, config.GitProjectName, config.Branch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return prset.AssignReviewers(o.gitea, maintainers)
|
|
}
|