0
1
Fork 0

set insteadOf url for org-id (#621)

This commit is contained in:
eric sciple 2021-11-01 11:43:18 -05:00 committed by GitHub
parent fd47087372
commit ec3a7ce113
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 293 additions and 131 deletions

View File

@ -518,12 +518,17 @@ describe('git-auth-helper tests', () => {
await authHelper.configureSubmoduleAuth() await authHelper.configureSubmoduleAuth()
// Assert // Assert
expect(mockSubmoduleForeach).toHaveBeenCalledTimes(3) expect(mockSubmoduleForeach).toHaveBeenCalledTimes(4)
expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch( expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
/unset-all.*insteadOf/ /unset-all.*insteadOf/
) )
expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(/http.*extraheader/) expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(/http.*extraheader/)
expect(mockSubmoduleForeach.mock.calls[2][0]).toMatch(/url.*insteadOf/) expect(mockSubmoduleForeach.mock.calls[2][0]).toMatch(
/url.*insteadOf.*git@github.com:/
)
expect(mockSubmoduleForeach.mock.calls[3][0]).toMatch(
/url.*insteadOf.*org-123456@github.com:/
)
} }
) )
@ -770,7 +775,8 @@ async function setup(testName: string): Promise<void> {
repositoryPath: '', repositoryPath: '',
sshKey: sshPath ? 'some ssh private key' : '', sshKey: sshPath ? 'some ssh private key' : '',
sshKnownHosts: '', sshKnownHosts: '',
sshStrict: true sshStrict: true,
workflowOrganizationId: 123456
} }
} }

View File

@ -1,9 +1,9 @@
import * as assert from 'assert'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as fsHelper from '../lib/fs-helper' import * as fsHelper from '../lib/fs-helper'
import * as github from '@actions/github' import * as github from '@actions/github'
import * as inputHelper from '../lib/input-helper' import * as inputHelper from '../lib/input-helper'
import * as path from 'path' import * as path from 'path'
import * as workflowContextHelper from '../lib/workflow-context-helper'
import {IGitSourceSettings} from '../lib/git-source-settings' import {IGitSourceSettings} from '../lib/git-source-settings'
const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE'] const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
@ -43,6 +43,11 @@ describe('input-helper tests', () => {
.spyOn(fsHelper, 'directoryExistsSync') .spyOn(fsHelper, 'directoryExistsSync')
.mockImplementation((path: string) => path == gitHubWorkspace) .mockImplementation((path: string) => path == gitHubWorkspace)
// Mock ./workflowContextHelper getOrganizationId()
jest
.spyOn(workflowContextHelper, 'getOrganizationId')
.mockImplementation(() => Promise.resolve(123456))
// GitHub workspace // GitHub workspace
process.env['GITHUB_WORKSPACE'] = gitHubWorkspace process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
}) })
@ -67,8 +72,8 @@ describe('input-helper tests', () => {
jest.restoreAllMocks() jest.restoreAllMocks()
}) })
it('sets defaults', () => { it('sets defaults', async () => {
const settings: IGitSourceSettings = inputHelper.getInputs() const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings).toBeTruthy() expect(settings).toBeTruthy()
expect(settings.authToken).toBeFalsy() expect(settings.authToken).toBeFalsy()
expect(settings.clean).toBe(true) expect(settings.clean).toBe(true)
@ -82,11 +87,11 @@ describe('input-helper tests', () => {
expect(settings.repositoryPath).toBe(gitHubWorkspace) expect(settings.repositoryPath).toBe(gitHubWorkspace)
}) })
it('qualifies ref', () => { it('qualifies ref', async () => {
let originalRef = github.context.ref let originalRef = github.context.ref
try { try {
github.context.ref = 'some-unqualified-ref' github.context.ref = 'some-unqualified-ref'
const settings: IGitSourceSettings = inputHelper.getInputs() const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings).toBeTruthy() expect(settings).toBeTruthy()
expect(settings.commit).toBe('1234567890123456789012345678901234567890') expect(settings.commit).toBe('1234567890123456789012345678901234567890')
expect(settings.ref).toBe('refs/heads/some-unqualified-ref') expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
@ -95,32 +100,42 @@ describe('input-helper tests', () => {
} }
}) })
it('requires qualified repo', () => { it('requires qualified repo', async () => {
inputs.repository = 'some-unqualified-repo' inputs.repository = 'some-unqualified-repo'
assert.throws(() => { try {
inputHelper.getInputs() await inputHelper.getInputs()
}, /Invalid repository 'some-unqualified-repo'/) throw 'should not reach here'
} catch (err) {
expect(`(${(err as any).message}`).toMatch(
"Invalid repository 'some-unqualified-repo'"
)
}
}) })
it('roots path', () => { it('roots path', async () => {
inputs.path = 'some-directory/some-subdirectory' inputs.path = 'some-directory/some-subdirectory'
const settings: IGitSourceSettings = inputHelper.getInputs() const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.repositoryPath).toBe( expect(settings.repositoryPath).toBe(
path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory') path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory')
) )
}) })
it('sets ref to empty when explicit sha', () => { it('sets ref to empty when explicit sha', async () => {
inputs.ref = '1111111111222222222233333333334444444444' inputs.ref = '1111111111222222222233333333334444444444'
const settings: IGitSourceSettings = inputHelper.getInputs() const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.ref).toBeFalsy() expect(settings.ref).toBeFalsy()
expect(settings.commit).toBe('1111111111222222222233333333334444444444') expect(settings.commit).toBe('1111111111222222222233333333334444444444')
}) })
it('sets sha to empty when explicit ref', () => { it('sets sha to empty when explicit ref', async () => {
inputs.ref = 'refs/heads/some-other-ref' inputs.ref = 'refs/heads/some-other-ref'
const settings: IGitSourceSettings = inputHelper.getInputs() const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.ref).toBe('refs/heads/some-other-ref') expect(settings.ref).toBe('refs/heads/some-other-ref')
expect(settings.commit).toBeFalsy() expect(settings.commit).toBeFalsy()
}) })
it('sets workflow organization ID', async () => {
const settings: IGitSourceSettings = await inputHelper.getInputs()
expect(settings.workflowOrganizationId).toBe(123456)
})
}) })

