2024-07-07 21:08:41 +02:00
|
|
|
package main
|
|
|
|
|
2024-09-10 18:24:41 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Autogits.
|
|
|
|
*
|
|
|
|
* Copyright © 2024 SUSE LLC
|
|
|
|
*
|
|
|
|
* Autogits is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 2 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* Foobar. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
import (
|
2024-07-09 23:22:42 +02:00
|
|
|
"fmt"
|
2024-08-19 17:14:20 +02:00
|
|
|
"os"
|
2024-07-09 23:22:42 +02:00
|
|
|
"path"
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
"src.opensuse.org/autogits/common"
|
2024-08-19 17:14:20 +02:00
|
|
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
2024-07-07 21:08:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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-07 21:08:41 +02:00
|
|
|
)
|
|
|
|
|
2024-08-19 17:14:20 +02:00
|
|
|
func fetchPrGit(h *common.RequestHandler, pr *models.PullRequest) error {
|
|
|
|
// clone PR head and base and return path
|
|
|
|
if h.HasError() {
|
|
|
|
return h.Error
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(path.Join(h.GitPath, pr.Head.Sha)); os.IsNotExist(err) {
|
|
|
|
h.GitExec("", "clone", "--depth", "1", pr.Head.Repo.CloneURL, pr.Head.Sha)
|
|
|
|
h.GitExec(pr.Head.Sha, "fetch", "--depth", "1", "origin", pr.Head.Sha, pr.Base.Sha)
|
|
|
|
} else if err != nil {
|
|
|
|
h.Error = err
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.Error
|
|
|
|
}
|
|
|
|
|
2024-07-11 16:45:49 +02:00
|
|
|
func processPullRequestClosed(h *common.RequestHandler) error {
|
2024-08-19 17:14:20 +02:00
|
|
|
// this needs to be moved to pull merger
|
|
|
|
return nil
|
2024-09-10 18:24:41 +02:00
|
|
|
/*
|
|
|
|
req := h.Data.(*common.PullRequestAction)
|
2024-08-19 17:14:20 +02:00
|
|
|
|
2024-09-10 18:24:41 +02:00
|
|
|
if req.Repository.Name != common.DefaultGitPrj {
|
|
|
|
// we only handle project git PR updates here
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-19 17:14:20 +02:00
|
|
|
|
2024-09-10 18:24:41 +02:00
|
|
|
if err := fetchPrGit(h, req.Pull_Request); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
headSubmodules := h.GitSubmoduleList(dir, pr.Head.Sha)
|
|
|
|
baseSubmodules := h.GitSubmoduleList(dir, pr.Base.Sha)
|
|
|
|
return nil
|
2024-08-19 17:14:20 +02:00
|
|
|
*/
|
2024-07-11 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func processPrjGitPullRequestSync(h *common.RequestHandler) error {
|
|
|
|
// req := h.Data.(*common.PullRequestAction)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-15 19:19:34 +02:00
|
|
|
func prGitBranchNameForPR(req *common.PullRequestAction) string {
|
|
|
|
return fmt.Sprintf("PR_%s#%d", req.Repository.Name, req.Pull_Request.Number)
|
|
|
|
}
|
|
|
|
|
2024-07-16 15:19:14 +02:00
|
|
|
func updateOrCreatePRBranch(h *common.RequestHandler, commitMsg, branchName string) {
|
2024-07-15 19:19:34 +02:00
|
|
|
req := h.Data.(*common.PullRequestAction)
|
|
|
|
|
|
|
|
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)
|
|
|
|
h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha)
|
|
|
|
h.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg)
|
|
|
|
h.GitExec(common.DefaultGitPrj, "push", "-f", "origin", branchName)
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:20:23 +02:00
|
|
|
func processPullRequestSync(h *common.RequestHandler) error {
|
|
|
|
req := h.Data.(*common.PullRequestAction)
|
|
|
|
|
2024-07-11 16:45:49 +02:00
|
|
|
if req.Repository.Name == common.DefaultGitPrj {
|
|
|
|
return processPrjGitPullRequestSync(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to verify that submodule in the PR for prjgit
|
|
|
|
// is still pointing to the HEAD of the PR
|
|
|
|
prjPr := h.GetAssociatedPrjGitPR(req)
|
|
|
|
|
2024-07-15 20:48:14 +02:00
|
|
|
if h.HasError() {
|
|
|
|
h.LogError("%v", h.Error)
|
|
|
|
return h.Error
|
|
|
|
}
|
|
|
|
h.Log("associated pr: %v", prjPr)
|
|
|
|
|
2024-07-11 16:45:49 +02:00
|
|
|
h.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj)
|
2024-07-16 15:04:53 +02:00
|
|
|
commitId, ok := h.GitSubmoduleCommitId(common.DefaultGitPrj, req.Repository.Name, prjPr.Head.Sha)
|
2024-07-10 17:29:36 +02:00
|
|
|
|
2024-07-15 19:19:34 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Cannot fetch submodule commit id in prjgit for '%s'", req.Repository.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nothing changed, still in sync
|
2024-07-16 15:30:58 +02:00
|
|
|
if commitId == req.Pull_Request.Head.Sha {
|
|
|
|
h.Log("commitID already match - nothing to do")
|
2024-07-15 19:19:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-16 15:30:58 +02:00
|
|
|
h.Log("different ids: '%s' vs. '%s'", req.Pull_Request.Head.Sha, commitId)
|
2024-07-16 15:29:08 +02:00
|
|
|
|
2024-07-15 19:19:34 +02:00
|
|
|
commitMsg := fmt.Sprintf(`Sync PR
|
|
|
|
|
|
|
|
Update to %s`, req.Pull_Request.Head.Sha)
|
|
|
|
|
2024-07-16 15:04:53 +02:00
|
|
|
h.Log("will create new commit msg: %s", commitMsg)
|
|
|
|
h.Log("error? %v", h.Error)
|
|
|
|
|
2024-07-15 19:19:34 +02:00
|
|
|
// we need to update prjgit PR with the new head hash
|
|
|
|
branchName := prGitBranchNameForPR(req)
|
2024-07-16 15:19:14 +02:00
|
|
|
updateOrCreatePRBranch(h, commitMsg, branchName)
|
2024-07-15 19:19:34 +02:00
|
|
|
|
|
|
|
return h.Error
|
2024-07-10 17:20:23 +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-15 19:19:34 +02:00
|
|
|
branchName := prGitBranchNameForPR(req)
|
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 17:29:36 +02:00
|
|
|
PullRequest: %s/%s#%d`, req.Repository.Owner.Username,
|
2024-07-10 17:20:23 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-16 15:19:14 +02:00
|
|
|
h.GitExec("", "clone", "--depth", "1", prjGit.SSHURL, common.DefaultGitPrj)
|
|
|
|
h.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch)
|
|
|
|
updateOrCreatePRBranch(h, commitMsg, branchName)
|
2024-07-09 23:22:42 +02:00
|
|
|
|
2024-07-10 11:41:34 +02:00
|
|
|
PR := h.CreatePullRequest(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:
|
|
|
|
|
2024-07-11 16:45:49 +02:00
|
|
|
`+common.PrPattern,
|
|
|
|
GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number),
|
2024-07-10 11:41:34 +02:00
|
|
|
)
|
|
|
|
|
2024-07-09 23:22:42 +02:00
|
|
|
if h.HasError() {
|
|
|
|
return h.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// request build review
|
|
|
|
h.RequestReviews(PR, common.Bot_BuildReview)
|
|
|
|
|
2024-07-15 19:19:34 +02:00
|
|
|
return h.Error
|
2024-07-07 21:08:41 +02:00
|
|
|
}
|
|
|
|
|
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-08-13 09:28:04 +02:00
|
|
|
case "opened", "reopened":
|
2024-07-10 10:29:02 +02:00
|
|
|
return processPullRequestOpened(h)
|
2024-07-10 17:30:40 +02:00
|
|
|
case "synchronized":
|
2024-07-10 17:20:23 +02:00
|
|
|
return processPullRequestSync(h)
|
2024-07-11 16:45:49 +02:00
|
|
|
case "edited":
|
|
|
|
// not need to be handled??
|
|
|
|
return nil
|
|
|
|
case "closed":
|
|
|
|
return processPullRequestClosed(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-10 17:29:36 +02:00
|
|
|
defs.Handlers[common.RequestType_PR_sync] = 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
|
|
|
}
|