2023-08-08 19:41:14 +02:00
|
|
|
package features
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.front.kjuulh.io/kjuulh/contractor/internal/models"
|
|
|
|
"git.front.kjuulh.io/kjuulh/contractor/internal/providers"
|
|
|
|
"git.front.kjuulh.io/kjuulh/contractor/internal/queue"
|
|
|
|
"git.front.kjuulh.io/kjuulh/contractor/internal/renovate"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RegisterGiteaQueues(goqueue *queue.GoQueue, renovate *renovate.RenovateClient, giteaClient *providers.GiteaClient) {
|
|
|
|
goqueue.Subscribe(
|
2023-08-09 15:44:31 +02:00
|
|
|
models.MessageTypeRefreshGiteaRepository,
|
2023-08-08 19:41:14 +02:00
|
|
|
func(ctx context.Context, item *queue.QueueMessage) error {
|
|
|
|
log.Printf("handling message: %s, content: %s", item.Type, item.Content)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
goqueue.Subscribe(
|
2023-08-09 15:44:31 +02:00
|
|
|
models.MessageTypeRefreshGiteaRepositoryDone,
|
2023-08-08 19:41:14 +02:00
|
|
|
func(ctx context.Context, item *queue.QueueMessage) error {
|
|
|
|
log.Printf("handling message: %s, content: %s", item.Type, item.Content)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
goqueue.Subscribe(
|
2023-08-09 15:44:31 +02:00
|
|
|
models.MessageTypeRefreshGiteaRepository,
|
2023-08-08 19:41:14 +02:00
|
|
|
func(ctx context.Context, item *queue.QueueMessage) error {
|
2023-08-09 15:44:31 +02:00
|
|
|
var request models.RefreshGiteaRepositoryRequest
|
2023-08-08 19:41:14 +02:00
|
|
|
if err := json.Unmarshal([]byte(item.Content), &request); err != nil {
|
|
|
|
log.Printf("failed to unmarshal request body: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelCtx, cancel := context.WithTimeout(ctx, time.Minute*5)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := renovate.RefreshRepository(cancelCtx, request.Owner, request.Repository); err != nil {
|
2023-08-09 15:44:31 +02:00
|
|
|
goqueue.Insert(models.MessageTypeRefreshGiteaRepositoryDone, models.RefreshGiteaRepositoryDoneRequest{
|
2023-08-08 19:41:14 +02:00
|
|
|
Repository: request.Repository,
|
|
|
|
Owner: request.Owner,
|
|
|
|
PullRequestID: request.PullRequestID,
|
|
|
|
CommentID: request.CommentID,
|
|
|
|
CommentBody: request.CommentBody,
|
|
|
|
ReportProgress: request.ReportProgress,
|
|
|
|
Status: "failed",
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-09 15:44:31 +02:00
|
|
|
goqueue.Insert(models.MessageTypeRefreshGiteaRepositoryDone, models.RefreshGiteaRepositoryDoneRequest{
|
2023-08-08 19:41:14 +02:00
|
|
|
Repository: request.Repository,
|
|
|
|
Owner: request.Owner,
|
|
|
|
PullRequestID: request.PullRequestID,
|
|
|
|
CommentID: request.CommentID,
|
|
|
|
CommentBody: request.CommentBody,
|
|
|
|
ReportProgress: request.ReportProgress,
|
|
|
|
Status: "done",
|
|
|
|
Error: "",
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
goqueue.Subscribe(
|
2023-08-09 15:44:31 +02:00
|
|
|
models.MessageTypeRefreshGiteaRepositoryDone,
|
2023-08-08 19:41:14 +02:00
|
|
|
func(ctx context.Context, item *queue.QueueMessage) error {
|
2023-08-09 15:44:31 +02:00
|
|
|
var doneRequest models.RefreshGiteaRepositoryDoneRequest
|
2023-08-08 19:41:14 +02:00
|
|
|
if err := json.Unmarshal([]byte(item.Content), &doneRequest); err != nil {
|
|
|
|
log.Printf("failed to unmarshal request body: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return giteaClient.EditComment(ctx, &doneRequest)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|