forked from adamm/autogits
150 lines
4.5 KiB
Go
150 lines
4.5 KiB
Go
package main
|
|
|
|
/*
|
|
* 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/>.
|
|
*/
|
|
|
|
import (
|
|
"flag"
|
|
"net/url"
|
|
"os"
|
|
"slices"
|
|
"time"
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
|
)
|
|
|
|
const (
|
|
AppName = "workflow-pr"
|
|
GitAuthor = "AutoGits - pr-review"
|
|
GitEmail = "adam+autogits-pr@zombino.com"
|
|
)
|
|
|
|
var ListPROnly bool
|
|
var PRID int64
|
|
var CurrentUser *models.User
|
|
var GitHandler common.GitHandlerGenerator
|
|
|
|
func main() {
|
|
workflowConfig := flag.String("config", "", "Repository and workflow definition file")
|
|
giteaUrl := flag.String("gitea-url", "https://src.opensuse.org", "Gitea instance")
|
|
rabbitUrl := flag.String("url", "amqps://rabbit.opensuse.org", "URL for RabbitMQ instance")
|
|
debugMode := flag.Bool("debug", false, "Extra debugging information")
|
|
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.BoolVar(&ListPROnly, "list-prs-only", false, "Only lists PRs without acting on them")
|
|
flag.Int64Var(&PRID, "id", -1, "Process only the specific ID and ignore the rest. Use for debugging")
|
|
basePath := flag.String("repo-path", "", "Repository path. Default is temporary directory")
|
|
flag.Parse()
|
|
|
|
common.SetLoggingLevel(common.LogLevelInfo)
|
|
if *debugMode {
|
|
common.SetLoggingLevel(common.LogLevelDebug)
|
|
}
|
|
|
|
if err := common.RequireGiteaSecretToken(); err != nil {
|
|
common.LogError("No Gitea secrets:", err)
|
|
return
|
|
}
|
|
if err := common.RequireRabbitSecrets(); err != nil {
|
|
common.LogError("No RabbitMQ secret:", err)
|
|
return
|
|
}
|
|
|
|
common.LogDebug("Parsing config:", *workflowConfig)
|
|
if len(*workflowConfig) == 0 {
|
|
common.LogError("No configuratio file specified. Aborting")
|
|
return
|
|
}
|
|
|
|
gitea := common.AllocateGiteaTransport(*giteaUrl)
|
|
config, err := common.ReadConfigFile(*workflowConfig)
|
|
if err != nil {
|
|
common.LogError("Cannot read config files:", err)
|
|
return
|
|
}
|
|
|
|
configs, err := common.ResolveWorkflowConfigs(gitea, config)
|
|
if err != nil {
|
|
common.LogError("Cannot resolve config files:", err)
|
|
return
|
|
}
|
|
|
|
req := new(RequestProcessor)
|
|
req.configuredRepos = make(map[string][]*common.AutogitConfig)
|
|
|
|
if len(*basePath) == 0 {
|
|
*basePath, err = os.MkdirTemp(os.TempDir(), AppName)
|
|
common.PanicOnError(err)
|
|
defer os.RemoveAll(*basePath)
|
|
}
|
|
GitHandler, err = common.AllocateGitWorkTree(*basePath, GitAuthor, GitEmail)
|
|
common.PanicOnError(err)
|
|
|
|
orgs := make([]string, 0, 1)
|
|
for _, c := range configs {
|
|
if slices.Contains(c.Workflows, "pr") {
|
|
common.LogDebug(" + adding org:", c.Organization, " branch:", c.Branch, " prjgit:", c.GitProjectName)
|
|
configs := req.configuredRepos[c.Organization]
|
|
if configs == nil {
|
|
configs = make([]*common.AutogitConfig, 0, 1)
|
|
}
|
|
configs = append(configs, c)
|
|
req.configuredRepos[c.Organization] = configs
|
|
|
|
orgs = append(orgs, c.Organization)
|
|
}
|
|
}
|
|
|
|
if CurrentUser, err = gitea.GetCurrentUser(); err != nil {
|
|
common.LogError("Failed to fetch current gitea user:", err)
|
|
return
|
|
}
|
|
common.LogInfo("Running with token from", CurrentUser.UserName)
|
|
|
|
req.Synced = &PullRequestSynced{
|
|
gitea: gitea,
|
|
}
|
|
req.Opened = &PullRequestOpened{
|
|
gitea: gitea,
|
|
}
|
|
req.Closed = &PullRequestClosed{
|
|
gitea: gitea,
|
|
}
|
|
req.Review = &PullRequestReviewed{
|
|
gitea: gitea,
|
|
}
|
|
|
|
checker := CreateDefaultStateChecker(*checkOnStart, req, gitea, time.Duration(*checkIntervalHours)*time.Hour)
|
|
go checker.ConsistencyCheckProcess()
|
|
|
|
listenDefs := common.ListenDefinitions{
|
|
Orgs: orgs,
|
|
GitAuthor: GitAuthor,
|
|
Handlers: map[string]common.RequestProcessor{
|
|
common.RequestType_PR: req,
|
|
common.RequestType_PRSync: req,
|
|
common.RequestType_PRReviewAccepted: req,
|
|
common.RequestType_PRReviewRejected: req,
|
|
},
|
|
}
|
|
listenDefs.RabbitURL, _ = url.Parse(*rabbitUrl)
|
|
|
|
common.PanicOnError(listenDefs.ProcessRabbitMQEvents())
|
|
}
|