Skip to content

Commit

Permalink
feat: Allow for more than 1 recursive redirection upon receiving HTTP…
Browse files Browse the repository at this point in the history
… 301 status
  • Loading branch information
k0ral committed Sep 25, 2023
1 parent aa3fcbe commit c45c26b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions github/actions_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func (s *ActionsService) GetArtifact(ctx context.Context, owner, repo string, ar
// DownloadArtifact gets a redirect URL to download an archive for a repository.
//
// GitHub API docs: https://docs.github.com/en/rest/actions/artifacts#download-an-artifact
func (s *ActionsService) DownloadArtifact(ctx context.Context, owner, repo string, artifactID int64, followRedirects bool) (*url.URL, *Response, error) {
func (s *ActionsService) DownloadArtifact(ctx context.Context, owner, repo string, artifactID int64, maxRedirects int) (*url.URL, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v/zip", owner, repo, artifactID)

resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions github/actions_workflow_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ func (s *ActionsService) GetWorkflowJobByID(ctx context.Context, owner, repo str
// GetWorkflowJobLogs gets a redirect URL to download a plain text file of logs for a workflow job.
//
// GitHub API docs: https://docs.github.com/en/rest/actions/workflow-jobs#download-job-logs-for-a-workflow-run
func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, followRedirects bool) (*url.URL, *Response, error) {
func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, maxRedirects int) (*url.URL, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/logs", owner, repo, jobID)

resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
if err != nil {
return nil, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions github/actions_workflow_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo
// GetWorkflowRunAttemptLogs gets a redirect URL to download a plain text file of logs for a workflow run for attempt number.
//
// GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#download-workflow-run-attempt-logs
func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, followRedirects bool) (*url.URL, *Response, error) {
func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, maxRedirects int) (*url.URL, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v/logs", owner, repo, runID, attemptNumber)

resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -287,10 +287,10 @@ func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo
// GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run.
//
// GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#download-workflow-run-logs
func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, maxRedirects int) (*url.URL, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)

resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
if err != nil {
return nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ func formatRateReset(d time.Duration) string {

// When using roundTripWithOptionalFollowRedirect, note that it
// is the responsibility of the caller to close the response body.
func (c *Client) roundTripWithOptionalFollowRedirect(ctx context.Context, u string, followRedirects bool, opts ...RequestOption) (*http.Response, error) {
func (c *Client) roundTripWithOptionalFollowRedirect(ctx context.Context, u string, maxRedirects int, opts ...RequestOption) (*http.Response, error) {
req, err := c.NewRequest("GET", u, nil, opts...)
if err != nil {
return nil, err
Expand All @@ -1577,10 +1577,10 @@ func (c *Client) roundTripWithOptionalFollowRedirect(ctx context.Context, u stri
}

// If redirect response is returned, follow it
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
if maxRedirects > 0 && resp.StatusCode == http.StatusMovedPermanently {
_ = resp.Body.Close()
u = resp.Header.Get("Location")
resp, err = c.roundTripWithOptionalFollowRedirect(ctx, u, false, opts...)
resp, err = c.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects-1, opts...)
}
return resp, err
}
Expand Down
4 changes: 2 additions & 2 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -1274,10 +1274,10 @@ func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, re
// GetBranch gets the specified branch for a repository.
//
// GitHub API docs: https://docs.github.com/en/rest/branches/branches#get-a-branch
func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string, followRedirects bool) (*Branch, *Response, error) {
func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string, maxRedirects int) (*Branch, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, branch)

resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions github/repos_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ const (
// or github.Zipball constant.
//
// GitHub API docs: https://docs.github.com/en/rest/repos/contents/#get-archive-link
func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat ArchiveFormat, opts *RepositoryContentGetOptions, followRedirects bool) (*url.URL, *Response, error) {
func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat ArchiveFormat, opts *RepositoryContentGetOptions, maxRedirects int) (*url.URL, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/%s", owner, repo, archiveformat)
if opts != nil && opts.Ref != "" {
u += fmt.Sprintf("/%s", opts.Ref)
}
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit c45c26b

Please sign in to comment.