Skip to content

Commit

Permalink
parses github hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Oct 27, 2015
1 parent 7daa32b commit e719052
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions remote/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ func (g *Github) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
return g.pullRequest(r)
case "push":
return g.push(r)
case "deployment":
return g.deployment(r)
default:
return nil, nil, nil
}
Expand Down Expand Up @@ -403,6 +405,56 @@ func (g *Github) pullRequest(r *http.Request) (*model.Repo, *model.Build, error)
return repo, build, nil
}

func (g *Github) deployment(r *http.Request) (*model.Repo, *model.Build, error) {
payload := GetPayload(r)
hook := &deployHook{}

err := json.Unmarshal(payload, hook)
if err != nil {
return nil, nil, err
}

repo := &model.Repo{}
repo.Owner = hook.Repo.Owner.Login
if len(repo.Owner) == 0 {
repo.Owner = hook.Repo.Owner.Name
}
repo.Name = hook.Repo.Name
repo.FullName = fmt.Sprintf("%s/%s", repo.Owner, repo.Name)
repo.Link = hook.Repo.HTMLURL
repo.IsPrivate = hook.Repo.Private
repo.Clone = hook.Repo.CloneURL
repo.Branch = hook.Repo.DefaultBranch

// ref can be
// branch, tag, or sha

build := &model.Build{}
build.Event = model.EventDeploy
build.Commit = hook.Deployment.Sha
build.Link = hook.Deployment.Url
build.Message = hook.Deployment.Desc
build.Avatar = hook.Sender.Avatar
build.Author = hook.Sender.Login
build.Ref = hook.Deployment.Ref
build.Branch = hook.Deployment.Ref

// if the ref is a sha or short sha we need to manually
// construct the ref.
if strings.HasPrefix(build.Commit, build.Ref) || build.Commit == build.Ref {
build.Branch = repo.Branch
build.Ref = fmt.Sprintf("refs/heads/%s", repo.Branch)

}
// if the ref is a branch we should make sure it has refs/heads prefix
if !strings.HasPrefix(build.Ref, "refs/") { // branch or tag
build.Ref = fmt.Sprintf("refs/heads/%s", build.Branch)

}

return repo, build, nil
}

const (
StatusPending = "pending"
StatusSuccess = "success"
Expand Down

0 comments on commit e719052

Please sign in to comment.