Skip to content

Commit

Permalink
chore: always discover remote URL (if not set)
Browse files Browse the repository at this point in the history
this allows us to inject tokens from environment even if `--repo` is not set manually
  • Loading branch information
JohannesHoppe committed Jan 5, 2020
1 parent 1066240 commit c5f7b36
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/engine/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export const defaults = {
silent: true,
dotfiles: true,
cname: undefined,
dryRun: false
dryRun: false,
remote: 'origin',
git: 'git'
};
48 changes: 37 additions & 11 deletions src/engine/engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ import * as engine from './engine';

describe('engine', () => {
describe('prepareOptions', () => {
const logger = new NullLogger();

beforeEach(() => {
process.env = {};
});

it('should replace the string GH_TOKEN in the repo url (for backwards compatibility)', () => {
it('should replace the string GH_TOKEN in the repo url (for backwards compatibility)', async () => {
const options = {
repo: 'https://GH_TOKEN@github.com/organisation/your-repo.git'
};
process.env.GH_TOKEN = 'XXX';
const finalOptions = engine.prepareOptions(options, new NullLogger());
const finalOptions = await engine.prepareOptions(options, logger);

expect(finalOptions.repo).toBe(
'https://XXX@github.com/organisation/your-repo.git'
);
});

// see https://github.com/EdricChan03/rss-reader/commit/837dc10c18bfa453c586bb564a662e7dad1e68ab#r36665276 as an example
it('should be possible to use GH_TOKEN in repo url as a workaround for other tokens (for backwards compatibility)', () => {
it('should be possible to use GH_TOKEN in repo url as a workaround for other tokens (for backwards compatibility)', async () => {
const options = {
repo:
'https://x-access-token:GH_TOKEN@github.com/organisation/your-repo.git'
};
process.env.GH_TOKEN = 'XXX';
const finalOptions = engine.prepareOptions(options, new NullLogger());
const finalOptions = await engine.prepareOptions(options, logger);

expect(finalOptions.repo).toBe(
'https://x-access-token:XXX@github.com/organisation/your-repo.git'
Expand All @@ -36,41 +38,65 @@ describe('engine', () => {

// ----

it('should also add a personal access token (GH_TOKEN) to the repo url', () => {
it('should also add a personal access token (GH_TOKEN) to the repo url', async () => {
const options = {
repo: 'https://github.com/organisation/your-repo.git'
};
process.env.GH_TOKEN = 'XXX';
const finalOptions = engine.prepareOptions(options, new NullLogger());
const finalOptions = await engine.prepareOptions(options, logger);

expect(finalOptions.repo).toBe(
'https://x-access-token:XXX@github.com/organisation/your-repo.git'
);
});

it('should also add a personal access token (PERSONAL_TOKEN) to the repo url', () => {
it('should also add a personal access token (PERSONAL_TOKEN) to the repo url', async () => {
const options = {
repo: 'https://github.com/organisation/your-repo.git'
};
process.env.PERSONAL_TOKEN = 'XXX';
const finalOptions = engine.prepareOptions(options, new NullLogger());
const finalOptions = await engine.prepareOptions(options, logger);

expect(finalOptions.repo).toBe(
'https://x-access-token:XXX@github.com/organisation/your-repo.git'
);
});

it('should also add a installation access token (GITHUB_TOKEN) to the repo url', () => {
debugger;
it('should also add a installation access token (GITHUB_TOKEN) to the repo url', async () => {
const options = {
repo: 'https://github.com/organisation/your-repo.git'
};
process.env.GITHUB_TOKEN = 'XXX';
const finalOptions = engine.prepareOptions(options, new NullLogger());
const finalOptions = await engine.prepareOptions(options, logger);

expect(finalOptions.repo).toBe(
'https://x-access-token:XXX@github.com/organisation/your-repo.git'
);
});

// NEW in 0.6.2: always discover remote URL (if not set)
// this allows us to inject tokens from environment even if --repo is not set manually
// it uses gh-pages lib directly for this
it('should discover the remote url, if no --repo is set', async () => {
const options = {};
const finalOptions = await engine.prepareOptions(options, logger);

expect(finalOptions.repo).toMatch(/angular-schule\/angular-cli-ghpages/);
});

/*
// i was not able to somehow catch an error... :-(
it('should should throw an exception, if remote url could not be discovered', async () => {
expect.assertions(1);
const options = { git: 'xxx' };
try {
await engine.prepareOptions(options, logger);
} catch (e) {
expect(e).toBeTruthy();
}
})*/
});
});
20 changes: 18 additions & 2 deletions src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { Schema } from '../deploy/schema';
import { GHPages } from '../interfaces';
import { defaults } from './defaults';

import Git from 'gh-pages/lib/git';

export async function run(
dir: string,
options: Schema,
logger: logging.LoggerApi
) {
options = prepareOptions(options, logger);
options = await prepareOptions(options, logger);

// this has to occur _after_ the monkeypatch of util.debuglog:
const ghpages = require('gh-pages');
Expand All @@ -34,7 +36,10 @@ export async function run(
);
}

export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
export async function prepareOptions(
origOptions: Schema,
logger: logging.LoggerApi
) {
const options = {
...defaults,
...origOptions
Expand Down Expand Up @@ -103,6 +108,12 @@ export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
process.env.CIRCLE_BUILD_URL;
}

// NEW in 0.6.2: always discover remote URL (if not set)
// this allows us to inject tokens from environment even if `--repo` is not set manually
if (!options.repo) {
options.repo = await getRemoteUrl(options);
}

// for backwards compatibility only,
// in the past --repo=https://GH_TOKEN@github.com/<username>/<repositoryname>.git was advised
//
Expand Down Expand Up @@ -253,3 +264,8 @@ async function publishViaGhPages(
});
});
}

async function getRemoteUrl(options) {
const git = new Git(process.cwd(), options.git);
return await git.getRemoteUrl(options.remote);
}
2 changes: 1 addition & 1 deletion src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5f7b36

Please sign in to comment.