From 888088946b376cecd55eeeae84fc3bad965236a7 Mon Sep 17 00:00:00 2001 From: Lukas Matt Date: Sat, 17 Mar 2018 12:51:20 +0100 Subject: [PATCH] Implement opt-in/out for pull requests --- frontend.go | 6 ++++++ repo.go | 1 + templates/auth.html | 10 ++++++++++ webhook.go | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/frontend.go b/frontend.go index 5c694af..316dcd6 100644 --- a/frontend.go +++ b/frontend.go @@ -104,12 +104,18 @@ func resultPage(w http.ResponseWriter, r *http.Request) { return } + var optIn bool + if strings.ToUpper(r.URL.Query().Get("optin")) == "ON" { + optIn = true + } + secret := Secret(16) repo := Repo{ Project: project, Slug: repo, Token: accessToken, Secret: secret, + OptIn: optIn, } name := "web" diff --git a/repo.go b/repo.go index c7c2f85..2e14d0a 100644 --- a/repo.go +++ b/repo.go @@ -27,6 +27,7 @@ type Repo struct { Slug string Token string Secret string + OptIn bool } type Repos []Repo diff --git a/templates/auth.html b/templates/auth.html index ba62653..b15865f 100644 --- a/templates/auth.html +++ b/templates/auth.html @@ -15,6 +15,16 @@ Your project slug is the Github user- and repository name separated by a slash e.g. ganggo/federation +
+ + + + That would mean that tests are triggered if you specify [ci] in the PR title, message + or if the PR was tagged with ci. If you leave the box unchecked + tests will be triggered on every pull-request. However you can skip them by writing [ci skip] in the PR title, + message or if the PR was tagged with ci skip. + +
diff --git a/webhook.go b/webhook.go index a9bec60..9bf351f 100644 --- a/webhook.go +++ b/webhook.go @@ -25,6 +25,7 @@ import ( "encoding/json" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" + "strings" ) func webhook(w http.ResponseWriter, r *http.Request) { @@ -76,6 +77,46 @@ func webhook(w http.ResponseWriter, r *http.Request) { // return //} + var flagExists = false + var buildFlag = "ci skip" + if repo.OptIn { + buildFlag = "ci" + } + + // check PR title and body for [ci] or [ci skip] flag + if pr.Title != nil && pr.Body != nil && + strings.Contains(strings.ToLower(*pr.Title), + fmt.Sprintf("[%s]", buildFlag)) && + strings.Contains(strings.ToLower(*pr.Body), + fmt.Sprintf("[%s]", buildFlag)) { + flagExists = true + } + + if !flagExists { + // check labels for build flag if we haven't already found it + for _, label := range pr.Labels { + if label.Name != nil && strings.Contains( + strings.ToLower(*label.Name), buildFlag) { + flagExists = true + break + } + } + } + + // ignoring pull-request! Repository is set + // to opt-in and no build flag was found + if repo.OptIn && !flagExists { + fmt.Fprintf(w, `{}`) + return + } + + // ignoring pull-request! Repository is set + // to opt-out and a skip flag was found + if !repo.OptIn && flagExists { + fmt.Fprintf(w, `{}`) + return + } + build := Build{ RepoID: repo.ID, Matrix: fmt.Sprintf(