Skip to content

Commit c45c26b

Browse files
committed
feat: Allow for more than 1 recursive redirection upon receiving HTTP 301 status
1 parent aa3fcbe commit c45c26b

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

github/actions_artifacts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ func (s *ActionsService) GetArtifact(ctx context.Context, owner, repo string, ar
121121
// DownloadArtifact gets a redirect URL to download an archive for a repository.
122122
//
123123
// GitHub API docs: https://docs.github.com/en/rest/actions/artifacts#download-an-artifact
124-
func (s *ActionsService) DownloadArtifact(ctx context.Context, owner, repo string, artifactID int64, followRedirects bool) (*url.URL, *Response, error) {
124+
func (s *ActionsService) DownloadArtifact(ctx context.Context, owner, repo string, artifactID int64, maxRedirects int) (*url.URL, *Response, error) {
125125
u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v/zip", owner, repo, artifactID)
126126

127-
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
127+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
128128
if err != nil {
129129
return nil, nil, err
130130
}

github/actions_workflow_jobs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ func (s *ActionsService) GetWorkflowJobByID(ctx context.Context, owner, repo str
115115
// GetWorkflowJobLogs gets a redirect URL to download a plain text file of logs for a workflow job.
116116
//
117117
// GitHub API docs: https://docs.github.com/en/rest/actions/workflow-jobs#download-job-logs-for-a-workflow-run
118-
func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, followRedirects bool) (*url.URL, *Response, error) {
118+
func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, maxRedirects int) (*url.URL, *Response, error) {
119119
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/logs", owner, repo, jobID)
120120

121-
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
121+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
122122
if err != nil {
123123
return nil, nil, err
124124
}

github/actions_workflow_runs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo
211211
// GetWorkflowRunAttemptLogs gets a redirect URL to download a plain text file of logs for a workflow run for attempt number.
212212
//
213213
// GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#download-workflow-run-attempt-logs
214-
func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, followRedirects bool) (*url.URL, *Response, error) {
214+
func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, maxRedirects int) (*url.URL, *Response, error) {
215215
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v/logs", owner, repo, runID, attemptNumber)
216216

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

293-
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
293+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
294294
if err != nil {
295295
return nil, nil, err
296296
}

github/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ func formatRateReset(d time.Duration) string {
15581558

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

15791579
// If redirect response is returned, follow it
1580-
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
1580+
if maxRedirects > 0 && resp.StatusCode == http.StatusMovedPermanently {
15811581
_ = resp.Body.Close()
15821582
u = resp.Header.Get("Location")
1583-
resp, err = c.roundTripWithOptionalFollowRedirect(ctx, u, false, opts...)
1583+
resp, err = c.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects-1, opts...)
15841584
}
15851585
return resp, err
15861586
}

github/repos.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,10 +1274,10 @@ func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, re
12741274
// GetBranch gets the specified branch for a repository.
12751275
//
12761276
// GitHub API docs: https://docs.github.com/en/rest/branches/branches#get-a-branch
1277-
func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string, followRedirects bool) (*Branch, *Response, error) {
1277+
func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string, maxRedirects int) (*Branch, *Response, error) {
12781278
u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, branch)
12791279

1280-
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
1280+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
12811281
if err != nil {
12821282
return nil, nil, err
12831283
}

github/repos_contents.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,12 @@ const (
313313
// or github.Zipball constant.
314314
//
315315
// GitHub API docs: https://docs.github.com/en/rest/repos/contents/#get-archive-link
316-
func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat ArchiveFormat, opts *RepositoryContentGetOptions, followRedirects bool) (*url.URL, *Response, error) {
316+
func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat ArchiveFormat, opts *RepositoryContentGetOptions, maxRedirects int) (*url.URL, *Response, error) {
317317
u := fmt.Sprintf("repos/%s/%s/%s", owner, repo, archiveformat)
318318
if opts != nil && opts.Ref != "" {
319319
u += fmt.Sprintf("/%s", opts.Ref)
320320
}
321-
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
321+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
322322
if err != nil {
323323
return nil, nil, err
324324
}

0 commit comments

Comments
 (0)