Skip to content

Commit 1066240

Browse files
committed
chore: support for PERSONAL_TOKEN & GITHUB_TOKEN env vars
see #88 FYI @EdricChan03
1 parent 2a5d2dd commit 1066240

File tree

2 files changed

+81
-14
lines changed

2 files changed

+81
-14
lines changed

src/engine/engine.spec.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import * as engine from './engine';
44

55
describe('engine', () => {
66
describe('prepareOptions', () => {
7+
beforeEach(() => {
8+
process.env = {};
9+
});
10+
711
it('should replace the string GH_TOKEN in the repo url (for backwards compatibility)', () => {
812
const options = {
913
repo: 'https://[email protected]/organisation/your-repo.git'
@@ -16,15 +20,56 @@ describe('engine', () => {
1620
);
1721
});
1822

19-
it('should add a GH_TOKEN to the repo url', () => {
23+
// 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)', () => {
25+
const options = {
26+
repo:
27+
'https://x-access-token:[email protected]/organisation/your-repo.git'
28+
};
29+
process.env.GH_TOKEN = 'XXX';
30+
const finalOptions = engine.prepareOptions(options, new NullLogger());
31+
32+
expect(finalOptions.repo).toBe(
33+
'https://x-access-token:[email protected]/organisation/your-repo.git'
34+
);
35+
});
36+
37+
// ----
38+
39+
it('should also add a personal access token (GH_TOKEN) to the repo url', () => {
2040
const options = {
2141
repo: 'https://github.com/organisation/your-repo.git'
2242
};
2343
process.env.GH_TOKEN = 'XXX';
2444
const finalOptions = engine.prepareOptions(options, new NullLogger());
2545

2646
expect(finalOptions.repo).toBe(
27-
'https://[email protected]/organisation/your-repo.git'
47+
'https://x-access-token:[email protected]/organisation/your-repo.git'
48+
);
49+
});
50+
51+
it('should also add a personal access token (PERSONAL_TOKEN) to the repo url', () => {
52+
const options = {
53+
repo: 'https://github.com/organisation/your-repo.git'
54+
};
55+
process.env.PERSONAL_TOKEN = 'XXX';
56+
const finalOptions = engine.prepareOptions(options, new NullLogger());
57+
58+
expect(finalOptions.repo).toBe(
59+
'https://x-access-token:[email protected]/organisation/your-repo.git'
60+
);
61+
});
62+
63+
it('should also add a installation access token (GITHUB_TOKEN) to the repo url', () => {
64+
debugger;
65+
const options = {
66+
repo: 'https://github.com/organisation/your-repo.git'
67+
};
68+
process.env.GITHUB_TOKEN = 'XXX';
69+
const finalOptions = engine.prepareOptions(options, new NullLogger());
70+
71+
expect(finalOptions.repo).toBe(
72+
'https://x-access-token:[email protected]/organisation/your-repo.git'
2873
);
2974
});
3075
});

src/engine/engine.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,41 @@ export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
103103
process.env.CIRCLE_BUILD_URL;
104104
}
105105

106-
if (process.env.GH_TOKEN && options.repo) {
107-
options.repo = options.repo.replace(
108-
'http://github.com/',
109-
110-
);
111-
options.repo = options.repo.replace(
112-
'https://github.com/',
113-
'https://[email protected]/'
114-
);
106+
// for backwards compatibility only,
107+
// in the past --repo=https://[email protected]/<username>/<repositoryname>.git was advised
108+
//
109+
// this repalcement was also used to inject other tokens into the URL,
110+
// so it should only be removed with the next major version
111+
if (
112+
process.env.GH_TOKEN &&
113+
options.repo &&
114+
options.repo.includes('GH_TOKEN')
115+
) {
115116
options.repo = options.repo.replace('GH_TOKEN', process.env.GH_TOKEN);
116117
}
118+
// preffered way: token is replaced from plain URL
119+
else if (options.repo && !options.repo.includes('x-access-token:')) {
120+
if (process.env.GH_TOKEN) {
121+
options.repo = options.repo.replace(
122+
'https://github.com/',
123+
`https://x-access-token:${process.env.GH_TOKEN}@github.com/`
124+
);
125+
}
126+
127+
if (process.env.PERSONAL_TOKEN) {
128+
options.repo = options.repo.replace(
129+
'https://github.com/',
130+
`https://x-access-token:${process.env.PERSONAL_TOKEN}@github.com/`
131+
);
132+
}
133+
134+
if (process.env.GITHUB_TOKEN) {
135+
options.repo = options.repo.replace(
136+
'https://github.com/',
137+
`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/`
138+
);
139+
}
140+
}
117141

118142
return options;
119143
}
@@ -215,9 +239,7 @@ async function publishViaGhPages(
215239
return;
216240
}
217241

218-
logger.info(
219-
'👨‍🚀 Uploading via git, please wait...'
220-
);
242+
logger.info('👨‍🚀 Uploading via git, please wait...');
221243

222244
// do NOT (!!) await ghPages.publish,
223245
// the promise is implemented in such a way that it always succeeds – even on errors!

0 commit comments

Comments
 (0)