|
1 | 1 | const core = require('@actions/core'); |
2 | 2 | const github = require('@actions/github'); |
3 | | -const github_defaults = require('@actions/github/lib/utils').defaults; |
| 3 | +const github_utils = require('@actions/github/lib/utils'); |
4 | 4 | const tc = require('@actions/tool-cache'); |
5 | 5 | const exec = require('@actions/exec'); |
6 | 6 | const semver = require('semver'); |
@@ -111,16 +111,36 @@ async function determinePmdRelease(pmdVersion, token) { |
111 | 111 |
|
112 | 112 |
|
113 | 113 | 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 | + |
114 | 119 | 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}`); |
116 | 122 | // 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 }); |
119 | 124 | } 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}`); |
120 | 126 | // explicitly overwrite base url to be public github api, as pmd/pmd is only available there |
121 | 127 | // 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 | + } |
124 | 144 | } |
125 | 145 |
|
126 | 146 | let release; |
@@ -162,7 +182,18 @@ const determineModifiedFiles = async function(token, sourcePath) { |
162 | 182 | // creating new context instead of using "github.context" to reinitialize for unit testing |
163 | 183 | const context = new github.context.constructor(); |
164 | 184 | 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 | + |
166 | 197 | if (context.eventName === 'pull_request') { |
167 | 198 | core.debug(`Pull request ${eventData.number}: ${eventData.pull_request.html_url}`); |
168 | 199 |
|
|
0 commit comments