Skip to content

Commit

Permalink
feat(source): support github deployments (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp authored Apr 24, 2020
1 parent 0da8e57 commit 00626f0
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/gin-gonic/gin v1.5.0
github.com/go-redis/redis v6.15.6+incompatible
github.com/go-vela/compiler v0.4.0-rc1
github.com/go-vela/types v0.4.0-rc1
github.com/go-vela/types v0.4.0-rc1.0.20200423134744-338fccc2db97
github.com/google/go-github/v29 v29.0.3
github.com/google/uuid v1.1.1
github.com/hashicorp/go-hclog v0.10.0 // indirect
Expand All @@ -34,4 +34,5 @@ require (
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
gopkg.in/square/go-jose.v2 v2.4.0 // indirect
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
gopkg.in/yaml.v2 v2.2.8 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ github.com/go-vela/compiler v0.4.0-rc1 h1:CN7MnHcznakQZBhle75WrCHLiIVPB/iUr/CTsD
github.com/go-vela/compiler v0.4.0-rc1/go.mod h1:mEBy+f9nPTVJSDOsRxzWmXs7DZjXYCCvJQVggXnYD5E=
github.com/go-vela/types v0.4.0-rc1 h1:pXHj1pcCCWiBiReAPpACK2V0qltQaBlqf9IPro24FlQ=
github.com/go-vela/types v0.4.0-rc1/go.mod h1:vph+YTAkIw0fJr7gmlIjXRIuc4QXUnQvdDvH2/Oh7ts=
github.com/go-vela/types v0.4.0-rc1.0.20200423134744-338fccc2db97 h1:RcVNqJpET1YUgTDDDa5XiVk0DJ4IcFr7fOlK60hwDCE=
github.com/go-vela/types v0.4.0-rc1.0.20200423134744-338fccc2db97/go.mod h1:ig7fC+MKGsgAlKqovkFsUQlbZtLDLhl/djiedHmsL5s=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
Expand Down Expand Up @@ -407,6 +409,8 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
111 changes: 111 additions & 0 deletions source/github/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package github

import (
"github.com/google/go-github/v29/github"

"github.com/go-vela/types/library"

"github.com/sirupsen/logrus"
)

// GetDeployment gets a deployment from the GitHub repo.
func (c *client) GetDeployment(u *library.User, r *library.Repo, id int64) (*library.Deployment, error) {
logrus.Tracef("capturing deployment %d for %s", id, r.GetFullName())

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// send API call to capture the deployment
deployment, _, err := client.Repositories.GetDeployment(ctx, r.GetOrg(), r.GetName(), id)
if err != nil {
return nil, err
}

return &library.Deployment{
ID: deployment.ID,
RepoID: r.ID,
URL: deployment.URL,
User: deployment.Creator.Login,
Commit: deployment.SHA,
Ref: deployment.Ref,
Task: deployment.Task,
Target: deployment.Environment,
Description: deployment.Description,
}, nil
}

// GetDeployment gets a list of deployments from the GitHub repo.
func (c *client) GetDeploymentList(u *library.User, r *library.Repo, page, perPage int) ([]*library.Deployment, error) {
logrus.Tracef("capturing deployments for %s", r.GetFullName())

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// set pagination options for listing deployments
opts := &github.DeploymentsListOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: perPage,
},
}

// send API call to capture the list of deployments
d, _, err := client.Repositories.ListDeployments(ctx, r.GetOrg(), r.GetName(), opts)
if err != nil {
return nil, err
}

// variable we want to return
deployments := []*library.Deployment{}

// iterate through all API results
for _, deployment := range d {
// convert query result to library type
deployments = append(deployments, &library.Deployment{
ID: deployment.ID,
RepoID: r.ID,
URL: deployment.URL,
User: deployment.Creator.Login,
Commit: deployment.SHA,
Ref: deployment.Ref,
Task: deployment.Task,
Target: deployment.Environment,
Description: deployment.Description,
})
}

return deployments, nil
}

// CreateDeployment creates a new deployment for the GitHub repo.
func (c *client) CreateDeployment(u *library.User, r *library.Repo, d *library.Deployment) error {
logrus.Tracef("creating deployment for %s", r.GetFullName())

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// create the hook object to make the API call
deployment := &github.DeploymentRequest{
Ref: d.Ref,
Task: d.Task,
AutoMerge: github.Bool(true),
RequiredContexts: &[]string{""},
Payload: github.String(""),
Environment: d.Target,
Description: d.Description,
TransientEnvironment: github.Bool(false),
ProductionEnvironment: github.Bool(false),
}

// send API call to create the deployment
_, _, err := client.Repositories.CreateDeployment(ctx, r.GetOrg(), r.GetName(), deployment)
if err != nil {
return err
}

return nil
}
185 changes: 185 additions & 0 deletions source/github/deployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package github

import (
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/gin-gonic/gin"

"github.com/go-vela/types/library"
)

