Skip to content

Commit

Permalink
feat: Add https auth (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorley authored Oct 3, 2024
1 parent b40086a commit 0dd67d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
31 changes: 31 additions & 0 deletions src/adapters/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
});
});
});
15 changes: 10 additions & 5 deletions src/adapters/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<SafetyStatus> {
Expand Down

0 comments on commit 0dd67d6

Please sign in to comment.