Skip to content

Commit

Permalink
Add PR branch validation
Browse files Browse the repository at this point in the history
This checks if the branch a pull request is raised from is non-master.
Validates, as well, if a pull request is raised against the default
branch.

Signed-off-by: Ivana Yovcheva (VMware) <[email protected]>
  • Loading branch information
ivanayov committed Apr 1, 2018
1 parent 15f1fdf commit 7b4b048
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
64 changes: 54 additions & 10 deletions pullRequestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func handlePullRequest(req types.PullRequestOuter) {

client := auth.MakeClient(ctx, token)

doBranchValidation(req, client, ctx)

hasUnsignedCommits, err := hasUnsigned(req, client)

if err != nil {
Expand All @@ -57,16 +59,7 @@ func handlePullRequest(req types.PullRequestOuter) {
body := `Thank you for your contribution. I've just checked and your commit doesn't appear to be signed-off.
That's something we need before your Pull Request can be merged. Please see our [contributing guide](` + link + `).`

comment := &github.IssueComment{
Body: &body,
}

comment, resp, err := client.Issues.CreateComment(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, comment)
if err != nil {
log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, resp.Limit, resp.Remaining)
log.Fatal(err)
}
fmt.Println(comment, resp.Rate)
comment(req, body, client, ctx)
}
} else {
fmt.Println("Things look OK right now.")
Expand Down Expand Up @@ -126,6 +119,57 @@ func hasUnsigned(req types.PullRequestOuter, client *github.Client) (bool, error
return hasUnsigned, err
}

func doBranchValidation(req types.PullRequestOuter, client *github.Client, ctx Context) {
if isMasterHeadBranch(req) || !isAgainstDefaultBranch(req) {
body := `Thank you for your contribution. You have opened a pull request from master branch or against the non-default branch.
Please rename your branch to a non-master and reopen the pull request against the default branch. Thank you!
Closing. `
comment(req, body, client, ctx)

closePullRequest(req)
}
}

func getDefaultBranch(req types.PullRequestOuter) string {
return req.Repository.DefaultBranch
}

func getBaseBranch(req types.PullRequestOuter) string {
return req.BaseBranch.Name
}

func getHeadBranch(req types.PullRequestOuter) string {
return req.HeadBranch.Name
}

func isMasterHeadBranch(req types.PullRequestOuter) bool {
return req.HeadBranch.Name == "master"
}

func isAgainstDefaultBranch(req types.PullRequestOuter) bool {
return getDefaultBranch(req) == getBaseBranch(req)
}

func isSigned(msg string) bool {
return strings.Contains(msg, "Signed-off-by:")
}

/*
* TODO check how to close with go-github or the GitHUb API
*/
func closePullRequest(req types.PullRequestOuter) {
req.PullRequest.Status = "closed"
}

func comment(req types.PullRequestOuter, body string, client *github.Client, ctx Context) {
comment := &github.IssueComment{
Body: &body,
}

comment, resp, err := client.Issues.CreateComment(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, comment)
if err != nil {
log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, resp.Limit, resp.Remaining)
log.Fatal(err)
}
fmt.Println(comment, resp.Rate)
}
15 changes: 12 additions & 3 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package types

type Repository struct {
Owner Owner `json:"owner"`
Name string `json:"name"`
Owner Owner `json:"owner"`
Name string `json:"name"`
DefaultBranch string `json:"default_branch"`
}

type Branch struct {
Repository Repository `json:"repository"`
Name string `json:"ref"`
}

type Owner struct {
Expand All @@ -11,7 +17,8 @@ type Owner struct {
}

type PullRequest struct {
Number int `json:"number"`
Number int `json:"number"`
State string `json:"state"`
}

type InstallationRequest struct {
Expand All @@ -25,6 +32,8 @@ type ID struct {
type PullRequestOuter struct {
Repository Repository `json:"repository"`
PullRequest PullRequest `json:"pull_request"`
BaseBranch Branch `json:"base"`
HeadBranch Branch `json:"head"`
Action string `json:"action"`
InstallationRequest
}
Expand Down

0 comments on commit 7b4b048

Please sign in to comment.