From 6ddc2abf47cbf5d101106d966d90186941637de0 Mon Sep 17 00:00:00 2001 From: Jeff Storey Date: Tue, 29 Mar 2016 14:07:50 -0400 Subject: [PATCH 1/6] #1550 deployment status hooks for github --- remote/github/github.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/remote/github/github.go b/remote/github/github.go index fe7244bba..1f698ded8 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -236,7 +236,14 @@ func (g *Github) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ // An example would be the GitHub pull request status. func (g *Github) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { client := NewClient(g.API, u.Token, g.SkipVerify) + if ( b.Event == "deployment") { + return deploymentStatus(client,r,b,link) + } else { + return repoStatus(client,r,b,link) + } +} +func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error { status := getStatus(b.Status) desc := getDesc(b.Status) data := github.RepoStatus{ @@ -249,6 +256,21 @@ func (g *Github) Status(u *model.User, r *model.Repo, b *model.Build, link strin return err } +func deploymentStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error { + // the deployment ID is only available in the the link to the build as the last element in the URL + parts := strings.Split(b.Link,"/") + id, _ := strconv.Atoi(parts[len(parts)-1]) + status := getStatus(b.Status) + desc := getDesc(b.Status) + data := github.DeploymentStatusRequest{ + State: github.String(status), + Description: github.String(desc), + TargetURL: github.String(link), + } + _, _, err := client.Repositories.CreateDeploymentStatus(r.Owner, r.Name, id, &data) + return err +} + // Netrc returns a .netrc file that can be used to clone // private repositories from a remote system. func (g *Github) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) { From 3e4b871991936d8a32e3895228e3c7896d9f1828 Mon Sep 17 00:00:00 2001 From: Jeff Storey Date: Tue, 29 Mar 2016 20:05:28 -0400 Subject: [PATCH 2/6] adding check for github link --- remote/github/github.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/remote/github/github.go b/remote/github/github.go index 1f698ded8..867c23ec8 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "regexp" "strconv" "strings" @@ -25,6 +26,8 @@ const ( DefaultMergeRef = "merge" ) +var githubDeployRegex = regexp.MustCompile(".+/deployments/(\\d+)") + type Github struct { URL string API string @@ -236,10 +239,10 @@ func (g *Github) File(u *model.User, r *model.Repo, b *model.Build, f string) ([ // An example would be the GitHub pull request status. func (g *Github) Status(u *model.User, r *model.Repo, b *model.Build, link string) error { client := NewClient(g.API, u.Token, g.SkipVerify) - if ( b.Event == "deployment") { - return deploymentStatus(client,r,b,link) + if b.Event == "deployment" { + return deploymentStatus(client, r, b, link) } else { - return repoStatus(client,r,b,link) + return repoStatus(client, r, b, link) } } @@ -258,14 +261,19 @@ func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link strin func deploymentStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error { // the deployment ID is only available in the the link to the build as the last element in the URL - parts := strings.Split(b.Link,"/") + parts := strings.Split(b.Link, "/") + matches := githubDeployRegex.FindStringSubmatch(b.Link) + // if the deployment was not triggered from github, don't send a deployment status + if len(matches) != 2 { + return nil + } id, _ := strconv.Atoi(parts[len(parts)-1]) status := getStatus(b.Status) desc := getDesc(b.Status) data := github.DeploymentStatusRequest{ - State: github.String(status), - Description: github.String(desc), - TargetURL: github.String(link), + State: github.String(status), + Description: github.String(desc), + TargetURL: github.String(link), } _, _, err := client.Repositories.CreateDeploymentStatus(r.Owner, r.Name, id, &data) return err From e68ae7145bbe75381c5535a4dd6f434c197363e3 Mon Sep 17 00:00:00 2001 From: Jeff Storey Date: Tue, 29 Mar 2016 18:40:24 -0400 Subject: [PATCH 3/6] changing text based on whether this was a deployment --- static/scripts/repo.js | 11 +++++++++-- template/amber/build.amber | 10 ++++++++-- template/amber/repo.amber | 10 ++++++++-- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/static/scripts/repo.js b/static/scripts/repo.js index 9f89105c5..47d456fac 100644 --- a/static/scripts/repo.js +++ b/static/scripts/repo.js @@ -19,6 +19,13 @@ function RepoViewModel(repo) { // construct the build entry if it doesn't already exist // so that we can append to the DOM. The code may not be // pretty, but it is simple enough and it works. + authoredOrDeployed = "authored" + branchOrDeploy = data.branch + if ( data.event == "deployment" ) { + authoredOrDeployed = "deployed" + branchOrDeploy = data.deploy_to + } + el = $("").attr("class", "card").attr("href", "/"+repo+"/"+data.number).attr("data-build", data.number) .append( $("
").attr("class", "card-header").append( @@ -36,13 +43,13 @@ function RepoViewModel(repo) { $("

