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

fix: enable custom API URL for enterprise accounts #91

Merged
merged 1 commit into from
Sep 6, 2023
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
4 changes: 3 additions & 1 deletion internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// GithubClient handles all interaction with Github's API
// Designed this way for easier software testing
// Designed this way for easier testing
type GithubClient interface {
GetPullRequest(context.Context, string, string, int) (*github.PullRequest, error)
GetUser(context.Context, string) (*github.User, error)
Expand Down Expand Up @@ -39,6 +39,8 @@ func NewGithubClient(config *entity.Configuration) GithubClient {
oauth := oauth2.NewClient(ctx, ts)
github := github.NewClient(oauth)

github.BaseURL = config.BaseURL

return &githubClient{client: github}
}

Expand Down
1 change: 1 addition & 0 deletions internal/constants/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
ErrInvalidTitlePattern = errors.New("[Config] Invalid pull request title pattern")
ErrInvalidCommitPattern = errors.New("[Config] Invalid pull request commit message pattern")
ErrInvalidBranchPattern = errors.New("[Config] Invalid pull request branch name pattern")
ErrInvalidBaseURL = errors.New("[Config] Invalid GitHub API base URL")
)

// Event error
Expand Down
14 changes: 14 additions & 0 deletions internal/entity/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package entity

import (
"net/url"
"regexp"
"strings"

"github.com/Namchee/conventional-pr/internal/constants"
"github.com/Namchee/conventional-pr/internal/utils"
Expand All @@ -26,6 +28,7 @@ type Configuration struct {
Verbose bool
Edit bool
IgnoredUsers []string
BaseURL *url.URL
}

// ReadConfig reads environment variables for input values which are supplied
Expand Down Expand Up @@ -76,6 +79,16 @@ func ReadConfig() (*Configuration, error) {

ignoredUsers := utils.ReadEnvStringArray("INPUT_IGNORED_USERS")

apiUrl := utils.ReadEnvString("GITHUB_API_URL")
if !strings.HasSuffix(apiUrl, "/") {
apiUrl += "/"
}

baseUrl, err := url.Parse(apiUrl)
if err != nil {
return nil, constants.ErrInvalidBaseURL
}

return &Configuration{
Token: token,
Draft: draft,
Expand All @@ -94,5 +107,6 @@ func ReadConfig() (*Configuration, error) {
Signed: signed,
IgnoredUsers: ignoredUsers,
Verbose: verbose,
BaseURL: baseUrl,
}, nil
}
40 changes: 40 additions & 0 deletions internal/entity/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entity

import (
"net/url"
"os"
"testing"

Expand All @@ -9,6 +10,8 @@ import (
)

func TestReadConfig(t *testing.T) {
baseUrl, _ := url.Parse("https://api.github.com/")

tests := []struct {
name string
mocks map[string]string
Expand All @@ -31,6 +34,7 @@ func TestReadConfig(t *testing.T) {
"INPUT_LABEL": "cpr:invalid",
"INPUT_VERBOSE": "true",
"INPUT_MESSAGE": "foo bar",
"GITHUB_API_URL": "https://api.github.com/",
},
want: &Configuration{
Token: "foo_bar",
Expand All @@ -46,6 +50,7 @@ func TestReadConfig(t *testing.T) {
Edit: true,
Verbose: true,
Message: "foo bar",
BaseURL: baseUrl,
},
wantErr: nil,
},
Expand Down Expand Up @@ -94,6 +99,41 @@ func TestReadConfig(t *testing.T) {
want: nil,
wantErr: constants.ErrInvalidBranchPattern,
},
{
name: "should throw an error when base URL is invalid",
mocks: map[string]string{
"INPUT_ACCESS_TOKEN": "token",
"GITHUB_API_URL": " https://api.github.com",
},
want: nil,
wantErr: constants.ErrInvalidBaseURL,
},
{
name: "should not append trailing slash on base URL when URL has trailing slash",
mocks: map[string]string{
"INPUT_ACCESS_TOKEN": "token",
"GITHUB_API_URL": "https://api.github.com/",
},
want: &Configuration{
Token: "token",
BaseURL: baseUrl,
IgnoredUsers: []string{},
},
wantErr: nil,
},
{
name: "should append trailing slash on base URL",
mocks: map[string]string{
"INPUT_ACCESS_TOKEN": "token",
"GITHUB_API_URL": "https://api.github.com",
},
want: &Configuration{
Token: "token",
BaseURL: baseUrl,
IgnoredUsers: []string{},
},
wantErr: nil,
},
}

for _, tc := range tests {
Expand Down
Loading