Skip to content

Commit

Permalink
Implement opt-in/out for pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Matt committed Mar 17, 2018
1 parent 4fa3117 commit 8880889
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Repo struct {
Slug string
Token string
Secret string
OptIn bool
}

type Repos []Repo
10 changes: 10 additions & 0 deletions templates/auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
<input type="text" class="form-control" id="repoInput" name="repo" aria-describedby="repoHelp" placeholder="Enter your project slug">
<small id="repoHelp" class="form-text text-muted">Your project slug is the Github user- and repository name separated by a slash e.g. ganggo/federation</small>
</div>
<div class="form-check mb-3">
<input type="checkbox" class="form-check-input" id="optinInput" name="optin" describedby="optinHelp">
<label class="form-check-label" for="optinInput">Check if you want to opt-in on a new test</label>
<small id="optinHelp" class="form-text text-muted">
That would mean that tests are triggered if you specify <code>[ci]</code> in the PR title, message
or if the PR was tagged with <span class="badge badge-primary">ci</span>. If you leave the box unchecked
tests will be triggered on every pull-request. However you can skip them by writing <code>[ci skip]</code> in the PR title,
message or if the PR was tagged with <span class="badge badge-primary">ci skip</span>.
</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
Expand Down
41 changes: 41 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 8880889

Please sign in to comment.