-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(source): support github deployments (#138)
- Loading branch information
Showing
8 changed files
with
401 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.