Skip to content

Commit

Permalink
Use github client instead of making raw http request
Browse files Browse the repository at this point in the history
Signed-off-by: Matias Pan <[email protected]>
  • Loading branch information
matipan authored and alexellis committed Feb 22, 2019
1 parent 1ba27d2 commit fe03a33
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions handler/permissionsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import (
yaml "gopkg.in/yaml.v2"

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

const (
configFile = ".DEREK.yml"
configURLFormat = "https://github.com/%s/%s/raw/master/%s"
privateConfigURLFormat = "https://api.github.com/repos/%s/%s/contents/%s"
configFile = ".DEREK.yml"
configURLFormat = "https://github.com/%s/%s/raw/master/%s"
)

func EnabledFeature(attemptedFeature string, config *types.DerekRepoConfig) bool {
Expand Down Expand Up @@ -59,21 +58,6 @@ func readConfigFromURL(client http.Client, url string) []byte {
log.Fatalln(err)
}

return fetchConfig(client, req)
}

func readConfigFromURLWithToken(client http.Client, url string, accessToken string) []byte {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", accessToken))
req.Header.Set("Accept", "application/vnd.github.v3.raw'")

return fetchConfig(client, req)
}

func fetchConfig(client http.Client, req *http.Request) []byte {
res, resErr := client.Do(req)
if resErr != nil {
log.Fatalln(resErr)
Expand Down Expand Up @@ -108,19 +92,24 @@ func validateRedirectURL(url string) error {
// for the specified repository. Since the repository is
// private we use the github API to fetch `.DEREK.yml`.
func GetPrivateRepoConfig(owner, repository string, installation int, config config.Config) (*types.DerekRepoConfig, error) {
accessToken, err := auth.MakeAccessTokenForInstallation(config.ApplicationID, installation, config.PrivateKey)
client, ctx := makeClient(installation, config)
response, err := client.Repositories.DownloadContents(ctx, owner, repository, configFile, &github.RepositoryContentGetOptions{
Ref: "master",
})
if err != nil {
return nil, fmt.Errorf("unable to get a signed JWT token: %s", err)
return nil, fmt.Errorf("unable to download config file: %s", err)
}
defer response.Close()

client := http.Client{
Timeout: 30 * time.Second,
bytesConfig, err := ioutil.ReadAll(response)
if err != nil {
return nil, fmt.Errorf("unable to read github's response: %s", err)
}

configFile := fmt.Sprintf(privateConfigURLFormat, owner, repository, configFile)
bytesConfig := readConfigFromURLWithToken(client, configFile, accessToken)

return buildDerekConfig(client, bytesConfig)
httpClient := http.Client{
Timeout: 30 * time.Second,
}
return buildDerekConfig(httpClient, bytesConfig)
}

// GetRepoConfig returns derek's configuration for the specified
Expand Down

0 comments on commit fe03a33

Please sign in to comment.