From 0dd67d64fa678a4d1af47616cf17aff7200746c7 Mon Sep 17 00:00:00 2001 From: Jonathan Morley Date: Thu, 3 Oct 2024 01:50:45 -0400 Subject: [PATCH] feat: Add https auth (#748) --- README.md | 2 +- src/adapters/github.test.ts | 31 +++++++++++++++++++++++++++++++ src/adapters/github.ts | 15 ++++++++++----- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 268ec298..2a4f96fb 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ npm install -g @nerdwallet/shepherd If using GitHub Enterprise, ensure the following environment variables are exported: ``` -export SHEPHERD_GITHUB_ENTERPRISE_BASE_URL={company_github_enterprise_base_url} # e.g., api.github.com +export SHEPHERD_GITHUB_ENTERPRISE_BASE_URL={company_github_enterprise_base_url} # e.g., github.com export SHEPHERD_GITHUB_ENTERPRISE_URL={company_github_enterprise_url} # e.g., api.github.com/api/v3 ``` diff --git a/src/adapters/github.test.ts b/src/adapters/github.test.ts index 8a4effb4..5f653bc5 100644 --- a/src/adapters/github.test.ts +++ b/src/adapters/github.test.ts @@ -255,4 +255,35 @@ describe('GithubAdapter', () => { expect(service.updatePullRequest).not.toBeCalled(); }); }); + + describe('getRepositoryUrl', () => { + const context = mockMigrationContext(); + const octokit = {} as any as Octokit; + const service: any = new GithubService(context, octokit); + const adapter = new GithubAdapter(context, service); + const repo = { + owner: 'NerdWallet', + name: 'shepherd', + }; + + it('returns the SSH URL, when not given a protocol', () => { + delete process.env.SHEPHERD_GITHUB_PROTOCOL; + expect(adapter['getRepositoryUrl'](repo)).toBe('git@github.com:NerdWallet/shepherd.git'); + }); + + it('returns the SSH URL, when given protocol=ssh', () => { + process.env.SHEPHERD_GITHUB_PROTOCOL = 'ssh'; + expect(adapter['getRepositoryUrl'](repo)).toBe('git@github.com:NerdWallet/shepherd.git'); + }); + + it('returns the HTTPS URL when given protocol=https', () => { + process.env.SHEPHERD_GITHUB_PROTOCOL = 'https'; + expect(adapter['getRepositoryUrl'](repo)).toBe('https://github.com/NerdWallet/shepherd.git'); + }); + + it('throws on unexpected protocols', () => { + process.env.SHEPHERD_GITHUB_PROTOCOL = 'not-a-protocol'; + expect(() => adapter['getRepositoryUrl'](repo)).toThrow("Unknown protocol not-a-protocol. Valid values are 'ssh' and 'https'"); + }); + }); }); diff --git a/src/adapters/github.ts b/src/adapters/github.ts index 5ce8704f..1ed43d95 100644 --- a/src/adapters/github.ts +++ b/src/adapters/github.ts @@ -8,10 +8,6 @@ import { IEnvironmentVariables, IRepo } from './base.js'; import GitAdapter from './git.js'; import GithubService from '../services/github.js'; -const { SHEPHERD_GITHUB_ENTERPRISE_BASE_URL } = process.env; - -const gitHubEnterpriseBaseUrl = SHEPHERD_GITHUB_ENTERPRISE_BASE_URL || 'api.github.com'; - enum SafetyStatus { Success, PullRequestExisted, @@ -309,7 +305,16 @@ class GithubAdapter extends GitAdapter { } protected getRepositoryUrl(repo: IRepo): string { - return `git@${gitHubEnterpriseBaseUrl}:${repo.owner}/${repo.name}.git`; + const gitHubEnterpriseBaseUrl = process.env.SHEPHERD_GITHUB_ENTERPRISE_BASE_URL ?? 'github.com'; + const githubProtocol = process.env.SHEPHERD_GITHUB_PROTOCOL ?? 'ssh'; + + if (githubProtocol === 'ssh') { + return `git@${gitHubEnterpriseBaseUrl}:${repo.owner}/${repo.name}.git`; + } else if (githubProtocol === 'https') { + return `https://${gitHubEnterpriseBaseUrl}/${repo.owner}/${repo.name}.git`; + } else { + throw new Error(`Unknown protocol ${githubProtocol}. Valid values are 'ssh' and 'https'`); + } } private async checkActionSafety(repo: IRepo): Promise {