package main import ( "log" "os" "regexp" "strconv" "src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common/gitea-generated/models" ) const ( GitAuthor = "GiteaBot - Obs Staging" ObsBuildBot = "/obsbuild" ) var GiteaToken string var runId uint func failOnError(err error, msg string) { if err != nil { log.Panicf("%s: %s", err, msg) } } func allocateRequestHandler() *common.RequestHandler { return &common.RequestHandler{ Logger: common.CreateStdoutLogger(os.Stdout, os.Stdout), } } func processPullNotification(h *common.RequestHandler, notification *models.NotificationSubject) { rx := regexp.MustCompile(`^https://src\.(?:open)?suse\.org/api/v\d+/repos/(?[a-zA-Z0-9]+)/(?[_a-zA-Z0-9]+)/issues/(?[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) } 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: // start review case common.ReviewStatePending: // waiting for build results case common.ReviewStateApproved: // done, mark notification as read case common.ReviewStateRequestChanges: // build failures, mark notification as read } } } func pollWorkNotifications() { h := allocateRequestHandler() data, err := h.GetNotifications(nil) if err != nil { h.LogPlainError(err) return } if data != nil { for _, notification := range data { switch notification.Subject.Type { case "Pull": processPullNotification(h, notification.Subject) default: h.SetNotificationRead(notification.ID) } } } } func main() { var defs common.ListenDefinitions defs.Url = ObsBuildBot defs.GitAuthor = GitAuthor failOnError(common.RequireGiteaSecretToken(), "Cannot find GITEA_TOKEN") failOnError(common.RequireObsSecretToken(), "Cannot find OBS_USER and OBS_PASSWORD") // go ProcessingObsMessages("rabbit.opensuse.org", "opensuse", "opensuse", "") pollWorkNotifications() stuck := make(chan int) <-stuck }