mirror of
https://github.com/peter-evans/create-pull-request.git
synced 2026-10-02 02:17:43 +08:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f6978faf0 | ||
|
|
d32e88dac7 | ||
|
|
8170bccad1 | ||
|
|
00418193b4 | ||
|
|
b993918c85 | ||
|
|
36d7c8468b | ||
|
|
a45d1fb447 | ||
|
|
3499eb6183 | ||
|
|
3f3b473b8c | ||
|
|
6699836a21 |
@@ -29,11 +29,11 @@ jobs:
|
||||
- run: npm run format-check
|
||||
- run: npm run lint
|
||||
- run: npm run test
|
||||
- uses: actions/upload-artifact@v6
|
||||
- uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: dist
|
||||
path: dist
|
||||
- uses: actions/upload-artifact@v6
|
||||
- uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: action.yml
|
||||
path: action.yml
|
||||
@@ -50,12 +50,12 @@ jobs:
|
||||
with:
|
||||
ref: main
|
||||
- if: matrix.target == 'built' || github.event_name == 'pull_request'
|
||||
uses: actions/download-artifact@v7
|
||||
uses: actions/download-artifact@v8
|
||||
with:
|
||||
name: dist
|
||||
path: dist
|
||||
- if: matrix.target == 'built' || github.event_name == 'pull_request'
|
||||
uses: actions/download-artifact@v7
|
||||
uses: actions/download-artifact@v8
|
||||
with:
|
||||
name: action.yml
|
||||
path: .
|
||||
@@ -119,7 +119,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: actions/download-artifact@v7
|
||||
- uses: actions/download-artifact@v8
|
||||
with:
|
||||
name: dist
|
||||
path: dist
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import {GitCommandManager} from '../lib/git-command-manager'
|
||||
import {GitConfigHelper} from '../lib/git-config-helper'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
|
||||
const REPO_PATH = '/git/local/repos/test-base'
|
||||
|
||||
@@ -9,104 +7,29 @@ const extraheaderConfigKey = 'http.https://127.0.0.1/.extraheader'
|
||||
|
||||
describe('git-config-helper integration tests', () => {
|
||||
let git: GitCommandManager
|
||||
let originalRunnerTemp: string | undefined
|
||||
|
||||
beforeAll(async () => {
|
||||
git = await GitCommandManager.create(REPO_PATH)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
// Save original RUNNER_TEMP
|
||||
originalRunnerTemp = process.env['RUNNER_TEMP']
|
||||
// Create a temp directory for tests
|
||||
const tempDir = await fs.promises.mkdtemp('/tmp/cpr-test-')
|
||||
process.env['RUNNER_TEMP'] = tempDir
|
||||
process.env['GITHUB_WORKSPACE'] = REPO_PATH
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
// Clean up RUNNER_TEMP
|
||||
const runnerTemp = process.env['RUNNER_TEMP']
|
||||
if (runnerTemp && runnerTemp.startsWith('/tmp/cpr-test-')) {
|
||||
await fs.promises.rm(runnerTemp, {recursive: true, force: true})
|
||||
}
|
||||
// Restore original RUNNER_TEMP
|
||||
if (originalRunnerTemp !== undefined) {
|
||||
process.env['RUNNER_TEMP'] = originalRunnerTemp
|
||||
} else {
|
||||
delete process.env['RUNNER_TEMP']
|
||||
}
|
||||
})
|
||||
|
||||
it('tests save and restore with no persisted auth', async () => {
|
||||
const gitConfigHelper = await GitConfigHelper.create(git)
|
||||
await gitConfigHelper.close()
|
||||
})
|
||||
|
||||
it('tests configure and removal of auth using credentials file', async () => {
|
||||
const runnerTemp = process.env['RUNNER_TEMP']!
|
||||
it('tests configure and removal of auth', async () => {
|
||||
const gitConfigHelper = await GitConfigHelper.create(git)
|
||||
await gitConfigHelper.configureToken('github-token')
|
||||
|
||||
// Verify credentials file was created in RUNNER_TEMP
|
||||
const files = await fs.promises.readdir(runnerTemp)
|
||||
const credentialsFiles = files.filter(
|
||||
f => f.startsWith('git-credentials-') && f.endsWith('.config')
|
||||
)
|
||||
expect(credentialsFiles.length).toBe(1)
|
||||
|
||||
// Verify credentials file contains the auth token
|
||||
const credentialsPath = path.join(runnerTemp, credentialsFiles[0])
|
||||
const credentialsContent = await fs.promises.readFile(
|
||||
credentialsPath,
|
||||
'utf8'
|
||||
)
|
||||
expect(credentialsContent).toContain(
|
||||
expect(await git.configExists(extraheaderConfigKey)).toBeTruthy()
|
||||
expect(await git.getConfigValue(extraheaderConfigKey)).toEqual(
|
||||
'AUTHORIZATION: basic eC1hY2Nlc3MtdG9rZW46Z2l0aHViLXRva2Vu'
|
||||
)
|
||||
|
||||
// Verify includeIf entries were added to local config
|
||||
const includeIfKeys = await git.tryGetConfigKeys('^includeIf\\.gitdir:')
|
||||
expect(includeIfKeys.length).toBeGreaterThan(0)
|
||||
|
||||
// Count credential includes pointing to this action's credentials file
|
||||
let credentialIncludesForThisAction = 0
|
||||
for (const key of includeIfKeys) {
|
||||
const values = await git.tryGetConfigValues(key)
|
||||
for (const value of values) {
|
||||
if (value === credentialsPath) {
|
||||
credentialIncludesForThisAction++
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(credentialIncludesForThisAction).toBeGreaterThan(0)
|
||||
|
||||
await gitConfigHelper.close()
|
||||
|
||||
// Verify credentials file was removed
|
||||
const filesAfter = await fs.promises.readdir(runnerTemp)
|
||||
const credentialsFilesAfter = filesAfter.filter(
|
||||
f => f.startsWith('git-credentials-') && f.endsWith('.config')
|
||||
)
|
||||
expect(credentialsFilesAfter.length).toBe(0)
|
||||
|
||||
// Verify includeIf entries pointing to our specific credentials file were removed
|
||||
const includeIfKeysAfter = await git.tryGetConfigKeys(
|
||||
'^includeIf\\.gitdir:'
|
||||
)
|
||||
let credentialIncludesForThisActionAfter = 0
|
||||
for (const key of includeIfKeysAfter) {
|
||||
const values = await git.tryGetConfigValues(key)
|
||||
for (const value of values) {
|
||||
if (value === credentialsPath) {
|
||||
credentialIncludesForThisActionAfter++
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(credentialIncludesForThisActionAfter).toBe(0)
|
||||
expect(await git.configExists(extraheaderConfigKey)).toBeFalsy()
|
||||
})
|
||||
|
||||
it('tests save and restore of persisted auth (old-style)', async () => {
|
||||
it('tests save and restore of persisted auth', async () => {
|
||||
const extraheaderConfigValue = 'AUTHORIZATION: basic ***persisted-auth***'
|
||||
await git.config(extraheaderConfigKey, extraheaderConfigValue)
|
||||
|
||||
|
||||
@@ -118,3 +118,71 @@ describe('utils tests', () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('retryWithBackoff', () => {
|
||||
const makeConsistencyError = () => {
|
||||
const error = new Error(
|
||||
'Validation Failed: "Could not resolve to a node with the global id of \'PR_abc123\'."'
|
||||
)
|
||||
;(error as any).status = 422
|
||||
return error
|
||||
}
|
||||
|
||||
const shouldRetry = (e: unknown): boolean =>
|
||||
e instanceof Error &&
|
||||
(e as any).status === 422 &&
|
||||
e.message.includes('Could not resolve to a node')
|
||||
|
||||
test('succeeds on first attempt without retrying', async () => {
|
||||
const fn = jest.fn().mockResolvedValue('success')
|
||||
const result = await utils.retryWithBackoff(fn, shouldRetry, 2, 1)
|
||||
expect(result).toBe('success')
|
||||
expect(fn).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('retries on eventual consistency 422 and succeeds', async () => {
|
||||
const fn = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(makeConsistencyError())
|
||||
.mockResolvedValue('success')
|
||||
const result = await utils.retryWithBackoff(fn, shouldRetry, 2, 1)
|
||||
expect(result).toBe('success')
|
||||
expect(fn).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
test('exhausts retries on persistent 422 and throws', async () => {
|
||||
const fn = jest.fn().mockRejectedValue(makeConsistencyError())
|
||||
await expect(utils.retryWithBackoff(fn, shouldRetry, 2, 1)).rejects.toThrow(
|
||||
'Could not resolve to a node'
|
||||
)
|
||||
expect(fn).toHaveBeenCalledTimes(3) // 1 initial + 2 retries
|
||||
})
|
||||
|
||||
test('does not retry on non-422 errors', async () => {
|
||||
const error = new Error('Forbidden')
|
||||
;(error as any).status = 403
|
||||
const fn = jest.fn().mockRejectedValue(error)
|
||||
await expect(utils.retryWithBackoff(fn, shouldRetry, 2, 1)).rejects.toThrow(
|
||||
'Forbidden'
|
||||
)
|
||||
expect(fn).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('does not retry on 422 without the consistency error message', async () => {
|
||||
const error = new Error('Validation Failed: invalid label')
|
||||
;(error as any).status = 422
|
||||
const fn = jest.fn().mockRejectedValue(error)
|
||||
await expect(utils.retryWithBackoff(fn, shouldRetry, 2, 1)).rejects.toThrow(
|
||||
'Validation Failed: invalid label'
|
||||
)
|
||||
expect(fn).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('does not retry on plain Error objects', async () => {
|
||||
const fn = jest.fn().mockRejectedValue(new Error('Something broke'))
|
||||
await expect(utils.retryWithBackoff(fn, shouldRetry, 2, 1)).rejects.toThrow(
|
||||
'Something broke'
|
||||
)
|
||||
expect(fn).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
Vendored
+120
-216
@@ -730,15 +730,9 @@ class GitCommandManager {
|
||||
return yield this.exec(args, { allowAllExitCodes: allowAllExitCodes });
|
||||
});
|
||||
}
|
||||
config(configKey, configValue, globalConfig, add, configFile) {
|
||||
config(configKey, configValue, globalConfig, add) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const args = ['config'];
|
||||
if (configFile) {
|
||||
args.push('--file', configFile);
|
||||
}
|
||||
else {
|
||||
args.push(globalConfig ? '--global' : '--local');
|
||||
}
|
||||
const args = ['config', globalConfig ? '--global' : '--local'];
|
||||
if (add) {
|
||||
args.push('--add');
|
||||
}
|
||||
@@ -970,60 +964,6 @@ class GitCommandManager {
|
||||
return output.exitCode === 0;
|
||||
});
|
||||
}
|
||||
tryConfigUnsetValue(configKey, configValue, globalConfig, configFile) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const args = ['config'];
|
||||
if (configFile) {
|
||||
args.push('--file', configFile);
|
||||
}
|
||||
else {
|
||||
args.push(globalConfig ? '--global' : '--local');
|
||||
}
|
||||
args.push('--fixed-value', '--unset', configKey, configValue);
|
||||
const output = yield this.exec(args, { allowAllExitCodes: true });
|
||||
return output.exitCode === 0;
|
||||
});
|
||||
}
|
||||
tryGetConfigValues(configKey, globalConfig, configFile) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const args = ['config'];
|
||||
if (configFile) {
|
||||
args.push('--file', configFile);
|
||||
}
|
||||
else {
|
||||
args.push(globalConfig ? '--global' : '--local');
|
||||
}
|
||||
args.push('--get-all', configKey);
|
||||
const output = yield this.exec(args, { allowAllExitCodes: true });
|
||||
if (output.exitCode !== 0) {
|
||||
return [];
|
||||
}
|
||||
return output.stdout
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(value => value.trim());
|
||||
});
|
||||
}
|
||||
tryGetConfigKeys(pattern, globalConfig, configFile) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const args = ['config'];
|
||||
if (configFile) {
|
||||
args.push('--file', configFile);
|
||||
}
|
||||
else {
|
||||
args.push(globalConfig ? '--global' : '--local');
|
||||
}
|
||||
args.push('--name-only', '--get-regexp', pattern);
|
||||
const output = yield this.exec(args, { allowAllExitCodes: true });
|
||||
if (output.exitCode !== 0) {
|
||||
return [];
|
||||
}
|
||||
return output.stdout
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(key => key.trim());
|
||||
});
|
||||
}
|
||||
tryGetRemoteUrl() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const output = yield this.exec(['config', '--local', '--get', 'remote.origin.url'], { allowAllExitCodes: true });
|
||||
@@ -1160,9 +1100,9 @@ const fs = __importStar(__nccwpck_require__(9896));
|
||||
const path = __importStar(__nccwpck_require__(6928));
|
||||
const url_1 = __nccwpck_require__(7016);
|
||||
const utils = __importStar(__nccwpck_require__(9277));
|
||||
const uuid_1 = __nccwpck_require__(2048);
|
||||
class GitConfigHelper {
|
||||
constructor(git) {
|
||||
this.gitConfigPath = '';
|
||||
this.safeDirectoryConfigKey = 'safe.directory';
|
||||
this.safeDirectoryAdded = false;
|
||||
this.remoteUrl = '';
|
||||
@@ -1170,8 +1110,7 @@ class GitConfigHelper {
|
||||
this.extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***';
|
||||
this.extraheaderConfigValueRegex = '^AUTHORIZATION:';
|
||||
this.persistedExtraheaderConfigValue = '';
|
||||
// Path to the credentials config file in RUNNER_TEMP (new v6-style auth)
|
||||
this.credentialsConfigPath = '';
|
||||
this.backedUpCredentialFiles = [];
|
||||
this.git = git;
|
||||
this.workingDirectory = this.git.getWorkingDirectory();
|
||||
}
|
||||
@@ -1251,15 +1190,16 @@ class GitConfigHelper {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const serverUrl = new url_1.URL(`https://${this.getGitRemote().hostname}`);
|
||||
this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`;
|
||||
// Save and unset persisted extraheader credential in git config if it exists (old-style auth)
|
||||
// Note: checkout@v6 uses credentials files with includeIf, so we don't need to
|
||||
// manipulate those - they work independently via git's include mechanism
|
||||
// Backup checkout@v6 credential files if they exist
|
||||
yield this.hideCredentialFiles();
|
||||
// Save and unset persisted extraheader credential in git config if it exists
|
||||
this.persistedExtraheaderConfigValue = yield this.getAndUnset();
|
||||
});
|
||||
}
|
||||
restorePersistedAuth() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Restore old-style extraheader config if it was persisted
|
||||
// Restore checkout@v6 credential files if they were backed up
|
||||
yield this.unhideCredentialFiles();
|
||||
if (this.persistedExtraheaderConfigValue) {
|
||||
try {
|
||||
yield this.setExtraheaderConfig(this.persistedExtraheaderConfigValue);
|
||||
@@ -1273,160 +1213,69 @@ class GitConfigHelper {
|
||||
}
|
||||
configureToken(token) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Encode the basic credential for HTTPS access
|
||||
// Encode and configure the basic credential for HTTPS access
|
||||
const basicCredential = Buffer.from(`x-access-token:${token}`, 'utf8').toString('base64');
|
||||
core.setSecret(basicCredential);
|
||||
const extraheaderConfigValue = `AUTHORIZATION: basic ${basicCredential}`;
|
||||
// Get or create the credentials config file path
|
||||
const credentialsConfigPath = this.getCredentialsConfigPath();
|
||||
// Write placeholder to the separate credentials config file using git config.
|
||||
// This approach avoids the credential being captured by process creation audit events,
|
||||
// which are commonly logged. For more information, refer to
|
||||
// https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/manage/component-updates/command-line-process-auditing
|
||||
yield this.git.config(this.extraheaderConfigKey, this.extraheaderConfigPlaceholderValue, false, // globalConfig
|
||||
false, // add
|
||||
credentialsConfigPath);
|
||||
// Replace the placeholder in the credentials config file
|
||||
let content = (yield fs.promises.readFile(credentialsConfigPath)).toString();
|
||||
const placeholderIndex = content.indexOf(this.extraheaderConfigPlaceholderValue);
|
||||
if (placeholderIndex < 0 ||
|
||||
placeholderIndex !=
|
||||
content.lastIndexOf(this.extraheaderConfigPlaceholderValue)) {
|
||||
throw new Error(`Unable to replace auth placeholder in ${credentialsConfigPath}`);
|
||||
}
|
||||
content = content.replace(this.extraheaderConfigPlaceholderValue, extraheaderConfigValue);
|
||||
yield fs.promises.writeFile(credentialsConfigPath, content);
|
||||
// Configure includeIf entries to reference the credentials config file
|
||||
yield this.configureIncludeIf(credentialsConfigPath);
|
||||
yield this.setExtraheaderConfig(extraheaderConfigValue);
|
||||
});
|
||||
}
|
||||
removeAuth() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Remove old-style extraheader config if it exists
|
||||
yield this.getAndUnset();
|
||||
// Remove includeIf entries that point to git-credentials-*.config files
|
||||
// and clean up the credentials config files
|
||||
yield this.removeIncludeIfCredentials();
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Gets or creates the path to the credentials config file in RUNNER_TEMP.
|
||||
* @returns The absolute path to the credentials config file
|
||||
*/
|
||||
getCredentialsConfigPath() {
|
||||
if (this.credentialsConfigPath) {
|
||||
return this.credentialsConfigPath;
|
||||
}
|
||||
const runnerTemp = process.env['RUNNER_TEMP'] || '';
|
||||
if (!runnerTemp) {
|
||||
throw new Error('RUNNER_TEMP is not defined');
|
||||
}
|
||||
// Create a unique filename for this action instance
|
||||
const configFileName = `git-credentials-${(0, uuid_1.v4)()}.config`;
|
||||
this.credentialsConfigPath = path.join(runnerTemp, configFileName);
|
||||
core.debug(`Credentials config path: ${this.credentialsConfigPath}`);
|
||||
return this.credentialsConfigPath;
|
||||
}
|
||||
/**
|
||||
* Configures includeIf entries in the local git config to reference the credentials file.
|
||||
* Sets up entries for both host and container paths to support Docker container actions.
|
||||
*/
|
||||
configureIncludeIf(credentialsConfigPath) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Host git directory
|
||||
const gitDir = yield this.git.getGitDirectory();
|
||||
let hostGitDir = path.join(this.workingDirectory, gitDir);
|
||||
hostGitDir = hostGitDir.replace(/\\/g, '/'); // Use forward slashes, even on Windows
|
||||
// Configure host includeIf
|
||||
const hostIncludeKey = `includeIf.gitdir:${hostGitDir}.path`;
|
||||
yield this.git.config(hostIncludeKey, credentialsConfigPath);
|
||||
// Configure host includeIf for worktrees
|
||||
const hostWorktreeIncludeKey = `includeIf.gitdir:${hostGitDir}/worktrees/*.path`;
|
||||
yield this.git.config(hostWorktreeIncludeKey, credentialsConfigPath);
|
||||
// Container paths for Docker container actions
|
||||
const githubWorkspace = process.env['GITHUB_WORKSPACE'];
|
||||
if (githubWorkspace) {
|
||||
let relativePath = path.relative(githubWorkspace, this.workingDirectory);
|
||||
relativePath = relativePath.replace(/\\/g, '/'); // Use forward slashes, even on Windows
|
||||
const containerGitDir = path.posix.join('/github/workspace', relativePath, '.git');
|
||||
// Container credentials config path
|
||||
const containerCredentialsPath = path.posix.join('/github/runner_temp', path.basename(credentialsConfigPath));
|
||||
// Configure container includeIf
|
||||
const containerIncludeKey = `includeIf.gitdir:${containerGitDir}.path`;
|
||||
yield this.git.config(containerIncludeKey, containerCredentialsPath);
|
||||
// Configure container includeIf for worktrees
|
||||
const containerWorktreeIncludeKey = `includeIf.gitdir:${containerGitDir}/worktrees/*.path`;
|
||||
yield this.git.config(containerWorktreeIncludeKey, containerCredentialsPath);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Removes the includeIf entry and credentials config file created by this action instance.
|
||||
* Only cleans up the specific credentials file tracked in this.credentialsConfigPath,
|
||||
* leaving credentials created by other actions (e.g., actions/checkout) intact.
|
||||
*/
|
||||
removeIncludeIfCredentials() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Only clean up if this action instance created a credentials config file
|
||||
if (!this.credentialsConfigPath) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Get all includeIf.gitdir keys from local config
|
||||
const keys = yield this.git.tryGetConfigKeys('^includeIf\\.gitdir:');
|
||||
for (const key of keys) {
|
||||
// Get all values for this key
|
||||
const values = yield this.git.tryGetConfigValues(key);
|
||||
for (const value of values) {
|
||||
// Only remove entries pointing to our specific credentials file
|
||||
if (value === this.credentialsConfigPath) {
|
||||
yield this.git.tryConfigUnsetValue(key, value);
|
||||
core.debug(`Removed includeIf entry: ${key} = ${value}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
// Ignore errors during cleanup
|
||||
core.debug(`Error during includeIf cleanup: ${utils.getErrorMessage(e)}`);
|
||||
}
|
||||
// Delete only our credentials config file
|
||||
const runnerTemp = process.env['RUNNER_TEMP'];
|
||||
const resolvedCredentialsPath = path.resolve(this.credentialsConfigPath);
|
||||
const resolvedRunnerTemp = runnerTemp ? path.resolve(runnerTemp) : '';
|
||||
if (resolvedRunnerTemp &&
|
||||
resolvedCredentialsPath.startsWith(resolvedRunnerTemp + path.sep)) {
|
||||
try {
|
||||
yield fs.promises.unlink(this.credentialsConfigPath);
|
||||
core.info(`Removed credentials config file: ${this.credentialsConfigPath}`);
|
||||
}
|
||||
catch (e) {
|
||||
core.debug(`Could not remove credentials file ${this.credentialsConfigPath}: ${utils.getErrorMessage(e)}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Sets extraheader config directly in .git/config (old-style auth).
|
||||
* Used only for restoring persisted credentials from checkout@v4/v5.
|
||||
*/
|
||||
setExtraheaderConfig(extraheaderConfigValue) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Configure a placeholder value. This approach avoids the credential being captured
|
||||
// by process creation audit events, which are commonly logged. For more information,
|
||||
// refer to https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/manage/component-updates/command-line-process-auditing
|
||||
// See https://github.com/actions/checkout/blob/main/src/git-auth-helper.ts#L267-L274
|
||||
yield this.git.config(this.extraheaderConfigKey, this.extraheaderConfigPlaceholderValue);
|
||||
// Replace the placeholder in the local git config
|
||||
const gitDir = yield this.git.getGitDirectory();
|
||||
const gitConfigPath = path.join(this.workingDirectory, gitDir, 'config');
|
||||
let content = (yield fs.promises.readFile(gitConfigPath)).toString();
|
||||
const index = content.indexOf(this.extraheaderConfigPlaceholderValue);
|
||||
if (index < 0 ||
|
||||
index != content.lastIndexOf(this.extraheaderConfigPlaceholderValue)) {
|
||||
throw new Error(`Unable to replace '${this.extraheaderConfigPlaceholderValue}' in ${gitConfigPath}`);
|
||||
// Replace the placeholder
|
||||
yield this.gitConfigStringReplace(this.extraheaderConfigPlaceholderValue, extraheaderConfigValue);
|
||||
});
|
||||
}
|
||||
hideCredentialFiles() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Temporarily hide checkout@v6 credential files to avoid duplicate auth headers
|
||||
const runnerTemp = process.env['RUNNER_TEMP'];
|
||||
if (!runnerTemp) {
|
||||
return;
|
||||
}
|
||||
content = content.replace(this.extraheaderConfigPlaceholderValue, extraheaderConfigValue);
|
||||
yield fs.promises.writeFile(gitConfigPath, content);
|
||||
try {
|
||||
const files = yield fs.promises.readdir(runnerTemp);
|
||||
for (const file of files) {
|
||||
if (file.startsWith('git-credentials-') && file.endsWith('.config')) {
|
||||
const sourcePath = path.join(runnerTemp, file);
|
||||
const backupPath = `${sourcePath}.bak`;
|
||||
yield fs.promises.rename(sourcePath, backupPath);
|
||||
this.backedUpCredentialFiles.push(backupPath);
|
||||
core.info(`Temporarily hiding checkout credential file: ${file} (will be restored after)`);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
// If directory doesn't exist or we can't read it, just continue
|
||||
core.debug(`Could not backup credential files: ${utils.getErrorMessage(e)}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
unhideCredentialFiles() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Restore checkout@v6 credential files that were backed up
|
||||
for (const backupPath of this.backedUpCredentialFiles) {
|
||||
try {
|
||||
const originalPath = backupPath.replace(/\.bak$/, '');
|
||||
yield fs.promises.rename(backupPath, originalPath);
|
||||
const fileName = path.basename(originalPath);
|
||||
core.info(`Restored checkout credential file: ${fileName}`);
|
||||
}
|
||||
catch (e) {
|
||||
core.warning(`Failed to restore credential file ${backupPath}: ${utils.getErrorMessage(e)}`);
|
||||
}
|
||||
}
|
||||
this.backedUpCredentialFiles = [];
|
||||
});
|
||||
}
|
||||
getAndUnset() {
|
||||
@@ -1445,6 +1294,21 @@ class GitConfigHelper {
|
||||
return configValue;
|
||||
});
|
||||
}
|
||||
gitConfigStringReplace(find, replace) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
if (this.gitConfigPath.length === 0) {
|
||||
const gitDir = yield this.git.getGitDirectory();
|
||||
this.gitConfigPath = path.join(this.workingDirectory, gitDir, 'config');
|
||||
}
|
||||
let content = (yield fs.promises.readFile(this.gitConfigPath)).toString();
|
||||
const index = content.indexOf(find);
|
||||
if (index < 0 || index != content.lastIndexOf(find)) {
|
||||
throw new Error(`Unable to replace '${find}' in ${this.gitConfigPath}`);
|
||||
}
|
||||
content = content.replace(find, replace);
|
||||
yield fs.promises.writeFile(this.gitConfigPath, content);
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.GitConfigHelper = GitConfigHelper;
|
||||
|
||||
@@ -1511,6 +1375,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.GitHubHelper = void 0;
|
||||
const core = __importStar(__nccwpck_require__(7484));
|
||||
const request_error_1 = __nccwpck_require__(1015);
|
||||
const octokit_client_1 = __nccwpck_require__(3489);
|
||||
const p_limit_1 = __importDefault(__nccwpck_require__(7989));
|
||||
const utils = __importStar(__nccwpck_require__(9277));
|
||||
@@ -1637,20 +1502,28 @@ class GitHubHelper {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Create or update the pull request
|
||||
const pull = yield this.createOrUpdate(inputs, baseRepository, headRepository);
|
||||
// After creating a new PR, follow-up API calls can fail with a 422
|
||||
// "Could not resolve to a node" error due to GitHub API eventual
|
||||
// consistency. Wrap post-creation calls with targeted retry logic.
|
||||
// See: https://github.com/peter-evans/create-pull-request/issues/4321
|
||||
const isEventualConsistencyError = (e) => e instanceof request_error_1.RequestError &&
|
||||
e.status === 422 &&
|
||||
e.message.includes('Could not resolve to a node');
|
||||
const withRetryForNewPr = (fn) => pull.created ? utils.retryWithBackoff(fn, isEventualConsistencyError) : fn();
|
||||
// Apply milestone
|
||||
if (inputs.milestone) {
|
||||
core.info(`Applying milestone '${inputs.milestone}'`);
|
||||
yield this.octokit.rest.issues.update(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { issue_number: pull.number, milestone: inputs.milestone }));
|
||||
yield withRetryForNewPr(() => this.octokit.rest.issues.update(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { issue_number: pull.number, milestone: inputs.milestone })));
|
||||
}
|
||||
// Apply labels
|
||||
if (inputs.labels.length > 0) {
|
||||
core.info(`Applying labels '${inputs.labels}'`);
|
||||
yield this.octokit.rest.issues.addLabels(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { issue_number: pull.number, labels: inputs.labels }));
|
||||
yield withRetryForNewPr(() => this.octokit.rest.issues.addLabels(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { issue_number: pull.number, labels: inputs.labels })));
|
||||
}
|
||||
// Apply assignees
|
||||
if (inputs.assignees.length > 0) {
|
||||
core.info(`Applying assignees '${inputs.assignees}'`);
|
||||
yield this.octokit.rest.issues.addAssignees(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { issue_number: pull.number, assignees: inputs.assignees }));
|
||||
yield withRetryForNewPr(() => this.octokit.rest.issues.addAssignees(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { issue_number: pull.number, assignees: inputs.assignees })));
|
||||
}
|
||||
// Request reviewers and team reviewers
|
||||
const requestReviewersParams = {};
|
||||
@@ -1665,7 +1538,7 @@ class GitHubHelper {
|
||||
}
|
||||
if (Object.keys(requestReviewersParams).length > 0) {
|
||||
try {
|
||||
yield this.octokit.rest.pulls.requestReviewers(Object.assign(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { pull_number: pull.number }), requestReviewersParams));
|
||||
yield withRetryForNewPr(() => this.octokit.rest.pulls.requestReviewers(Object.assign(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { pull_number: pull.number }), requestReviewersParams)));
|
||||
}
|
||||
catch (e) {
|
||||
if (utils.getErrorMessage(e).includes(ERROR_PR_REVIEW_TOKEN_SCOPE)) {
|
||||
@@ -2036,6 +1909,15 @@ var __importStar = (this && this.__importStar) || (function () {
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.isSelfHosted = void 0;
|
||||
exports.getInputAsArray = getInputAsArray;
|
||||
@@ -2049,6 +1931,7 @@ exports.parseDisplayNameEmail = parseDisplayNameEmail;
|
||||
exports.fileExistsSync = fileExistsSync;
|
||||
exports.readFile = readFile;
|
||||
exports.getErrorMessage = getErrorMessage;
|
||||
exports.retryWithBackoff = retryWithBackoff;
|
||||
const core = __importStar(__nccwpck_require__(7484));
|
||||
const fs = __importStar(__nccwpck_require__(9896));
|
||||
const path = __importStar(__nccwpck_require__(6928));
|
||||
@@ -2150,6 +2033,26 @@ const isSelfHosted = () => process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted'
|
||||
(process.env['AGENT_ISSELFHOSTED'] === '1' ||
|
||||
process.env['AGENT_ISSELFHOSTED'] === undefined);
|
||||
exports.isSelfHosted = isSelfHosted;
|
||||
function retryWithBackoff(fn_1, shouldRetry_1) {
|
||||
return __awaiter(this, arguments, void 0, function* (fn, shouldRetry, maxRetries = 2, delayMs = 1000) {
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
return yield fn();
|
||||
}
|
||||
catch (e) {
|
||||
if (attempt < maxRetries && shouldRetry(e)) {
|
||||
const delay = delayMs * Math.pow(2, attempt);
|
||||
core.info(`Request failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms...`);
|
||||
yield new Promise(resolve => setTimeout(resolve, delay));
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error('Unexpected: retry loop exited without return or throw');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
@@ -32961,7 +32864,7 @@ async function fetchWrapper(requestOptions) {
|
||||
}
|
||||
}
|
||||
}
|
||||
const requestError = new dist_src/* RequestError */.G(message, 500, {
|
||||
const requestError = new dist_src.RequestError(message, 500, {
|
||||
request: requestOptions
|
||||
});
|
||||
requestError.cause = error;
|
||||
@@ -32993,21 +32896,21 @@ async function fetchWrapper(requestOptions) {
|
||||
if (status < 400) {
|
||||
return octokitResponse;
|
||||
}
|
||||
throw new dist_src/* RequestError */.G(fetchResponse.statusText, status, {
|
||||
throw new dist_src.RequestError(fetchResponse.statusText, status, {
|
||||
response: octokitResponse,
|
||||
request: requestOptions
|
||||
});
|
||||
}
|
||||
if (status === 304) {
|
||||
octokitResponse.data = await getResponseData(fetchResponse);
|
||||
throw new dist_src/* RequestError */.G("Not modified", status, {
|
||||
throw new dist_src.RequestError("Not modified", status, {
|
||||
response: octokitResponse,
|
||||
request: requestOptions
|
||||
});
|
||||
}
|
||||
if (status >= 400) {
|
||||
octokitResponse.data = await getResponseData(fetchResponse);
|
||||
throw new dist_src/* RequestError */.G(toErrorMessage(octokitResponse.data), status, {
|
||||
throw new dist_src.RequestError(toErrorMessage(octokitResponse.data), status, {
|
||||
response: octokitResponse,
|
||||
request: requestOptions
|
||||
});
|
||||
@@ -36356,7 +36259,7 @@ async function requestWithGraphqlErrorHandling(state, octokit, request, options)
|
||||
if (response.data && response.data.errors && response.data.errors.length > 0 && /Something went wrong while executing your query/.test(
|
||||
response.data.errors[0].message
|
||||
)) {
|
||||
const error = new _octokit_request_error__WEBPACK_IMPORTED_MODULE_1__/* .RequestError */ .G(response.data.errors[0].message, 500, {
|
||||
const error = new _octokit_request_error__WEBPACK_IMPORTED_MODULE_1__.RequestError(response.data.errors[0].message, 500, {
|
||||
request: options,
|
||||
response
|
||||
});
|
||||
@@ -36641,8 +36544,9 @@ throttling.triggersNotification = triggersNotification;
|
||||
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __nccwpck_require__) => {
|
||||
|
||||
"use strict";
|
||||
__nccwpck_require__.r(__webpack_exports__);
|
||||
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
|
||||
/* harmony export */ G: () => (/* binding */ RequestError)
|
||||
/* harmony export */ RequestError: () => (/* binding */ RequestError)
|
||||
/* harmony export */ });
|
||||
class RequestError extends Error {
|
||||
name;
|
||||
|
||||
Generated
+832
-390
File diff suppressed because it is too large
Load Diff
+6
-5
@@ -39,6 +39,7 @@
|
||||
"@octokit/plugin-rest-endpoint-methods": "^13.5.0",
|
||||
"@octokit/plugin-retry": "^7.2.1",
|
||||
"@octokit/plugin-throttling": "^9.6.1",
|
||||
"@octokit/request-error": "^6.1.8",
|
||||
"node-fetch-native": "^1.6.7",
|
||||
"p-limit": "^6.2.0",
|
||||
"uuid": "^9.0.1"
|
||||
@@ -54,14 +55,14 @@
|
||||
"eslint-plugin-github": "^4.10.2",
|
||||
"eslint-plugin-import": "^2.32.0",
|
||||
"eslint-plugin-jest": "^27.9.0",
|
||||
"eslint-plugin-prettier": "^5.5.4",
|
||||
"eslint-plugin-prettier": "^5.5.5",
|
||||
"jest": "^29.7.0",
|
||||
"jest-circus": "^29.7.0",
|
||||
"jest-environment-jsdom": "^29.7.0",
|
||||
"jest-environment-jsdom": "^30.3.0",
|
||||
"js-yaml": "^4.1.1",
|
||||
"prettier": "^3.7.4",
|
||||
"ts-jest": "^29.4.6",
|
||||
"prettier": "^3.8.1",
|
||||
"ts-jest": "^29.4.9",
|
||||
"typescript": "^5.9.3",
|
||||
"undici": "^6.23.0"
|
||||
"undici": "^6.24.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,15 +96,9 @@ export class GitCommandManager {
|
||||
configKey: string,
|
||||
configValue: string,
|
||||
globalConfig?: boolean,
|
||||
add?: boolean,
|
||||
configFile?: string
|
||||
add?: boolean
|
||||
): Promise<void> {
|
||||
const args: string[] = ['config']
|
||||
if (configFile) {
|
||||
args.push('--file', configFile)
|
||||
} else {
|
||||
args.push(globalConfig ? '--global' : '--local')
|
||||
}
|
||||
const args: string[] = ['config', globalConfig ? '--global' : '--local']
|
||||
if (add) {
|
||||
args.push('--add')
|
||||
}
|
||||
@@ -356,67 +350,6 @@ export class GitCommandManager {
|
||||
return output.exitCode === 0
|
||||
}
|
||||
|
||||
async tryConfigUnsetValue(
|
||||
configKey: string,
|
||||
configValue: string,
|
||||
globalConfig?: boolean,
|
||||
configFile?: string
|
||||
): Promise<boolean> {
|
||||
const args = ['config']
|
||||
if (configFile) {
|
||||
args.push('--file', configFile)
|
||||
} else {
|
||||
args.push(globalConfig ? '--global' : '--local')
|
||||
}
|
||||
args.push('--fixed-value', '--unset', configKey, configValue)
|
||||
const output = await this.exec(args, {allowAllExitCodes: true})
|
||||
return output.exitCode === 0
|
||||
}
|
||||
|
||||
async tryGetConfigValues(
|
||||
configKey: string,
|
||||
globalConfig?: boolean,
|
||||
configFile?: string
|
||||
): Promise<string[]> {
|
||||
const args = ['config']
|
||||
if (configFile) {
|
||||
args.push('--file', configFile)
|
||||
} else {
|
||||
args.push(globalConfig ? '--global' : '--local')
|
||||
}
|
||||
args.push('--get-all', configKey)
|
||||
const output = await this.exec(args, {allowAllExitCodes: true})
|
||||
if (output.exitCode !== 0) {
|
||||
return []
|
||||
}
|
||||
return output.stdout
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(value => value.trim())
|
||||
}
|
||||
|
||||
async tryGetConfigKeys(
|
||||
pattern: string,
|
||||
globalConfig?: boolean,
|
||||
configFile?: string
|
||||
): Promise<string[]> {
|
||||
const args = ['config']
|
||||
if (configFile) {
|
||||
args.push('--file', configFile)
|
||||
} else {
|
||||
args.push(globalConfig ? '--global' : '--local')
|
||||
}
|
||||
args.push('--name-only', '--get-regexp', pattern)
|
||||
const output = await this.exec(args, {allowAllExitCodes: true})
|
||||
if (output.exitCode !== 0) {
|
||||
return []
|
||||
}
|
||||
return output.stdout
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(key => key.trim())
|
||||
}
|
||||
|
||||
async tryGetRemoteUrl(): Promise<string> {
|
||||
const output = await this.exec(
|
||||
['config', '--local', '--get', 'remote.origin.url'],
|
||||
|
||||
+74
-194
@@ -4,7 +4,6 @@ import {GitCommandManager} from './git-command-manager'
|
||||
import * as path from 'path'
|
||||
import {URL} from 'url'
|
||||
import * as utils from './utils'
|
||||
import {v4 as uuid} from 'uuid'
|
||||
|
||||
interface GitRemote {
|
||||
hostname: string
|
||||
@@ -14,6 +13,7 @@ interface GitRemote {
|
||||
|
||||
export class GitConfigHelper {
|
||||
private git: GitCommandManager
|
||||
private gitConfigPath = ''
|
||||
private workingDirectory: string
|
||||
private safeDirectoryConfigKey = 'safe.directory'
|
||||
private safeDirectoryAdded = false
|
||||
@@ -22,8 +22,7 @@ export class GitConfigHelper {
|
||||
private extraheaderConfigPlaceholderValue = 'AUTHORIZATION: basic ***'
|
||||
private extraheaderConfigValueRegex = '^AUTHORIZATION:'
|
||||
private persistedExtraheaderConfigValue = ''
|
||||
// Path to the credentials config file in RUNNER_TEMP (new v6-style auth)
|
||||
private credentialsConfigPath = ''
|
||||
private backedUpCredentialFiles: string[] = []
|
||||
|
||||
private constructor(git: GitCommandManager) {
|
||||
this.git = git
|
||||
@@ -123,14 +122,15 @@ export class GitConfigHelper {
|
||||
async savePersistedAuth(): Promise<void> {
|
||||
const serverUrl = new URL(`https://${this.getGitRemote().hostname}`)
|
||||
this.extraheaderConfigKey = `http.${serverUrl.origin}/.extraheader`
|
||||
// Save and unset persisted extraheader credential in git config if it exists (old-style auth)
|
||||
// Note: checkout@v6 uses credentials files with includeIf, so we don't need to
|
||||
// manipulate those - they work independently via git's include mechanism
|
||||
// Backup checkout@v6 credential files if they exist
|
||||
await this.hideCredentialFiles()
|
||||
// Save and unset persisted extraheader credential in git config if it exists
|
||||
this.persistedExtraheaderConfigValue = await this.getAndUnset()
|
||||
}
|
||||
|
||||
async restorePersistedAuth(): Promise<void> {
|
||||
// Restore old-style extraheader config if it was persisted
|
||||
// Restore checkout@v6 credential files if they were backed up
|
||||
await this.unhideCredentialFiles()
|
||||
if (this.persistedExtraheaderConfigValue) {
|
||||
try {
|
||||
await this.setExtraheaderConfig(this.persistedExtraheaderConfigValue)
|
||||
@@ -142,218 +142,81 @@ export class GitConfigHelper {
|
||||
}
|
||||
|
||||
async configureToken(token: string): Promise<void> {
|
||||
// Encode the basic credential for HTTPS access
|
||||
// Encode and configure the basic credential for HTTPS access
|
||||
const basicCredential = Buffer.from(
|
||||
`x-access-token:${token}`,
|
||||
'utf8'
|
||||
).toString('base64')
|
||||
core.setSecret(basicCredential)
|
||||
const extraheaderConfigValue = `AUTHORIZATION: basic ${basicCredential}`
|
||||
|
||||
// Get or create the credentials config file path
|
||||
const credentialsConfigPath = this.getCredentialsConfigPath()
|
||||
|
||||
// Write placeholder to the separate credentials config file using git config.
|
||||
// This approach avoids the credential being captured by process creation audit events,
|
||||
// which are commonly logged. For more information, refer to
|
||||
// https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/manage/component-updates/command-line-process-auditing
|
||||
await this.git.config(
|
||||
this.extraheaderConfigKey,
|
||||
this.extraheaderConfigPlaceholderValue,
|
||||
false, // globalConfig
|
||||
false, // add
|
||||
credentialsConfigPath
|
||||
)
|
||||
|
||||
// Replace the placeholder in the credentials config file
|
||||
let content = (await fs.promises.readFile(credentialsConfigPath)).toString()
|
||||
const placeholderIndex = content.indexOf(
|
||||
this.extraheaderConfigPlaceholderValue
|
||||
)
|
||||
if (
|
||||
placeholderIndex < 0 ||
|
||||
placeholderIndex !=
|
||||
content.lastIndexOf(this.extraheaderConfigPlaceholderValue)
|
||||
) {
|
||||
throw new Error(
|
||||
`Unable to replace auth placeholder in ${credentialsConfigPath}`
|
||||
)
|
||||
}
|
||||
content = content.replace(
|
||||
this.extraheaderConfigPlaceholderValue,
|
||||
extraheaderConfigValue
|
||||
)
|
||||
await fs.promises.writeFile(credentialsConfigPath, content)
|
||||
|
||||
// Configure includeIf entries to reference the credentials config file
|
||||
await this.configureIncludeIf(credentialsConfigPath)
|
||||
await this.setExtraheaderConfig(extraheaderConfigValue)
|
||||
}
|
||||
|
||||
async removeAuth(): Promise<void> {
|
||||
// Remove old-style extraheader config if it exists
|
||||
await this.getAndUnset()
|
||||
|
||||
// Remove includeIf entries that point to git-credentials-*.config files
|
||||
// and clean up the credentials config files
|
||||
await this.removeIncludeIfCredentials()
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets or creates the path to the credentials config file in RUNNER_TEMP.
|
||||
* @returns The absolute path to the credentials config file
|
||||
*/
|
||||
private getCredentialsConfigPath(): string {
|
||||
if (this.credentialsConfigPath) {
|
||||
return this.credentialsConfigPath
|
||||
}
|
||||
|
||||
const runnerTemp = process.env['RUNNER_TEMP'] || ''
|
||||
if (!runnerTemp) {
|
||||
throw new Error('RUNNER_TEMP is not defined')
|
||||
}
|
||||
|
||||
// Create a unique filename for this action instance
|
||||
const configFileName = `git-credentials-${uuid()}.config`
|
||||
this.credentialsConfigPath = path.join(runnerTemp, configFileName)
|
||||
|
||||
core.debug(`Credentials config path: ${this.credentialsConfigPath}`)
|
||||
return this.credentialsConfigPath
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures includeIf entries in the local git config to reference the credentials file.
|
||||
* Sets up entries for both host and container paths to support Docker container actions.
|
||||
*/
|
||||
private async configureIncludeIf(
|
||||
credentialsConfigPath: string
|
||||
): Promise<void> {
|
||||
// Host git directory
|
||||
const gitDir = await this.git.getGitDirectory()
|
||||
let hostGitDir = path.join(this.workingDirectory, gitDir)
|
||||
hostGitDir = hostGitDir.replace(/\\/g, '/') // Use forward slashes, even on Windows
|
||||
|
||||
// Configure host includeIf
|
||||
const hostIncludeKey = `includeIf.gitdir:${hostGitDir}.path`
|
||||
await this.git.config(hostIncludeKey, credentialsConfigPath)
|
||||
|
||||
// Configure host includeIf for worktrees
|
||||
const hostWorktreeIncludeKey = `includeIf.gitdir:${hostGitDir}/worktrees/*.path`
|
||||
await this.git.config(hostWorktreeIncludeKey, credentialsConfigPath)
|
||||
|
||||
// Container paths for Docker container actions
|
||||
const githubWorkspace = process.env['GITHUB_WORKSPACE']
|
||||
if (githubWorkspace) {
|
||||
let relativePath = path.relative(githubWorkspace, this.workingDirectory)
|
||||
relativePath = relativePath.replace(/\\/g, '/') // Use forward slashes, even on Windows
|
||||
const containerGitDir = path.posix.join(
|
||||
'/github/workspace',
|
||||
relativePath,
|
||||
'.git'
|
||||
)
|
||||
|
||||
// Container credentials config path
|
||||
const containerCredentialsPath = path.posix.join(
|
||||
'/github/runner_temp',
|
||||
path.basename(credentialsConfigPath)
|
||||
)
|
||||
|
||||
// Configure container includeIf
|
||||
const containerIncludeKey = `includeIf.gitdir:${containerGitDir}.path`
|
||||
await this.git.config(containerIncludeKey, containerCredentialsPath)
|
||||
|
||||
// Configure container includeIf for worktrees
|
||||
const containerWorktreeIncludeKey = `includeIf.gitdir:${containerGitDir}/worktrees/*.path`
|
||||
await this.git.config(
|
||||
containerWorktreeIncludeKey,
|
||||
containerCredentialsPath
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the includeIf entry and credentials config file created by this action instance.
|
||||
* Only cleans up the specific credentials file tracked in this.credentialsConfigPath,
|
||||
* leaving credentials created by other actions (e.g., actions/checkout) intact.
|
||||
*/
|
||||
private async removeIncludeIfCredentials(): Promise<void> {
|
||||
// Only clean up if this action instance created a credentials config file
|
||||
if (!this.credentialsConfigPath) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// Get all includeIf.gitdir keys from local config
|
||||
const keys = await this.git.tryGetConfigKeys('^includeIf\\.gitdir:')
|
||||
|
||||
for (const key of keys) {
|
||||
// Get all values for this key
|
||||
const values = await this.git.tryGetConfigValues(key)
|
||||
for (const value of values) {
|
||||
// Only remove entries pointing to our specific credentials file
|
||||
if (value === this.credentialsConfigPath) {
|
||||
await this.git.tryConfigUnsetValue(key, value)
|
||||
core.debug(`Removed includeIf entry: ${key} = ${value}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore errors during cleanup
|
||||
core.debug(`Error during includeIf cleanup: ${utils.getErrorMessage(e)}`)
|
||||
}
|
||||
|
||||
// Delete only our credentials config file
|
||||
const runnerTemp = process.env['RUNNER_TEMP']
|
||||
const resolvedCredentialsPath = path.resolve(this.credentialsConfigPath)
|
||||
const resolvedRunnerTemp = runnerTemp ? path.resolve(runnerTemp) : ''
|
||||
if (
|
||||
resolvedRunnerTemp &&
|
||||
resolvedCredentialsPath.startsWith(resolvedRunnerTemp + path.sep)
|
||||
) {
|
||||
try {
|
||||
await fs.promises.unlink(this.credentialsConfigPath)
|
||||
core.info(
|
||||
`Removed credentials config file: ${this.credentialsConfigPath}`
|
||||
)
|
||||
} catch (e) {
|
||||
core.debug(
|
||||
`Could not remove credentials file ${this.credentialsConfigPath}: ${utils.getErrorMessage(e)}`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets extraheader config directly in .git/config (old-style auth).
|
||||
* Used only for restoring persisted credentials from checkout@v4/v5.
|
||||
*/
|
||||
private async setExtraheaderConfig(
|
||||
extraheaderConfigValue: string
|
||||
): Promise<void> {
|
||||
// Configure a placeholder value. This approach avoids the credential being captured
|
||||
// by process creation audit events, which are commonly logged. For more information,
|
||||
// refer to https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/manage/component-updates/command-line-process-auditing
|
||||
// See https://github.com/actions/checkout/blob/main/src/git-auth-helper.ts#L267-L274
|
||||
await this.git.config(
|
||||
this.extraheaderConfigKey,
|
||||
this.extraheaderConfigPlaceholderValue
|
||||
)
|
||||
// Replace the placeholder in the local git config
|
||||
const gitDir = await this.git.getGitDirectory()
|
||||
const gitConfigPath = path.join(this.workingDirectory, gitDir, 'config')
|
||||
let content = (await fs.promises.readFile(gitConfigPath)).toString()
|
||||
const index = content.indexOf(this.extraheaderConfigPlaceholderValue)
|
||||
if (
|
||||
index < 0 ||
|
||||
index != content.lastIndexOf(this.extraheaderConfigPlaceholderValue)
|
||||
) {
|
||||
throw new Error(
|
||||
`Unable to replace '${this.extraheaderConfigPlaceholderValue}' in ${gitConfigPath}`
|
||||
)
|
||||
}
|
||||
content = content.replace(
|
||||
// Replace the placeholder
|
||||
await this.gitConfigStringReplace(
|
||||
this.extraheaderConfigPlaceholderValue,
|
||||
extraheaderConfigValue
|
||||
)
|
||||
await fs.promises.writeFile(gitConfigPath, content)
|
||||
}
|
||||
|
||||
private async hideCredentialFiles(): Promise<void> {
|
||||
// Temporarily hide checkout@v6 credential files to avoid duplicate auth headers
|
||||
const runnerTemp = process.env['RUNNER_TEMP']
|
||||
if (!runnerTemp) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const files = await fs.promises.readdir(runnerTemp)
|
||||
for (const file of files) {
|
||||
if (file.startsWith('git-credentials-') && file.endsWith('.config')) {
|
||||
const sourcePath = path.join(runnerTemp, file)
|
||||
const backupPath = `${sourcePath}.bak`
|
||||
await fs.promises.rename(sourcePath, backupPath)
|
||||
this.backedUpCredentialFiles.push(backupPath)
|
||||
core.info(
|
||||
`Temporarily hiding checkout credential file: ${file} (will be restored after)`
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// If directory doesn't exist or we can't read it, just continue
|
||||
core.debug(
|
||||
`Could not backup credential files: ${utils.getErrorMessage(e)}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private async unhideCredentialFiles(): Promise<void> {
|
||||
// Restore checkout@v6 credential files that were backed up
|
||||
for (const backupPath of this.backedUpCredentialFiles) {
|
||||
try {
|
||||
const originalPath = backupPath.replace(/\.bak$/, '')
|
||||
await fs.promises.rename(backupPath, originalPath)
|
||||
const fileName = path.basename(originalPath)
|
||||
core.info(`Restored checkout credential file: ${fileName}`)
|
||||
} catch (e) {
|
||||
core.warning(
|
||||
`Failed to restore credential file ${backupPath}: ${utils.getErrorMessage(e)}`
|
||||
)
|
||||
}
|
||||
}
|
||||
this.backedUpCredentialFiles = []
|
||||
}
|
||||
|
||||
private async getAndUnset(): Promise<string> {
|
||||
@@ -384,4 +247,21 @@ export class GitConfigHelper {
|
||||
}
|
||||
return configValue
|
||||
}
|
||||
|
||||
private async gitConfigStringReplace(
|
||||
find: string,
|
||||
replace: string
|
||||
): Promise<void> {
|
||||
if (this.gitConfigPath.length === 0) {
|
||||
const gitDir = await this.git.getGitDirectory()
|
||||
this.gitConfigPath = path.join(this.workingDirectory, gitDir, 'config')
|
||||
}
|
||||
let content = (await fs.promises.readFile(this.gitConfigPath)).toString()
|
||||
const index = content.indexOf(find)
|
||||
if (index < 0 || index != content.lastIndexOf(find)) {
|
||||
throw new Error(`Unable to replace '${find}' in ${this.gitConfigPath}`)
|
||||
}
|
||||
content = content.replace(find, replace)
|
||||
await fs.promises.writeFile(this.gitConfigPath, content)
|
||||
}
|
||||
}
|
||||
|
||||
+42
-20
@@ -1,4 +1,5 @@
|
||||
import * as core from '@actions/core'
|
||||
import {RequestError} from '@octokit/request-error'
|
||||
import {Inputs} from './create-pull-request'
|
||||
import {Commit, GitCommandManager} from './git-command-manager'
|
||||
import {
|
||||
@@ -209,32 +210,51 @@ export class GitHubHelper {
|
||||
headRepository
|
||||
)
|
||||
|
||||
// After creating a new PR, follow-up API calls can fail with a 422
|
||||
// "Could not resolve to a node" error due to GitHub API eventual
|
||||
// consistency. Wrap post-creation calls with targeted retry logic.
|
||||
// See: https://github.com/peter-evans/create-pull-request/issues/4321
|
||||
const isEventualConsistencyError = (e: unknown): boolean =>
|
||||
e instanceof RequestError &&
|
||||
e.status === 422 &&
|
||||
e.message.includes('Could not resolve to a node')
|
||||
const withRetryForNewPr = <T>(fn: () => Promise<T>): Promise<T> =>
|
||||
pull.created
|
||||
? utils.retryWithBackoff(fn, isEventualConsistencyError)
|
||||
: fn()
|
||||
|
||||
// Apply milestone
|
||||
if (inputs.milestone) {
|
||||
core.info(`Applying milestone '${inputs.milestone}'`)
|
||||
await this.octokit.rest.issues.update({
|
||||
...this.parseRepository(baseRepository),
|
||||
issue_number: pull.number,
|
||||
milestone: inputs.milestone
|
||||
})
|
||||
await withRetryForNewPr(() =>
|
||||
this.octokit.rest.issues.update({
|
||||
...this.parseRepository(baseRepository),
|
||||
issue_number: pull.number,
|
||||
milestone: inputs.milestone
|
||||
})
|
||||
)
|
||||
}
|
||||
// Apply labels
|
||||
if (inputs.labels.length > 0) {
|
||||
core.info(`Applying labels '${inputs.labels}'`)
|
||||
await this.octokit.rest.issues.addLabels({
|
||||
...this.parseRepository(baseRepository),
|
||||
issue_number: pull.number,
|
||||
labels: inputs.labels
|
||||
})
|
||||
await withRetryForNewPr(() =>
|
||||
this.octokit.rest.issues.addLabels({
|
||||
...this.parseRepository(baseRepository),
|
||||
issue_number: pull.number,
|
||||
labels: inputs.labels
|
||||
})
|
||||
)
|
||||
}
|
||||
// Apply assignees
|
||||
if (inputs.assignees.length > 0) {
|
||||
core.info(`Applying assignees '${inputs.assignees}'`)
|
||||
await this.octokit.rest.issues.addAssignees({
|
||||
...this.parseRepository(baseRepository),
|
||||
issue_number: pull.number,
|
||||
assignees: inputs.assignees
|
||||
})
|
||||
await withRetryForNewPr(() =>
|
||||
this.octokit.rest.issues.addAssignees({
|
||||
...this.parseRepository(baseRepository),
|
||||
issue_number: pull.number,
|
||||
assignees: inputs.assignees
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
// Request reviewers and team reviewers
|
||||
@@ -250,11 +270,13 @@ export class GitHubHelper {
|
||||
}
|
||||
if (Object.keys(requestReviewersParams).length > 0) {
|
||||
try {
|
||||
await this.octokit.rest.pulls.requestReviewers({
|
||||
...this.parseRepository(baseRepository),
|
||||
pull_number: pull.number,
|
||||
...requestReviewersParams
|
||||
})
|
||||
await withRetryForNewPr(() =>
|
||||
this.octokit.rest.pulls.requestReviewers({
|
||||
...this.parseRepository(baseRepository),
|
||||
pull_number: pull.number,
|
||||
...requestReviewersParams
|
||||
})
|
||||
)
|
||||
} catch (e) {
|
||||
if (utils.getErrorMessage(e).includes(ERROR_PR_REVIEW_TOKEN_SCOPE)) {
|
||||
core.error(
|
||||
|
||||
@@ -140,3 +140,27 @@ export const isSelfHosted = (): boolean =>
|
||||
process.env['RUNNER_ENVIRONMENT'] !== 'github-hosted' &&
|
||||
(process.env['AGENT_ISSELFHOSTED'] === '1' ||
|
||||
process.env['AGENT_ISSELFHOSTED'] === undefined)
|
||||
|
||||
export async function retryWithBackoff<T>(
|
||||
fn: () => Promise<T>,
|
||||
shouldRetry: (error: unknown) => boolean,
|
||||
maxRetries = 2,
|
||||
delayMs = 1000
|
||||
): Promise<T> {
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (e) {
|
||||
if (attempt < maxRetries && shouldRetry(e)) {
|
||||
const delay = delayMs * Math.pow(2, attempt)
|
||||
core.info(
|
||||
`Request failed (attempt ${attempt + 1}/${maxRetries + 1}), retrying in ${delay}ms...`
|
||||
)
|
||||
await new Promise(resolve => setTimeout(resolve, delay))
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error('Unexpected: retry loop exited without return or throw')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user