diff --git a/internal/clients/gitea_sdk/gitea_sdk.go b/internal/clients/gitea_sdk/gitea_sdk.go index e879761..068bd9a 100644 --- a/internal/clients/gitea_sdk/gitea_sdk.go +++ b/internal/clients/gitea_sdk/gitea_sdk.go @@ -3,12 +3,14 @@ package gitea_sdk import ( "fmt" "gitea-sonarqube-pr-bot/internal/settings" + webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube" "code.gitea.io/sdk/gitea" ) type GiteaSdkInterface interface { PostComment(settings.GiteaRepository, int, string) error + UpdateStatus(settings.GiteaRepository, *webhook.Webhook) error } type GiteaSdk struct { @@ -25,6 +27,26 @@ func (sdk *GiteaSdk) PostComment(repo settings.GiteaRepository, idx int, msg str return err } +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 +} + func New() *GiteaSdk { client, err := gitea.NewClient(settings.Gitea.Url, gitea.SetToken(settings.Gitea.Token.Value)) if err != nil { diff --git a/internal/webhook_handler/sonarqube.go b/internal/webhook_handler/sonarqube.go index 2cb6f8a..e901cbf 100644 --- a/internal/webhook_handler/sonarqube.go +++ b/internal/webhook_handler/sonarqube.go @@ -54,6 +54,11 @@ func (h *SonarQubeWebhookHandler) processData(w *webhook.Webhook, repo settings. h.fetchDetails(w) + err := h.giteaSdk.UpdateStatus(repo, w) + if err != nil { + log.Printf("Error updating status: %s", err.Error()) + } + comment, err := h.composeGiteaComment(w) if err != nil { log.Printf("Error composing Gitea comment: %s", err.Error())