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 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 return nil
} }

View File

@ -16,6 +16,7 @@ package gitea
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
@ -55,7 +56,7 @@ func fromCommitStatus(status gitsource.CommitStatus) gitea.StatusState {
case gitsource.CommitStatusFailed: case gitsource.CommitStatusFailed:
return gitea.StatusFailure return gitea.StatusFailure
default: 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 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) { 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) { switch r.Header.Get(hookEvent) {
case hookPush: case hookPush:
return parsePushHook(r.Body) return parsePushHook(r.Body)

View File

@ -26,7 +26,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
gitsource "github.com/sorintlab/agola/internal/gitsources" gitsource "github.com/sorintlab/agola/internal/gitsources"
"github.com/sorintlab/agola/internal/services/types"
gitlab "github.com/xanzy/go-gitlab" gitlab "github.com/xanzy/go-gitlab"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
@ -57,6 +56,20 @@ type Client struct {
oauth2Secret string 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) { func New(opts Opts) (*Client, error) {
// copied from net/http until it has a clone function: https://github.com/golang/go/issues/26013 // copied from net/http until it has a clone function: https://github.com/golang/go/issues/26013
transport := &http.Transport{ transport := &http.Transport{
@ -235,6 +248,12 @@ func (c *Client) DeleteRepoWebhook(repopath, u string) error {
return nil return nil
} }
func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) { func (c *Client) CreateCommitStatus(repopath, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error {
return parseWebhook(r) _, _, 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" 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) { switch r.Header.Get(hookEvent) {
case hookPush: case hookPush:
return parsePushHook(r.Body) return parsePushHook(r.Body)

View File

@ -38,6 +38,7 @@ type GitSource interface {
DeleteRepoWebhook(repopath, url string) error DeleteRepoWebhook(repopath, url string) error
CreateRepoWebhook(repopath, url, secret string) error CreateRepoWebhook(repopath, url, secret string) error
ParseWebhook(r *http.Request) (*types.WebhookData, error) ParseWebhook(r *http.Request) (*types.WebhookData, error)
CreateCommitStatus(repopath, commitSHA string, status CommitStatus, targetURL, description, context string) error
} }
type UserSource interface { type UserSource interface {