Skip to content

Commit 6c4c7ab

Browse files
committed
Using fetch-mock to mock octokit requests
1 parent 1247fd9 commit 6c4c7ab

File tree

5 files changed

+956
-864
lines changed

5 files changed

+956
-864
lines changed

dist/index.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const core = require('@actions/core');
22
const github = require('@actions/github');
3-
const github_defaults = require('@actions/github/lib/utils').defaults;
3+
const github_utils = require('@actions/github/lib/utils');
44
const tc = require('@actions/tool-cache');
55
const exec = require('@actions/exec');
66
const semver = require('semver');
@@ -111,16 +111,36 @@ async function determinePmdRelease(pmdVersion, token) {
111111

112112

113113
const PUBLIC_GITHUB_API_URL = 'https://api.github.com';
114+
// the configured GitHubToken can only be used for the public GitHub instance.
115+
// If the action is used on a on-premise GHES instance, then the given token is
116+
// not valid for the public instance.
117+
const canUseToken = github_utils.defaults.baseUrl === PUBLIC_GITHUB_API_URL;
118+
114119
let octokit;
115-
if (github_defaults.baseUrl === PUBLIC_GITHUB_API_URL) {
120+
if (canUseToken) {
121+
core.debug(`Using token to access repos/pmd/pmd/releases/latest on ${github_utils.defaults.baseUrl}`);
116122
// only use authenticated token, if on public github and not on a custom GHES instance
117-
octokit = github.getOctokit(token);
118-
core.debug(`Using token to access repos/pmd/pmd/releases/latest on ${github_defaults.baseUrl}`);
123+
octokit = new github_utils.GitHub({ auth: token });
119124
} else {
125+
core.debug(`Not using token to access repos/pmd/pmd/releases/latest on ${PUBLIC_GITHUB_API_URL}, as token is for ${github_utils.defaults.baseUrl}`);
120126
// explicitly overwrite base url to be public github api, as pmd/pmd is only available there
121127
// not using the token, as that would only be valid for GHES
122-
octokit = new Octokit({ baseUrl: PUBLIC_GITHUB_API_URL });
123-
core.debug(`Not using token to access repos/pmd/pmd/releases/latest on ${PUBLIC_GITHUB_API_URL}, as token is for ${github_defaults.baseUrl}`);
128+
octokit = new github_utils.GitHub({ baseUrl: PUBLIC_GITHUB_API_URL });
129+
}
130+
131+
if (process.env['JEST_WORKER_ID']) {
132+
core.debug('Detected unit test - directly using Octokit without proxy configuration');
133+
// during unit test, we use Octokit directly. This uses then fetch to do the requests,
134+
// which can be mocked with fetch-mock(-jest). The octokit instance retrieved via @actions/github
135+
// uses under the hood undici, which uses a raw socket (node:tls), which can neither be mocked
136+
// by nock nor by fetch-mock.
137+
// Using @actions/github to get the octokit instance would also make sure, that the proxy configuration
138+
// is respected - which is ignored now in unit tests.
139+
if (canUseToken) {
140+
octokit = new Octokit({ baseUrl: PUBLIC_GITHUB_API_URL, auth: token });
141+
} else {
142+
octokit = new Octokit({ baseUrl: PUBLIC_GITHUB_API_URL });
143+
}
124144
}
125145

126146
let release;
@@ -162,7 +182,18 @@ const determineModifiedFiles = async function(token, sourcePath) {
162182
// creating new context instead of using "github.context" to reinitialize for unit testing
163183
const context = new github.context.constructor();
164184
const eventData = context.payload;
165-
const octokit = github.getOctokit(token);
185+
let octokit = github.getOctokit(token);
186+
if (process.env['JEST_WORKER_ID']) {
187+
core.debug('Detected unit test - directly using Octokit without proxy configuration');
188+
// during unit test, we use Octokit directly. This uses then fetch to do the requests,
189+
// which can be mocked with fetch-mock(-jest). The octokit instance retrieved via @actions/github
190+
// uses under the hood undici, which uses a raw socket (node:tls), which can neither be mocked
191+
// by nock nor by fetch-mock.
192+
// Using @actions/github to get the octokit instance would also make sure, that the proxy configuration
193+
// is respected - which is ignored now in unit tests.
194+
octokit = new Octokit({ baseUrl: github_utils.defaults.baseUrl, auth: token });
195+
}
196+
166197
if (context.eventName === 'pull_request') {
167198
core.debug(`Pull request ${eventData.number}: ${eventData.pull_request.html_url}`);
168199

0 commit comments

Comments
 (0)