From 0fc042ca35b9ee6d4c6dc650662d40f189018a49 Mon Sep 17 00:00:00 2001 From: eric sciple Date: Mon, 1 Nov 2021 11:43:18 -0500 Subject: set insteadOf url for org-id (#621) --- src/git-auth-helper.ts | 25 +++++++++++++++++-------- src/git-command-manager.ts | 18 ++++++++++-------- src/git-source-settings.ts | 5 +++++ src/input-helper.ts | 6 +++++- src/main.ts | 2 +- src/workflow-context-helper.ts | 30 ++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 src/workflow-context-helper.ts (limited to 'src') diff --git a/src/git-auth-helper.ts b/src/git-auth-helper.ts index af400ab..233b3e6 100644 --- a/src/git-auth-helper.ts +++ b/src/git-auth-helper.ts @@ -37,7 +37,7 @@ class GitAuthHelper { private readonly tokenConfigValue: string private readonly tokenPlaceholderConfigValue: string private readonly insteadOfKey: string - private readonly insteadOfValue: string + private readonly insteadOfValues: string[] = [] private sshCommand = '' private sshKeyPath = '' private sshKnownHostsPath = '' @@ -45,7 +45,7 @@ class GitAuthHelper { constructor( gitCommandManager: IGitCommandManager, - gitSourceSettings?: IGitSourceSettings + gitSourceSettings: IGitSourceSettings | undefined ) { this.git = gitCommandManager this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings) @@ -63,7 +63,12 @@ class GitAuthHelper { // Instead of SSH URL 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 { @@ -118,7 +123,9 @@ class GitAuthHelper { // Configure HTTPS instead of SSH await this.git.tryConfigUnset(this.insteadOfKey, true) 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) { // Unset in case somehow written to the real global config @@ -159,10 +166,12 @@ class GitAuthHelper { ) } else { // Configure HTTPS instead of SSH - await this.git.submoduleForeach( - `git config --local '${this.insteadOfKey}' '${this.insteadOfValue}'`, - this.settings.nestedSubmodules - ) + for (const insteadOfValue of this.insteadOfValues) { + await this.git.submoduleForeach( + `git config --local --add '${this.insteadOfKey}' '${insteadOfValue}'`, + this.settings.nestedSubmodules + ) + } } } } diff --git a/src/git-command-manager.ts b/src/git-command-manager.ts index 409a161..699a963 100644 --- a/src/git-command-manager.ts +++ b/src/git-command-manager.ts @@ -21,7 +21,8 @@ export interface IGitCommandManager { config( configKey: string, configValue: string, - globalConfig?: boolean + globalConfig?: boolean, + add?: boolean ): Promise configExists(configKey: string, globalConfig?: boolean): Promise fetch(refSpec: string[], fetchDepth?: number): Promise @@ -140,14 +141,15 @@ class GitCommandManager { async config( configKey: string, configValue: string, - globalConfig?: boolean + globalConfig?: boolean, + add?: boolean ): Promise { - await this.execGit([ - 'config', - globalConfig ? '--global' : '--local', - configKey, - configValue - ]) + const args: string[] = ['config', globalConfig ? '--global' : '--local'] + if (add) { + args.push('--add') + } + args.push(...[configKey, configValue]) + await this.execGit(args) } async configExists( diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 2786222..19f4651 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -73,4 +73,9 @@ export interface IGitSourceSettings { * Indicates whether to persist the credentials on disk to enable scripting authenticated git commands */ persistCredentials: boolean + + /** + * Organization ID for the currently running workflow (used for auth settings) + */ + workflowOrganizationId: number | undefined } diff --git a/src/input-helper.ts b/src/input-helper.ts index 4c05d6e..40e6de4 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -2,9 +2,10 @@ import * as core from '@actions/core' import * as fsHelper from './fs-helper' import * as github from '@actions/github' import * as path from 'path' +import * as workflowContextHelper from './workflow-context-helper' import {IGitSourceSettings} from './git-source-settings' -export function getInputs(): IGitSourceSettings { +export async function getInputs(): Promise { const result = ({} as unknown) as IGitSourceSettings // GitHub workspace @@ -118,5 +119,8 @@ export function getInputs(): IGitSourceSettings { result.persistCredentials = (core.getInput('persist-credentials') || 'false').toUpperCase() === 'TRUE' + // Workflow organization ID + result.workflowOrganizationId = await workflowContextHelper.getOrganizationId() + return result } diff --git a/src/main.ts b/src/main.ts index 07a7ce3..97a27af 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ import * as stateHelper from './state-helper' async function run(): Promise { try { - const sourceSettings = inputHelper.getInputs() + const sourceSettings = await inputHelper.getInputs() try { // Register problem matcher diff --git a/src/workflow-context-helper.ts b/src/workflow-context-helper.ts new file mode 100644 index 0000000..c7d7918 --- /dev/null +++ b/src/workflow-context-helper.ts @@ -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 { + 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}` + ) + } +} -- cgit v1.2.3