Skip to content

Commit abe96c5

Browse files
authored
Merge pull request #49 from github/fix-cryptic-redirect-error
Fix a cryptic error you get if you use the wrong URL for your GitHub Enterprise Server instance.
2 parents c4d4a6e + 7ef8d60 commit abe96c5

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

internal/push/push.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ const repositoryHomepage = "https://github.com/github/codeql-action-sync-tool/"
3737
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."
3838
const errorInvalidDestinationToken = "The destination token you've provided is not valid."
3939

40+
const enterpriseAPIPath = "/api/v3"
41+
const enterpriseUploadsPath = "/api/uploads"
42+
4043
type pushService struct {
4144
ctx context.Context
4245
cacheDirectory cachedirectory.CacheDirectory
@@ -361,10 +364,31 @@ func Push(ctx context.Context, cacheDirectory cachedirectory.CacheDirectory, des
361364
&token,
362365
)
363366
tokenClient := oauth2.NewClient(ctx, tokenSource)
364-
client, err := github.NewEnterpriseClient(destinationURL+"/api/v3", destinationURL+"/api/uploads", tokenClient)
367+
client, err := github.NewEnterpriseClient(destinationURL+enterpriseAPIPath, destinationURL+enterpriseUploadsPath, tokenClient)
365368
if err != nil {
366369
return errors.Wrap(err, "Error creating GitHub Enterprise client.")
367370
}
371+
rootRequest, err := client.NewRequest("GET", enterpriseAPIPath, nil)
372+
if err != nil {
373+
return errors.Wrap(err, "Error constructing request for GitHub Enterprise client.")
374+
}
375+
rootResponse, err := client.Do(ctx, rootRequest, nil)
376+
if err != nil {
377+
return errors.Wrap(err, "Error checking connectivity for GitHub Enterprise client.")
378+
}
379+
if rootRequest.URL != rootResponse.Request.URL {
380+
updatedBaseURL, _ := url.Parse(client.BaseURL.String())
381+
updatedBaseURL.Scheme = rootResponse.Request.URL.Scheme
382+
updatedBaseURL.Host = rootResponse.Request.URL.Host
383+
log.Warnf("%s redirected to %s. The URL %s will be used for all API requests.", rootRequest.URL, rootResponse.Request.URL, updatedBaseURL)
384+
updatedUploadsURL, _ := url.Parse(client.UploadURL.String())
385+
updatedUploadsURL.Scheme = rootResponse.Request.URL.Scheme
386+
updatedUploadsURL.Host = rootResponse.Request.URL.Host
387+
client, err = github.NewEnterpriseClient(updatedBaseURL.String(), updatedUploadsURL.String(), tokenClient)
388+
if err != nil {
389+
return errors.Wrap(err, "Error creating GitHub Enterprise client.")
390+
}
391+
}
368392

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

0 commit comments

Comments
 (0)