2024-07-07 21:08:41 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-09 23:22:42 +02:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
"src.opensuse.org/autogits/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-07-09 23:22:42 +02:00
|
|
|
ListenAddr = "[::1]:8001"
|
2024-07-07 21:08:41 +02:00
|
|
|
|
|
|
|
GitAuthor = "GiteaBot - AutoStaging"
|
2024-07-09 12:06:24 +02:00
|
|
|
PrReview = "pr-review"
|
2024-07-09 23:22:42 +02:00
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
)
|
|
|
|
|
2024-07-09 23:22:42 +02:00
|
|
|
func processPullRequestCreated(h *common.RequestHandler) error {
|
2024-07-09 12:06:24 +02:00
|
|
|
req := h.Data.(common.PullRequestAction)
|
2024-07-09 23:22:42 +02:00
|
|
|
|
2024-07-09 12:06:24 +02:00
|
|
|
// requests against project are not handled here
|
|
|
|
if req.Repository.Name == common.DefaultGitPrj {
|
|
|
|
return nil
|
2024-07-07 21:08:41 +02:00
|
|
|
}
|
|
|
|
|
2024-07-09 23:22:42 +02:00
|
|
|
// create PrjGit branch for buidling the pull request
|
2024-07-10 00:04:54 +02:00
|
|
|
branchName := fmt.Sprintf("PR_%s#%d", req.Pull_Request.Repository.Name, req.Pull_Request.Number)
|
2024-07-09 23:22:42 +02:00
|
|
|
commitMsg := fmt.Sprintf(`auto-created for %s
|
|
|
|
|
|
|
|
This commit was autocreated by %s
|
|
|
|
referencing
|
|
|
|
|
2024-07-10 00:04:54 +02:00
|
|
|
PullRequest: %s#%d`, req.Pull_Request.Repository.Name, GitAuthor, req.Pull_Request.Repository.Name, req.Pull_Request.Number)
|
2024-07-09 23:22:42 +02:00
|
|
|
|
|
|
|
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)
|
2024-07-10 00:04:54 +02:00
|
|
|
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", req.Pull_Request.Head.Sha)
|
|
|
|
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha)
|
2024-07-09 23:22:42 +02:00
|
|
|
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)
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-09 23:22:42 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
func main() {
|
2024-07-09 12:06:24 +02:00
|
|
|
var defs common.ListenDefinitions
|
2024-07-07 21:08:41 +02:00
|
|
|
|
2024-07-09 12:06:24 +02:00
|
|
|
defs.Url = PrReview
|
|
|
|
defs.GitAuthor = GitAuthor
|
2024-07-09 15:05:49 +02:00
|
|
|
|
2024-07-09 23:22:42 +02:00
|
|
|
defs.Handlers = make(map[string]common.RequestProcessor)
|
2024-07-09 12:06:24 +02:00
|
|
|
defs.Handlers[common.RequestType_PR] = processPullRequest
|
2024-07-07 21:08:41 +02:00
|
|
|
|
2024-07-09 12:06:24 +02:00
|
|
|
common.RequireGiteaSecretToken()
|
|
|
|
common.RequireObsSecretToken()
|
2024-07-09 23:22:42 +02:00
|
|
|
common.StartServerWithAddress(defs, ListenAddr)
|
2024-07-07 21:08:41 +02:00
|
|
|
}
|