Skip to content

Commit

Permalink
add handler package
Browse files Browse the repository at this point in the history
Signed-off-by: Cheikh Seck <[email protected]>

add signature

Signed-off-by: Cheikh Seck <[email protected]>

Revert "add signature"

This reverts commit ed54c2f.

refactor main.go to use handler package. Signed-off-by: Cheikh Seck <[email protected]>

export ClosedConstant from handler package.
Signed-off-by: Cheikh Seck <[email protected]>
  • Loading branch information
cheikhshift authored and alexellis committed Oct 29, 2018
1 parent 698b186 commit 9aadd63
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
10 changes: 5 additions & 5 deletions commentHandler.go → handler/commentHandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package main
package handler

import (
"bytes"
Expand All @@ -20,7 +20,7 @@ import (

const (
openConstant string = "open"
closedConstant string = "closed"
ClosedConstant string = "closed"
closeConstant string = "close"
reopenConstant string = "reopen"
lockConstant string = "Lock"
Expand Down Expand Up @@ -56,7 +56,7 @@ func makeClient(installation int, config config.Config) (*github.Client, context
return client, ctx
}

func handleComment(req types.IssueCommentOuter, config config.Config) {
func HandleComment(req types.IssueCommentOuter, config config.Config) {

var feedback string
var err error
Expand Down Expand Up @@ -361,8 +361,8 @@ func validAction(running bool, requestedAction string, start string, stop string

func checkTransition(requestedAction string, currentState string) (string, bool) {

if requestedAction == closeConstant && currentState != closedConstant {
return closedConstant, true
if requestedAction == closeConstant && currentState != ClosedConstant {
return ClosedConstant, true
} else if requestedAction == reopenConstant && currentState != openConstant {
return openConstant, true
}
Expand Down
8 changes: 4 additions & 4 deletions commentHandler_test.go → handler/commentHandler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package main
package handler

import (
"os"
Expand Down Expand Up @@ -240,7 +240,7 @@ func Test_assessState(t *testing.T) {
{
title: "Currently Closed and trying to close",
requestedAction: closeConstant,
currentState: closedConstant,
currentState: ClosedConstant,
expectedNewState: "",
expectedBool: false,
},
Expand All @@ -254,15 +254,15 @@ func Test_assessState(t *testing.T) {
{
title: "Currently Closed and trying to open",
requestedAction: reopenConstant,
currentState: closedConstant,
currentState: ClosedConstant,
expectedNewState: openConstant,
expectedBool: true,
},
{
title: "Currently Open and trying to close",
requestedAction: closeConstant,
currentState: openConstant,
expectedNewState: closedConstant,
expectedNewState: ClosedConstant,
expectedBool: true,
},
}
Expand Down
10 changes: 5 additions & 5 deletions permissionsHandler.go → handler/permissionsHandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package main
package handler

import (
"fmt"
Expand All @@ -18,7 +18,7 @@ import (

const configFile = ".DEREK.yml"

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

featureEnabled := false

Expand All @@ -31,11 +31,11 @@ func enabledFeature(attemptedFeature string, config *types.DerekRepoConfig) bool
return featureEnabled
}

func permittedUserFeature(attemptedFeature string, config *types.DerekRepoConfig, user string) bool {
func PermittedUserFeature(attemptedFeature string, config *types.DerekRepoConfig, user string) bool {

permitted := false

if enabledFeature(attemptedFeature, config) {
if EnabledFeature(attemptedFeature, config) {
for _, maintainer := range config.Maintainers {
if strings.EqualFold(user, maintainer) {
permitted = true
Expand Down Expand Up @@ -80,7 +80,7 @@ func validateRedirectURL(url string) error {
return fmt.Errorf("the redirect URL doesn't seem to be GitHub based")
}

func getRepoConfig(owner string, repository string) (*types.DerekRepoConfig, error) {
func GetRepoConfig(owner string, repository string) (*types.DerekRepoConfig, error) {
var config types.DerekRepoConfig

client := http.Client{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package main
package handler

import (
"testing"
Expand Down Expand Up @@ -80,7 +80,7 @@ func Test_curatorequalsmaintainer(t *testing.T) {
}
}

func Test_enabledFeature(t *testing.T) {
func Test_EnabledFeature(t *testing.T) {

var enableFeatureOpts = []struct {
title string
Expand Down Expand Up @@ -136,7 +136,7 @@ func Test_enabledFeature(t *testing.T) {

inputConfig := &types.DerekRepoConfig{Features: test.configFeatures}

featureEnabled := enabledFeature(test.attemptedFeature, inputConfig)
featureEnabled := EnabledFeature(test.attemptedFeature, inputConfig)

if featureEnabled != test.expectedVal {
t.Errorf("Enabled feature - wanted: %t, found %t", test.expectedVal, featureEnabled)
Expand All @@ -145,7 +145,7 @@ func Test_enabledFeature(t *testing.T) {
}
}

func Test_permittedUserFeature(t *testing.T) {
func Test_PermittedUserFeature(t *testing.T) {

var permittedUserFeatureOpts = []struct {
title string
Expand Down Expand Up @@ -221,7 +221,7 @@ func Test_permittedUserFeature(t *testing.T) {

inputConfig := &test.config

permittedFeature := permittedUserFeature(test.attemptedFeature, inputConfig, test.user)
permittedFeature := PermittedUserFeature(test.attemptedFeature, inputConfig, test.user)

if permittedFeature != test.expectedVal {
t.Errorf("Permitted user feature - wanted: %t, found %t", test.expectedVal, permittedFeature)
Expand Down
4 changes: 2 additions & 2 deletions pullRequestHandler.go → handler/pullRequestHandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package main
package handler

import (
"context"
Expand All @@ -18,7 +18,7 @@ import (
"github.com/google/go-github/github"
)

func handlePullRequest(req types.PullRequestOuter, contributingURL string, config config.Config) {
func HandlePullRequest(req types.PullRequestOuter, contributingURL string, config config.Config) {
ctx := context.Background()

token := os.Getenv("personal_access_token")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package main
package handler

import (
"testing"
Expand Down
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (

"github.com/alexellis/derek/auth"
"github.com/alexellis/derek/config"

"github.com/alexellis/derek/handler"

"github.com/alexellis/derek/types"
"github.com/alexellis/hmac"
)
Expand Down Expand Up @@ -72,14 +75,14 @@ func handleEvent(eventType string, bytesIn []byte, config config.Config) error {
return fmt.Errorf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}

derekConfig, err := getRepoConfig(req.Repository.Owner.Login, req.Repository.Name)
derekConfig, err := handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name)
if err != nil {
return fmt.Errorf("Unable to access maintainers file at: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}
if req.Action != closedConstant {
if enabledFeature(dcoCheck, derekConfig) {
if req.Action != handler.ClosedConstant {
if handler.EnabledFeature(dcoCheck, derekConfig) {
contributingURL := getContributingURL(derekConfig.ContributingURL, req.Repository.Owner.Login, req.Repository.Name)
handlePullRequest(req, contributingURL, config)
handler.HandlePullRequest(req, contributingURL, config)
}
}
break
Expand All @@ -97,14 +100,14 @@ func handleEvent(eventType string, bytesIn []byte, config config.Config) error {
return fmt.Errorf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}

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

if req.Action != deleted {
if permittedUserFeature(comments, derekConfig, req.Comment.User.Login) {
handleComment(req, config)
if handler.PermittedUserFeature(comments, derekConfig, req.Comment.User.Login) {
handler.HandleComment(req, config)
}
}
break
Expand Down

0 comments on commit 9aadd63

Please sign in to comment.