gitsources: add CreateCommitStatus method

This commit is contained in:
Simone Gotti 2019-04-29 17:35:07 +02:00
parent 2fab8ad85b
commit 507a9edb87
6 changed files with 41 additions and 10 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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)

View File

@ -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 {