83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
)
|
|
|
|
const (
|
|
ListenAddr = "[::1]:8001"
|
|
|
|
GitAuthor = "GiteaBot - AutoStaging"
|
|
PrReview = "pr-review"
|
|
|
|
)
|
|
|
|
func processPullRequestCreated(h *common.RequestHandler) error {
|
|
req := h.Data.(common.PullRequestAction)
|
|
|
|
// requests against project are not handled here
|
|
if req.Repository.Name == common.DefaultGitPrj {
|
|
return nil
|
|
}
|
|
|
|
// create PrjGit branch for buidling the pull request
|
|
branchName := fmt.Sprintf("PR_%s#%d", req.PullRequest.Repository.Name, req.PullRequest.Number)
|
|
commitMsg := fmt.Sprintf(`auto-created for %s
|
|
|
|
This commit was autocreated by %s
|
|
referencing
|
|
|
|
PullRequest: %s#%d`, req.PullRequest.Repository.Name, GitAuthor, req.PullRequest.Repository.Name, req.PullRequest.Number)
|
|
|
|
prjGit := h.CreateRepositoryIfNotExist(*req.Repository.Owner, common.DefaultGitPrj)
|
|
if h.HasError() {
|
|
return h.Error
|
|
}
|
|
|
|
h.GitExec("", "clone", "--depth", "1", prjGit.SSHURL, common.DefaultGitPrj)
|
|
h.GitExec("", "checkout", "-B", branchName, prjGit.DefaultBranch)
|
|
h.GitExec(common.DefaultGitPrj, "submodule", "update", "--checkout", "--depth", "1", req.Repository.Name)
|
|
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", req.PullRequest.Head.Sha)
|
|
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.PullRequest.Head.Sha)
|
|
h.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg)
|
|
h.GitExec(common.DefaultGitPrj, "push", branchName)
|
|
|
|
PR := h.CreatePullRequest(prjGit, branchName, prjGit.DefaultBranch)
|
|
if h.HasError() {
|
|
return h.Error
|
|
}
|
|
|
|
// request build review
|
|
h.RequestReviews(PR, common.Bot_BuildReview)
|
|
|
|
return nil
|
|
}
|
|
|
|
func processPullRequest(h *common.RequestHandler) error {
|
|
req := h.Data.(common.PullRequestAction)
|
|
|
|
switch req.Action {
|
|
case "created":
|
|
return processPullRequestCreated(h)
|
|
}
|
|
|
|
return fmt.Errorf("Unhandled pull request action: %s", req.Action)
|
|
}
|
|
|
|
func main() {
|
|
var defs common.ListenDefinitions
|
|
|
|
defs.Url = PrReview
|
|
defs.GitAuthor = GitAuthor
|
|
|
|
defs.Handlers = make(map[string]common.RequestProcessor)
|
|
defs.Handlers[common.RequestType_PR] = processPullRequest
|
|
|
|
common.RequireGiteaSecretToken()
|
|
common.RequireObsSecretToken()
|
|
common.StartServerWithAddress(defs, ListenAddr)
|
|
}
|