2024-07-08 17:14:26 +02:00
|
|
|
package common
|
|
|
|
|
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-08 17:14:26 +02:00
|
|
|
import (
|
2024-08-27 17:55:03 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2024-07-08 17:14:26 +02:00
|
|
|
"log"
|
|
|
|
"net/url"
|
2024-09-04 14:04:13 +02:00
|
|
|
"slices"
|
2024-08-27 17:55:03 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
rabbitmq "github.com/rabbitmq/amqp091-go"
|
2024-07-08 17:14:26 +02:00
|
|
|
)
|
|
|
|
|
2024-08-26 17:07:52 +02:00
|
|
|
const RequestType_CreateBrachTag = "create"
|
|
|
|
const RequestType_DeleteBranchTag = "delete"
|
|
|
|
const RequestType_Fork = "fork"
|
|
|
|
const RequestType_Issue = "issues"
|
|
|
|
const RequestType_IssueAssign = "issue_assign"
|
|
|
|
const RequestType_IssueComment = "issue_comment"
|
|
|
|
const RequestType_IssueLabel = "issue_label"
|
|
|
|
const RequestType_IssueMilestone = "issue_milestone"
|
2024-07-08 17:14:26 +02:00
|
|
|
const RequestType_Push = "push"
|
|
|
|
const RequestType_Repository = "repository"
|
2024-08-26 17:07:52 +02:00
|
|
|
const RequestType_Release = "release"
|
2024-07-09 12:06:24 +02:00
|
|
|
const RequestType_PR = "pull_request"
|
2024-08-26 17:30:36 +02:00
|
|
|
const RequestType_PRAssign = "pull_request_assign"
|
2024-08-26 17:38:55 +02:00
|
|
|
const RequestType_PRLabel = "pull_request_label"
|
2024-08-26 17:07:52 +02:00
|
|
|
const RequestType_PRComment = "pull_request_comment"
|
2024-08-26 17:30:36 +02:00
|
|
|
const RequestType_PRMilestone = "pull_request_milestone"
|
|
|
|
const RequestType_PRSync = "pull_request_sync"
|
|
|
|
const RequestType_PRReviewAccepted = "pull_request_review_approved"
|
|
|
|
const RequestType_PRReviewRejected = "pull_request_review_rejected"
|
|
|
|
const RequestType_PRReviewRequest = "pull_request_review_request"
|
2024-09-11 12:15:51 +02:00
|
|
|
const RequestType_PRReviewComment = "pull_request_review_comment"
|
2024-08-26 17:30:36 +02:00
|
|
|
const RequestType_Wiki = "wiki"
|
2024-07-08 17:14:26 +02:00
|
|
|
|
|
|
|
type RequestProcessor func(*RequestHandler) error
|
|
|
|
|
|
|
|
type ListenDefinitions struct {
|
2024-08-27 17:55:03 +02:00
|
|
|
RabbitURL string // amqps://user:password@host/queue
|
|
|
|
|
|
|
|
GitAuthor string
|
|
|
|
Handlers map[string]RequestProcessor
|
|
|
|
}
|
|
|
|
|
|
|
|
type RabbitMessage rabbitmq.Delivery
|
|
|
|
|
|
|
|
func processRabbitMQ(msgCh chan<- RabbitMessage, server url.URL, topics []string) error {
|
|
|
|
queueName := server.Path
|
|
|
|
server.Path = ""
|
|
|
|
|
2024-09-04 14:04:13 +02:00
|
|
|
if len(queueName) > 0 && queueName[0] == '/' {
|
2024-08-27 17:55:03 +02:00
|
|
|
queueName = queueName[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
connection, err := rabbitmq.DialTLS(server.String(), &tls.Config{
|
|
|
|
ServerName: server.Hostname(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot connect to %s . Err: %w", server.Hostname(), err)
|
|
|
|
}
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
ch, err := connection.Channel()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot create a channel. Err: %w", err)
|
|
|
|
}
|
|
|
|
defer ch.Close()
|
|
|
|
|
|
|
|
if err = ch.ExchangeDeclarePassive("pubsub", "topic", true, false, false, false, nil); err != nil {
|
|
|
|
return fmt.Errorf("Cannot find pubsub exchange? Err: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var q rabbitmq.Queue
|
|
|
|
if len(queueName) == 0 {
|
|
|
|
q, err = ch.QueueDeclare("", false, true, true, false, nil)
|
|
|
|
} else {
|
|
|
|
q, err = ch.QueueDeclarePassive(queueName, true, false, true, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("queue not found .. trying to create it: %v\n", err)
|
|
|
|
if ch.IsClosed() {
|
|
|
|
ch, err = connection.Channel()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Channel cannot be re-opened. Err: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
q, err = ch.QueueDeclare(queueName, true, false, true, false, nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("can't create persistent queue ... falling back to temporaty queue: %v\n", err)
|
|
|
|
if ch.IsClosed() {
|
|
|
|
ch, err = connection.Channel()
|
|
|
|
return fmt.Errorf("Channel cannot be re-opened. Err: %w", err)
|
|
|
|
}
|
|
|
|
q, err = ch.QueueDeclare("", false, true, true, false, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot declare queue. Err: %w", err)
|
|
|
|
}
|
|
|
|
// log.Printf("queue: %s:%d", q.Name, q.Consumers)
|
|
|
|
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Println(" -- listening to topics:")
|
2024-08-27 17:55:03 +02:00
|
|
|
for _, topic := range topics {
|
|
|
|
err = ch.QueueBind(q.Name, topic, "pubsub", false, nil)
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Println(" +", topic)
|
2024-08-27 17:55:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot find queue to exchange with topic %s. Err: %w", topic, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := ch.Consume(q.Name, "", true, true, false, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot start consumer. Err: %w", err)
|
|
|
|
}
|
|
|
|
// log.Printf("queue: %s:%d", q.Name, q.Consumers)
|
|
|
|
|
|
|
|
for {
|
|
|
|
msg, ok := <-msgs
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("channel/connection closed?\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
msgCh <- RabbitMessage(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectAndProcessRabbitMQ(log *log.Logger, ch chan<- RabbitMessage, server url.URL, topics []string) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Println(r)
|
2024-08-27 17:55:03 +02:00
|
|
|
log.Println("'crash' RabbitMQ worker. Recovering... reconnecting...")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
go connectAndProcessRabbitMQ(log, ch, server, topics)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := processRabbitMQ(ch, server, topics)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error in RabbitMQ connection. %#v", err)
|
|
|
|
log.Println("Reconnecting in 2 seconds...")
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-28 00:45:47 +02:00
|
|
|
func connectToRabbitMQ(log *log.Logger, server url.URL, topics []string) chan RabbitMessage {
|
2024-08-27 17:55:03 +02:00
|
|
|
ch := make(chan RabbitMessage, 100)
|
|
|
|
go connectAndProcessRabbitMQ(log, ch, server, topics)
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2024-08-28 00:45:47 +02:00
|
|
|
func ProcessEvent(f RequestProcessor, h *RequestHandler) {
|
2024-09-04 14:04:13 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
log.Println(r)
|
|
|
|
}
|
2024-08-28 00:45:47 +02:00
|
|
|
}()
|
|
|
|
|
2024-09-04 14:04:13 +02:00
|
|
|
if err := f(h); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-09-10 18:24:41 +02:00
|
|
|
|
2024-08-28 00:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ProcessRabbitMQEvents(listenDefs ListenDefinitions, orgs []string) error {
|
2024-08-27 17:55:03 +02:00
|
|
|
server, err := url.Parse(listenDefs.RabbitURL)
|
|
|
|
if err != nil {
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Panicf("cannot parse server URL. Err: %#v\n", err)
|
2024-08-27 17:55:03 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Println("RabbitMQ connection:", *server)
|
2024-08-28 00:45:47 +02:00
|
|
|
topics := make([]string, 0, len(listenDefs.Handlers)*len(orgs))
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Println(len(listenDefs.Handlers), len(orgs))
|
|
|
|
|
|
|
|
server.User = url.UserPassword(rabbitUser, rabbitPassword)
|
|
|
|
|
|
|
|
domain := "suse"
|
|
|
|
if server.Hostname() == "rabbit.opensuse.org" {
|
|
|
|
domain = "opensuse"
|
|
|
|
}
|
2024-08-28 00:45:47 +02:00
|
|
|
|
|
|
|
for _, org := range orgs {
|
|
|
|
for k := range listenDefs.Handlers {
|
2024-09-04 14:04:13 +02:00
|
|
|
topics = append(topics, fmt.Sprintf("%s.gitea.%s.%s.#", domain, org, k))
|
2024-08-28 00:45:47 +02:00
|
|
|
}
|
2024-08-27 17:55:03 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:45:47 +02:00
|
|
|
ch := connectToRabbitMQ(log.Default(), *server, topics)
|
2024-08-27 17:55:03 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
msg, ok := <-ch
|
|
|
|
if !ok {
|
2024-08-28 00:45:47 +02:00
|
|
|
return nil
|
2024-08-27 17:55:03 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Println("event:", msg.RoutingKey)
|
|
|
|
|
2024-08-27 17:55:03 +02:00
|
|
|
route := strings.Split(msg.RoutingKey, ".")
|
2024-08-28 00:45:47 +02:00
|
|
|
if len(route) > 3 {
|
|
|
|
reqType := route[3]
|
|
|
|
org := route[2]
|
2024-08-27 17:55:03 +02:00
|
|
|
|
2024-09-04 14:04:13 +02:00
|
|
|
if !slices.Contains(orgs, org) {
|
|
|
|
log.Println("Got even for unhandeled org:", org)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("org:", org, "type:", reqType)
|
|
|
|
if handler, found := listenDefs.Handlers[reqType]; found {
|
|
|
|
log.Println("handler found", handler)
|
|
|
|
h, err := CreateRequestHandler()
|
2024-08-28 17:20:09 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot create request handler: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
2024-08-28 00:45:47 +02:00
|
|
|
req, err := ParseRequestJSON(reqType, msg.Body)
|
|
|
|
if err != nil {
|
2024-08-28 17:20:09 +02:00
|
|
|
log.Printf("Error parsing request JSON: %v\n", err)
|
|
|
|
continue
|
2024-08-28 00:45:47 +02:00
|
|
|
} else {
|
2024-09-04 14:04:13 +02:00
|
|
|
log.Println("processing req", req.Type)
|
2024-08-28 00:45:47 +02:00
|
|
|
h.Request = req
|
|
|
|
ProcessEvent(handler, h)
|
2024-09-04 14:04:13 +02:00
|
|
|
|
2024-08-27 17:55:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-08 17:14:26 +02:00
|
|
|
}
|