2024-11-07 18:25:35 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PullRequestOpened 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 *PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error {
|
2024-11-07 18:25:35 +01:00
|
|
|
// requests against project are not handled here
|
|
|
|
if req.Repository.Name == config.GitProjectName {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// create PrjGit branch for buidling the pull request
|
|
|
|
branchName := prGitBranchNameForPR(req)
|
|
|
|
commitMsg := fmt.Sprintf(`auto-created for %s
|
|
|
|
|
|
|
|
This commit was autocreated by %s
|
|
|
|
referencing
|
|
|
|
|
|
|
|
PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
|
|
|
|
req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number)
|
|
|
|
|
2024-11-08 16:08:53 +01:00
|
|
|
prjGit, err := o.gitea.CreateRepositoryIfNotExist(git, *req.Repository.Owner, config.GitProjectName)
|
2024-11-07 18:25:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
2024-11-08 16:08:53 +01:00
|
|
|
PR, err := o.gitea.CreatePullRequestIfNotExist(prjGit, branchName, prjGit.DefaultBranch,
|
2024-11-07 18:25:35 +01:00
|
|
|
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),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// request build review
|
|
|
|
for _, reviewer := range config.Reviewers {
|
2024-11-08 16:08:53 +01:00
|
|
|
_, err := o.gitea.RequestReviews(PR, reviewer)
|
2024-11-07 18:25:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create reviewer '%s' for request: %s/%s/%d Err: %w", reviewer, PR.Base.Repo.Owner.UserName, PR.Base.Repo.Name, PR.Index, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|