Skip to content

Commit

Permalink
Fix branch retrieval from webhook event
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Mar 8, 2018
1 parent f32beb7 commit e88d1b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 6 additions & 0 deletions common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,9 @@ func SimplifyGitErr(err error) error {
}
return nil
}

// GetBranchFromRef gets the branch name from a git ref of form refs/...
func GetBranchFromRef(ref string) string {
parts := strings.Split(ref, "/")
return parts[len(parts)-1]
}
5 changes: 5 additions & 0 deletions common/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ func TestGetSSHRemoteURL(t *testing.T) {
assert.Equal(t, sshURL, GetSSHRemoteURL(httpsURL))
assert.Equal(t, sshURL, GetSSHRemoteURL(sshURL))
}

func TestGetBranchFromRef(t *testing.T) {
branch := GetBranchFromRef("refs/heads/master")
assert.Equal(t, "master", branch)
}
12 changes: 6 additions & 6 deletions daemon/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
// processPushEvent prints information about the given PushEvent.
func processPushEvent(event *github.PushEvent) {
repo := event.GetRepo()
branch := common.GetBranchFromRef(event.GetRef())
println("Received PushEvent")
println(fmt.Sprintf("Repository Name: %s", *repo.Name))
println(fmt.Sprintf("Repository Git URL: %s", *repo.GitURL))
println(fmt.Sprintf("Branch: %s", event.GetBaseRef()))
println(fmt.Sprintf("Branch: %s", branch))

// Ignore event if repository not set up yet, otherwise
// let deploy() handle the update.
Expand Down Expand Up @@ -48,23 +49,22 @@ func processPushEvent(event *github.PushEvent) {
println(err)
return
}
if head.Name().Short() == event.GetBaseRef() {
if head.Name().Short() == branch {
println("Event branch matches deployed branch " + head.Name().Short())
cli, err := docker.NewEnvClient()
if err != nil {
println(err)
return
}
defer cli.Close()
err = deploy(localRepo, event.GetBaseRef(), cli, os.Stdout)
err = deploy(localRepo, branch, cli, os.Stdout)
if err != nil {
println(err)
}
} else {
println(
"Event branch " + head.Name().Short() +
" does not match deployed branch " +
event.GetBaseRef() + " - ignoring event.",
"Event branch " + head.Name().Short() + " does not match deployed branch " +
branch + " - ignoring event.",
)
}
}
Expand Down

0 comments on commit e88d1b8

Please sign in to comment.