281
dist/index.js vendored
View File

@ -4149,7 +4149,7 @@ function run() {
var _a, _b; var _a, _b;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const sourceSettings = inputHelper.getInputs(); const sourceSettings = yield inputHelper.getInputs();
try { try {
// Register problem matcher // Register problem matcher
coreCommand.issueCommand('add-matcher', {}, path.join(__dirname, 'problem-matcher.json')); coreCommand.issueCommand('add-matcher', {}, path.join(__dirname, 'problem-matcher.json'));
@ -6542,6 +6542,7 @@ function createAuthHelper(git, settings) {
exports.createAuthHelper = createAuthHelper; exports.createAuthHelper = createAuthHelper;
class GitAuthHelper { class GitAuthHelper {
constructor(gitCommandManager, gitSourceSettings) { constructor(gitCommandManager, gitSourceSettings) {
this.insteadOfValues = [];
this.sshCommand = ''; this.sshCommand = '';
this.sshKeyPath = ''; this.sshKeyPath = '';
this.sshKnownHostsPath = ''; this.sshKnownHostsPath = '';
@ -6557,7 +6558,10 @@ class GitAuthHelper {
this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}`; this.tokenConfigValue = `AUTHORIZATION: basic ${basicCredential}`;
// Instead of SSH URL // Instead of SSH URL
this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf`; // "origin" is SCHEME://HOSTNAME[:PORT] this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf`; // "origin" is SCHEME://HOSTNAME[:PORT]
this.insteadOfValue = `git@${serverUrl.hostname}:`; this.insteadOfValues.push(`git@${serverUrl.hostname}:`);
if (this.settings.workflowOrganizationId) {
this.insteadOfValues.push(`org-${this.settings.workflowOrganizationId}@github.com:`);
}
} }
configureAuth() { configureAuth() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@ -6606,7 +6610,9 @@ class GitAuthHelper {
// Configure HTTPS instead of SSH // Configure HTTPS instead of SSH
yield this.git.tryConfigUnset(this.insteadOfKey, true); yield this.git.tryConfigUnset(this.insteadOfKey, true);
if (!this.settings.sshKey) { if (!this.settings.sshKey) {
yield this.git.config(this.insteadOfKey, this.insteadOfValue, true); for (const insteadOfValue of this.insteadOfValues) {
yield this.git.config(this.insteadOfKey, insteadOfValue, true, true);
}
} }
} }
catch (err) { catch (err) {
@ -6638,7 +6644,9 @@ class GitAuthHelper {
} }
else { else {
// Configure HTTPS instead of SSH // Configure HTTPS instead of SSH
yield this.git.submoduleForeach(`git config --local '${this.insteadOfKey}' '${this.insteadOfValue}'`, this.settings.nestedSubmodules); for (const insteadOfValue of this.insteadOfValues) {
yield this.git.submoduleForeach(`git config --local --add '${this.insteadOfKey}' '${insteadOfValue}'`, this.settings.nestedSubmodules);
}
} }
} }
}); });
@ -6928,14 +6936,14 @@ class GitCommandManager {
yield this.execGit(args); yield this.execGit(args);
}); });
} }
config(configKey, configValue, globalConfig) { config(configKey, configValue, globalConfig, add) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield this.execGit([ const args = ['config', globalConfig ? '--global' : '--local'];
'config', if (add) {
globalConfig ? '--global' : '--local', args.push('--add');
configKey, }
configValue args.push(...[configKey, configValue]);
]); yield this.execGit(args);
}); });
} }
configExists(configKey, globalConfig) { configExists(configKey, globalConfig) {
@ -13522,6 +13530,75 @@ module.exports = function callBoundIntrinsic(name, allowMissing) {
module.exports = require("net"); module.exports = require("net");
/***/ }),
/***/ 642:
/***/ (function(__unusedmodule, exports, __webpack_require__) {
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
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.getOrganizationId = void 0;
const core = __importStar(__webpack_require__(470));
const fs = __importStar(__webpack_require__(747));
/**
* Gets the organization ID of the running workflow or undefined if the value cannot be loaded from the GITHUB_EVENT_PATH
*/
function getOrganizationId() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
try {
const eventPath = process.env.GITHUB_EVENT_PATH;
if (!eventPath) {
core.debug(`GITHUB_EVENT_PATH is not defined`);
return;
}
const content = yield fs.promises.readFile(eventPath, { encoding: 'utf8' });
const event = JSON.parse(content);
const id = (_b = (_a = event === null || event === void 0 ? void 0 : event.repository) === null || _a === void 0 ? void 0 : _a.owner) === null || _b === void 0 ? void 0 : _b.id;
if (typeof id !== 'number') {
core.debug('Repository owner ID not found within GITHUB event info');
return;
}
return id;
}
catch (err) {
core.debug(`Unable to load organization ID from GITHUB_EVENT_PATH: ${err
.message || err}`);
}
});
}
exports.getOrganizationId = getOrganizationId;
/***/ }), /***/ }),
/***/ 649: /***/ 649:
@ -17062,99 +17139,113 @@ var __importStar = (this && this.__importStar) || function (mod) {
__setModuleDefault(result, mod); __setModuleDefault(result, mod);
return result; 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 }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getInputs = void 0; exports.getInputs = void 0;
const core = __importStar(__webpack_require__(470)); const core = __importStar(__webpack_require__(470));
const fsHelper = __importStar(__webpack_require__(618)); const fsHelper = __importStar(__webpack_require__(618));
const github = __importStar(__webpack_require__(469)); const github = __importStar(__webpack_require__(469));
const path = __importStar(__webpack_require__(622)); const path = __importStar(__webpack_require__(622));
const workflowContextHelper = __importStar(__webpack_require__(642));
function getInputs() { function getInputs() {
const result = {}; return __awaiter(this, void 0, void 0, function* () {
// GitHub workspace const result = {};
let githubWorkspacePath = process.env['GITHUB_WORKSPACE']; // GitHub workspace
if (!githubWorkspacePath) { let githubWorkspacePath = process.env['GITHUB_WORKSPACE'];
throw new Error('GITHUB_WORKSPACE not defined'); if (!githubWorkspacePath) {
} throw new Error('GITHUB_WORKSPACE not defined');
githubWorkspacePath = path.resolve(githubWorkspacePath); }
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`); githubWorkspacePath = path.resolve(githubWorkspacePath);
fsHelper.directoryExistsSync(githubWorkspacePath, true); core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`);
// Qualified repository fsHelper.directoryExistsSync(githubWorkspacePath, true);
const qualifiedRepository = core.getInput('repository') || // Qualified repository
`${github.context.repo.owner}/${github.context.repo.repo}`; const qualifiedRepository = core.getInput('repository') ||
core.debug(`qualified repository = '${qualifiedRepository}'`); `${github.context.repo.owner}/${github.context.repo.repo}`;
const splitRepository = qualifiedRepository.split('/'); core.debug(`qualified repository = '${qualifiedRepository}'`);
if (splitRepository.length !== 2 || const splitRepository = qualifiedRepository.split('/');
!splitRepository[0] || if (splitRepository.length !== 2 ||
!splitRepository[1]) { !splitRepository[0] ||
throw new Error(`Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`); !splitRepository[1]) {
} throw new Error(`Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`);
result.repositoryOwner = splitRepository[0]; }
result.repositoryName = splitRepository[1]; result.repositoryOwner = splitRepository[0];
// Repository path result.repositoryName = splitRepository[1];
result.repositoryPath = core.getInput('path') || '.'; // Repository path
result.repositoryPath = path.resolve(githubWorkspacePath, result.repositoryPath); result.repositoryPath = core.getInput('path') || '.';
if (!(result.repositoryPath + path.sep).startsWith(githubWorkspacePath + path.sep)) { result.repositoryPath = path.resolve(githubWorkspacePath, result.repositoryPath);
throw new Error(`Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`); if (!(result.repositoryPath + path.sep).startsWith(githubWorkspacePath + path.sep)) {
} throw new Error(`Repository path '${result.repositoryPath}' is not under '${githubWorkspacePath}'`);
// Workflow repository? }
const isWorkflowRepository = qualifiedRepository.toUpperCase() === // Workflow repository?
`${github.context.repo.owner}/${github.context.repo.repo}`.toUpperCase(); const isWorkflowRepository = qualifiedRepository.toUpperCase() ===
// Source branch, source version `${github.context.repo.owner}/${github.context.repo.repo}`.toUpperCase();
result.ref = core.getInput('ref'); // Source branch, source version
if (!result.ref) { result.ref = core.getInput('ref');
if (isWorkflowRepository) { if (!result.ref) {
result.ref = github.context.ref; if (isWorkflowRepository) {
result.commit = github.context.sha; result.ref = github.context.ref;
// Some events have an unqualifed ref. For example when a PR is merged (pull_request closed event), result.commit = github.context.sha;
// the ref is unqualifed like "main" instead of "refs/heads/main". // Some events have an unqualifed ref. For example when a PR is merged (pull_request closed event),
if (result.commit && result.ref && !result.ref.startsWith('refs/')) { // the ref is unqualifed like "main" instead of "refs/heads/main".
result.ref = `refs/heads/${result.ref}`; if (result.commit && result.ref && !result.ref.startsWith('refs/')) {
result.ref = `refs/heads/${result.ref}`;
}
} }
} }
} // SHA?
// SHA? else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) { result.commit = result.ref;
result.commit = result.ref; result.ref = '';
result.ref = ''; }
} core.debug(`ref = '${result.ref}'`);
core.debug(`ref = '${result.ref}'`); core.debug(`commit = '${result.commit}'`);
core.debug(`commit = '${result.commit}'`); // Clean
// Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE';
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'; core.debug(`clean = ${result.clean}`);
core.debug(`clean = ${result.clean}`); // Fetch depth
// Fetch depth result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1'));
result.fetchDepth = Math.floor(Number(core.getInput('fetch-depth') || '1')); if (isNaN(result.fetchDepth) || result.fetchDepth < 0) {
if (isNaN(result.fetchDepth) || result.fetchDepth < 0) { result.fetchDepth = 0;
result.fetchDepth = 0; }
} core.debug(`fetch depth = ${result.fetchDepth}`);
core.debug(`fetch depth = ${result.fetchDepth}`); // LFS
// LFS result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE';
result.lfs = (core.getInput('lfs') || 'false').toUpperCase() === 'TRUE'; core.debug(`lfs = ${result.lfs}`);
core.debug(`lfs = ${result.lfs}`); // Submodules
// Submodules result.submodules = false;
result.submodules = false; result.nestedSubmodules = false;
result.nestedSubmodules = false; const submodulesString = (core.getInput('submodules') || '').toUpperCase();
const submodulesString = (core.getInput('submodules') || '').toUpperCase(); if (submodulesString == 'RECURSIVE') {
if (submodulesString == 'RECURSIVE') { result.submodules = true;
result.submodules = true; result.nestedSubmodules = true;
result.nestedSubmodules = true; }
} else if (submodulesString == 'TRUE') {
else if (submodulesString == 'TRUE') { result.submodules = true;
result.submodules = true; }
} core.debug(`submodules = ${result.submodules}`);
core.debug(`submodules = ${result.submodules}`); core.debug(`recursive submodules = ${result.nestedSubmodules}`);
core.debug(`recursive submodules = ${result.nestedSubmodules}`); // Auth token
// Auth token result.authToken = core.getInput('token', { required: true });
result.authToken = core.getInput('token', { required: true }); // SSH
// SSH result.sshKey = core.getInput('ssh-key');
result.sshKey = core.getInput('ssh-key'); result.sshKnownHosts = core.getInput('ssh-known-hosts');
result.sshKnownHosts = core.getInput('ssh-known-hosts'); result.sshStrict =
result.sshStrict = (core.getInput('ssh-strict') || 'true').toUpperCase() === 'TRUE';
(core.getInput('ssh-strict') || 'true').toUpperCase() === 'TRUE'; // Persist credentials
// Persist credentials result.persistCredentials =
result.persistCredentials = (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE';
(core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE'; // Workflow organization ID
return result; result.workflowOrganizationId = yield workflowContextHelper.getOrganizationId();
return result;
});
} }
exports.getInputs = getInputs; exports.getInputs = getInputs;

