Skip to content

Fix a cryptic error you get if you use the wrong URL for your GitHub Enterprise Server instance. #49

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

Merged
merged 1 commit into from
Jan 11, 2021
Merged
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
26 changes: 25 additions & 1 deletion internal/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const repositoryHomepage = "https://github.com/github/codeql-action-sync-tool/"
const errorAlreadyExists = "The destination repository already exists, but it was not created with the CodeQL Action sync tool. If you are sure you want to push the CodeQL Action to it, re-run this command with the `--force` flag."
const errorInvalidDestinationToken = "The destination token you've provided is not valid."

const enterpriseAPIPath = "/api/v3"
const enterpriseUploadsPath = "/api/uploads"

type pushService struct {
ctx context.Context
cacheDirectory cachedirectory.CacheDirectory
Expand Down Expand Up @@ -361,10 +364,31 @@ func Push(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, des
&token,
)
tokenClient := oauth2.NewClient(ctx, tokenSource)
client, err := github.NewEnterpriseClient(destinationURL+"/api/v3", destinationURL+"/api/uploads", tokenClient)
client, err := github.NewEnterpriseClient(destinationURL+enterpriseAPIPath, destinationURL+enterpriseUploadsPath, tokenClient)
if err != nil {
return errors.Wrap(err, "Error creating GitHub Enterprise client.")
}
rootRequest, err := client.NewRequest("GET", enterpriseAPIPath, nil)
if err != nil {
return errors.Wrap(err, "Error constructing request for GitHub Enterprise client.")
}
rootResponse, err := client.Do(ctx, rootRequest, nil)
if err != nil {
return errors.Wrap(err, "Error checking connectivity for GitHub Enterprise client.")
}
if rootRequest.URL != rootResponse.Request.URL {
updatedBaseURL, _ := url.Parse(client.BaseURL.String())
updatedBaseURL.Scheme = rootResponse.Request.URL.Scheme
updatedBaseURL.Host = rootResponse.Request.URL.Host
log.Warnf("%s redirected to %s. The URL %s will be used for all API requests.", rootRequest.URL, rootResponse.Request.URL, updatedBaseURL)
updatedUploadsURL, _ := url.Parse(client.UploadURL.String())
updatedUploadsURL.Scheme = rootResponse.Request.URL.Scheme
updatedUploadsURL.Host = rootResponse.Request.URL.Host
client, err = github.NewEnterpriseClient(updatedBaseURL.String(), updatedUploadsURL.String(), tokenClient)
if err != nil {
return errors.Wrap(err, "Error creating GitHub Enterprise client.")
}
}

destinationRepositorySplit := strings.Split(destinationRepository, "/")
destinationRepositoryOwner := destinationRepositorySplit[0]
Expand Down