Skip to content

Commit

Permalink
support github enterprise by allowing custom api and base urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Niesen committed Aug 10, 2018
1 parent 42132dc commit a026c31
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ Download the release asset `foo.exe` from a GitHub release where the tag is exac
fetch --repo="https://github.com/foo/bar" --tag="0.1.5" --release-asset="foo.exe" /tmp
```

#### Usage Example 7

Download the release asset `foo.exe` from a GitHub release hosted on a GitHub Enterprise instance running at `ghe.mycompany.com` where the tag is exactly `0.1.5`, and save it to `/tmp`:

```
fetch --repo="https://github.com/foo/bar" --tag="0.1.5" --release-asset="foo.exe" --github-base-url="ghe.mycompany.com" --github-api-url="ghe.mycompany.com/api/v3" /tmp
```

## License

This code is released under the MIT License. See [LICENSE.txt](/LICENSE.txt).
Expand Down
30 changes: 17 additions & 13 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
)

type GitHubRepo struct {
Url string // The URL of the GitHub repo
Owner string // The GitHub account name under which the repo exists
Name string // The GitHub repo name
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
Owner string // The GitHub account name under which the repo exists
Name string // The GitHub repo name
Token string // The personal access token to access this repo (if it's a private repo)
}

// Represents a specific git commit.
Expand Down Expand Up @@ -63,10 +65,10 @@ type GitHubReleaseAsset struct {
}

// Fetch all tags from the given GitHub repo
func FetchTags(githubRepoUrl string, githubToken string) ([]string, *FetchError) {
func FetchTags(githubRepoUrl string, githubBaseUrl string, githubApiUrl string, githubToken string) ([]string, *FetchError) {
var tagsString []string

repo, err := ParseUrlIntoGitHubRepo(githubRepoUrl, githubToken)
repo, err := ParseUrlIntoGitHubRepo(githubRepoUrl, githubBaseUrl, githubApiUrl, githubToken)
if err != nil {
return tagsString, wrapError(err)
}
Expand Down Expand Up @@ -96,10 +98,10 @@ func FetchTags(githubRepoUrl string, githubToken string) ([]string, *FetchError)
}

// Convert a URL into a GitHubRepo struct
func ParseUrlIntoGitHubRepo(url string, token string) (GitHubRepo, *FetchError) {
func ParseUrlIntoGitHubRepo(url string, githubBaseUrl string, githubApiUrl string, token string) (GitHubRepo, *FetchError) {
var gitHubRepo GitHubRepo

regex, regexErr := regexp.Compile("https?://(?:www\\.)?github.com/(.+?)/(.+?)(?:$|\\?|#|/)")
regex, regexErr := regexp.Compile("https?://(?:www\\.)?" + githubBaseUrl + "/(.+?)/(.+?)(?:$|\\?|#|/)")
if regexErr != nil {
return gitHubRepo, newError(GITHUB_REPO_URL_MALFORMED_OR_NOT_PARSEABLE, fmt.Sprintf("GitHub Repo URL %s is malformed.", url))
}
Expand All @@ -110,10 +112,12 @@ func ParseUrlIntoGitHubRepo(url string, token string) (GitHubRepo, *FetchError)
}

gitHubRepo = GitHubRepo{
Url: url,
Owner: matches[1],
Name: matches[2],
Token: token,
Url: url,
BaseUrl: githubBaseUrl,
ApiUrl: githubApiUrl,
Owner: matches[1],
Name: matches[2],
Token: token,
}

return gitHubRepo, nil
Expand Down Expand Up @@ -161,7 +165,7 @@ func createGitHubRepoUrlForPath(repo GitHubRepo, path string) string {
func callGitHubApi(repo GitHubRepo, path string, customHeaders map[string]string) (*http.Response, *FetchError) {
httpClient := &http.Client{}

request, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/%s", path), nil)
request, err := http.NewRequest("GET", fmt.Sprintf("https://" + repo.ApiUrl + "/%s", path), nil)
if err != nil {
return nil, wrapError(err)
}
Expand Down
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type FetchOptions struct {
ReleaseAssetChecksum string
ReleaseAssetChecksumAlgo string
LocalDownloadPath string
GithubBaseUrl string
GithubApiUrl string
}

const OPTION_REPO = "repo"
Expand All @@ -34,6 +36,8 @@ const OPTION_SOURCE_PATH = "source-path"
const OPTION_RELEASE_ASSET = "release-asset"
const OPTION_RELEASE_ASSET_CHECKSUM = "release-asset-checksum"
const OPTION_RELEASE_ASSET_CHECKSUM_ALGO = "release-asset-checksum-algo"
const OPTION_GITHUB_BASE_URL = "github-base-url"
const OPTION_GITHUB_API_URL = "github-api-url"

const ENV_VAR_GITHUB_TOKEN = "GITHUB_OAUTH_TOKEN"

Expand Down Expand Up @@ -82,6 +86,14 @@ func main() {
Name: OPTION_RELEASE_ASSET_CHECKSUM_ALGO,
Usage: "The algorithm Fetch will use to compute a checksum of the release asset. Acceptable values\n\tare \"sha256\" and \"sha512\".",
},
cli.StringFlag{
Name: OPTION_GITHUB_BASE_URL,
Usage: "The base url of the GitHub instance. If left blank, github.com will be used.",
},
cli.StringFlag{
Name: OPTION_GITHUB_API_URL,
Usage: "The api url of the GitHub instance. If left blank, api.github.com will be used.",
},
}

app.Action = runFetchWrapper
Expand All @@ -107,7 +119,7 @@ func runFetch(c *cli.Context) error {
}

// Get the tags for the given repo
tags, fetchErr := FetchTags(options.RepoUrl, options.GithubToken)
tags, fetchErr := FetchTags(options.RepoUrl, options.GithubBaseUrl, options.GithubApiUrl, options.GithubToken)
if fetchErr != nil {
if fetchErr.errorCode == INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED {
return errors.New(getErrorMessage(INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED, fetchErr.details))
Expand All @@ -133,7 +145,7 @@ func runFetch(c *cli.Context) error {
}

// Prepare the vars we'll need to download
repo, fetchErr := ParseUrlIntoGitHubRepo(options.RepoUrl, options.GithubToken)
repo, fetchErr := ParseUrlIntoGitHubRepo(options.RepoUrl, options.GithubBaseUrl, options.GithubApiUrl, options.GithubToken)
if fetchErr != nil {
return fmt.Errorf("Error occurred while parsing GitHub URL: %s", fetchErr)
}
Expand Down Expand Up @@ -188,6 +200,8 @@ func parseOptions(c *cli.Context) FetchOptions {
ReleaseAssetChecksum: c.String(OPTION_RELEASE_ASSET_CHECKSUM),
ReleaseAssetChecksumAlgo: c.String(OPTION_RELEASE_ASSET_CHECKSUM_ALGO),
LocalDownloadPath: localDownloadPath,
GithubBaseUrl: c.String(OPTION_GITHUB_BASE_URL),
GithubApiUrl: c.String(OPTION_GITHUB_API_URL),
}
}

Expand Down

0 comments on commit a026c31

Please sign in to comment.