func TestGithub_CreateDeployment(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.POST("/api/v3/repos/:org/:repo/deployments", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/deployment.json")
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
u := new(library.User)
u.SetName("foo")
u.SetToken("bar")

r := new(library.Repo)
r.SetID(1)
r.SetOrg("foo")
r.SetName("bar")
r.SetFullName("foo/bar")

d := new(library.Deployment)
d.SetID(1)
d.SetRepoID(1)
d.SetURL("https://api.github.com/repos/foo/bar/deployments/1")
d.SetUser("octocat")
d.SetCommit("a84d88e7554fc1fa21bcbc4efae3c782a70d2b9d")
d.SetRef("topic-branch")
d.SetTask("deploy")
d.SetTarget("production")
d.SetDescription("Deploy request from Vela")

client, _ := NewTest(s.URL, "https://foo.bar.com")

// run test
err := client.CreateDeployment(u, r, d)

if resp.Code != http.StatusOK {
t.Errorf("CreateDeployment returned %v, want %v", resp.Code, http.StatusOK)
}

if err != nil {
t.Errorf("CreateDeployment returned err: %v", err)
}
}

func TestGithub_GetDeployment(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.GET("/api/v3/repos/:org/:repo/deployments/:deployment", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/deployment.json")
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
u := new(library.User)
u.SetName("foo")
u.SetToken("bar")

r := new(library.Repo)
r.SetID(1)
r.SetOrg("foo")
r.SetName("bar")
r.SetFullName("foo/bar")

want := new(library.Deployment)
want.SetID(1)
want.SetRepoID(1)
want.SetURL("https://api.github.com/repos/foo/bar/deployments/1")
want.SetUser("octocat")
want.SetCommit("a84d88e7554fc1fa21bcbc4efae3c782a70d2b9d")
want.SetRef("topic-branch")
want.SetTask("deploy")
want.SetTarget("production")
want.SetDescription("Deploy request from Vela")

client, _ := NewTest(s.URL, "https://foo.bar.com")

// run test
got, err := client.GetDeployment(u, r, 1)

if resp.Code != http.StatusOK {
t.Errorf("GetDeployment returned %v, want %v", resp.Code, http.StatusOK)
}

if err != nil {
t.Errorf("GetDeployment returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("GetDeployment is %v, want %v", got, want)
}
}

func TestGithub_GetDeploymentList(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
_, engine := gin.CreateTestContext(resp)

// setup mock server
engine.GET("/api/v3/repos/:org/:repo/deployments", func(c *gin.Context) {
c.Header("Content-Type", "application/json")
c.Status(http.StatusOK)
c.File("testdata/deployments.json")
})

s := httptest.NewServer(engine)
defer s.Close()

// setup types
u := new(library.User)
u.SetName("foo")
u.SetToken("bar")

r := new(library.Repo)
r.SetID(1)
r.SetOrg("foo")
r.SetName("bar")
r.SetFullName("foo/bar")

d := new(library.Deployment)
d.SetID(1)
d.SetRepoID(1)
d.SetURL("https://api.github.com/repos/foo/bar/deployments/1")
d.SetUser("octocat")
d.SetCommit("a84d88e7554fc1fa21bcbc4efae3c782a70d2b9d")
d.SetRef("topic-branch")
d.SetTask("deploy")
d.SetTarget("production")
d.SetDescription("Deploy request from Vela")

want := []*library.Deployment{d}

client, _ := NewTest(s.URL, "https://foo.bar.com")

// run test
got, err := client.GetDeploymentList(u, r, 1, 100)

if resp.Code != http.StatusOK {
t.Errorf("GetDeployment returned %v, want %v", resp.Code, http.StatusOK)
}

if err != nil {
t.Errorf("GetDeployment returned err: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("GetDeployment is %v, want %v", got, want)
}
}
5 changes: 5 additions & 0 deletions source/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
func (c *client) Config(u *library.User, org, name, ref string) ([]byte, error) {
logrus.Tracef("Capturing configuration file for %s/%s/commit/%s", org, name, ref)

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// set the reference for the options to capture the pipeline configuration
Expand Down Expand Up @@ -70,6 +71,7 @@ func (c *client) Config(u *library.User, org, name, ref string) ([]byte, error)
func (c *client) Disable(u *library.User, org, name string) error {
logrus.Tracef("Deleting repository webhook for %s/%s", org, name)

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// send API call to capture the hooks for the repo
Expand Down Expand Up @@ -112,6 +114,7 @@ func (c *client) Disable(u *library.User, org, name string) error {
func (c *client) Enable(u *library.User, org, name, secret string) (string, error) {
logrus.Tracef("Creating repository webhook for %s/%s", org, name)

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

// create the hook object to make the API call
Expand Down Expand Up @@ -150,6 +153,7 @@ func (c *client) Enable(u *library.User, org, name, secret string) (string, erro
func (c *client) Status(u *library.User, b *library.Build, org, name string) error {
logrus.Tracef("Setting commit status for %s/%s/%d @ %s", org, name, b.GetNumber(), b.GetCommit())

// create GitHub OAuth client with user's token
client := c.newClientToken(*u.Token)

context := fmt.Sprintf("%s/%s", c.StatusContext, b.GetEvent())
Expand Down Expand Up @@ -204,6 +208,7 @@ func (c *client) ListUserRepos(u *library.User) ([]*library.Repo, error) {

// create GitHub OAuth client with user's token
client := c.newClientToken(u.GetToken())

r := []*github.Repository{}
f := []*library.Repo{}

Expand Down
Loading

0 comments on commit 00626f0

Please sign in to comment.