autogits/obs-staging-bot/main.go

123 lines
2.7 KiB
Go
Raw Normal View History

2024-07-07 21:08:41 +02:00
package main
import (
2024-07-18 23:36:41 +02:00
"log"
2024-07-18 17:26:17 +02:00
"os"
2024-07-18 23:36:41 +02:00
"regexp"
"strconv"
2024-07-16 22:05:44 +02:00
2024-07-07 21:08:41 +02:00
"src.opensuse.org/autogits/common"
2024-07-18 23:36:41 +02:00
"src.opensuse.org/autogits/common/gitea-generated/models"
2024-07-07 21:08:41 +02:00
)
const (
2024-07-18 23:36:41 +02:00
GitAuthor = "GiteaBot - Obs Staging"
2024-07-16 17:05:43 +02:00
ObsBuildBot = "/obsbuild"
2024-07-07 21:08:41 +02:00
)
var GiteaToken string
2024-07-18 23:36:41 +02:00
var runId uint
2024-07-07 21:08:41 +02:00
2024-07-18 23:36:41 +02:00
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", err, msg)
}
}
2024-07-16 22:05:44 +02:00
2024-07-18 17:26:17 +02:00
func allocateRequestHandler() *common.RequestHandler {
2024-07-18 23:36:41 +02:00
return &common.RequestHandler{
2024-07-18 17:26:17 +02:00
Logger: common.CreateStdoutLogger(os.Stdout, os.Stdout),
2024-07-16 22:05:44 +02:00
}
}
2024-07-07 21:08:41 +02:00
2024-07-18 23:36:41 +02:00
func processPullNotification(h *common.RequestHandler, notification *models.NotificationSubject) {
rx := regexp.MustCompile(`^https://src\.(?:open)?suse\.org/api/v\d+/repos/(?<org>[a-zA-Z0-9]+)/(?<project>[_a-zA-Z0-9]+)/issues/(?<num>[0-9]+)$`)
match := rx.FindStringSubmatch(notification.URL)
if match == nil {
log.Panicf("Unexpected format of notification: %s", notification.URL)
}
h.Log("processing")
h.Log("project: %s", match[2])
h.Log("org: %s", match[1])
h.Log("number: %s", match[3])
org := match[1]
repo := match[2]
id, _ := strconv.ParseInt(match[3], 10, 64)
pr, reviews, err := h.GetPullRequestAndReviews(org, repo, id)
if err != nil {
return
}
for _, reviewer := range pr.RequestedReviewers {
h.Log(reviewer.UserName)
}
2024-07-22 17:09:45 +02:00
obsClient, err := common.NewObsClient("api.opensuse.org")
if err != nil {
h.LogPlainError(err)
return
}
2024-07-18 23:36:41 +02:00
for _, review := range reviews {
h.Log("state: %s, body: %s, id:%d\n", string(review.State), review.Body, review.ID)
if *review.User.LoginName != "autogits_obs_staging_bot" {
continue
}
switch review.State {
case common.ReviewStateUnknown, common.ReviewStateRequestReview:
2024-07-22 17:09:45 +02:00
// create build project, if doesn't exist, and add it to pending requests
2024-07-18 23:36:41 +02:00
case common.ReviewStatePending:
// waiting for build results
case common.ReviewStateApproved:
// done, mark notification as read
case common.ReviewStateRequestChanges:
2024-07-21 22:15:11 +02:00
// build failures, mark notification as read
2024-07-18 23:36:41 +02:00
}
}
}
func pollWorkNotifications() {
2024-07-18 16:43:27 +02:00
h := allocateRequestHandler()
data, err := h.GetNotifications(nil)
if err != nil {
h.LogPlainError(err)
return
}
if data != nil {
for _, notification := range data {
2024-07-18 23:36:41 +02:00
switch notification.Subject.Type {
case "Pull":
processPullNotification(h, notification.Subject)
default:
h.SetNotificationRead(notification.ID)
}
2024-07-18 16:43:27 +02:00
}
}
}
2024-07-16 17:05:43 +02:00
func main() {
var defs common.ListenDefinitions
2024-07-07 21:08:41 +02:00
2024-07-16 17:05:43 +02:00
defs.Url = ObsBuildBot
defs.GitAuthor = GitAuthor
2024-07-07 21:08:41 +02:00
2024-07-18 23:36:41 +02:00
failOnError(common.RequireGiteaSecretToken(), "Cannot find GITEA_TOKEN")
failOnError(common.RequireObsSecretToken(), "Cannot find OBS_USER and OBS_PASSWORD")
2024-07-16 22:05:44 +02:00
2024-07-18 23:36:41 +02:00
// go ProcessingObsMessages("rabbit.opensuse.org", "opensuse", "opensuse", "")
2024-07-18 16:43:27 +02:00
2024-07-18 23:36:41 +02:00
pollWorkNotifications()
2024-07-17 17:20:24 +02:00
2024-07-21 22:15:11 +02:00
2024-07-18 23:36:41 +02:00
stuck := make(chan int)
2024-07-17 17:20:24 +02:00
<-stuck
2024-07-07 21:08:41 +02:00
}