Skip to content

Commit

Permalink
Feature: Respond w. comment to pull requests (#11)
Browse files Browse the repository at this point in the history
* add code for commenting

* Update test file

* update test payload to real lunarway payload

* Add explantion about integrationID

* gitignore

* delete payload

* gitignore

* Update Makefile

Co-authored-by: Bjørn <[email protected]>

* make generic private key path

Co-authored-by: Bjørn <[email protected]>

* Update issue_release_status.go

Co-authored-by: Bjørn <[email protected]>

* Add comment and edit string formatting

* Use env variable refering to path

* extra logging

Co-authored-by: Bjørn <[email protected]>
  • Loading branch information
JacobValdemar and Bjørn authored Aug 10, 2020
1 parent fbbbe36 commit 5642ac2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 484 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.vscode
/release-manager-bot
/dist
/payload.json
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# url to a running release-manager
URL=localhost:8080
URL=http://localhost:8080
FILE=payload.json
PAYLOAD=`cat $(FILE)`

github-webhook:
curl -H 'X-GitHub-Event: pull_request' \
-H 'Content-Type: application/json' \
-d '$(shell cat ${FILE})' \
$(URL)/webhook/github
$(URL)/webhook/github/bot



start:
go build
./release-manager-bot \
--release-manager-auth-token $(HAMCTL_AUTH_TOKEN) \
--release-manager-url http://localhost:8081/ \
--github-privateKey "`cat $(GITHUB_PRIVATE_KEY_PATH)`" \
--github-integrationID 75542
37 changes: 11 additions & 26 deletions issue_release_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/google/go-github/v32/github"
"github.com/palantir/go-githubapp/githubapp"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -45,6 +46,8 @@ type ListPoliciesResponse struct {
}

func (handler *PRCreateHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
zerolog.Ctx(ctx).Info().RawJSON("payload", payload).Msgf("handling delivery ID: '%s', eventtype '%s'", deliveryID, eventType)

// Recieve webhook
var event github.PullRequestEvent

Expand All @@ -71,7 +74,7 @@ func (handler *PRCreateHandler) Handle(ctx context.Context, eventType, deliveryI
servicePath := handler.releaseManagerURL + "/policies?service="

// Create client
client := &http.Client{}
httpClient := &http.Client{}

req, err := http.NewRequest("GET", servicePath+serviceName, nil)
if err != nil {
Expand All @@ -80,7 +83,7 @@ func (handler *PRCreateHandler) Handle(ctx context.Context, eventType, deliveryI

req.Header.Add("Authorization", "Bearer "+handler.releaseManagerAuthToken)

resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "sending HTTP request")
}
Expand Down Expand Up @@ -131,41 +134,23 @@ func (handler *PRCreateHandler) Handle(ctx context.Context, eventType, deliveryI

logger.Debug().Msgf("%s", botMessage)

/*
var path string
var resp httpinternal.ListPoliciesResponse
var client *httpinternal.Client
var serviceName string
params := url.Values{}
params.Add("service", serviceName)
path, err := client.URLWithQuery(path, params)
if err != nil {
return err
}
err = client.Do(http.MethodGet, path, nil, &resp)
*/

// Send PR comment

/*client, err := handler.NewInstallationClient(installationID)
client, err := handler.NewInstallationClient(installationID)
if err != nil {
return errors.Wrap(err, "installing client")
return errors.Wrapf(err, "creating new github.Client from installation id '%d'", installationID)
}

repositoryOwner := repository.GetOwner().GetLogin()
repositoryName := repository.GetName()

msg := fmt.Sprintf("To the stars! :-)")
// It's intentional that it's an IssueComment. The alternative PullRequestComment is a review comment
newComment := github.IssueComment{
Body: &msg,
Body: &botMessage,
}

if _, _, err := client.Issues.CreateComment(ctx, repositoryOwner, repositoryName, prNum, &newComment); err != nil {
logger.Error().Err(err).Msg("Failed to comment on pull request")
}*/
return errors.Wrapf(err, "commenting on pull request, with DeliveryID '%v'", deliveryID)
}

return nil
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {

var githubappConfig githubapp.Config
pflag.StringVar(&githubappConfig.V3APIURL, "github-v3_api_url", "https://api.github.com/", "github v3 api url")
pflag.Int64Var(&githubappConfig.App.IntegrationID, "github-integrationID", 0, "github integration ID")
pflag.Int64Var(&githubappConfig.App.IntegrationID, "github-integrationID", 0, "github App ID (App->General->About->App ID)")
pflag.StringVar(&githubappConfig.App.WebhookSecret, "github-webhookSecret", "", "github webhook secret")
pflag.StringVar(&githubappConfig.App.PrivateKey, "github-privateKey", "", "github app private key content")

Expand Down
Loading

0 comments on commit 5642ac2

Please sign in to comment.