Skip to content

Commit

Permalink
Refactoring and build fix
Browse files Browse the repository at this point in the history
- test cases move into test functions
- break out method that returns error instead of log.Fatal
- add Makefile

Signed-off-by: Alex Ellis <[email protected]>
  • Loading branch information
alexellis committed Nov 24, 2017
1 parent d611a51 commit 35d732d
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 246 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ WORKDIR /go/src/github.com/alexellis/derek
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o derek .

FROM alpine:3.5
FROM alpine:3.6

RUN apk --no-cache add curl ca-certificates \
&& echo "Pulling watchdog binary from Github." \
&& curl -sSL https://github.com/alexellis/faas/releases/download/0.6.9/fwatchdog > /usr/bin/fwatchdog \
&& curl -sSL https://github.com/alexellis/faas/releases/download/0.6.11/fwatchdog > /usr/bin/fwatchdog \
&& chmod +x /usr/bin/fwatchdog \
&& apk del curl --no-cache

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
all: test build
build:
go build -o derek

test:
go test -cover
15 changes: 10 additions & 5 deletions commentHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"strings"

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

const open = "open"
Expand All @@ -20,7 +20,11 @@ func makeClient(installation int) (*github.Client, context.Context) {

token := os.Getenv("access_token")
if len(token) == 0 {
newToken, tokenErr := auth.MakeAccessTokenForInstallation(os.Getenv("application"), installation, os.Getenv("private_key"))

applicationID := os.Getenv("application")
privateKeyPath := os.Getenv("private_key")

newToken, tokenErr := auth.MakeAccessTokenForInstallation(applicationID, installation, privateKeyPath)
if tokenErr != nil {
log.Fatalln(tokenErr.Error())
}
Expand Down Expand Up @@ -98,6 +102,7 @@ func handleComment(req types.IssueCommentOuter) {
if assignee == "me" {
assignee = req.Comment.User.Login
}

_, _, err := client.Issues.AddAssignees(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, []string{assignee})
if err != nil {
log.Fatalln(err)
Expand All @@ -121,6 +126,7 @@ func handleComment(req types.IssueCommentOuter) {
fmt.Printf("%s unassigned successfully or already unassigned.\n", command.Value)

break

case "close", "reopen":
fmt.Printf("%s wants to %s issue #%d\n", req.Comment.User.Login, command.Type, req.Issue.Number)

Expand Down Expand Up @@ -176,7 +182,6 @@ func parse(body string) *types.CommentAction {
}

func isValidCommand(body string, trigger string) bool {

return (len(body) > len(trigger) && body[0:len(trigger)] == trigger) || (body == trigger && !strings.HasSuffix(trigger, ": "))

return (len(body) > len(trigger) && body[0:len(trigger)] == trigger) ||
(body == trigger && !strings.HasSuffix(trigger, ": "))
}
140 changes: 70 additions & 70 deletions commentHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,34 @@ func Test_Parsing_OpenClose(t *testing.T) {
}
}

var labelOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{ //this case replaces Test_Parsing_AddLabel
title: "Add label of demo",
body: "Derek add label: demo",
expectedType: "AddLabel",
expectedVal: "demo",
},
{
title: "Remove label of demo",
body: "Derek remove label: demo",
expectedType: "RemoveLabel",
expectedVal: "demo",
},
{
title: "Invalid label action",
body: "Derek peel label: demo",
expectedType: "",
expectedVal: "",
},
}

func Test_Parsing_Labels(t *testing.T) {

var labelOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{ //this case replaces Test_Parsing_AddLabel
title: "Add label of demo",
body: "Derek add label: demo",
expectedType: "AddLabel",
expectedVal: "demo",
},
{
title: "Remove label of demo",
body: "Derek remove label: demo",
expectedType: "RemoveLabel",
expectedVal: "demo",
},
{
title: "Invalid label action",
body: "Derek peel label: demo",
expectedType: "",
expectedVal: "",
},
}

for _, test := range labelOptions {
t.Run(test.title, func(t *testing.T) {

Expand All @@ -90,52 +90,52 @@ func Test_Parsing_Labels(t *testing.T) {
}
}

var assignmentOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{
title: "Assign to burt",
body: "Derek assign: burt",
expectedType: "Assign",
expectedVal: "burt",
},
{
title: "Unassign burt",
body: "Derek unassign: burt",
expectedType: "Unassign",
expectedVal: "burt",
},
{
title: "Assign to me",
body: "Derek assign: me",
expectedType: "Assign",
expectedVal: "me",
},
{
title: "Unassign me",
body: "Derek unassign: me",
expectedType: "Unassign",
expectedVal: "me",
},
{
title: "Invalid assignment action",
body: "Derek consign: burt",
expectedType: "",
expectedVal: "",
},
{
title: "Unassign blank",
body: "Derek unassign: ",
expectedType: "",
expectedVal: "",
},
}

func Test_Parsing_Assignments(t *testing.T) {

var assignmentOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{
title: "Assign to burt",
body: "Derek assign: burt",
expectedType: "Assign",
expectedVal: "burt",
},
{
title: "Unassign burt",
body: "Derek unassign: burt",
expectedType: "Unassign",
expectedVal: "burt",
},
{
title: "Assign to me",
body: "Derek assign: me",
expectedType: "Assign",
expectedVal: "me",
},
{
title: "Unassign me",
body: "Derek unassign: me",
expectedType: "Unassign",
expectedVal: "me",
},
{
title: "Invalid assignment action",
body: "Derek consign: burt",
expectedType: "",
expectedVal: "",
},
{
title: "Unassign blank",
body: "Derek unassign: ",
expectedType: "",
expectedVal: "",
},
}

for _, test := range assignmentOptions {
t.Run(test.title, func(t *testing.T) {

Expand Down
32 changes: 21 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

Expand Down Expand Up @@ -40,23 +41,30 @@ func main() {
// HMAC Validated or not turned on.
eventType := os.Getenv("Http_X_Github_Event")

if err := handleEvent(eventType, bytesIn); err != nil {
log.Fatal(err)
}
}

func handleEvent(eventType string, bytesIn []byte) error {

switch eventType {
case "pull_request":
req := types.PullRequestOuter{}
if err := json.Unmarshal(bytesIn, &req); err != nil {
log.Fatalf("Cannot parse input %s", err.Error())
return fmt.Errorf("Cannot parse input %s", err.Error())
}

customer, err := auth.IsCustomer(req.Repository)
if err != nil {
log.Fatalf("Unable to verify customer: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
} else if !customer {
log.Fatalf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
return fmt.Errorf("Unable to verify customer: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
} else if customer == false {
return fmt.Errorf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}

derekConfig, err := getConfig(req.Repository.Owner.Login, req.Repository.Name)
if err != nil {
log.Fatalf("Unable to access maintainers file at: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
return fmt.Errorf("Unable to access maintainers file at: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}

if enabledFeature(dcoCheck, derekConfig) {
Expand All @@ -67,26 +75,28 @@ func main() {
case "issue_comment":
req := types.IssueCommentOuter{}
if err := json.Unmarshal(bytesIn, &req); err != nil {
log.Fatalf("Cannot parse input %s", err.Error())
return fmt.Errorf("Cannot parse input %s", err.Error())
}

customer, err := auth.IsCustomer(req.Repository)
if err != nil {
log.Fatalf("Unable to verify customer: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
} else if !customer {
log.Fatalf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
return fmt.Errorf("Unable to verify customer: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
} else if customer == false {
return fmt.Errorf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}

derekConfig, err := getConfig(req.Repository.Owner.Login, req.Repository.Name)
if err != nil {
log.Fatalf("Unable to access maintainers file at: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
return fmt.Errorf("Unable to access maintainers file at: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}

if permittedUserFeature(comments, derekConfig, req.Comment.User.Login) {
handleComment(req)
}
break
default:
log.Fatalln("X_Github_Event want: ['pull_request', 'issue_comment'], got: " + eventType)
return fmt.Errorf("X_Github_Event want: ['pull_request', 'issue_comment'], got: " + eventType)
}

return nil
}
4 changes: 2 additions & 2 deletions permissionsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/alexellis/derek/types"
)

const defaultMaintFile = ".DEREK.yml"
const configFile = ".DEREK.yml"

func enabledFeature(attemptedFeature string, config *types.DerekConfig) bool {

Expand Down Expand Up @@ -46,7 +46,7 @@ func permittedUserFeature(attemptedFeature string, config *types.DerekConfig, us

func getConfig(owner string, repository string) (*types.DerekConfig, error) {

maintainersFile := fmt.Sprintf("https://github.com/%s/%s/raw/master/%s", owner, repository, defaultMaintFile)
maintainersFile := fmt.Sprintf("https://github.com/%s/%s/raw/master/%s", owner, repository, configFile)

client := http.Client{
Timeout: 30 * time.Second,
Expand Down
Loading

0 comments on commit 35d732d

Please sign in to comment.