Skip to content

Commit

Permalink
Merge pull request #64 from dwyl/sync-fail
Browse files Browse the repository at this point in the history
Sync Fail Handled
  • Loading branch information
nelsonic authored Apr 20, 2017
2 parents 18039e8 + 22638bd commit fa85fa4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 9 deletions.
23 changes: 18 additions & 5 deletions lib/copy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var hoek = require('hoek');

var ghReq = require('./gh_request.js');


function labelsPath (owner, repo) {
return '/repos/' + owner + '/' + repo + '/labels';
}
Expand All @@ -21,7 +21,7 @@ module.exports = function copy (request, reply, decoded) {
}, function (err, labels) {
hoek.assert(!err, err);
if (labels.message) {
return reply.view('sync-fail', { error: 'Repo not found' })
return reply.view('sync-fail', { error: 'Source repo not found' })
.state('last', repoData)
;
}
Expand All @@ -31,15 +31,28 @@ module.exports = function copy (request, reply, decoded) {
method: 'POST',
path: labelsPath(repoData.targetOwner, repoData.targetRepo),
body: { name: label.name, color: label.color }
}, function (error) {
}, function (error, data) {
hoek.assert(!error, error);
labelsSet += 1;
if (labelsSet === labels.length) {
reply.view('sync-success', {
if (labelsSet === labels.length && !data.message) {
return reply.view('sync-success', {
targetOwner: repoData.targetOwner,
targetRepo: repoData.targetRepo
}).state('last', repoData);
} else if (labelsSet === labels.length
&& data.message === 'Not Found') {
return reply.view('sync-fail', {
error: 'Label-Sync doesn\'t have'
+ 'access to this repository. Approve access '
+ 'in your organisation settings and try again.'
}).state('last', repoData);
} else if (labelsSet === labels.length
&& data.message === 'Validation Failed') {
return reply.view('sync-fail', { error: 'Labels already synced' })
.state('last', repoData);
}

return null;
});
});
});
Expand Down
4 changes: 1 addition & 3 deletions test/must_contain.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

module.exports = function mustContain (response, texts) {
var squashedRes = response.result
.replace(/ /g, '')
;
var squashedRes = response.result.replace(/ /g, '');

return texts.reduce(function (success, text) {
return success && squashedRes.indexOf(text) > -1;
Expand Down
66 changes: 65 additions & 1 deletion test/routes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ test('/ : post : logged in: invalid source repo', function (t) {
function (response) {
t.equal(response.statusCode, 200, 'correct headers given to gh');
t.ok(mustContain(response, [
'Reponotfound'
'reponotfound'
]), 'invalid source repo gives error page');
t.equal(findSetCookies(response)[0].name, 'last', 'last cookie set');

Expand Down Expand Up @@ -169,6 +169,70 @@ test('/ : post : logged in: valid source repo with 2 labels', function (t) {
);
});

test('/ : post : logged in: Invalid Permissions', function (t) {
nock('https://api.github.com')
.get('/repos/source/repo/labels')
.reply(200, [
{ name: 'label1', color: '#234243', something: 'else' },
{ name: 'label2', color: '#123456', something: 'else' }
])
.post('/repos/target/repo/labels', function (body) {
return body.name && body.color && Object.keys(body).length === 2;
})
.times(2)
.reply(200, { message: 'Not Found' })
;
server.inject(
{
method: 'POST',
url: '/',
headers: { cookie: 'token=' + testSessionToken },
payload: testFormData
},
function (response) {
t.equal(response.statusCode, 200, 'correct headers given to gh post');
t.ok(mustContain(response, [
'SyncFail'
]), 'sync fail message');
t.equal(findSetCookies(response)[0].name, 'last', 'last cookie set');

t.end();
}
);
});

test('/ : post : logged in: validation failed', function (t) {
nock('https://api.github.com')
.get('/repos/source/repo/labels')
.reply(200, [
{ name: 'label1', color: '#234243', something: 'else' },
{ name: 'label2', color: '#123456', something: 'else' }
])
.post('/repos/target/repo/labels', function (body) {
return body.name && body.color && Object.keys(body).length === 2;
})
.times(2)
.reply(200, { message: 'Validation Failed' })
;
server.inject(
{
method: 'POST',
url: '/',
headers: { cookie: 'token=' + testSessionToken },
payload: testFormData
},
function (response) {
t.equal(response.statusCode, 200, 'correct headers given to gh post');
t.ok(mustContain(response, [
'SyncFail'
]), 'sync fail message');
t.equal(findSetCookies(response)[0].name, 'last', 'last cookie set');

t.end();
}
);
});


test('/logout', function (t) {
server.inject(
Expand Down

0 comments on commit fa85fa4

Please sign in to comment.