Merge pull request #1258 from bradrydzewski/master

parse deploy hooks into Builds for #1144
This commit is contained in:
Brad Rydzewski 2015-10-26 21:06:01 -07:00
commit f235f0b413
5 changed files with 93 additions and 9 deletions

View file

@ -43,15 +43,15 @@ func ShowIndex(c *gin.Context) {
// for each repository in the remote system we get // for each repository in the remote system we get
// the intersection of those repostiories in Drone // the intersection of those repostiories in Drone
repos_, err := store.GetRepoListOf(c, repos) // repos_, err := store.GetRepoListOf(c, repos)
if err != nil { // if err != nil {
log.Errorf("Failure to get repository list for %s. %s.", // log.Errorf("Failure to get repository list for %s. %s.",
user.Login, err) // user.Login, err)
} // }
c.HTML(200, "repos.html", gin.H{ c.HTML(200, "repos.html", gin.H{
"User": user, "User": user,
"Repos": repos_, "Repos": repos,
}) })
} }

View file

@ -291,6 +291,8 @@ func (g *Github) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
return g.pullRequest(r) return g.pullRequest(r)
case "push": case "push":
return g.push(r) return g.push(r)
case "deployment":
return g.deployment(r)
default: default:
return nil, nil, nil return nil, nil, nil
} }
@ -403,6 +405,56 @@ func (g *Github) pullRequest(r *http.Request) (*model.Repo, *model.Build, error)
return repo, build, nil 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 ( const (
StatusPending = "pending" StatusPending = "pending"
StatusSuccess = "success" StatusSuccess = "success"

View file

@ -29,7 +29,39 @@ type pushHook struct {
Sender struct { Sender struct {
Login string `json:"login"` Login string `json:"login"`
Avatar string `json:"avatar_url"` Avatar string `json:"avatar_url"`
} } `json:"sender"`
Repo struct {
Owner struct {
Login string `json:"login"`
Name string `json:"name"`
} `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Language string `json:"language"`
Private bool `json:"private"`
HTMLURL string `json:"html_url"`
CloneURL string `json:"clone_url"`
DefaultBranch string `json:"default_branch"`
} `json:"repository"`
}
type deployHook struct {
Deployment struct {
ID int64 `json:"id"`
Sha string `json:"sha"`
Ref string `json:"ref"`
Task string `json:"task"`
Env string `json:"environment"`
Url string `json:"url"`
Desc string `json:"description"`
} `json:"deployment"`
Sender struct {
Login string `json:"login"`
Avatar string `json:"avatar_url"`
} `json:"sender"`
Repo struct { Repo struct {
Owner struct { Owner struct {

View file

@ -10,7 +10,7 @@
border-radius: 0px; border-radius: 0px;
border-top: 1px solid #eceeef; border-top: 1px solid #eceeef;
text-decoration: none; text-decoration: none;
color: 2b303b; color: #2b303b;
.card-header .card-header
background: #FFF; background: #FFF;
border: none; border: none;

View file

@ -88,7 +88,7 @@ input[type=range]:focus::-ms-fill-upper { background: #367ebd; }
.timeline { padding-left: 50px; position: relative; margin-top: 10px; margin-bottom: 40px; } .timeline { padding-left: 50px; position: relative; margin-top: 10px; margin-bottom: 40px; }
.timeline .card { display: flex; border: none; border-radius: 0px; border-top: 1px solid #eceeef; text-decoration: none; color: 2b303b; } .timeline .card { display: flex; border: none; border-radius: 0px; border-top: 1px solid #eceeef; text-decoration: none; color: #2b303b; }
.timeline .card .card-header { background: #FFF; border: none; padding: 0px; width: 50px; min-width: 50px; max-width: 50px; padding-top: 30px; } .timeline .card .card-header { background: #FFF; border: none; padding: 0px; width: 50px; min-width: 50px; max-width: 50px; padding-top: 30px; }