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-09-13 13:41:40 +02:00
|
|
|
"flag"
|
2024-07-09 23:22:42 +02:00
|
|
|
"fmt"
|
2024-09-13 13:41:40 +02:00
|
|
|
"log"
|
2024-09-27 17:58:09 +02:00
|
|
|
"math/rand"
|
2024-07-09 23:22:42 +02:00
|
|
|
"path"
|
2024-09-13 13:41:40 +02:00
|
|
|
"slices"
|
2024-09-27 17:58:09 +02:00
|
|
|
"strings"
|
2024-09-13 13:41:40 +02:00
|
|
|
"time"
|
2024-07-09 23:22:42 +02:00
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
"src.opensuse.org/autogits/common"
|
|
|
|
)
|
|
|
|
|
2024-11-07 18:25:35 +01:00
|
|
|
//go:generate mockgen -source=main.go -destination=mock/main.go -typed
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
const (
|
2024-09-29 15:32:32 +02:00
|
|
|
AppName = "workflow-pr"
|
2024-09-13 13:41:40 +02:00
|
|
|
GitAuthor = "AutoGits - pr-review"
|
|
|
|
GitEmail = "adam+autogits-pr@zombino.com"
|
2024-07-07 21:08:41 +02:00
|
|
|
)
|
|
|
|
|
2024-09-13 13:41:40 +02:00
|
|
|
var configuredRepos map[string][]*common.AutogitConfig
|
2024-11-03 22:21:57 +01:00
|
|
|
var gitea common.Gitea
|
2024-09-13 13:41:40 +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-09-13 13:41:40 +02:00
|
|
|
}*/
|
2024-08-19 17:14:20 +02:00
|
|
|
|
2024-09-13 13:41:40 +02:00
|
|
|
func processPrjGitPullRequestSync(req *common.PullRequestWebhookEvent) error {
|
2024-07-11 16:45:49 +02:00
|
|
|
// req := h.Data.(*common.PullRequestAction)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-13 13:41:40 +02:00
|
|
|
func prGitBranchNameForPR(req *common.PullRequestWebhookEvent) string {
|
2024-07-15 19:19:34 +02:00
|
|
|
return fmt.Sprintf("PR_%s#%d", req.Repository.Name, req.Pull_Request.Number)
|
|
|
|
}
|
|
|
|
|
2024-09-13 13:41:40 +02:00
|
|
|
func updateOrCreatePRBranch(req *common.PullRequestWebhookEvent, git *common.GitHandler, commitMsg, branchName string) error {
|
|
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--checkout", "--depth", "1", req.Repository.Name))
|
|
|
|
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", "origin", req.Pull_Request.Head.Sha))
|
|
|
|
common.PanicOnError(git.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha))
|
|
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg))
|
2024-11-03 22:21:57 +01:00
|
|
|
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:" + branchName))
|
2024-09-13 13:41:40 +02:00
|
|
|
return nil
|
2024-07-15 19:19:34 +02:00
|
|
|
}
|
|
|
|
|
2024-11-07 18:25:35 +01:00
|
|
|
type PullRequestProcessor interface {
|
|
|
|
Process(req *common.PullRequestWebhookEvent, git *common.GitHandler, config *common.AutogitConfig) error
|
2024-07-10 17:20:23 +02:00
|
|
|
}
|
|
|
|
|
2024-11-07 18:25:35 +01:00
|
|
|
type RequestProcessor struct {
|
|
|
|
Opened, Synced, Closed PullRequestProcessor
|
2024-07-07 21:08:41 +02:00
|
|
|
}
|
2024-11-07 18:25:35 +01:00
|
|
|
|
|
|
|
func (w *RequestProcessor) ProcessFunc(request *common.Request) error {
|
2024-09-13 13:41:40 +02:00
|
|
|
req, ok := request.Data.(*common.PullRequestWebhookEvent)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("*** Invalid data format for PR processing.")
|
|
|
|
}
|
|
|
|
|
|
|
|
configs := configuredRepos[req.Repository.Owner.Username]
|
|
|
|
if len(configs) < 1 {
|
|
|
|
// ignoring pull request against unconfigured project (could be just regular sources?)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var config *common.AutogitConfig
|
|
|
|
for _, c := range configs {
|
2024-09-27 17:58:09 +02:00
|
|
|
if c.GitProjectName == req.Pull_Request.Base.Repo.Name ||
|
|
|
|
c.Branch == req.Pull_Request.Base.Ref {
|
|
|
|
|
2024-09-13 13:41:40 +02:00
|
|
|
config = c
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 17:58:09 +02:00
|
|
|
if config == nil {
|
|
|
|
return fmt.Errorf("Cannot find config for branch '%s'", req.Pull_Request.Base.Ref)
|
|
|
|
}
|
2024-09-13 13:41:40 +02:00
|
|
|
|
2024-09-29 15:32:32 +02:00
|
|
|
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
|
2024-09-13 13:41:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
|
|
|
|
}
|
2024-07-09 23:22:42 +02:00
|
|
|
|
|
|
|
switch req.Action {
|
2024-08-13 09:28:04 +02:00
|
|
|
case "opened", "reopened":
|
2024-11-07 18:25:35 +01:00
|
|
|
return w.Opened.Process(req, git, config)
|
2024-07-10 17:30:40 +02:00
|
|
|
case "synchronized":
|
2024-11-07 18:25:35 +01:00
|
|
|
return w.Synced.Process(req, git, config)
|
2024-07-11 16:45:49 +02:00
|
|
|
case "edited":
|
|
|
|
// not need to be handled??
|
|
|
|
return nil
|
|
|
|
case "closed":
|
2024-11-07 18:25:35 +01:00
|
|
|
return w.Closed.Process(req, git, config)
|
2024-07-09 23:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Unhandled pull request action: %s", req.Action)
|
|
|
|
}
|
|
|
|
|
2024-09-13 13:41:40 +02:00
|
|
|
var DebugMode bool
|
|
|
|
var checkOnStart bool
|
|
|
|
var checkInterval time.Duration
|
|
|
|
|
2024-11-07 18:25:35 +01:00
|
|
|
func verifyProjectState(processor *RequestProcessor, git *common.GitHandler, orgName string, config *common.AutogitConfig, configs []*common.AutogitConfig) error {
|
2024-09-27 17:58:09 +02:00
|
|
|
org := common.Organization{
|
|
|
|
Username: orgName,
|
|
|
|
}
|
|
|
|
repo, err := gitea.CreateRepositoryIfNotExist(git, org, config.GitProjectName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error fetching or creating '%s/%s' -- aborting verifyProjectState(). Err: %w", orgName, config.GitProjectName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
common.PanicOnError(git.GitExec("", "clone", "--depth", "1", repo.SSHURL, config.GitProjectName))
|
|
|
|
log.Println("getting submodule list")
|
|
|
|
submodules, err := git.GitSubmoduleList(config.GitProjectName, "HEAD")
|
|
|
|
|
|
|
|
nextSubmodule:
|
|
|
|
for sub, commitID := range submodules {
|
2024-09-29 23:11:51 +02:00
|
|
|
log.Println(" + checking", sub, commitID)
|
2024-09-27 17:58:09 +02:00
|
|
|
submoduleName := sub
|
|
|
|
if n := strings.LastIndex(sub, "/"); n != -1 {
|
|
|
|
submoduleName = sub[n+1:]
|
|
|
|
}
|
2024-09-30 15:09:45 +02:00
|
|
|
|
|
|
|
// check if open PR have PR against project
|
|
|
|
prs, err := gitea.GetRecentPullRequests(config.Organization, submoduleName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error fetching pull requests for %s/%s. Err: %w", config.Organization, submoduleName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if DebugMode {
|
2024-09-30 16:19:40 +02:00
|
|
|
log.Println(" - # of PRs to check:", len(prs))
|
2024-09-30 15:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, pr := range prs {
|
|
|
|
var event common.PullRequestWebhookEvent
|
|
|
|
|
|
|
|
event.Pull_Request = common.PullRequestFromModel(pr)
|
|
|
|
event.Action = string(pr.State)
|
|
|
|
event.Number = int(pr.Index)
|
|
|
|
event.Repository = common.RepositoryFromModel(pr.Base.Repo)
|
|
|
|
event.Sender = *common.UserFromModel(pr.User)
|
|
|
|
event.Requested_reviewer = nil
|
|
|
|
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error allocating GitHandler. Err: %w", err)
|
|
|
|
}
|
|
|
|
if !DebugMode {
|
|
|
|
defer git.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch pr.State {
|
|
|
|
case "open":
|
2024-11-07 18:25:35 +01:00
|
|
|
processor.Opened.Process(&event, git, config)
|
2024-09-30 15:09:45 +02:00
|
|
|
case "closed":
|
2024-11-07 18:25:35 +01:00
|
|
|
processor.Closed.Process(&event, git, config)
|
2024-09-30 15:09:45 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unhandled pull request state: '%s'. %s/%s/%d", pr.State, config.Organization, submoduleName, pr.Index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the commited changes are syned with branches
|
2024-09-27 17:58:09 +02:00
|
|
|
commits, err := gitea.GetRecentCommits(config.Organization, submoduleName, config.Branch, 10)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error fetching recent commits for %s/%s. Err: %w", config.Organization, submoduleName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, commit := range commits {
|
|
|
|
if commit.SHA == commitID {
|
|
|
|
if idx != 0 {
|
|
|
|
// commit in past ...
|
|
|
|
log.Println(" W -", submoduleName, " is behind the branch by", idx, "This should not happen in PR workflow alone")
|
|
|
|
}
|
|
|
|
continue nextSubmodule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// not found in past, check if we should advance the branch label ... pull the submodule
|
2024-09-29 23:11:51 +02:00
|
|
|
git.GitExecOrPanic(config.GitProjectName, "submodule", "update", "--init", "--filter", "blob:none", "--", sub)
|
|
|
|
subDir := path.Join(config.GitProjectName, sub)
|
2024-09-30 10:25:08 +02:00
|
|
|
newCommits := common.SplitStringNoEmpty(git.GitExecWithOutputOrPanic(subDir, "rev-list", "^origin/"+config.Branch, commitID), "\n")
|
|
|
|
|
|
|
|
if len(newCommits) >= 1 {
|
|
|
|
if DebugMode {
|
|
|
|
log.Println(" - updating branch", config.Branch, "to new head", commitID, " - len:", len(newCommits))
|
|
|
|
}
|
|
|
|
git.GitExecOrPanic(subDir, "checkout", "-B", config.Branch, commitID)
|
|
|
|
url := git.GitExecWithOutputOrPanic(subDir, "remote", "get-url", "origin", "--push")
|
|
|
|
sshUrl, err := common.TranslateHttpsToSshUrl(strings.TrimSpace(url))
|
2024-09-29 23:11:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot traslate HTTPS git URL to SSH_URL. %w", err)
|
|
|
|
}
|
2024-09-30 10:25:08 +02:00
|
|
|
git.GitExecOrPanic(subDir, "remote", "set-url", "origin", "--push", sshUrl)
|
|
|
|
git.GitExecOrPanic(subDir, "push", "origin", config.Branch)
|
2024-09-29 23:11:51 +02:00
|
|
|
}
|
2024-09-27 17:58:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// forward any package-gits referred by the project git, but don't go back
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-07 18:25:35 +01:00
|
|
|
func checkRepos(processor *RequestProcessor) {
|
2024-09-27 17:58:09 +02:00
|
|
|
for org, configs := range configuredRepos {
|
|
|
|
for _, config := range configs {
|
|
|
|
if checkInterval > 0 {
|
|
|
|
sleepInterval := checkInterval - checkInterval/2 + time.Duration(rand.Int63n(int64(checkInterval)))
|
|
|
|
log.Println(" - sleep interval", sleepInterval, "until next check")
|
|
|
|
time.Sleep(sleepInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(" ++ starting verification, org: `%s` config: `%s`\n", org, config.GitProjectName)
|
|
|
|
git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Faield to allocate GitHandler:", err)
|
|
|
|
return
|
|
|
|
}
|
2024-09-29 23:11:51 +02:00
|
|
|
if !DebugMode {
|
|
|
|
defer git.Close()
|
|
|
|
}
|
2024-11-07 18:25:35 +01:00
|
|
|
if err := verifyProjectState(processor, git, org, config, configs); err != nil {
|
2024-09-27 17:58:09 +02:00
|
|
|
log.Printf(" *** verification failed, org: `%s`, err: %#v\n", org, err)
|
|
|
|
}
|
|
|
|
log.Printf(" ++ verification complete, org: `%s` config: `%s`\n", org, config.GitProjectName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-07 18:25:35 +01:00
|
|
|
func consistencyCheckProcess(processor *RequestProcessor) {
|
2024-09-27 17:58:09 +02:00
|
|
|
if checkOnStart {
|
|
|
|
savedCheckInterval := checkInterval
|
|
|
|
checkInterval = 0
|
|
|
|
log.Println("== Startup consistency check begin...")
|
2024-11-07 18:25:35 +01:00
|
|
|
checkRepos(processor)
|
2024-09-27 17:58:09 +02:00
|
|
|
log.Println("== Startup consistency check done...")
|
|
|
|
checkInterval = savedCheckInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2024-11-07 18:25:35 +01:00
|
|
|
checkRepos(processor)
|
2024-09-27 17:58:09 +02:00
|
|
|
}
|
2024-09-13 13:41:40 +02:00
|
|
|
}
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
func main() {
|
2024-09-13 13:41:40 +02:00
|
|
|
if err := common.RequireGiteaSecretToken(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := common.RequireRabbitSecrets(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
workflowConfig := flag.String("config", "", "Repository and workflow definition file")
|
|
|
|
giteaHost := flag.String("gitea", "src.opensuse.org", "Gitea instance")
|
|
|
|
rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance")
|
|
|
|
flag.BoolVar(&DebugMode, "debug", false, "Extra debugging information")
|
|
|
|
flag.BoolVar(&checkOnStart, "check-on-start", false, "Check all repositories for consistency on start, without delays")
|
|
|
|
checkIntervalHours := flag.Float64("check-interval", 5, "Check interval (+-random delay) for repositories for consitency, in hours")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
checkInterval = time.Duration(*checkIntervalHours) * time.Hour
|
|
|
|
|
|
|
|
if len(*workflowConfig) == 0 {
|
|
|
|
log.Fatalln("No configuratio file specified. Aborting")
|
|
|
|
}
|
|
|
|
|
|
|
|
configs, err := common.ReadWorkflowConfigsFile(*workflowConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configuredRepos = make(map[string][]*common.AutogitConfig)
|
|
|
|
orgs := make([]string, 0, 1)
|
|
|
|
for _, c := range configs {
|
|
|
|
if slices.Contains(c.Workflows, "pr") {
|
|
|
|
if DebugMode {
|
|
|
|
log.Printf(" + adding org: '%s', branch: '%s', prjgit: '%s'\n", c.Organization, c.Branch, c.GitProjectName)
|
|
|
|
}
|
|
|
|
configs := configuredRepos[c.Organization]
|
|
|
|
if configs == nil {
|
|
|
|
configs = make([]*common.AutogitConfig, 0, 1)
|
|
|
|
}
|
|
|
|
configs = append(configs, c)
|
|
|
|
configuredRepos[c.Organization] = configs
|
|
|
|
|
|
|
|
orgs = append(orgs, c.Organization)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gitea = common.AllocateGiteaTransport(*giteaHost)
|
2024-11-07 18:25:35 +01:00
|
|
|
|
|
|
|
req := new(RequestProcessor)
|
|
|
|
|
|
|
|
req.Synced = &PullRequestSynced{}
|
|
|
|
req.Opened = &PullRequestOpened{}
|
|
|
|
req.Closed = &PullRequestClosed{}
|
|
|
|
|
|
|
|
go consistencyCheckProcess(req)
|
2024-09-13 13:41:40 +02:00
|
|
|
|
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.GitAuthor = GitAuthor
|
2024-09-13 13:41:40 +02:00
|
|
|
defs.RabbitURL = *rabbitUrl
|
2024-07-09 15:05:49 +02:00
|
|
|
|
2024-07-09 23:22:42 +02:00
|
|
|
defs.Handlers = make(map[string]common.RequestProcessor)
|
2024-11-07 18:25:35 +01:00
|
|
|
defs.Handlers[common.RequestType_PR] = req
|
|
|
|
defs.Handlers[common.RequestType_PRSync] = req
|
2024-07-07 21:08:41 +02:00
|
|
|
|
2024-09-13 13:41:40 +02:00
|
|
|
log.Fatal(common.ProcessRabbitMQEvents(defs, orgs))
|
2024-07-07 21:08:41 +02:00
|
|
|
}
|