Skip to content

Use OkHttpGitHubConnector so OkHttp interceptors see GitHub API calls - #1576

Merged
jtnord merged 2 commits into
jenkinsci:masterfrom
NoamGoren:use-okhttp-github-connector
Nov 19, 2026
Merged

jtnord merged 2 commits into
jenkinsci:masterfrom
NoamGoren:use-okhttp-github-connector

Conversation

@NoamGoren

Copy link
Copy Markdown
Contributor

Description

Fixes #1575.

Connector now gives github-api an OkHttpGitHubConnector instead of the deprecated OkHttpConnector. The old connector sends each request through ObsoleteUrlFactory, which clears the OkHttp client's interceptors and network interceptors on every call, so OkHttp instrumentation (the OpenTelemetry Java agent, for example) never saw the plugin's GitHub API calls. github-api recommends this switch (hub4j/github-api#1262) and removes OkHttpConnector in 2.0.

 Connector.createGitHubBuilder(apiUrl, cache)
-  gb.withConnector(new OkHttpConnector(baseClient + cache))     // per request: client rebuilt, interceptors cleared
+  createGitHubBuilder(apiUrl, cache, baseClient)
+    gb.withConnector(new OkHttpGitHubConnector(client + cache)) // per request: client.newCall(request).execute()

The new package-private createGitHubBuilder(apiUrl, cache, client) lets the test pass a client with interceptors. Production code still passes baseClient.

Same as before (github-api 1.330, both connectors):

  • cacheMaxAge 0, TLS connection specs, Cache-Control: max-age=0 when there is a cache, ETag revalidation from the disk cache
  • okhttp-api's proxy selector and authenticator, timeouts, redirects, gzip, error bodies
  • CUSTOMIZED still reads the X-RateLimit-* headers. github-api now hands it a GitHubConnectorResponseHttpUrlConnectionAdapter instead of the ObsoleteUrlFactory connection.

Changes:

  • Client interceptors run. This is the fix.
  • The User-Agent header changes from ObsoleteUrlFactory to OkHttp's default (okhttp/4.12.0), because github-api doesn't set one. Related: JENKINS-59318.
  • GitHub.getConnector() throws UnsupportedOperationException with this connector, as the github-api 1.300 release notes say. The plugin doesn't call it, and a code search of jenkinsci finds no caller.
  • A RuntimeException thrown inside OkHttp is no longer wrapped in an IOException.

Testing done

ConnectorTest is new. For the "before" column, only the connector line was put back to OkHttpConnector:

Test OkHttpConnector (before) OkHttpGitHubConnector (after)
requestsGoThroughTheClientInterceptors fails: both interceptor lists are empty passes
cachedResponseIsRevalidatedWithETag passes passes
rateLimitExceededReportsTheRateLimitHeaders passes passes

The last two show that the disk cache and CUSTOMIZED behave as before. No existing test covered CUSTOMIZED.

mvn verify:

  • This branch: 579 tests, 0 failures, 15 skipped, on JDK 21.0.8 and on JDK 25.0.5 (macOS).
  • master: 576 tests, 0 failures, 15 skipped.
  • Not run on Windows. The cache test closes its Cache, so Windows can delete the temporary directory.

End to end with the OpenTelemetry Java agent 2.33.1 (OkHttp instrumentation only, logging exporter). A JenkinsRule test ran Connector.connect("https://api.github.com", null) and two getRepository calls, unauthenticated:

before (OkHttpConnector):       no http.client.request.duration points
after  (OkHttpGitHubConnector): io.opentelemetry.okhttp-3.0  api.github.com  GET  200  count=3  (/, /rate_limit, repository)
                                io.opentelemetry.okhttp-3.0  api.github.com  GET  304  count=1  (repository again, served from the disk cache)

Manual test: start Jenkins with the agent flags from #1575 and run its script console snippet. The agent's metric output then shows http.client.request.duration for api.github.com.

Submitter checklist

Reviewer checklist

  • Run the changes and verify that the change matches the issue description
  • Reviewed the code
  • Verified that the appropriate tests have been written or valid explanation given

Documentation changes

  • Link to jenkins.io PR, or an explanation for why no doc changes are needed: no configuration or documented behavior changes.

Users/aliases to notify

The deprecated OkHttpConnector sends each request through github-api's
ObsoleteUrlFactory, which clears the client's interceptors and network
interceptors on every call. OkHttp instrumentation, such as the
OpenTelemetry Java agent, never sees the plugin's GitHub API requests.
github-api deprecated OkHttpConnector in favor of OkHttpGitHubConnector
(hub4j/github-api#1262) and removes it in 2.0.

ConnectorTest checks that client interceptors run, that a cached
response is revalidated with its ETag, and that the rate limit handler
still reads the X-RateLimit headers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
JenkinsRule turns URLConnection caches off on Windows. ObsoleteUrlFactory
followed that and dropped the OkHttp cache, so on Windows
statWhenRoot404andThenIncorrectCached never used it. OkHttpGitHubConnector
uses the cache, and its open journal keeps JenkinsRule from deleting
JENKINS_HOME.

Enable the cache only off Windows, where the default cache size is not 0,
expect no Cache-Control header when there is no cache, and restore the cache
size after each test so that it no longer leaks into later tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jtnord
jtnord merged commit 87ea663 into jenkinsci:master Nov 19, 2026
16 checks passed
@NoamGoren

Copy link
Copy Markdown
Contributor Author

@jtnord thanks for reviewing the PR. Would you also be able to review jenkinsci/github-plugin#800 as well?

@jtnord

jtnord commented Nov 19, 2026

Copy link
Copy Markdown
Member

@jtnord thanks for reviewing the PR. Would you also be able to review jenkinsci/github-plugin#800 as well?

Whilst the PR looks OK, I am not a maintainer of the github-plugin.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GitHub API calls skip OkHttp interceptors, so the OpenTelemetry Java agent records none

2 participants