2021-07-12 12:36:22 +02:00
|
|
|
package gitea_sdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gitea-sonarqube-pr-bot/internal/settings"
|
2021-10-09 20:45:21 +02:00
|
|
|
webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube"
|
2021-10-03 17:57:22 +02:00
|
|
|
|
2021-07-12 12:36:22 +02:00
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GiteaSdkInterface interface {
|
|
|
|
PostComment(settings.GiteaRepository, int, string) error
|
2021-10-09 20:45:21 +02:00
|
|
|
UpdateStatus(settings.GiteaRepository, *webhook.Webhook) error
|
2021-07-12 12:36:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type GiteaSdk struct {
|
|
|
|
client *gitea.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sdk *GiteaSdk) PostComment(repo settings.GiteaRepository, idx int, msg string) error {
|
|
|
|
opt := gitea.CreateIssueCommentOption{
|
|
|
|
Body: msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := sdk.client.CreateIssueComment(repo.Owner, repo.Name, int64(idx), opt)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-09 20:45:21 +02:00
|
|
|
func (sdk *GiteaSdk) UpdateStatus(repo settings.GiteaRepository, w *webhook.Webhook) error {
|
|
|
|
status := gitea.StatusPending
|
|
|
|
switch w.QualityGate.Status {
|
|
|
|
case "OK":
|
|
|
|
status = gitea.StatusSuccess
|
|
|
|
case "ERROR":
|
|
|
|
status = gitea.StatusFailure
|
|
|
|
}
|
|
|
|
opt := gitea.CreateStatusOption{
|
|
|
|
TargetURL: w.Branch.Url,
|
|
|
|
Context: "gitea-sonarqube-pr-bot",
|
|
|
|
Description: w.QualityGate.Status,
|
|
|
|
State: status,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := sdk.client.CreateStatus(repo.Owner, repo.Name, w.Revision, opt)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-12 12:36:22 +02:00
|
|
|
func New() *GiteaSdk {
|
|
|
|
client, err := gitea.NewClient(settings.Gitea.Url, gitea.SetToken(settings.Gitea.Token.Value))
|
|
|
|
if err != nil {
|
2021-10-09 18:09:54 +02:00
|
|
|
panic(fmt.Errorf("cannot initialize Gitea client: %w", err))
|
2021-07-12 12:36:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &GiteaSdk{client}
|
|
|
|
}
|