feat(github): add github support
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
@@ -10,11 +10,12 @@ import (
|
||||
)
|
||||
|
||||
type BotHandler struct {
|
||||
giteaClient *providers.GiteaClient
|
||||
giteaClient *providers.GiteaClient
|
||||
githubClient *providers.GitHubClient
|
||||
}
|
||||
|
||||
func NewBotHandler(gitea *providers.GiteaClient) *BotHandler {
|
||||
return &BotHandler{giteaClient: gitea}
|
||||
func NewBotHandler(gitea *providers.GiteaClient, github *providers.GitHubClient) *BotHandler {
|
||||
return &BotHandler{giteaClient: gitea, githubClient: github}
|
||||
}
|
||||
|
||||
func (b *BotHandler) Handle(input string) (output string, err error) {
|
||||
@@ -62,6 +63,14 @@ func (b *BotHandler) AppendComment(
|
||||
repository string,
|
||||
pullRequest int,
|
||||
comment string,
|
||||
backend models.SupportedBackend,
|
||||
) (*models.AddCommentResponse, error) {
|
||||
return b.giteaClient.AddComment(owner, repository, pullRequest, comment)
|
||||
switch backend {
|
||||
case models.SupportedBackendGitHub:
|
||||
return b.githubClient.AddComment(owner, repository, pullRequest, comment)
|
||||
case models.SupportedBackendGitea:
|
||||
return b.giteaClient.AddComment(owner, repository, pullRequest, comment)
|
||||
default:
|
||||
panic("backend chosen was not a valid option")
|
||||
}
|
||||
}
|
||||
|
@@ -14,23 +14,23 @@ import (
|
||||
|
||||
func RegisterGiteaQueues(goqueue *queue.GoQueue, renovate *renovate.RenovateClient, giteaClient *providers.GiteaClient) {
|
||||
goqueue.Subscribe(
|
||||
models.MessageTypeRefreshRepository,
|
||||
models.MessageTypeRefreshGiteaRepository,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
log.Printf("handling message: %s, content: %s", item.Type, item.Content)
|
||||
return nil
|
||||
},
|
||||
)
|
||||
goqueue.Subscribe(
|
||||
models.MessageTypeRefreshRepositoryDone,
|
||||
models.MessageTypeRefreshGiteaRepositoryDone,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
log.Printf("handling message: %s, content: %s", item.Type, item.Content)
|
||||
return nil
|
||||
},
|
||||
)
|
||||
goqueue.Subscribe(
|
||||
models.MessageTypeRefreshRepository,
|
||||
models.MessageTypeRefreshGiteaRepository,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
var request models.RefreshRepositoryRequest
|
||||
var request models.RefreshGiteaRepositoryRequest
|
||||
if err := json.Unmarshal([]byte(item.Content), &request); err != nil {
|
||||
log.Printf("failed to unmarshal request body: %s", err.Error())
|
||||
return err
|
||||
@@ -40,7 +40,7 @@ func RegisterGiteaQueues(goqueue *queue.GoQueue, renovate *renovate.RenovateClie
|
||||
defer cancel()
|
||||
|
||||
if err := renovate.RefreshRepository(cancelCtx, request.Owner, request.Repository); err != nil {
|
||||
goqueue.Insert(models.MessageTypeRefreshRepositoryDone, models.RefreshDoneRepositoryRequest{
|
||||
goqueue.Insert(models.MessageTypeRefreshGiteaRepositoryDone, models.RefreshGiteaRepositoryDoneRequest{
|
||||
Repository: request.Repository,
|
||||
Owner: request.Owner,
|
||||
PullRequestID: request.PullRequestID,
|
||||
@@ -54,7 +54,7 @@ func RegisterGiteaQueues(goqueue *queue.GoQueue, renovate *renovate.RenovateClie
|
||||
return err
|
||||
}
|
||||
|
||||
goqueue.Insert(models.MessageTypeRefreshRepositoryDone, models.RefreshDoneRepositoryRequest{
|
||||
goqueue.Insert(models.MessageTypeRefreshGiteaRepositoryDone, models.RefreshGiteaRepositoryDoneRequest{
|
||||
Repository: request.Repository,
|
||||
Owner: request.Owner,
|
||||
PullRequestID: request.PullRequestID,
|
||||
@@ -70,9 +70,9 @@ func RegisterGiteaQueues(goqueue *queue.GoQueue, renovate *renovate.RenovateClie
|
||||
)
|
||||
|
||||
goqueue.Subscribe(
|
||||
models.MessageTypeRefreshRepositoryDone,
|
||||
models.MessageTypeRefreshGiteaRepositoryDone,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
var doneRequest models.RefreshDoneRepositoryRequest
|
||||
var doneRequest models.RefreshGiteaRepositoryDoneRequest
|
||||
if err := json.Unmarshal([]byte(item.Content), &doneRequest); err != nil {
|
||||
log.Printf("failed to unmarshal request body: %s", err.Error())
|
||||
return err
|
||||
|
@@ -54,12 +54,13 @@ func (gw *GiteaWebhook) HandleGiteaWebhook(ctx context.Context, request *GiteaWe
|
||||
parts[1],
|
||||
request.Issue.Number,
|
||||
output,
|
||||
models.SupportedBackendGitea,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := gw.queue.Insert(models.MessageTypeRefreshRepository, models.RefreshRepositoryRequest{
|
||||
if err := gw.queue.Insert(models.MessageTypeRefreshGiteaRepository, models.RefreshGiteaRepositoryRequest{
|
||||
Repository: parts[1],
|
||||
Owner: parts[0],
|
||||
PullRequestID: request.Issue.Number,
|
||||
|
84
internal/features/handle_github_events.go
Normal file
84
internal/features/handle_github_events.go
Normal file
@@ -0,0 +1,84 @@
|
||||
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 RegisterGitHubQueues(goqueue *queue.GoQueue, renovate *renovate.RenovateClient, giteaClient *providers.GitHubClient) {
|
||||
goqueue.Subscribe(
|
||||
models.MessageTypeRefreshGitHubRepository,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
log.Printf("handling message: %s, content: %s", item.Type, item.Content)
|
||||
return nil
|
||||
},
|
||||
)
|
||||
goqueue.Subscribe(
|
||||
models.MessageTypeRefreshGitHubRepositoryDone,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
log.Printf("handling message: %s, content: %s", item.Type, item.Content)
|
||||
return nil
|
||||
},
|
||||
)
|
||||
goqueue.Subscribe(
|
||||
models.MessageTypeRefreshGitHubRepository,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
var request models.RefreshGitHubRepositoryRequest
|
||||
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 {
|
||||
goqueue.Insert(models.MessageTypeRefreshGitHubRepositoryDone, models.RefreshGitHubRepositoryDoneRequest{
|
||||
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
|
||||
}
|
||||
|
||||
goqueue.Insert(models.MessageTypeRefreshGitHubRepositoryDone, models.RefreshGitHubRepositoryDoneRequest{
|
||||
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(
|
||||
models.MessageTypeRefreshGitHubRepositoryDone,
|
||||
func(ctx context.Context, item *queue.QueueMessage) error {
|
||||
var doneRequest models.RefreshGitHubRepositoryDoneRequest
|
||||
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)
|
||||
},
|
||||
)
|
||||
}
|
76
internal/features/handle_github_webhook.go
Normal file
76
internal/features/handle_github_webhook.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package features
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"git.front.kjuulh.io/kjuulh/contractor/internal/bot"
|
||||
"git.front.kjuulh.io/kjuulh/contractor/internal/models"
|
||||
"git.front.kjuulh.io/kjuulh/contractor/internal/queue"
|
||||
)
|
||||
|
||||
type GitHubWebhook struct {
|
||||
botHandler *bot.BotHandler
|
||||
queue *queue.GoQueue
|
||||
}
|
||||
|
||||
type GitHubWebhookRequest struct {
|
||||
Action string `json:"action"`
|
||||
Issue struct {
|
||||
Id int `json:"id"`
|
||||
Number int `json:"number"`
|
||||
} `json:"issue"`
|
||||
Comment struct {
|
||||
Body string `json:"body"`
|
||||
} `json:"comment"`
|
||||
Repository struct {
|
||||
FullName string `json:"full_name"`
|
||||
}
|
||||
}
|
||||
|
||||
func NewGitHubWebhook(botHandler *bot.BotHandler, queue *queue.GoQueue) *GitHubWebhook {
|
||||
return &GitHubWebhook{
|
||||
botHandler: botHandler,
|
||||
queue: queue,
|
||||
}
|
||||
}
|
||||
|
||||
func (gw *GitHubWebhook) HandleGitHubWebhook(ctx context.Context, request *GitHubWebhookRequest) error {
|
||||
command, ok := validateBotComment(request.Comment.Body)
|
||||
if ok {
|
||||
log.Printf("got webhook request: contractor %s", command)
|
||||
|
||||
bot := gw.botHandler
|
||||
output, err := bot.Handle(command)
|
||||
if err != nil {
|
||||
log.Printf("failed to run bot handler with error: %s", err.Error())
|
||||
}
|
||||
|
||||
parts := strings.Split(request.Repository.FullName, "/")
|
||||
|
||||
comment, err := bot.AppendComment(
|
||||
parts[0],
|
||||
parts[1],
|
||||
request.Issue.Number,
|
||||
output,
|
||||
models.SupportedBackendGitHub,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := gw.queue.Insert(models.MessageTypeRefreshGitHubRepository, models.RefreshGitHubRepositoryRequest{
|
||||
Repository: parts[1],
|
||||
Owner: parts[0],
|
||||
PullRequestID: request.Issue.Number,
|
||||
CommentID: comment.ID,
|
||||
CommentBody: comment.Body,
|
||||
ReportProgress: true,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@@ -1,8 +1,10 @@
|
||||
package models
|
||||
|
||||
const (
|
||||
MessageTypeRefreshRepository = "refresh_repository"
|
||||
MessageTypeRefreshRepositoryDone = "refresh_repository_done"
|
||||
MessageTypeRefreshGiteaRepository = "refresh_gitea_repository"
|
||||
MessageTypeRefreshGiteaRepositoryDone = "refresh_gitea_repository_done"
|
||||
MessageTypeRefreshGitHubRepository = "refresh_github_repository"
|
||||
MessageTypeRefreshGitHubRepositoryDone = "refresh_github_repository_done"
|
||||
)
|
||||
|
||||
type CreateHook struct {
|
||||
@@ -14,7 +16,7 @@ type CreateHook struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type RefreshRepositoryRequest struct {
|
||||
type RefreshGiteaRepositoryRequest struct {
|
||||
Repository string `json:"repository"`
|
||||
Owner string `json:"owner"`
|
||||
PullRequestID int `json:"pullRequestId"`
|
||||
@@ -23,7 +25,27 @@ type RefreshRepositoryRequest struct {
|
||||
ReportProgress bool `json:"reportProgress"`
|
||||
}
|
||||
|
||||
type RefreshDoneRepositoryRequest struct {
|
||||
type RefreshGiteaRepositoryDoneRequest struct {
|
||||
Repository string `json:"repository"`
|
||||
Owner string `json:"owner"`
|
||||
PullRequestID int `json:"pullRequestId"`
|
||||
CommentID int `json:"commentId"`
|
||||
CommentBody string `json:"commentBody"`
|
||||
ReportProgress bool `json:"reportProgress"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type RefreshGitHubRepositoryRequest struct {
|
||||
Repository string `json:"repository"`
|
||||
Owner string `json:"owner"`
|
||||
PullRequestID int `json:"pullRequestId"`
|
||||
CommentID int `json:"commentId"`
|
||||
CommentBody string `json:"commentBody"`
|
||||
ReportProgress bool `json:"reportProgress"`
|
||||
}
|
||||
|
||||
type RefreshGitHubRepositoryDoneRequest struct {
|
||||
Repository string `json:"repository"`
|
||||
Owner string `json:"owner"`
|
||||
PullRequestID int `json:"pullRequestId"`
|
||||
@@ -38,3 +60,10 @@ type AddCommentResponse struct {
|
||||
Body string `json:"body"`
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
type SupportedBackend string
|
||||
|
||||
const (
|
||||
SupportedBackendGitHub SupportedBackend = "github"
|
||||
SupportedBackendGitea SupportedBackend = "gitea"
|
||||
)
|
||||
|
@@ -31,7 +31,7 @@ func NewGiteaClient(url, token *string) *GiteaClient {
|
||||
|
||||
func (gc *GiteaClient) EditComment(
|
||||
ctx context.Context,
|
||||
doneRequest *models.RefreshDoneRepositoryRequest,
|
||||
doneRequest *models.RefreshGiteaRepositoryDoneRequest,
|
||||
) error {
|
||||
commentBody := html.UnescapeString(doneRequest.CommentBody)
|
||||
startCmnt := "<!-- Status update start -->"
|
||||
|
117
internal/providers/github.go
Normal file
117
internal/providers/github.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.front.kjuulh.io/kjuulh/contractor/internal/models"
|
||||
|
||||
"github.com/bradleyfalzon/ghinstallation/v2"
|
||||
"github.com/google/go-github/v53/github"
|
||||
)
|
||||
|
||||
type GitHubClient struct {
|
||||
appID *int64
|
||||
installationID *int64
|
||||
privateKeyPath *string
|
||||
|
||||
client *github.Client
|
||||
}
|
||||
|
||||
func NewGitHubClient(appID, installationID *int64, privateKeyPath *string) *GitHubClient {
|
||||
return &GitHubClient{
|
||||
appID: appID,
|
||||
installationID: installationID,
|
||||
privateKeyPath: privateKeyPath,
|
||||
}
|
||||
}
|
||||
|
||||
func (gc *GitHubClient) makeSureClientExist() {
|
||||
if gc.client != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tr := http.DefaultTransport
|
||||
itr, err := ghinstallation.NewKeyFromFile(tr, *gc.appID, *gc.installationID, *gc.privateKeyPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
client := github.NewClient(&http.Client{Transport: itr})
|
||||
|
||||
gc.client = client
|
||||
}
|
||||
|
||||
func (gc *GitHubClient) EditComment(
|
||||
ctx context.Context,
|
||||
doneRequest *models.RefreshGitHubRepositoryDoneRequest,
|
||||
) error {
|
||||
gc.makeSureClientExist()
|
||||
|
||||
commentBody := html.UnescapeString(doneRequest.CommentBody)
|
||||
startCmnt := "<!-- Status update start -->"
|
||||
startIdx := strings.Index(commentBody, startCmnt)
|
||||
endIdx := strings.Index(commentBody, "<!-- Status update end -->")
|
||||
if startIdx >= 0 && endIdx >= 0 {
|
||||
log.Println("found comment to replace")
|
||||
|
||||
var content string
|
||||
|
||||
if doneRequest.Error != "" {
|
||||
content = fmt.Sprintf("<pre>ERROR: %s</pre><br>", doneRequest.Error)
|
||||
}
|
||||
if doneRequest.Status != "" {
|
||||
content = fmt.Sprintf("%s<p>%s</p>", content, doneRequest.Status)
|
||||
}
|
||||
|
||||
doneRequest.CommentBody = fmt.Sprintf(
|
||||
"%s<br><hr>%s<hr><br>%s",
|
||||
commentBody[:startIdx+len(startCmnt)],
|
||||
content,
|
||||
commentBody[endIdx:],
|
||||
)
|
||||
}
|
||||
|
||||
_, _, err := gc.client.Issues.EditComment(ctx, doneRequest.Owner, doneRequest.Repository, int64(doneRequest.CommentID), &github.IssueComment{
|
||||
Body: &doneRequest.CommentBody,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("failed to update comment: %s", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gc *GitHubClient) CreateWebhook(owner, repository string) error {
|
||||
gc.makeSureClientExist()
|
||||
|
||||
// TODO: support for personal access tokens
|
||||
// We implicitly get support via. github apps
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gc *GitHubClient) AddComment(
|
||||
owner, repository string,
|
||||
pullRequest int,
|
||||
comment string,
|
||||
) (*models.AddCommentResponse, error) {
|
||||
gc.makeSureClientExist()
|
||||
|
||||
resp, _, err := gc.client.Issues.CreateComment(context.Background(), owner, repository, pullRequest, &github.IssueComment{
|
||||
Body: &comment,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &models.AddCommentResponse{
|
||||
Body: *resp.Body,
|
||||
ID: int(*resp.ID),
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user