Skip to content

Commit bb81d64

Browse files
committed
feat(github): support usage of gh-token for deployment from external env
In order to being able to push to github from CI (Travis for example), we should be able to provide a token to the cli, as `--gh-token=XYZ`. This modification allow to use this token in the destination url. This also allow user to push to another github repository if it uses a different `--gh-username` from the one used for the checkout.
1 parent 766394d commit bb81d64

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

packages/angular-cli/commands/github-pages-deploy.ts

+36-8
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,13 @@ const githubPagesDeployCommand = Command.extend({
182182
.then(function(stdout) {
183183
if (!/origin\s+(https:\/\/|git@)github\.com/m.test(stdout)) {
184184
return createGithubRepoTask.run(createGithubRepoOptions)
185-
.then(() => {
185+
.then(() => generateRemoteUrl())
186+
.then((upstream: string) => {
186187
// only push starting branch if it's not the destinationBranch
187188
// this happens commonly when using github user pages, since
188189
// they require the destination branch to be 'master'
189190
if (destinationBranch !== initialBranch) {
190-
execPromise(`git push -u origin ${initialBranch}`);
191+
execPromise(`git push -u ${upstream} ${initialBranch}`);
191192
}
192193
});
193194
}
@@ -257,16 +258,17 @@ const githubPagesDeployCommand = Command.extend({
257258
}
258259

259260
function pushToGitRepo() {
260-
return execPromise(`git push origin ${ghPagesBranch}:${destinationBranch}`)
261+
return generateRemoteUrl()
262+
.then(upstream => {
263+
return execPromise(`git push ${upstream} ${ghPagesBranch}:${destinationBranch}`);
264+
})
261265
.catch((err) => returnStartingBranch()
262-
.catch(() => Promise.reject(err) ));
266+
.catch(() => Promise.reject(err) ));
263267
}
264268

265269
function printProjectUrl() {
266-
return execPromise('git remote -v')
267-
.then((stdout) => {
268-
let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m);
269-
let userName = match[1].toLowerCase();
270+
return getUsernameFromGitOrigin()
271+
.then((userName) => {
270272
let url = `https://${userName}.github.io/${options.userPage ? '' : (baseHref + '/')}`;
271273
ui.writeLine(chalk.green(`Deployed! Visit ${url}`));
272274
ui.writeLine('Github pages might take a few minutes to show the deployed site.');
@@ -278,11 +280,37 @@ const githubPagesDeployCommand = Command.extend({
278280
ui.writeLine(error.message);
279281
let msg = 'There was a permissions error during git file operations, ' +
280282
'please close any open project files/folders and try again.';
283+
msg += `\nYou might also need to return to the ${initialBranch} branch manually.`;
281284
return Promise.reject(new SilentError(msg.concat(branchErrMsg)));
282285
} else {
283286
return Promise.reject(error);
284287
}
285288
}
289+
290+
function generateRemoteUrl(): Promise<String> {
291+
if (createGithubRepoOptions.ghToken && createGithubRepoOptions.ghUsername) {
292+
return Promise.resolve(`https://${createGithubRepoOptions.ghToken}@github.com/` +
293+
`${createGithubRepoOptions.ghUsername}/${createGithubRepoOptions.projectName}.git`);
294+
}
295+
296+
if (createGithubRepoOptions.ghToken && !createGithubRepoOptions.ghUsername) {
297+
return getUsernameFromGitOrigin()
298+
.then(username => {
299+
return Promise.resolve(`https://${createGithubRepoOptions.ghToken}@github.com/` +
300+
`${username}/${createGithubRepoOptions.projectName}.git`);
301+
});
302+
}
303+
304+
return Promise.resolve('origin');
305+
}
306+
307+
function getUsernameFromGitOrigin(): Promise<String> {
308+
return execPromise('git remote -v')
309+
.then((stdout) => {
310+
let match = stdout.match(/origin\s+(?:https:\/\/|git@)github\.com(?:\:|\/)([^\/]+)/m);
311+
return match[1].toLowerCase();
312+
});
313+
}
286314
}
287315
});
288316

tests/acceptance/github-pages-deploy.spec.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,44 @@ describe('Acceptance: ng github-pages:deploy', function() {
7777
return ng(['github-pages:deploy', '--skip-build']);
7878
});
7979

80+
it('should deploy with token and username', function () {
81+
let token = 'token',
82+
username = 'bar';
83+
84+
execStub.addExecSuccess('git status --porcelain')
85+
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
86+
.addExecSuccess('git remote -v', remote)
87+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
88+
.addExecSuccess('git ls-files')
89+
.addExecSuccess('git rm -r ')
90+
.addExecSuccess('git add .')
91+
.addExecSuccess(`git commit -m "${message}"`)
92+
.addExecSuccess(`git checkout ${initialBranch}`)
93+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
94+
.addExecSuccess('git remote -v', remote);
95+
96+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`, `--gh-username=${username}`]);
97+
})
98+
99+
it('should deploy with token only', function () {
100+
let token = 'token';
101+
102+
execStub.addExecSuccess('git status --porcelain')
103+
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
104+
.addExecSuccess('git remote -v', remote)
105+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
106+
.addExecSuccess('git ls-files')
107+
.addExecSuccess('git rm -r ')
108+
.addExecSuccess('git add .')
109+
.addExecSuccess(`git commit -m "${message}"`)
110+
.addExecSuccess(`git checkout ${initialBranch}`)
111+
.addExecSuccess('git remote -v', remote)
112+
.addExecSuccess(`git push https://${token}@github.com/username/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
113+
.addExecSuccess('git remote -v', remote);
114+
115+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`]);
116+
});
117+
80118
it('should deploy with changed defaults', function() {
81119
let userPageBranch = 'master',
82120
message = 'not new gh-pages version';
@@ -126,14 +164,14 @@ describe('Acceptance: ng github-pages:deploy', function() {
126164
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
127165
.addExecSuccess('git remote -v', noRemote)
128166
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
129-
.addExecSuccess(`git push -u origin ${initialBranch}`)
167+
.addExecSuccess(`git push -u https://${token}@github.com/${username}/${project}.git ${initialBranch}`)
130168
.addExecSuccess(`git checkout ${ghPagesBranch}`)
131169
.addExecSuccess('git ls-files')
132170
.addExecSuccess('git rm -r ')
133171
.addExecSuccess('git add .')
134172
.addExecSuccess(`git commit -m "${message}"`)
135173
.addExecSuccess(`git checkout ${initialBranch}`)
136-
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
174+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
137175
.addExecSuccess('git remote -v', remote);
138176

139177
var httpsStub = sinon.stub(https, 'request', httpsRequestStubFunc);

tests/runner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var glob = require('glob');
88
var path = require('path');
99

1010
var root = 'tests/{acceptance,models}';
11-
var specFiles = glob.sync(root + '/**/*.spec.*');
11+
var specFiles = glob.sync(root + '/**/github-pages-deploy.spec.*');
1212
var mocha = new Mocha({ timeout: 5000, reporter: 'spec' });
1313

1414
process.env.CLI_ROOT = process.env.CLI_ROOT || path.resolve(__dirname, '..');

0 commit comments

Comments
 (0)