").attr("class","card-text").append( $("").text(data.author) ).append( - $("").text("authored") + $("").text(authoredOrDeployed) ).append( $("").attr("data-livestamp", data.created_at) ).append( $("").text("to") ).append( - $("").text(data.branch) + $("").text(branchOrDeploy) ) ) ).css("display", "flex").hide().fadeIn(1000); diff --git a/template/amber/build.amber b/template/amber/build.amber index 3d8cbc8d0..55a117c8b 100644 --- a/template/amber/build.amber +++ b/template/amber/build.amber @@ -26,10 +26,16 @@ block content a.material-icons[href=Build.Link][target="_blank"] link p em #{Build.Author} - span authored + if Build.Event != "deployment" + span authored + else + span deployed em[data-livestamp=Build.Created] span to - em #{Build.Branch} + if Build.Event != "deployment" + em #{Build.Branch} + else + em #{Build.Deploy} diff --git a/template/amber/repo.amber b/template/amber/repo.amber index d7358c69b..dd1fd79c6 100644 --- a/template/amber/repo.amber +++ b/template/amber/repo.amber @@ -42,10 +42,16 @@ block content h3 #{$build.Message} p.card-text em #{$build.Author} - span authored + if $build.Event != "deployment" + span authored + else + span deployed em[data-livestamp=$build.Created] span to - em #{$build.Branch} + if $build.Event != "deployment" + em ${build.Branch} + else + em #{$build.Deploy} block append scripts From 79dc866f437ea53e9794f83d0296ab089dc6c6cf Mon Sep 17 00:00:00 2001 From: Jeff Storey Date: Wed, 30 Mar 2016 07:58:24 -0400 Subject: [PATCH 4/6] getting rid of string split and using the regex match --- remote/github/github.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/remote/github/github.go b/remote/github/github.go index 867c23ec8..9ace24773 100644 --- a/remote/github/github.go +++ b/remote/github/github.go @@ -260,14 +260,13 @@ func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link strin } func deploymentStatus(client *github.Client, r *model.Repo, b *model.Build, link string) error { - // the deployment ID is only available in the the link to the build as the last element in the URL - parts := strings.Split(b.Link, "/") matches := githubDeployRegex.FindStringSubmatch(b.Link) // if the deployment was not triggered from github, don't send a deployment status if len(matches) != 2 { return nil } - id, _ := strconv.Atoi(parts[len(parts)-1]) + // the deployment ID is only available in the the link to the build as the last element in the URL + id, _ := strconv.Atoi(matches[1]) status := getStatus(b.Status) desc := getDesc(b.Status) data := github.DeploymentStatusRequest{ From 442fc72eee0187f0d10ee0cfa155e16493137e23 Mon Sep 17 00:00:00 2001 From: Jeff Storey Date: Wed, 30 Mar 2016 08:02:42 -0400 Subject: [PATCH 5/6] variable scoping --- static/scripts/repo.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/scripts/repo.js b/static/scripts/repo.js index 47d456fac..582e8d1dc 100644 --- a/static/scripts/repo.js +++ b/static/scripts/repo.js @@ -19,8 +19,8 @@ function RepoViewModel(repo) { // construct the build entry if it doesn't already exist // so that we can append to the DOM. The code may not be // pretty, but it is simple enough and it works. - authoredOrDeployed = "authored" - branchOrDeploy = data.branch + var authoredOrDeployed = "authored" + var branchOrDeploy = data.branch if ( data.event == "deployment" ) { authoredOrDeployed = "deployed" branchOrDeploy = data.deploy_to From c2e83071d1b386ab85ccb75e87e9d7545acdc1eb Mon Sep 17 00:00:00 2001 From: Jeff Storey Date: Wed, 30 Mar 2016 15:22:18 -0400 Subject: [PATCH 6/6] fixing build branch variable substitution --- template/amber/repo.amber | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/amber/repo.amber b/template/amber/repo.amber index dd1fd79c6..665226bad 100644 --- a/template/amber/repo.amber +++ b/template/amber/repo.amber @@ -49,7 +49,7 @@ block content em[data-livestamp=$build.Created] span to if $build.Event != "deployment" - em ${build.Branch} + em #{$build.Branch} else em #{$build.Deploy}