This commit is contained in:
Adam Majer 2024-07-18 23:36:41 +02:00
parent 17181ae262
commit 4a1856a216
3 changed files with 130 additions and 21 deletions

View File

@ -28,6 +28,64 @@ func (h *RequestHandler) allocateGiteaTransport() (*transport.Runtime, *apiclien
return r, apiclient.New(r, nil) return r, apiclient.New(r, nil)
} }
const (
// from Gitea
// ReviewStateApproved pr is approved
ReviewStateApproved models.ReviewStateType = "APPROVED"
// ReviewStatePending pr state is pending
ReviewStatePending models.ReviewStateType = "PENDING"
// ReviewStateComment is a comment review
ReviewStateComment models.ReviewStateType = "COMMENT"
// ReviewStateRequestChanges changes for pr are requested
ReviewStateRequestChanges models.ReviewStateType = "REQUEST_CHANGES"
// ReviewStateRequestReview review is requested from user
ReviewStateRequestReview models.ReviewStateType = "REQUEST_REVIEW"
// ReviewStateUnknown state of pr is unknown
ReviewStateUnknown models.ReviewStateType = ""
)
func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) {
if h.HasError() {
return nil, nil, h.Error
}
transport, client := h.allocateGiteaTransport()
pr, err := client.Repository.RepoGetPullRequest(
repository.NewRepoGetPullRequestParams().
WithDefaults().
WithOwner(org).
WithRepo(project).
WithIndex(num),
transport.DefaultAuthentication,
)
if err != nil {
h.Error = err;
h.LogPlainError(err)
return nil, nil, err
}
limit := int64(1000)
reviews, err := client.Repository.RepoListPullReviews(
repository.NewRepoListPullReviewsParams().
WithDefaults().
WithOwner(org).
WithRepo(project).
WithIndex(num).
WithLimit(&limit),
transport.DefaultAuthentication,
)
if err != nil {
h.Error = err
h.LogPlainError(err)
return nil, nil, err
}
return pr.Payload, reviews.Payload, nil
}
func (h *RequestHandler) GetNotifications(since *time.Time) ([]*models.NotificationThread, error){ func (h *RequestHandler) GetNotifications(since *time.Time) ([]*models.NotificationThread, error){
if h.HasError() { if h.HasError() {
return nil, h.Error return nil, h.Error
@ -59,7 +117,7 @@ func (h *RequestHandler) GetNotifications(since *time.Time) ([]*models.Notificat
return list.Payload, nil return list.Payload, nil
} }
func (h *RequestHandler) SetNotificationRead(notificationId int) error { func (h *RequestHandler) SetNotificationRead(notificationId int64) error {
if h.HasError() { if h.HasError() {
return h.Error return h.Error
} }
@ -68,7 +126,7 @@ func (h *RequestHandler) SetNotificationRead(notificationId int) error {
list, err := client.Notification.NotifyReadThread( list, err := client.Notification.NotifyReadThread(
notification.NewNotifyReadThreadParams(). notification.NewNotifyReadThreadParams().
WithDefaults(). WithDefaults().
WithID(string(id)), WithID(fmt.Sprint(id)),
transport.DefaultAuthentication, transport.DefaultAuthentication,
) )

View File

@ -1,28 +1,81 @@
package main package main
import ( import (
"log"
"os" "os"
"regexp"
"strconv"
"src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
) )
const ( const (
GitAuthor = "GiteaBot - Obs Staging" GitAuthor = "GiteaBot - Obs Staging"
ObsBuildBot = "/obsbuild" ObsBuildBot = "/obsbuild"
) )
var GiteaToken string var GiteaToken string
var runId uint var runId uint
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", err, msg)
}
}
func allocateRequestHandler() *common.RequestHandler { func allocateRequestHandler() *common.RequestHandler {
return &common.RequestHandler { return &common.RequestHandler{
Logger: common.CreateStdoutLogger(os.Stdout, os.Stdout), Logger: common.CreateStdoutLogger(os.Stdout, os.Stdout),
} }
} }
func PollWorkNotifications() { 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)
}
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() h := allocateRequestHandler()
data, err := h.GetNotifications(nil) data, err := h.GetNotifications(nil)
@ -33,9 +86,13 @@ func PollWorkNotifications() {
if data != nil { if data != nil {
for _, notification := range data { for _, notification := range data {
h.Log("[+] id: %d", notification.ID)
h.Log(" title: %s", notification.Subject.Title) switch notification.Subject.Type {
h.Log(" subj: %#v", notification.Subject) case "Pull":
processPullNotification(h, notification.Subject)
default:
h.SetNotificationRead(notification.ID)
}
} }
} }
} }
@ -46,12 +103,12 @@ func main() {
defs.Url = ObsBuildBot defs.Url = ObsBuildBot
defs.GitAuthor = GitAuthor defs.GitAuthor = GitAuthor
common.RequireGiteaSecretToken() failOnError(common.RequireGiteaSecretToken(), "Cannot find GITEA_TOKEN")
common.RequireObsSecretToken() failOnError(common.RequireObsSecretToken(), "Cannot find OBS_USER and OBS_PASSWORD")
// go ProcessingObsMessages("rabbit.opensuse.org", "opensuse", "opensuse", "") // go ProcessingObsMessages("rabbit.opensuse.org", "opensuse", "opensuse", "")
PollWorkNotifications() pollWorkNotifications()
stuck := make(chan int) stuck := make(chan int)
<-stuck <-stuck

View File

@ -17,12 +17,6 @@ type BuildNotification struct {
Starttime, Endtime, Readytime string Starttime, Endtime, Readytime string
} }
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", err, msg)
}
}
func processObsMessage(msg *rabbitmq.Delivery) { func processObsMessage(msg *rabbitmq.Delivery) {
key := strings.SplitN(msg.RoutingKey, ".", 4) key := strings.SplitN(msg.RoutingKey, ".", 4)
if len(key) != 4 || len(key[3]) < 7 || key[3][:6] != "build_" { if len(key) != 4 || len(key[3]) < 7 || key[3][:6] != "build_" {