Skip to content

Commit

Permalink
fix(854): Normalize suspended user's permissions. (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
wdstar authored and tkyi committed Jul 19, 2018
1 parent c3b0f5b commit d937e66
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
31 changes: 23 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const joi = require('joi');
const schema = require('screwdriver-data-schema');
const Scm = require('screwdriver-scm-base');
const crypto = require('crypto');
const winston = require('winston');
const DEFAULT_AUTHOR = {
avatar: 'https://cd.screwdriver.cd/assets/unknown_user.png',
name: 'n/a',
Expand Down Expand Up @@ -445,16 +446,30 @@ class GithubScm extends Scm {
scmUri: config.scmUri,
token: config.token
});
const repo = await this.breaker.runCommand({
action: 'get',
token: config.token,
params: {
owner: scmInfo.owner,
repo: scmInfo.repo

try {
const repo = await this.breaker.runCommand({
action: 'get',
token: config.token,
params: {
owner: scmInfo.owner,
repo: scmInfo.repo
}
});

return repo.data.permissions;
} catch (err) {
// Suspended user
if (err.code === 403) {
winston.info(
`User's account suspended for ${scmInfo.owner}/${scmInfo.repo}, ` +
'it will be removed from pipeline admins.');

return { admin: false, push: false, pull: false };
}
});

return repo.data.permissions;
throw err;
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"hoek": "^5.0.3",
"joi": "^13.4.0",
"screwdriver-data-schema": "^18.29.2",
"screwdriver-scm-base": "^4.2.0"
"screwdriver-scm-base": "^4.2.0",
"winston": "^2.4.2"
},
"release": {
"debug": false,
Expand Down
41 changes: 41 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('index', function () {
let scm;
let githubMock;
let githubMockClass;
let winstonMock;

before(() => {
mockery.enable({
Expand Down Expand Up @@ -64,8 +65,13 @@ describe('index', function () {
}
};
githubMockClass = sinon.stub().returns(githubMock);
winstonMock = {
info: sinon.stub(),
error: sinon.stub()
};

mockery.registerMock('@octokit/rest', githubMockClass);
mockery.registerMock('winston', winstonMock);

// eslint-disable-next-line global-require
GithubScm = require('../');
Expand Down Expand Up @@ -392,6 +398,41 @@ describe('index', function () {
});
});
});

it('catches and discards Github errors when it has a 403 error code', () => {
const err = new Error('Sorry. Your account was suspended.');

err.code = 403;
githubMock.repos.get.yieldsAsync(err);

return scm.getPermissions(config)
.then((result) => {
assert.deepEqual(result, { admin: false, push: false, pull: false });

assert.calledWith(githubMock.repos.getById, {
id: '359478'
});

assert.calledWith(githubMock.repos.get, {
owner: 'screwdriver-cd',
repo: 'models'
});

assert.calledWith(githubMock.authenticate, {
type: 'oauth',
token: config.token
});

assert.calledWith(
winstonMock.info,
"User's account suspended for screwdriver-cd/models, " +
'it will be removed from pipeline admins.'
);
})
.catch(() => {
assert(false, 'Error should be handled if error code is 403');
});
});
});

describe('lookupScmUri', () => {
Expand Down

0 comments on commit d937e66

Please sign in to comment.