Add modifiable context for Github status API

This commit is contained in:
Fabio Rapposelli 2016-05-27 20:22:32 +02:00
parent c888fb3152
commit 837446825d
No known key found for this signature in database
GPG key ID: 3BF303AC750875CE
4 changed files with 16 additions and 4 deletions

View file

@ -99,6 +99,12 @@ var serverCmd = cli.Command{
Usage: "github server address",
Value: "https://github.com",
},
cli.StringFlag{
EnvVar: "DRONE_GITHUB_CONTEXT",
Name: "github-context",
Usage: "github status context",
Value: "continuous-integration/drone",
},
cli.StringFlag{
EnvVar: "DRONE_GITHUB_CLIENT",
Name: "github-client",

View file

@ -27,6 +27,7 @@ const (
// Opts defines configuration options.
type Opts struct {
URL string // GitHub server url.
Context string // Context to display in status check
Client string // GitHub oauth client id.
Secret string // GitHub oauth client secret.
Scopes []string // GitHub oauth scopes
@ -51,6 +52,7 @@ func New(opts Opts) (remote.Remote, error) {
remote := &client{
API: defaultAPI,
URL: defaultURL,
Context: opts.Context,
Client: opts.Client,
Secret: opts.Secret,
Scopes: opts.Scopes,
@ -70,6 +72,7 @@ func New(opts Opts) (remote.Remote, error) {
type client struct {
URL string
Context string
API string
Client string
Secret string
@ -261,7 +264,7 @@ func (c *client) newConfig(redirect string) *oauth2.Config {
return &oauth2.Config{
ClientID: c.Client,
ClientSecret: c.Secret,
Scopes: c.Scopes,
Scopes: c.Scopes,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", c.URL),
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", c.URL),
@ -345,12 +348,12 @@ func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link strin
case "deployment":
return deploymentStatus(client, r, b, link)
default:
return repoStatus(client, r, b, link)
return repoStatus(client, r, b, link, c.Context)
}
}
func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error {
context := "continuous-integration/drone"
func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link, ctx string) error {
context := ctx
switch b.Event {
case model.EventPull:
context += "/pr"

View file

@ -37,6 +37,7 @@ func Test_github(t *testing.T) {
Password: "password",
SkipVerify: true,
PrivateMode: true,
Context: "continuous-integration/test",
})
g.Assert(remote.(*client).URL).Equal("http://localhost:8080")
g.Assert(remote.(*client).API).Equal("http://localhost:8080/api/v3/")
@ -47,6 +48,7 @@ func Test_github(t *testing.T) {
g.Assert(remote.(*client).Secret).Equal("I1NiIsInR5")
g.Assert(remote.(*client).SkipVerify).Equal(true)
g.Assert(remote.(*client).PrivateMode).Equal(true)
g.Assert(remote.(*client).Context).Equal("continuous-integration/test")
})
g.It("Should handle malformed url", func() {
_, err := New(Opts{URL: "%gh&%ij"})

View file

@ -92,6 +92,7 @@ func setupGitlab(c *cli.Context) (remote.Remote, error) {
func setupGithub(c *cli.Context) (remote.Remote, error) {
return github.New(github.Opts{
URL: c.String("github-server"),
Context: c.String("github-context"),
Client: c.String("github-client"),
Secret: c.String("github-sercret"),
Scopes: c.StringSlice("github-scope"),