Skip to content

Commit

Permalink
chore: support for PERSONAL_TOKEN & GITHUB_TOKEN env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesHoppe committed Jan 5, 2020
1 parent 2a5d2dd commit 1066240
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
49 changes: 47 additions & 2 deletions src/engine/engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import * as engine from './engine';

describe('engine', () => {
describe('prepareOptions', () => {
beforeEach(() => {
process.env = {};
});

it('should replace the string GH_TOKEN in the repo url (for backwards compatibility)', () => {
const options = {
repo: 'https://GH_TOKEN@github.com/organisation/your-repo.git'
Expand All @@ -16,15 +20,56 @@ describe('engine', () => {
);
});

it('should add a GH_TOKEN to the repo url', () => {
// 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)', () => {
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());

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

// ----

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

expect(finalOptions.repo).toBe(
'https://XXX@github.com/organisation/your-repo.git'
'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', () => {
const options = {
repo: 'https://github.com/organisation/your-repo.git'
};
process.env.PERSONAL_TOKEN = 'XXX';
const finalOptions = engine.prepareOptions(options, new NullLogger());

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;
const options = {
repo: 'https://github.com/organisation/your-repo.git'
};
process.env.GITHUB_TOKEN = 'XXX';
const finalOptions = engine.prepareOptions(options, new NullLogger());

expect(finalOptions.repo).toBe(
'https://x-access-token:XXX@github.com/organisation/your-repo.git'
);
});
});
Expand Down
46 changes: 34 additions & 12 deletions src/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,41 @@ export function prepareOptions(origOptions: Schema, logger: logging.LoggerApi) {
process.env.CIRCLE_BUILD_URL;
}

if (process.env.GH_TOKEN && options.repo) {
options.repo = options.repo.replace(
'http://github.com/',
'http://GH_TOKEN@github.com/'
);
options.repo = options.repo.replace(
'https://github.com/',
'https://GH_TOKEN@github.com/'
);
// for backwards compatibility only,
// in the past --repo=https://GH_TOKEN@github.com/<username>/<repositoryname>.git was advised
//
// this repalcement was also used to inject other tokens into the URL,
// so it should only be removed with the next major version
if (
process.env.GH_TOKEN &&
options.repo &&
options.repo.includes('GH_TOKEN')
) {
options.repo = options.repo.replace('GH_TOKEN', process.env.GH_TOKEN);
}
// preffered way: token is replaced from plain URL
else if (options.repo && !options.repo.includes('x-access-token:')) {
if (process.env.GH_TOKEN) {
options.repo = options.repo.replace(
'https://github.com/',
`https://x-access-token:${process.env.GH_TOKEN}@github.com/`
);
}

if (process.env.PERSONAL_TOKEN) {
options.repo = options.repo.replace(
'https://github.com/',
`https://x-access-token:${process.env.PERSONAL_TOKEN}@github.com/`
);
}

if (process.env.GITHUB_TOKEN) {
options.repo = options.repo.replace(
'https://github.com/',
`https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/`
);
}
}

return options;
}
Expand Down Expand Up @@ -215,9 +239,7 @@ async function publishViaGhPages(
return;
}

logger.info(
'👨‍🚀 Uploading via git, please wait...'
);
logger.info('👨‍🚀 Uploading via git, please wait...');

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

0 comments on commit 1066240

Please sign in to comment.