From 507a9edb87380cec71604867aafd886f46fbb699 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Mon, 29 Apr 2019 17:35:07 +0200 Subject: [PATCH] gitsources: add CreateCommitStatus method --- internal/gitsources/agolagit/agolagit.go | 2 +- internal/gitsources/gitea/gitea.go | 17 +++++++++++++++- internal/gitsources/gitea/parse.go | 4 ---- internal/gitsources/gitlab/gitlab.go | 25 +++++++++++++++++++++--- internal/gitsources/gitlab/parse.go | 2 +- internal/gitsources/gitsource.go | 1 + 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/internal/gitsources/agolagit/agolagit.go b/internal/gitsources/agolagit/agolagit.go index 267bb37..e0f35a9 100644 --- a/internal/gitsources/agolagit/agolagit.go +++ b/internal/gitsources/agolagit/agolagit.go @@ -162,7 +162,7 @@ func (c *Client) DeleteRepoWebhook(repopath, u string) error { return nil } -func (c *Client) CreateStatus(repopath, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error { +func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error { return nil } diff --git a/internal/gitsources/gitea/gitea.go b/internal/gitsources/gitea/gitea.go index 12cd964..7de2f9e 100644 --- a/internal/gitsources/gitea/gitea.go +++ b/internal/gitsources/gitea/gitea.go @@ -16,6 +16,7 @@ package gitea import ( "crypto/tls" + "fmt" "net" "net/http" "strconv" @@ -55,7 +56,7 @@ func fromCommitStatus(status gitsource.CommitStatus) gitea.StatusState { case gitsource.CommitStatusFailed: return gitea.StatusFailure default: - return gitea.StatusError + panic(fmt.Errorf("unknown commit status %q", status)) } } @@ -273,3 +274,17 @@ func (c *Client) DeleteRepoWebhook(repopath, u string) error { return nil } + +func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error { + owner, reponame, err := parseRepoPath(repopath) + if err != nil { + return err + } + _, err = c.client.CreateStatus(owner, reponame, commitSHA, gitea.CreateStatusOption{ + State: fromCommitStatus(status), + TargetURL: targetURL, + Description: description, + Context: context, + }) + return err +} diff --git a/internal/gitsources/gitea/parse.go b/internal/gitsources/gitea/parse.go index 4a97769..cba3c63 100644 --- a/internal/gitsources/gitea/parse.go +++ b/internal/gitsources/gitea/parse.go @@ -41,10 +41,6 @@ const ( ) func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) { - return parseWebhook(r) -} - -func parseWebhook(r *http.Request) (*types.WebhookData, error) { switch r.Header.Get(hookEvent) { case hookPush: return parsePushHook(r.Body) diff --git a/internal/gitsources/gitlab/gitlab.go b/internal/gitsources/gitlab/gitlab.go index 3c492b0..c651896 100644 --- a/internal/gitsources/gitlab/gitlab.go +++ b/internal/gitsources/gitlab/gitlab.go @@ -26,7 +26,6 @@ import ( "github.com/pkg/errors" gitsource "github.com/sorintlab/agola/internal/gitsources" - "github.com/sorintlab/agola/internal/services/types" gitlab "github.com/xanzy/go-gitlab" "golang.org/x/oauth2" ) @@ -57,6 +56,20 @@ type Client struct { oauth2Secret string } +// fromCommitStatus converts a gitsource commit status to a gitea commit status +func fromCommitStatus(status gitsource.CommitStatus) gitlab.BuildStateValue { + switch status { + case gitsource.CommitStatusPending: + return gitlab.Pending + case gitsource.CommitStatusSuccess: + return gitlab.Success + case gitsource.CommitStatusFailed: + return gitlab.Failed + default: + panic(fmt.Errorf("unknown commit status %q", status)) + } +} + func New(opts Opts) (*Client, error) { // copied from net/http until it has a clone function: https://github.com/golang/go/issues/26013 transport := &http.Transport{ @@ -235,6 +248,12 @@ func (c *Client) DeleteRepoWebhook(repopath, u string) error { return nil } -func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) { - return parseWebhook(r) +func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error { + _, _, err := c.client.Commits.SetCommitStatus(repopath, commitSHA, &gitlab.SetCommitStatusOptions{ + State: fromCommitStatus(status), + TargetURL: gitlab.String(targetURL), + Description: gitlab.String(description), + Context: gitlab.String(context), + }) + return err } diff --git a/internal/gitsources/gitlab/parse.go b/internal/gitsources/gitlab/parse.go index f2a5ae9..5dbaacb 100644 --- a/internal/gitsources/gitlab/parse.go +++ b/internal/gitsources/gitlab/parse.go @@ -40,7 +40,7 @@ const ( prActionSync = "synchronized" ) -func parseWebhook(r *http.Request) (*types.WebhookData, error) { +func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) { switch r.Header.Get(hookEvent) { case hookPush: return parsePushHook(r.Body) diff --git a/internal/gitsources/gitsource.go b/internal/gitsources/gitsource.go index aaff4f6..2fa91d7 100644 --- a/internal/gitsources/gitsource.go +++ b/internal/gitsources/gitsource.go @@ -38,6 +38,7 @@ type GitSource interface { DeleteRepoWebhook(repopath, url string) error CreateRepoWebhook(repopath, url, secret string) error ParseWebhook(r *http.Request) (*types.WebhookData, error) + CreateCommitStatus(repopath, commitSHA string, status CommitStatus, targetURL, description, context string) error } type UserSource interface {