autogits/workflow-pr/main.go

140 lines
4.4 KiB
Go
Raw Normal View History

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 (
"flag"
2024-07-09 23:22:42 +02:00
"fmt"
"log"
2024-07-09 23:22:42 +02:00
"path"
"slices"
"time"
2024-07-09 23:22:42 +02:00
2024-07-07 21:08:41 +02:00
"src.opensuse.org/autogits/common"
)
const (
2024-09-29 15:32:32 +02:00
AppName = "workflow-pr"
GitAuthor = "AutoGits - pr-review"
GitEmail = "adam+autogits-pr@zombino.com"
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-08-19 17:14:20 +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)
}
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-08 16:08:53 +01:00
common.PanicOnError(git.GitExec(common.DefaultGitPrj, "push", "origin", "+HEAD:"+branchName))
return nil
2024-07-15 19:19:34 +02:00
}
var DebugMode bool
2024-07-07 21:08:41 +02:00
func main() {
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")
2024-11-25 17:02:48 +01:00
checkOnStart := flag.Bool("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()
if len(*workflowConfig) == 0 {
log.Fatalln("No configuratio file specified. Aborting")
}
configs, err := common.ReadWorkflowConfigsFile(*workflowConfig)
if err != nil {
log.Fatal(err)
}
2024-11-08 16:08:53 +01:00
req := new(RequestProcessor)
req.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)
}
2024-11-08 16:08:53 +01:00
configs := req.configuredRepos[c.Organization]
if configs == nil {
configs = make([]*common.AutogitConfig, 0, 1)
}
configs = append(configs, c)
2024-11-08 16:08:53 +01:00
req.configuredRepos[c.Organization] = configs
orgs = append(orgs, c.Organization)
}
}
2024-11-25 17:02:48 +01:00
gitea := common.AllocateGiteaTransport(*giteaHost)
2024-11-14 18:02:11 +01:00
2024-11-08 16:08:53 +01:00
req.Synced = &PullRequestSynced{
2024-11-25 17:02:48 +01:00
gitea: gitea,
2024-11-08 16:08:53 +01:00
}
req.Opened = &PullRequestOpened{
2024-11-25 17:02:48 +01:00
gitea: gitea,
2024-11-08 16:08:53 +01:00
}
req.Closed = &PullRequestClosed{
2024-11-25 17:02:48 +01:00
gitea: gitea,
2024-11-08 16:08:53 +01:00
}
2024-11-07 18:25:35 +01:00
2024-11-25 17:02:48 +01:00
checker := CreateDefaultStateChecker(*checkOnStart, req, gitea, time.Duration(*checkIntervalHours)*time.Hour)
go checker.ConsistencyCheckProcess()
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
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
log.Fatal(common.ProcessRabbitMQEvents(defs, orgs))
2024-07-07 21:08:41 +02:00
}