Skip to content

Commit 87e72d7

Browse files
davinkevinDavin Kevin
authored and
Davin Kevin
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 9b65481 commit 87e72d7

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
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

+36-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,40 @@ 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 add .')
89+
.addExecSuccess(`git commit -m "${message}"`)
90+
.addExecSuccess(`git checkout ${initialBranch}`)
91+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
92+
.addExecSuccess('git remote -v', remote);
93+
94+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`, `--gh-username=${username}`]);
95+
})
96+
97+
it('should deploy with token only', function () {
98+
let token = 'token';
99+
100+
execStub.addExecSuccess('git status --porcelain')
101+
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
102+
.addExecSuccess('git remote -v', remote)
103+
.addExecSuccess(`git checkout ${ghPagesBranch}`)
104+
.addExecSuccess('git add .')
105+
.addExecSuccess(`git commit -m "${message}"`)
106+
.addExecSuccess(`git checkout ${initialBranch}`)
107+
.addExecSuccess('git remote -v', remote)
108+
.addExecSuccess(`git push https://${token}@github.com/username/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
109+
.addExecSuccess('git remote -v', remote);
110+
111+
return ng(['github-pages:deploy', '--skip-build', `--gh-token=${token}`]);
112+
});
113+
80114
it('should deploy with changed defaults', function() {
81115
let userPageBranch = 'master',
82116
message = 'not new gh-pages version';
@@ -126,14 +160,14 @@ describe('Acceptance: ng github-pages:deploy', function() {
126160
.addExecSuccess('git rev-parse --abbrev-ref HEAD', initialBranch)
127161
.addExecSuccess('git remote -v', noRemote)
128162
.addExecSuccess(`git remote add origin [email protected]:${username}/${project}.git`)
129-
.addExecSuccess(`git push -u origin ${initialBranch}`)
163+
.addExecSuccess(`git push -u https://${token}@github.com/${username}/${project}.git ${initialBranch}`)
130164
.addExecSuccess(`git checkout ${ghPagesBranch}`)
131165
.addExecSuccess('git ls-files')
132166
.addExecSuccess('git rm -r ')
133167
.addExecSuccess('git add .')
134168
.addExecSuccess(`git commit -m "${message}"`)
135169
.addExecSuccess(`git checkout ${initialBranch}`)
136-
.addExecSuccess(`git push origin ${ghPagesBranch}:${ghPagesBranch}`)
170+
.addExecSuccess(`git push https://${token}@github.com/${username}/${project}.git ${ghPagesBranch}:${ghPagesBranch}`)
137171
.addExecSuccess('git remote -v', remote);
138172

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

0 commit comments

Comments
 (0)