Skip to content

feat!: Refactor request context#4151

Open
stevehipwell wants to merge 4 commits intogoogle:masterfrom
stevehipwell:request-context-refactor
Open

feat!: Refactor request context#4151
stevehipwell wants to merge 4 commits intogoogle:masterfrom
stevehipwell:request-context-refactor

Conversation

@stevehipwell
Copy link
Copy Markdown
Contributor

@stevehipwell stevehipwell commented Apr 14, 2026

Closes #4127

This PR makes the following changes:

  • Switch to using http.NewRequestWithContext so we no longer need the withContext helper
  • Refactor all HTTP calls to use client.BareDo to get benefits such as rate limit support

Comment thread github/actions_artifacts.go Outdated
@gmlewis gmlewis added NeedsReview PR is awaiting a review before merging. Breaking API Change PR will require a bump to the major version num in next release. Look here to see the change(s). labels Apr 14, 2026
@stevehipwell stevehipwell marked this pull request as draft April 14, 2026 13:08
@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis I'm going to completely rebase this PR as I need to split it into 3.

@stevehipwell
Copy link
Copy Markdown
Contributor Author

#4152 replaces the app engine removal part of this PR.

Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
@stevehipwell stevehipwell force-pushed the request-context-refactor branch from 361db80 to 9785804 Compare April 21, 2026 10:17
@stevehipwell stevehipwell marked this pull request as ready for review April 21, 2026 10:18
@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis I've rebased this PR and it should be ready for review now.

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis I had to update the extraneousnew linter now that client.Do only has 2 args, could you please pay particular attention to that change as I don't have the full context there.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 99.86922% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.18%. Comparing base (3430163) to head (58e431f).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
test/fields/fields.go 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4151      +/-   ##
==========================================
- Coverage   93.18%   93.18%   -0.01%     
==========================================
  Files         210      210              
  Lines       24568    24564       -4     
==========================================
- Hits        22894    22889       -5     
- Misses       1478     1479       +1     
  Partials      196      196              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case a force-push is performed, I'm going to stop my review here before examining the remaining 182 files.

Comment thread github/github.go
Comment thread github/github.go
Comment thread github/github.go
Comment thread github/github.go
@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis I won't force push this, I only force pushed to realign after we split the PR up and I did provide a warning that I was doing it. Could you possibly review the linter, the only real changes are there and in github.go.

@gmlewis
Copy link
Copy Markdown
Collaborator

gmlewis commented Apr 21, 2026

@gmlewis I won't force push this, I only force pushed to realign after we split the PR up and I did provide a warning that I was doing it. Could you possibly review the linter, the only real changes are there and in github.go.

OK, the linter LGTM.

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis I've made the requested docs changes.

Copy link
Copy Markdown
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question, otherwise LGTM.

cc: @alexandear - @zyfy29 - @Not-Dhananjay-Mishra - @munlicode

Comment thread github/github_test.go
Copy link
Copy Markdown
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Contributor

@Not-Dhananjay-Mishra Not-Dhananjay-Mishra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis how do you want me to resolve the conflict?

@gmlewis
Copy link
Copy Markdown
Collaborator

gmlewis commented Apr 22, 2026

@stevehipwell - apologies, but could you please resolve conflicts before I merge?
Oh, and we need to hear from @alexandear too.

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis how do you want me to resolve the conflict?

@gmlewis I'd normally rebase to fix a conflict like this, but I know you don't like the commits to be modified.

@gmlewis
Copy link
Copy Markdown
Collaborator

gmlewis commented Apr 22, 2026

@gmlewis I'd normally rebase to fix a conflict like this, but I know you don't like the commits to be modified

  1. Go to your master branch and git pull
  2. Go to your branch and git merge master
  3. Manually resolve conflicts or git mergetool if you have already set up a tool like kdiff3
  4. git push ... to this PRs branch

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis merge commit it is, was just checking.

@gmlewis
Copy link
Copy Markdown
Collaborator

gmlewis commented Apr 22, 2026

Although if you have a better mechanism, I am fine with that in this huge PR.

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis assuming that you're squash merging and you'd rather the commits aren't modified, a merge commit is likely to be the best compromise.

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis it should be ready again.

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@alexandear I think we're just waiting on you now?

@stevehipwell
Copy link
Copy Markdown
Contributor Author

@gmlewis I think we're ready to merge now.

Comment thread github/rate_limit.go
func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, error) {
req, err := s.client.NewRequest("GET", "rate_limit", nil)
// This resource is not subject to rate limits.
if !s.client.DisableRateLimitCheck {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this added? Before this PR we don't have if !s.client.DisableRateLimitCheck

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I comment out the check

Image

tests still pass:

❯ go test -failfast -count=1 ./...
ok      github.com/google/go-github/v85/github  2.472s
?       github.com/google/go-github/v85/test/fields     [no test files]
?       github.com/google/go-github/v85/test/integration        [no test files]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It short circuits the context modification as it's never going to be retrieved with s.client.DisableRateLimitCheck set to true.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to move this change to a separate PR and add a test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to remove this change as it's demonstrably "correct". @alexandear if you have a suggestion for how to test this I'm happy to add a test, but it doesn't look testable to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be something like the following:

func TestRateLimits_bypassRateLimitCheckContext(t *testing.T) {
	t.Parallel()
	tests := []struct {
		name                  string
		disableRateLimitCheck bool
		wantBypassValue       any
	}{
		{
			name:                  "DisableRateLimitCheck disabled",
			disableRateLimitCheck: false,
			wantBypassValue:       true,
		},
		{
			name:                  "DisableRateLimitCheck enabled",
			disableRateLimitCheck: true,
			wantBypassValue:       nil,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()
			client, mux, _ := setup(t)
			client.DisableRateLimitCheck = tt.disableRateLimitCheck

			mux.HandleFunc("/rate_limit", func(w http.ResponseWriter, r *http.Request) {
				testMethod(t, r, "GET")
				fmt.Fprint(w, `{"resources":{}}`)
			})

			_, resp, err := client.RateLimit.Get(t.Context())
			if err != nil {
				t.Errorf("RateLimits returned error: %v", err)
			}

			if got, want := resp.Request.Context().Value(BypassRateLimitCheck), tt.wantBypassValue; got != want {
				t.Errorf("Request context bypass value = %v, want %v", got, want)
			}
		})
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking API Change PR will require a bump to the major version num in next release. Look here to see the change(s). NeedsReview PR is awaiting a review before merging. waiting for reply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request context question

4 participants