Skip to content

support github enterprise by allowing custom api and base urls#43

Merged
brikis98 merged 13 commits into
gruntwork-io:masterfrom
jniesen:master
Aug 11, 2018
Merged

support github enterprise by allowing custom api and base urls#43
brikis98 merged 13 commits into
gruntwork-io:masterfrom
jniesen:master

Conversation

@jniesen

@jniesen jniesen commented Aug 9, 2018

Copy link
Copy Markdown

Fetch seems like a much more enjoyable way to fetch release artifacts from GitHub on the command line. More enjoyable then writing a curl script and more reusable.

However, I'm working with repos in both GitHub Enterprise (GHE) and GitHub.com. These changes make it so that I can pass in the URLs (base and api) of the GHE instance that I'm working with and be able to fetch artifacts from there.

I did some basic smoke testing on this. I was able to fetch from GHE. I can add/fix tests if it's required for merge, it just won't be until tomorrow.

@brikis98 brikis98 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

This definitely looks like a useful feature to have. Could you please:

  1. Run the existing automated tests to make sure they pass.
  2. Add a new automated test that sets the new base and API URL params. We don't have a GitHub enterprise instance to test with, so I suppose those could just be set to github.com and api.github.com to make sure those params are used correctly.

Comment thread github.go
Token string // The personal access token to access this repo (if it's a private repo)
Url string // The URL of the GitHub repo
BaseUrl string // The Base URL of the GitHub Instance
ApiUrl string // The API Url of the GitHub Instance

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these 3 ever different? Or, more accurately, is it always the case where:

Url = <domain>/<asset_path>
BaseUrl = <domain>
ApiUrl = <domain>/api/<version>

I'm mainly trying to figure out if we could deduce <domain> automatically from Url without a new parameter and have an optional ApiVersion param that defaults to something reasonable (v1)...

@jniesen jniesen Aug 10, 2018

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On github.com, the ApiUrl is always api.github.com and the version can be chosen using an HTTP header. For enterprise, it's always baseurl.com/api/v3.

So we probably could use the repo url to do something like:

baseUrl := parseBaseFromRepoUrl(options.RepoUrl)
apiUrl := "api.github.com"

if baseUrl != "github.com" {
  apiUrl := baseUrl+"/api/"+options.ApiVersion
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable to me and, if ApiVersion is set to a default of v3, it makes the user experience easier, as things will "just work" with no extra parameters. Might want to add a logging statement in that if-statement to make it explicit a non github.com URL was found. Also, does that if-statement also need to check against www.github.com?

@jniesen jniesen Aug 10, 2018

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, does that if-statement also need to check against www.github.com?

Yes. Good catch. It will either need to be an || or I can use strings.Contains(baseUrl, "github.com")

I'm running the test suite without my changes and getting some failure right away:

% go test                                                           
# github.com/jniesen/fetch
./github_test.go:159: Fatalf format %s has arg tc.expected of wrong type main.GitHubReleaseApiResponse
./github_test.go:191: Fatalf format %s has arg tc.assetId of wrong type int
./github_test.go:197: Fatalf format %s has arg tc.assetId of wrong type int
FAIL    github.com/jniesen/fetch [build failed]

I'm going to look into them, but I'm curious if there's something else in play that I'm missing.

% go version
go version go1.10.1 linux/amd64

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I get temporary read access to: https://github.com/gruntwork-io/fetch-test-private. So that some of the other acceptance tests pass?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I always forget about that. We have to have a private repo for testing, but then, of course, contributors can't access it 😄

Just added you.

@jniesen

jniesen commented Aug 10, 2018

Copy link
Copy Markdown
Author

@brikis98 I made the changes you suggested and updated the test suite. This is ready for review.

@brikis98 brikis98 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you. A few minor issues to fix and this is ready to merge.

Comment thread README.md Outdated
downloading from private GitHub repos. **NOTE:** fetch will also look for this token using the `GITHUB_OAUTH_TOKEN`
environment variable, which we recommend using instead of the command line option to ensure the token doesn't get
saved in bash history.
- `--github-api-version` (**Optional**): The used when fetching an artifact from a GitHub Enterprise instance.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "the" at the beginning of the sentence. Also, please add "Defaults to v3.

Comment thread checksum_test.go Outdated
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is wrong?

Comment thread github.go
Token string // The personal access token to access this repo (if it's a private repo)
Url string // The URL of the GitHub repo
BaseUrl string // The Base URL of the GitHub Instance
ApiUrl string // The API Url of the GitHub Instance

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I always forget about that. We have to have a private repo for testing, but then, of course, contributors can't access it 😄

Just added you.

Comment thread github.go Outdated
func ParseUrlIntoGithubInstance(url string, apiv string) (GitHubInstance, *FetchError) {
var instance GitHubInstance

regex, regexErr := regexp.Compile("https?://(?:www\\.)?(.+?\\.com).*")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't a user have GitHub Enterprise on a different TLD that isn't .com? Perhaps you should use url.Parse and extract hostname from that?

Comment thread github_test.go Outdated
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation? Please check your editor settings.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I thought I had vim-go installed, but had vim-polyglot instead which wasn't cutting it on it's own.

Comment thread github_test.go
}
}

func TestParseUrlIntoGithubInstance(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice tests 👍

@jniesen

jniesen commented Aug 11, 2018

Copy link
Copy Markdown
Author

Updated to address the things brought up in the last round of review. Thank you for adding me to the repo. I was able to get the complete test suite passing.

@brikis98 brikis98 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you! I'll merge this now and let the tests run. When they pass, I'll issue a new release and paste the link here.

@brikis98 brikis98 merged commit ad606a2 into gruntwork-io:master Aug 11, 2018
@brikis98

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants