Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 5e12762

Browse files
committed
Implement upload hook
1 parent 5cf0837 commit 5e12762

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ module.exports = {
185185
});
186186
}
187187

188+
/**
189+
* Private function for pushing to upstream branch.
190+
*
191+
* @params {Repo} repository
192+
* @params {String} branch
193+
* @params {String} remote
194+
* @params {Boolean} setUpstream
195+
* @return {Promise}
196+
*/
197+
function pushUpstream(repository, branch, remote, setUpstream) {
198+
return new Promise((resolve, reject) => {
199+
repository.git('push', {
200+
u: setUpstream,
201+
f: true
202+
}, [remote, branch], error => {
203+
if (error) {
204+
return reject(error);
205+
}
206+
207+
return resolve();
208+
});
209+
});
210+
}
211+
188212
var GHPagesPlugin = DeployPluginBase.extend({
189213
name: options.name,
190214

@@ -287,7 +311,8 @@ module.exports = {
287311
let branch = this.readConfig('gitBranch');
288312

289313
return addRemote(context.gitRepo, remoteName, remoteUrl)
290-
.then(function () {
314+
.then(() => {
315+
this.log('remote repository added', { color: 'green' });
291316
return setUpstream(context.gitRepo, branch, remoteName);
292317
})
293318
.catch(() => {
@@ -299,6 +324,18 @@ module.exports = {
299324
},
300325

301326
upload: function (context) {
327+
let repo = context.gitRepo;
328+
let branch = this.readConfig('gitBranch');
329+
let remoteName = this.readConfig('gitRemoteName');
330+
let setUpstream = context.setUpstreamOnPush;
331+
332+
this.log('pushing to remote repository');
333+
334+
return pushUpstream(repo, branch, remoteName, setUpstream)
335+
.then(() => {
336+
this.log('push success', { color: 'green' });
337+
})
338+
.catch(error => this.log(error, { color: 'red' }));
302339
},
303340

304341
teardown: function (context) {

tests/unit/index-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var expect = require('chai').expect;
55
var path = require('path');
66
var fs = require('fs-extra');
77
var git = require('gift');
8+
var RSVP = require('rsvp');
89

910
describe('github pages plugin', function () {
1011
let subject;
@@ -185,4 +186,55 @@ describe('github pages plugin', function () {
185186
});
186187
});
187188
});
189+
190+
describe('#upload hook', function () {
191+
this.timeout(10000);
192+
let repo;
193+
194+
before(function (done) {
195+
plugin.beforeHook(context);
196+
plugin.configure(context);
197+
plugin.setup(context)
198+
.then(function () {
199+
return plugin.didBuild(context);
200+
})
201+
.then(function () {
202+
return plugin.willUpload(context);
203+
})
204+
.then(function () {
205+
return plugin.upload(context);
206+
})
207+
.then(function () {
208+
repo = git(repoPath);
209+
done();
210+
});
211+
});
212+
213+
function revParse(branch) {
214+
return new RSVP.Promise((resolve, reject) => {
215+
repo.git('rev-parse', {}, [branch], (error, stdout) => {
216+
if (error) {
217+
return reject(error);
218+
}
219+
220+
return resolve(stdout);
221+
});
222+
});
223+
}
224+
225+
it('pushes branch to remote repository', function (done) {
226+
// Fetch from remote and see if local is up-to-date
227+
repo.remote_fetch('ember-cli-deploy-test', error => {
228+
expect(error).to.be.null;
229+
230+
RSVP.all([
231+
revParse('gh-pages'),
232+
revParse('ember-cli-deploy-test/gh-pages')
233+
]).then((shaHashes) => {
234+
expect(shaHashes[0]).to.be.equal(shaHashes[1]);
235+
done();
236+
});
237+
});
238+
});
239+
});
188240
});

0 commit comments

Comments
 (0)