Skip to content

Commit

Permalink
feat(build): add headref for pull requests (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanSussman authored Aug 12, 2020
1 parent fed87c6 commit 5f0912f
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion api/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func PostWebhook(c *gin.Context) {

// if this is a comment on a pull_request event
if strings.EqualFold(b.GetEvent(), constants.EventComment) && webhook.PRNumber > 0 {
commit, branch, baseref, err := source.FromContext(c).GetPullRequest(u, r, webhook.PRNumber)
commit, branch, baseref, headref, err := source.FromContext(c).GetPullRequest(u, r, webhook.PRNumber)
if err != nil {
retErr := fmt.Errorf("%s: failed to get pull request info for %s: %v", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand All @@ -279,6 +279,7 @@ func PostWebhook(c *gin.Context) {
b.SetCommit(commit)
b.SetBranch(strings.Replace(branch, "refs/heads/", "", -1))
b.SetBaseRef(baseref)
b.SetHeadRef(headref)
}

// send API call to capture the last build for the repo
Expand Down
1 change: 1 addition & 0 deletions database/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ func testBuild() *library.Build {
Branch: &str,
Ref: &str,
BaseRef: &str,
HeadRef: &str,
Host: &str,
Runtime: &str,
Distribution: &str,
Expand Down
1 change: 1 addition & 0 deletions database/ddl/postgres/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ builds (
branch VARCHAR(500),
ref VARCHAR(500),
base_ref VARCHAR(500),
head_ref VARCHAR(500),
host VARCHAR(250),
runtime VARCHAR(250),
distribution VARCHAR(250),
Expand Down
1 change: 1 addition & 0 deletions database/ddl/sqlite/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ builds (
branch TEXT,
ref TEXT,
base_ref TEXT,
head_ref TEXT,
host TEXT,
runtime TEXT,
distribution TEXT,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/gin-gonic/gin v1.6.3
github.com/go-redis/redis v6.15.8+incompatible
github.com/go-vela/compiler v0.5.0-rc1
github.com/go-vela/types v0.5.0-rc1
github.com/go-vela/types v0.5.0-rc1.0.20200811135328-b06ed6c89cb2
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 Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/go-vela/compiler v0.5.0-rc1 h1:/NZrlAkJmE58VMLQyb4xCTG1Q0frEvWbZT4Er2
github.com/go-vela/compiler v0.5.0-rc1/go.mod h1:vGA0cNBgAB+/vsUj6y5q7E+TAP6TI1+hFygePqcDuIU=
github.com/go-vela/types v0.5.0-rc1 h1:klHH5hYGb6JU+01gJxCgSe6d6DxxeU3FSpd9VjeLXNM=
github.com/go-vela/types v0.5.0-rc1/go.mod h1:qh+elSsJ5TrUItJw+HoEHfOUWs0lqkJuABuSTgp7fZQ=
github.com/go-vela/types v0.5.0-rc1.0.20200811135328-b06ed6c89cb2 h1:IAz5/LiLBE32T0zEEkzv6RSO2UhgMXL+wr9LK516ts8=
github.com/go-vela/types v0.5.0-rc1.0.20200811135328-b06ed6c89cb2/go.mod h1:qh+elSsJ5TrUItJw+HoEHfOUWs0lqkJuABuSTgp7fZQ=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
Expand Down
1 change: 1 addition & 0 deletions router/middleware/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestBuild_Establish(t *testing.T) {
want.SetBranch("")
want.SetRef("")
want.SetBaseRef("")
want.SetHeadRef("")
want.SetHost("")
want.SetRuntime("")
want.SetDistribution("")
Expand Down
7 changes: 4 additions & 3 deletions source/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,21 @@ func toLibraryRepo(gr github.Repository) *library.Repo {

// GetPullRequest defines a function that retrieves
// a pull request for a repo.
func (c *client) GetPullRequest(u *library.User, r *library.Repo, number int) (string, string, string, error) {
func (c *client) GetPullRequest(u *library.User, r *library.Repo, number int) (string, string, string, string, error) {
logrus.Tracef("Listing source repositories for %s", u.GetName())

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

pull, _, err := client.PullRequests.Get(ctx, r.GetOrg(), r.GetName(), number)
if err != nil {
return "", "", "", err
return "", "", "", "", err
}

commit := pull.GetHead().GetSHA()
branch := pull.GetBase().GetRef()
baseref := pull.GetBase().GetRef()
headref := pull.GetHead().GetRef()

return commit, branch, baseref, nil
return commit, branch, baseref, headref, nil
}
11 changes: 8 additions & 3 deletions source/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,12 @@ func TestGithub_GetPullRequest(t *testing.T) {
wantCommit := "6dcb09b5b57875f334f61aebed695e2e4193db5e"
wantBranch := "master"
wantBaseRef := "master"
wantHeadRef := "new-topic"

client, _ := NewTest(s.URL)

// run test
gotCommit, gotBranch, gotBaseRef, err := client.GetPullRequest(u, r, 1)
gotCommit, gotBranch, gotBaseRef, gotHeadRef, err := client.GetPullRequest(u, r, 1)

if err != nil {
t.Errorf("Status returned err: %v", err)
Expand All @@ -876,10 +877,14 @@ func TestGithub_GetPullRequest(t *testing.T) {
}

if !strings.EqualFold(gotBranch, wantBranch) {
t.Errorf("Branch is %v, want %v", gotCommit, wantCommit)
t.Errorf("Branch is %v, want %v", gotBranch, wantBranch)
}

if !strings.EqualFold(gotBaseRef, wantBaseRef) {
t.Errorf("BaseRef is %v, want %v", gotCommit, wantCommit)
t.Errorf("BaseRef is %v, want %v", gotBaseRef, wantBaseRef)
}

if !strings.EqualFold(gotHeadRef, wantHeadRef) {
t.Errorf("HeadRef is %v, want %v", gotHeadRef, wantHeadRef)
}
}
1 change: 1 addition & 0 deletions source/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func processPREvent(h *library.Hook, payload *github.PullRequestEvent) (*types.W
b.SetBranch(payload.GetPullRequest().GetBase().GetRef())
b.SetRef(fmt.Sprintf("refs/pull/%d/head", payload.GetNumber()))
b.SetBaseRef(payload.GetPullRequest().GetBase().GetRef())
b.SetHeadRef(payload.GetPullRequest().GetHead().GetRef())

// ensure the build reference is set
if payload.GetPullRequest().GetMerged() {
Expand Down
1 change: 1 addition & 0 deletions source/github/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) {
wantBuild.SetBranch("master")
wantBuild.SetRef("refs/pull/1/head")
wantBuild.SetBaseRef("master")
wantBuild.SetHeadRef("changes")

want := &types.Webhook{
Comment: "",
Expand Down
2 changes: 1 addition & 1 deletion source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type Service interface {
ListUserRepos(*library.User) ([]*library.Repo, error)
// GetPullRequest defines a function that retrieves
// a pull request for a repo.
GetPullRequest(*library.User, *library.Repo, int) (string, string, string, error)
GetPullRequest(*library.User, *library.Repo, int) (string, string, string, string, error)

// Webhook Source Interface Functions

Expand Down

0 comments on commit 5f0912f

Please sign in to comment.