View File

@ -37,7 +37,7 @@ class GitAuthHelper {
private readonly tokenConfigValue: string private readonly tokenConfigValue: string
private readonly tokenPlaceholderConfigValue: string private readonly tokenPlaceholderConfigValue: string
private readonly insteadOfKey: string private readonly insteadOfKey: string
private readonly insteadOfValue: string private readonly insteadOfValues: string[] = []
private sshCommand = '' private sshCommand = ''
private sshKeyPath = '' private sshKeyPath = ''
private sshKnownHostsPath = '' private sshKnownHostsPath = ''
@ -45,7 +45,7 @@ class GitAuthHelper {
constructor( constructor(
gitCommandManager: IGitCommandManager, gitCommandManager: IGitCommandManager,
gitSourceSettings?: IGitSourceSettings gitSourceSettings: IGitSourceSettings | undefined
) { ) {
this.git = gitCommandManager this.git = gitCommandManager
this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings) this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings)
@ -63,7 +63,12 @@ class GitAuthHelper {
// Instead of SSH URL // Instead of SSH URL
this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf` // "origin" is SCHEME://HOSTNAME[:PORT] this.insteadOfKey = `url.${serverUrl.origin}/.insteadOf` // "origin" is SCHEME://HOSTNAME[:PORT]
this.insteadOfValue = `git@${serverUrl.hostname}:` this.insteadOfValues.push(`git@${serverUrl.hostname}:`)
if (this.settings.workflowOrganizationId) {
this.insteadOfValues.push(
`org-${this.settings.workflowOrganizationId}@github.com:`
)
}
} }
async configureAuth(): Promise<void> { async configureAuth(): Promise<void> {
@ -118,7 +123,9 @@ class GitAuthHelper {
// Configure HTTPS instead of SSH // Configure HTTPS instead of SSH
await this.git.tryConfigUnset(this.insteadOfKey, true) await this.git.tryConfigUnset(this.insteadOfKey, true)
if (!this.settings.sshKey) { if (!this.settings.sshKey) {
await this.git.config(this.insteadOfKey, this.insteadOfValue, true) for (const insteadOfValue of this.insteadOfValues) {
await this.git.config(this.insteadOfKey, insteadOfValue, true, true)
}
} }
} catch (err) { } catch (err) {
// Unset in case somehow written to the real global config // Unset in case somehow written to the real global config
@ -159,10 +166,12 @@ class GitAuthHelper {
) )
} else { } else {
// Configure HTTPS instead of SSH // Configure HTTPS instead of SSH
await this.git.submoduleForeach( for (const insteadOfValue of this.insteadOfValues) {
`git config --local '${this.insteadOfKey}' '${this.insteadOfValue}'`, await this.git.submoduleForeach(
this.settings.nestedSubmodules `git config --local --add '${this.insteadOfKey}' '${insteadOfValue}'`,
) this.settings.nestedSubmodules
)
}
} }
} }
} }

View File

@ -21,7 +21,8 @@ export interface IGitCommandManager {
config( config(
configKey: string, configKey: string,
configValue: string, configValue: string,
globalConfig?: boolean globalConfig?: boolean,
add?: boolean
): Promise<void> ): Promise<void>
configExists(configKey: string, globalConfig?: boolean): Promise<boolean> configExists(configKey: string, globalConfig?: boolean): Promise<boolean>
fetch(refSpec: string[], fetchDepth?: number): Promise<void> fetch(refSpec: string[], fetchDepth?: number): Promise<void>
@ -140,14 +141,15 @@ class GitCommandManager {
async config( async config(
configKey: string, configKey: string,
configValue: string, configValue: string,
globalConfig?: boolean globalConfig?: boolean,
add?: boolean
): Promise<void> { ): Promise<void> {
await this.execGit([ const args: string[] = ['config', globalConfig ? '--global' : '--local']
'config', if (add) {
globalConfig ? '--global' : '--local', args.push('--add')
configKey, }
configValue args.push(...[configKey, configValue])
]) await this.execGit(args)
} }
async configExists( async configExists(

View File

@ -73,4 +73,9 @@ export interface IGitSourceSettings {
* Indicates whether to persist the credentials on disk to enable scripting authenticated git commands * Indicates whether to persist the credentials on disk to enable scripting authenticated git commands
*/ */
persistCredentials: boolean persistCredentials: boolean
/**
* Organization ID for the currently running workflow (used for auth settings)
*/
workflowOrganizationId: number | undefined
} }

View File

@ -2,9 +2,10 @@ import * as core from '@actions/core'
import * as fsHelper from './fs-helper' import * as fsHelper from './fs-helper'
import * as github from '@actions/github' import * as github from '@actions/github'
import * as path from 'path' import * as path from 'path'
import * as workflowContextHelper from './workflow-context-helper'
import {IGitSourceSettings} from './git-source-settings' import {IGitSourceSettings} from './git-source-settings'
export function getInputs(): IGitSourceSettings { export async function getInputs(): Promise<IGitSourceSettings> {
const result = ({} as unknown) as IGitSourceSettings const result = ({} as unknown) as IGitSourceSettings
// GitHub workspace // GitHub workspace
@ -118,5 +119,8 @@ export function getInputs(): IGitSourceSettings {
result.persistCredentials = result.persistCredentials =
(core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE' (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE'
// Workflow organization ID
result.workflowOrganizationId = await workflowContextHelper.getOrganizationId()
return result return result
} }

View File

@ -7,7 +7,7 @@ import * as stateHelper from './state-helper'
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
const sourceSettings = inputHelper.getInputs() const sourceSettings = await inputHelper.getInputs()
try { try {
// Register problem matcher // Register problem matcher

View File

@ -0,0 +1,30 @@
import * as core from '@actions/core'
import * as fs from 'fs'
/**
* Gets the organization ID of the running workflow or undefined if the value cannot be loaded from the GITHUB_EVENT_PATH
*/
export async function getOrganizationId(): Promise<number | undefined> {
try {
const eventPath = process.env.GITHUB_EVENT_PATH
if (!eventPath) {
core.debug(`GITHUB_EVENT_PATH is not defined`)
return
}
const content = await fs.promises.readFile(eventPath, {encoding: 'utf8'})
const event = JSON.parse(content)
const id = event?.repository?.owner?.id
if (typeof id !== 'number') {
core.debug('Repository owner ID not found within GITHUB event info')
return
}
return id as number
} catch (err) {
core.debug(
`Unable to load organization ID from GITHUB_EVENT_PATH: ${(err as any)
.message || err}`
)
}
}