Skip to content

[WIP] Feature/merge experimental #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .DEREK.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ curators:
features:
- dco_check
- comments

- merge

mergers:
- alexellis
6 changes: 6 additions & 0 deletions commentHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
addLabelConstant string = "AddLabel"
setMilestoneConstant string = "SetMilestone"
removeMilestoneConstant string = "RemoveMilestone"
mergePRConstant string = "Merge"
)

func makeClient(installation int) (*github.Client, context.Context) {
Expand Down Expand Up @@ -98,6 +99,10 @@ func handleComment(req types.IssueCommentOuter) {
feedback, err = updateMilestone(req, command.Type, command.Value)
break

case mergePRConstant:
merger := merge{}
feedback, err = merger.Merge(req, command.Type, command.Value)

default:
feedback = "Unable to work with comment: " + req.Comment.Body
err = nil
Expand Down Expand Up @@ -329,6 +334,7 @@ func parse(body string) *types.CommentAction {
"Derek unlock": unlockConstant,
"Derek set milestone: ": setMilestoneConstant,
"Derek remove milestone: ": removeMilestoneConstant,
"Derek merge": mergePRConstant,
}

for trigger, commandType := range commands {
Expand Down
5 changes: 4 additions & 1 deletion derek.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ provider:
functions:
derek:
handler: ./
image: alexellis/derek:0.5.2
image: alexellis/derek:0.6.0
lang: dockerfile
environment:
debug: true
customers_url: https://raw.githubusercontent.com/alexellis/derek/master/.CUSTOMERS
validate_hmac: false
validate_customers: true
secret_path: /var/openfaas/secrets/ # use /run/secrets/ for older OpenFaaS versions
application: 10167
write_debug: true
combined_output: false
environment_file:
- secrets.yml
# See secrets.example.yml
Expand Down
48 changes: 24 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ const (
const derekSecretKeyFile = "derek-secret-key"
const privateKeyFile = "derek-private-key"

func getSecretPath() (string, error) {
secretPath := os.Getenv("secret_path")

if len(secretPath) == 0 {
return "", fmt.Errorf("secret_path not set, this must be /var/openfaas/secrets or /run/secrets")

}

return secretPath, nil
}

func hmacValidation() bool {
val := os.Getenv("validate_hmac")
return len(val) > 0 && (val == "1" || val == "true")
}

func getFirstLine(secret []byte) []byte {
stringSecret := string(secret)
if newLine := strings.Index(stringSecret, "\n"); newLine != -1 {
secret = secret[:newLine]
}
return secret
}

func main() {

bytesIn, _ := ioutil.ReadAll(os.Stdin)
Expand Down Expand Up @@ -152,3 +128,27 @@ func handleEvent(eventType string, bytesIn []byte) error {

return nil
}

func getSecretPath() (string, error) {
secretPath := os.Getenv("secret_path")

if len(secretPath) == 0 {
return "", fmt.Errorf("secret_path not set, this must be /var/openfaas/secrets or /run/secrets")

}

return secretPath, nil
}

func hmacValidation() bool {
val := os.Getenv("validate_hmac")
return len(val) > 0 && (val == "1" || val == "true")
}

func getFirstLine(secret []byte) []byte {
stringSecret := string(secret)
if newLine := strings.Index(stringSecret, "\n"); newLine != -1 {
secret = secret[:newLine]
}
return secret
}
92 changes: 92 additions & 0 deletions merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"context"
"fmt"

"github.com/alexellis/derek/auth"
"github.com/alexellis/derek/types"
"github.com/google/go-github/github"
)

type merge struct {
}

func (m *merge) Merge(req types.IssueCommentOuter, cmdType string, cmdValue string) (string, error) {
result := ""

if req.Issue.PullRequest == nil {
return "can't merge a non-PR issue", nil
}

token := getAccessToken(req.Installation.ID)
client := auth.MakeClient(context.Background(), token)
pr, _, err := client.PullRequests.Get(context.Background(), req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number)
if err != nil {
return "unable to get pull request", err
}

if pr.GetMerged() == false {

if pr.GetMergeable() == true {

if validMergePolicy(req) == false {
sendComment(client, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number,
"I am unable to merge this PR due to merge-policy exception(s)")

return "invalid merge policy", nil
}

pullRequestOptions := github.PullRequestOptions{
MergeMethod: "rebase",
CommitTitle: fmt.Sprintf("Merge PR #%d", req.Issue.Number),
}
mergeRes, _, err := client.PullRequests.Merge(context.Background(),
req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number,
fmt.Sprintf(`Merging PR #%d by Derek
This is an automated merge by the bot Derek, find more
https://github.com/alexellis/derek/

Signed-off-by: [email protected]`, req.Issue.Number), &pullRequestOptions)

if err != nil {

body := fmt.Sprintf(`I have been unable to merge the requested PR: %s`, err.Error())

sendComment(client, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number,
body)

return fmt.Sprintf("Merge issue: %s, %t", mergeRes.GetMessage(), mergeRes.GetMerged()), err
}

sendComment(client, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number,
`I have merged the pull request using the rebase strategy.`)
} else {
sendComment(client, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number,
"This pull request cannot be merged. Rebase your work and try again.")
}
}

return result, err
}

func sendComment(client *github.Client, login string, repo string, issue int, comment string) {

issueComment := &github.IssueComment{
Body: &comment,
}
client.Issues.CreateComment(context.Background(),
login, repo, issue, issueComment)
}

func validMergePolicy(req types.IssueCommentOuter) bool {
validDCO := true
for _, label := range req.Issue.Labels {
if label.Name == "no-dco" {
validDCO = false
break
}
}

return validDCO
}
13 changes: 9 additions & 4 deletions pullRequestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ import (
"github.com/google/go-github/github"
)

func handlePullRequest(req types.PullRequestOuter) {
ctx := context.Background()

func getAccessToken(installationID int) string {
token := os.Getenv("access_token")
if len(token) == 0 {
keyPath, _ := getSecretPath()

newToken, tokenErr := auth.MakeAccessTokenForInstallation(
os.Getenv("application"),
req.Installation.ID,
installationID,
keyPath+privateKeyFile)

if tokenErr != nil {
Expand All @@ -34,6 +32,13 @@ func handlePullRequest(req types.PullRequestOuter) {

token = newToken
}
return token
}

func handlePullRequest(req types.PullRequestOuter) {
ctx := context.Background()

token := getAccessToken(req.Installation.ID)

client := auth.MakeClient(ctx, token)

Expand Down
20 changes: 13 additions & 7 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,27 @@ type IssueCommentOuter struct {
Comment Comment `json:"comment"`
Action string `json:"action"`
Issue Issue `json:"issue"`

InstallationRequest
}

type PullRequestIssueLink struct {
URL string `json:"url"`
}

type IssueLabel struct {
Name string `json:"name"`
}

type Issue struct {
Labels []IssueLabel `json:"labels"`
Number int `json:"number"`
Title string `json:"title"`
Locked bool `json:"locked"`
State string `json:"state"`
Milestone Milestone `json:"milestone"`
URL string `json:"url"`
Labels []IssueLabel `json:"labels"`
Number int `json:"number"`
Title string `json:"title"`
Locked bool `json:"locked"`
State string `json:"state"`
Milestone Milestone `json:"milestone"`
URL string `json:"url"`
PullRequest *PullRequestIssueLink `json:"pull_request"`
}

type Milestone struct {
Expand Down