Skip to content

Commit c5f7b36

Browse files
committed
chore: always discover remote URL (if not set)
this allows us to inject tokens from environment even if `--repo` is not set manually
1 parent 1066240 commit c5f7b36

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

src/engine/defaults.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ export const defaults = {
88
silent: true,
99
dotfiles: true,
1010
cname: undefined,
11-
dryRun: false
11+
dryRun: false,
12+
remote: 'origin',
13+
git: 'git'
1214
};

src/engine/engine.spec.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,32 @@ import * as engine from './engine';
44

55
describe('engine', () => {
66
describe('prepareOptions', () => {
7+
const logger = new NullLogger();
8+
79
beforeEach(() => {
810
process.env = {};
911
});
1012

11-
it('should replace the string GH_TOKEN in the repo url (for backwards compatibility)', () => {
13+
it('should replace the string GH_TOKEN in the repo url (for backwards compatibility)', async () => {
1214
const options = {
1315
repo: 'https://[email protected]/organisation/your-repo.git'
1416
};
1517
process.env.GH_TOKEN = 'XXX';
16-
const finalOptions = engine.prepareOptions(options, new NullLogger());
18+
const finalOptions = await engine.prepareOptions(options, logger);
1719

1820
expect(finalOptions.repo).toBe(
1921
'https://[email protected]/organisation/your-repo.git'
2022
);
2123
});
2224

2325
// see https://github.com/EdricChan03/rss-reader/commit/837dc10c18bfa453c586bb564a662e7dad1e68ab#r36665276 as an example
24-
it('should be possible to use GH_TOKEN in repo url as a workaround for other tokens (for backwards compatibility)', () => {
26+
it('should be possible to use GH_TOKEN in repo url as a workaround for other tokens (for backwards compatibility)', async () => {
2527
const options = {
2628
repo:
2729
'https://x-access-token:[email protected]/organisation/your-repo.git'
2830
};
2931
process.env.GH_TOKEN = 'XXX';
30-
const finalOptions = engine.prepareOptions(options, new NullLogger());
32+
const finalOptions = await engine.prepareOptions(options, logger);
3133

3234
expect(finalOptions.repo).toBe(
3335
'https://x-access-token:[email protected]/organisation/your-repo.git'
@@ -36,41 +38,65 @@ describe('engine', () => {
3638

3739
// ----
3840

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

4648
expect(finalOptions.repo).toBe(
4749
'https://x-access-token:[email protected]/organisation/your-repo.git'
4850
);
4951
});
5052

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

5860
expect(finalOptions.repo).toBe(
5961
'https://x-access-token:[email protected]/organisation/your-repo.git'
6062
);
6163
});
6264

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

7172
expect(finalOptions.repo).toBe(
7273
'https://x-access-token:[email protected]/organisation/your-repo.git'
7374
);
7475
});
76+
77+
// NEW in 0.6.2: always discover remote URL (if not set)
78+
// this allows us to inject tokens from environment even if --repo is not set manually
79+
// it uses gh-pages lib directly for this
80+
it('should discover the remote url, if no --repo is set', async () => {
81+
const options = {};
82+
const finalOptions = await engine.prepareOptions(options, logger);
83+
84+
expect(finalOptions.repo).toMatch(/angular-schule\/angular-cli-ghpages/);
85+
});
86+
87+
/*
88+
// i was not able to somehow catch an error... :-(
89+
it('should should throw an exception, if remote url could not be discovered', async () => {
90+
91+
expect.assertions(1);
92+
93+
const options = { git: 'xxx' };
94+
95+
try {
96+
await engine.prepareOptions(options, logger);
97+
} catch (e) {
98+
expect(e).toBeTruthy();
99+
}
100+
})*/
75101
});
76102
});

src/engine/engine.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { Schema } from '../deploy/schema';
66
import { GHPages } from '../interfaces';
77
import { defaults } from './defaults';
88

9+
import Git from 'gh-pages/lib/git';
10+
911
export async function run(
1012
dir: string,
1113
options: Schema,
1214
logger: logging.LoggerApi
1315
) {
14-
options = prepareOptions(options, logger);
16+
options = await prepareOptions(options, logger);
1517

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

37-
export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
39+
export async function prepareOptions(
40+
origOptions: Schema,
41+
logger: logging.LoggerApi
42+
) {
3843
const options = {
3944
...defaults,
4045
...origOptions
@@ -103,6 +108,12 @@ export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
103108
process.env.CIRCLE_BUILD_URL;
104109
}
105110

111+
// NEW in 0.6.2: always discover remote URL (if not set)
112+
// this allows us to inject tokens from environment even if `--repo` is not set manually
113+
if (!options.repo) {
114+
options.repo = await getRemoteUrl(options);
115+
}
116+
106117
// for backwards compatibility only,
107118
// in the past --repo=https://[email protected]/<username>/<repositoryname>.git was advised
108119
//
@@ -253,3 +264,8 @@ async function publishViaGhPages(
253264
});
254265
});
255266
}
267+
268+
async function getRemoteUrl(options) {
269+
const git = new Git(process.cwd(), options.git);
270+
return await git.getRemoteUrl(options.remote);
271+
}

src/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)