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-10 10:29:02 +02:00
|
|
|
func processPullRequestOpened(h *common.RequestHandler) error {
|
2024-07-10 10:23:51 +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 10:19:52 +02:00
|
|
|
branchName := fmt.Sprintf("PR_%s#%d", req.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 10:19:52 +02:00
|
|
|
PullRequest: %s#%d`, req.Repository.Name, GitAuthor, req.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)
|
2024-07-10 10:30:44 +02:00
|
|
|
h.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch)
|
2024-07-10 10:33:27 +02:00
|
|
|
h.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--checkout", "--depth", "1", req.Repository.Name)
|
|
|
|
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", "origin", req.Pull_Request.Head.Sha)
|
2024-07-10 00:04:54 +02:00
|
|
|
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 {
|
2024-07-10 10:27:00 +02:00
|
|
|
req := h.Data.(*common.PullRequestAction)
|
2024-07-09 23:22:42 +02:00
|
|
|
|
|
|
|
switch req.Action {
|
2024-07-10 10:29:02 +02:00
|
|
|
case "opened":
|
|
|
|
return processPullRequestOpened(h)
|
2024-07-09 23:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|