Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use repo default branch #174

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions handler/permissions_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

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

func EnabledFeature(attemptedFeature string, config *types.DerekRepoConfig) bool {
Expand Down Expand Up @@ -92,10 +92,10 @@ func validateRedirectURL(url string) error {
// GetPrivateRepoConfig returns the configuration for derek
// 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) {
func GetPrivateRepoConfig(owner, repository, branch string, installation int, config config.Config) (*types.DerekRepoConfig, error) {
client, ctx := makeClient(installation, config)
response, err := client.Repositories.DownloadContents(ctx, owner, repository, configFile, &github.RepositoryContentGetOptions{
Ref: "master",
Ref: branch,
})
if err != nil {
return nil, fmt.Errorf("unable to download config file: %s", err)
Expand All @@ -117,12 +117,12 @@ func GetPrivateRepoConfig(owner, repository string, installation int, config con
// repository. The repository has to be public since this function
// will fetch the file from the CDN. If you are trying to fetch
// the config from a private repo use `GetPrivateRepoConfig` instead.
func GetRepoConfig(owner string, repository string) (*types.DerekRepoConfig, error) {
func GetRepoConfig(owner, repository, branch string) (*types.DerekRepoConfig, error) {
client := http.Client{
Timeout: 30 * time.Second,
}

configFile := fmt.Sprintf(configURLFormat, owner, repository, configFile)
configFile := fmt.Sprintf(configURLFormat, owner, repository, branch, configFile)
bytesConfig, err := readConfigFromURL(client, configFile)
if err != nil {
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func handleEvent(eventType string, bytesIn []byte, config config.Config) error {

var derekConfig *types.DerekRepoConfig
if req.Repository.Private {
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Installation.ID, config)
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Repository.DefaultBranch, req.Installation.ID, config)
} else {
derekConfig, err = handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name)
derekConfig, err = handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Repository.DefaultBranch)
}

if err != nil {
Expand Down Expand Up @@ -146,9 +146,9 @@ func handleEvent(eventType string, bytesIn []byte, config config.Config) error {

var derekConfig *types.DerekRepoConfig
if req.Repository.Private {
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Installation.ID, config)
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Repository.DefaultBranch, req.Installation.ID, config)
} else {
derekConfig, err = handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name)
derekConfig, err = handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Repository.DefaultBranch)
}
if err != nil {
return fmt.Errorf("Unable to access maintainers file at: %s/%s\nError: %s",
Expand Down Expand Up @@ -183,9 +183,9 @@ func handleEvent(eventType string, bytesIn []byte, config config.Config) error {

var derekConfig *types.DerekRepoConfig
if req.Repository.Private {
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Installation.ID, config)
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Repository.DefaultBranch, req.Installation.ID, config)
} else {
derekConfig, err = handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name)
derekConfig, err = handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Repository.DefaultBranch)
}

if err != nil {
Expand Down Expand Up @@ -223,12 +223,12 @@ func handleEvent(eventType string, bytesIn []byte, config config.Config) error {

var derekConfig *types.DerekRepoConfig
if req.Repo.GetPrivate() {
derekConfig, err = handler.GetPrivateRepoConfig(req.Repo.Owner.GetLogin(), req.Repo.GetName(), int(req.Installation.GetID()), config)
derekConfig, err = handler.GetPrivateRepoConfig(req.Repo.Owner.GetLogin(), req.Repo.GetName(), req.Repo.GetDefaultBranch(), int(req.Installation.GetID()), config)
if err != nil {
return fmt.Errorf("unable to get private repo config: %s", err)
}
} else {
derekConfig, err = handler.GetRepoConfig(req.Repo.Owner.GetLogin(), req.Repo.GetName())
derekConfig, err = handler.GetRepoConfig(req.Repo.Owner.GetLogin(), req.Repo.GetName(), req.Repo.GetDefaultBranch())
if err != nil {
return fmt.Errorf("unable to get repo config: %s", err)
}
Expand Down
7 changes: 4 additions & 3 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package types

type Repository struct {
Owner Owner `json:"owner"`
Name string `json:"name"`
Private bool `json:"private"`
Owner Owner `json:"owner"`
Name string `json:"name"`
Private bool `json:"private"`
DefaultBranch string `json:"default_branch"`
}

type Owner struct {
Expand Down