Commit 9b2e706b authored by zauberstuhl's avatar zauberstuhl

Implement opt-in/out for pull requests

parent 6000f833
......@@ -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"
......
......@@ -27,6 +27,7 @@ type Repo struct {
Slug string
Token string
Secret string
OptIn bool
}
type Repos []Repo
......@@ -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>
......
......@@ -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(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment