Skip to content

Commit

Permalink
fix(1319): add getOrgPermissions function (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
minzcmu authored Oct 30, 2018
1 parent 4b3fa27 commit f689aa6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
41 changes: 41 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,47 @@ class GithubScm extends Scm {
}
}

/**
* Get a users permissions on an organization
* @method getOrgPermissions
* @param {Object} config Configuration
* @param {String} config.organization The organization to get permissions on
* @param {String} config.username The user to check against
* @param {String} config.token The token used to authenticate to the SCM
* @param {String} [config.scmContext] The scm context name
* @return {Promise}
*/
async _getOrgPermissions(config) {
const result = {
admin: false,
member: false
};

try {
const permission = await this.breaker.runCommand({
action: 'getOrgMembership',
scopeType: 'users',
token: config.token,
params: {
org: config.organization
}
});
const role = permission.data.role;
const state = permission.data.state;

if (state !== 'active') {
return result;
}

result[role] = true;

return result;
} catch (err) {
winston.error(err);
throw err;
}
}

/**
* Get a commit sha for a specific repo#branch or pull request
* @async _getCommitSha
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^4.6.0",
"eslint-config-screwdriver": "^3.0.0",
"jenkins-mocha": "^4.0.0",
"eslint-config-screwdriver": "^3.0.1",
"jenkins-mocha": "^6.0.0",
"mockery": "^2.0.0",
"sinon": "^1.17.5"
},
Expand All @@ -44,8 +44,8 @@
"circuit-fuses": "^2.0.3",
"hoek": "^5.0.3",
"joi": "^13.4.0",
"screwdriver-data-schema": "^18.29.2",
"screwdriver-scm-base": "^4.2.0",
"screwdriver-data-schema": "^18.33.5",
"screwdriver-scm-base": "^4.4.3",
"winston": "^2.4.2"
},
"release": {
Expand Down
64 changes: 63 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ describe('index', function () {
getBranches: sinon.stub()
},
users: {
getForUser: sinon.stub()
getForUser: sinon.stub(),
getOrgMembership: sinon.stub()
}
};
githubMockClass = sinon.stub().returns(githubMock);
Expand Down Expand Up @@ -461,6 +462,67 @@ describe('index', function () {
});
});

describe('getOrgPermissions', () => {
const permission = {
role: 'admin',
state: 'active'
};
const result = {
admin: true,
member: false
};
const config = {
organization: 'screwdriver-cd',
username: 'foo',
token: 'somerandomtoken'
};

beforeEach(() => {
githubMock.users.getOrgMembership.yieldsAsync(null, { data: permission });
});

it('promises to get organization permissions', () => {
githubMock.users.getOrgMembership.yieldsAsync(null, { data: permission });

return scm.getOrgPermissions(config)
.then((data) => {
assert.deepEqual(data, result);

assert.calledWith(githubMock.users.getOrgMembership, {
org: config.organization
});

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

it('returns an error when github command fails', () => {
const err = new Error('githubError');

githubMock.users.getOrgMembership.yieldsAsync(err);

return scm.getOrgPermissions(config)
.then(() => {
assert.fail('This should not fail the test');
})
.catch((error) => {
assert.deepEqual(error, err);

assert.calledWith(githubMock.users.getOrgMembership, {
org: config.organization
});

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

describe('lookupScmUri', () => {
const scmUri = 'github.com:23498:targetBranch';

Expand Down

0 comments on commit f689aa6

Please sign in to comment.