Skip to content

Commit

Permalink
Add error handling for release manager policy response + edit configu…
Browse files Browse the repository at this point in the history
…ration method (#10)

* Add Error handling for rel man response

* add body logging if error

* update clientuseragent

* move err handling block and use errors.errorf

* update example config.yml file

* lower case error + defer placement

* edit configuration method
  • Loading branch information
JacobValdemar authored Aug 10, 2020
1 parent 6fce31a commit fbbbe36
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 63 deletions.
36 changes: 0 additions & 36 deletions config.go

This file was deleted.

11 changes: 0 additions & 11 deletions config.yml

This file was deleted.

1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ github.com/palantir/go-baseapp v0.2.1 h1:9dTuuFgLum5zgdhhhTLVO9VmjdHIgMylygeWDKv
github.com/palantir/go-baseapp v0.2.1/go.mod h1:vQuqaqAh5XRwfllV8eJBs2fDczpOvOPAoLlJ+kVsF9A=
github.com/palantir/go-githubapp v0.4.0 h1:k4Ot5kzm61mR4Gi7bblGI7gGmbJZn+Mi55mfi+QOZCk=
github.com/palantir/go-githubapp v0.4.0/go.mod h1:/Xm5h66uEBX24An2Ln8H6Rk44z8uwk4E6m4gNrPadjQ=
github.com/palantir/go-githubapp v0.5.0 h1:mAdLgaaNV0zrqSv1q0zpJbKq65H92uIrAlSwRIRsGEE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
11 changes: 8 additions & 3 deletions issue_release_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -83,15 +84,19 @@ func (handler *PRCreateHandler) Handle(ctx context.Context, eventType, deliveryI
if err != nil {
return errors.Wrap(err, "sending HTTP request")
}

var policyResponse ListPoliciesResponse
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "reading release-manager HTTP response body")
}

defer resp.Body.Close()
if resp.StatusCode != 200 {
logger.Info().Msgf("Request body: %v", body)
return errors.Errorf("expected status code 200, but recieved " + fmt.Sprintf("%v", resp.StatusCode))
}

var policyResponse ListPoliciesResponse

err = json.Unmarshal(body, &policyResponse)
if err != nil {
Expand Down
29 changes: 16 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ import (
func main() {
logger := zerolog.New(os.Stdout).With().Timestamp().Logger()

configPath := pflag.String("config-path", "config.yml", "path to configuration file")
releaseManagerAuthToken := pflag.String("release-manager-auth-token", "", "auth token for accessing release manager")
releaseManagerURL := pflag.String("release-manager-url", "http://localhost:8080", "url to release manager")

var httpServerConfig baseapp.HTTPConfig
pflag.StringVar(&httpServerConfig.Address, "http-address", "localhost", "http listen address")
pflag.IntVar(&httpServerConfig.Port, "http-port", 8080, "http listen port")
pflag.StringVar(&httpServerConfig.PublicURL, "http-public_url", "https://localhost:8080", "http public url")

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.StringVar(&githubappConfig.App.WebhookSecret, "github-webhookSecret", "", "github webhook secret")
pflag.StringVar(&githubappConfig.App.PrivateKey, "github-privateKey", "", "github app private key content")

pflag.Parse()

if *releaseManagerAuthToken == "" {
Expand All @@ -26,15 +37,8 @@ func main() {
return
}

config, err := ReadConfig(*configPath)
if err != nil {
logger.Error().Msgf("Failed to parse config: %v", err)
os.Exit(1)
return
}

server, err := baseapp.NewServer(
config.Server,
httpServerConfig,
baseapp.DefaultParams(logger, "exampleapp.")...,
)
if err != nil {
Expand All @@ -44,8 +48,8 @@ func main() {
}

cc, err := githubapp.NewDefaultCachingClientCreator(
config.Github,
githubapp.WithClientUserAgent("example-app/1.0.0"),
githubappConfig,
githubapp.WithClientUserAgent("release-managar-bot/1.0.0"),
githubapp.WithClientTimeout(3*time.Second),
githubapp.WithClientCaching(false, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
githubapp.WithClientMiddleware(
Expand All @@ -60,12 +64,11 @@ func main() {

pullRequestHandler := &PRCreateHandler{
ClientCreator: cc,
preamble: config.AppConfig.PullRequestPreamble,
releaseManagerAuthToken: *releaseManagerAuthToken,
releaseManagerURL: *releaseManagerURL,
}

webhookHandler := githubapp.NewDefaultEventDispatcher(config.Github, pullRequestHandler)
webhookHandler := githubapp.NewDefaultEventDispatcher(githubappConfig, pullRequestHandler)

server.Mux().Handle(pat.Post("/webhook/github/bot"), webhookHandler)

Expand Down

0 comments on commit fbbbe36

Please sign in to comment.