Skip to content

Commit

Permalink
feat: add fn to check enterprise user (#224)
Browse files Browse the repository at this point in the history
* feat: add fn to check enterprise user

* Update test/index.test.js

Co-authored-by: Tiffany K <tiffanykyi@gmail.com>

* add gheCloudSlug to schema

* Update index.js

---------

Co-authored-by: ppaul <pritam.paul@yahooinc.com>
Co-authored-by: Tiffany K <tiffanykyi@gmail.com>
  • Loading branch information
3 people authored Mar 25, 2024
1 parent ce44df2 commit 7a87dea
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
40 changes: 39 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const Path = require('path');
const joi = require('joi');
const keygen = require('ssh-keygen');
const schema = require('screwdriver-data-schema');
const ScmGithubGraphQL = require('screwdriver-scm-github-graphql');

const CHECKOUT_URL_REGEX = schema.config.regex.CHECKOUT_URL;
const PR_COMMENTS_REGEX = /^.+pipelines\/(\d+)\/builds.+ ([\w-:]+)$/;
const PR_COMMENTS_KEYWORD_REGEX = /^__(.*)__.*$/;
Expand Down Expand Up @@ -50,6 +52,7 @@ const DEPLOY_KEY_GENERATOR_CONFIG = {
DEPLOY_KEY_TITLE: 'sd@screwdriver.cd'
};
const DEFAULT_BRANCH = 'main';
const ENTERPRISE_USER = 'EnterpriseUserAccount';

/**
* Escape quotes (single quote) for single quote enclosure
Expand Down Expand Up @@ -176,6 +179,9 @@ class GithubScm extends Scm {
* @param {String} config.oauthClientSecret OAuth Client Secret provided by GitHub application
* @param {Object} [config.fusebox={}] Circuit Breaker configuration
* @param {String} config.secret Secret to validate the signature of webhook events
* @param {Boolean} [config.gheCloud=false] Flag set to true if using Github Enterprise Cloud
* @param {Boolean} [config.gheCloudSlug] The Github Enterprise Cloud Slug
* @param {String} config.githubGraphQLUrl GraphQL endpoint for GitHub https://api.github.com/graphql
* @return {GithubScm}
*/
constructor(config = {}) {
Expand Down Expand Up @@ -236,7 +242,16 @@ class GithubScm extends Scm {
oauthClientId: joi.string().required(),
oauthClientSecret: joi.string().required(),
fusebox: joi.object().default({}),
secret: joi.string().required()
secret: joi.string().required(),
gheCloud: joi
.boolean()
.optional()
.default(false),
gheCloudSlug: joi.string().optional(),
githubGraphQLUrl: joi
.string()
.optional()
.default('https://api.github.com/graphql')
})
.unknown(true),
'Invalid config for GitHub'
Expand Down Expand Up @@ -264,6 +279,12 @@ class GithubScm extends Scm {
}
}
});

this.scmGithubGQL = config.gheCloud
? new ScmGithubGraphQL({
graphqlUrl: this.config.githubGraphQLUrl
})
: null;
}

/**
Expand Down Expand Up @@ -1967,6 +1988,23 @@ class GithubScm extends Scm {
throw err;
});
}

/**
* Returns if a user is an enterprise user
* @param {Object} config The configuration object
* @param {String} config.token The token used to authenticate to the SCM
* @param {String} config.slug The slug of the enterprise
* @param {String} config.login The login of the Github user
* @returns Boolean
*/
async _isEnterpriseUser(config) {
if (!this.scmGithubGQL) {
return false;
}
const user = await this.scmGithubGQL.getEnterpriseUserAccount(config);

return !!user && user.type === ENTERPRISE_USER;
}
}

module.exports = GithubScm;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"screwdriver-data-schema": "^23.0.4",
"screwdriver-logger": "^2.0.0",
"screwdriver-scm-base": "^8.1.0",
"screwdriver-scm-github-graphql": "^1.0.0",
"ssh-keygen": "^0.5.0"
},
"release": {
Expand Down
62 changes: 62 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ describe('index', function() {
error: sinon.stub()
};

class GithubGqlMock {
getEnterpriseUserAccount() {}
}

mockery.registerMock('@octokit/rest', githubMockClass);
mockery.registerMock('screwdriver-logger', winstonMock);
mockery.registerMock('screwdriver-scm-github-graphql', GithubGqlMock);

// eslint-disable-next-line global-require
GithubScm = require('..');
Expand Down Expand Up @@ -3354,4 +3359,61 @@ jobs:
});
});
});

describe('_isEnterpriseUser', () => {
const login = 'ai_humanoid';
const slug = 'EAU';
const id = 'EAU_1234567';
const token = 'a_secret_token';
let config;

beforeEach(() => {
scm = new GithubScm({
fusebox: {
retry: {
minTimeout: 1
}
},
readOnly: {},
oauthClientId: 'abcdefg',
oauthClientSecret: 'hijklmno',
secret: 'somesecret',
commentUserToken: 'sometoken',
gheHost: 'github.com',
gheCloud: true,

});

sinon.stub(scm.scmGithubGQL, 'getEnterpriseUserAccount').resolves();
});

it('returns true if user is an enterprise user', async () => {
config = {
login,
slug,
token
};

scm.scmGithubGQL.getEnterpriseUserAccount.resolves({
type: 'EnterpriseUserAccount',
login,
id
})

const result = await scm._isEnterpriseUser(config);

assert.strictEqual(result, true);
assert.calledWith(scm.scmGithubGQL.getEnterpriseUserAccount, config);
})

it('returns false if user is not an enterprise user', async () => {
scm.scmGithubGQL.getEnterpriseUserAccount.resolves(null);
config.login = 'not_an_enterprise_user';

const result = await scm._isEnterpriseUser(config);

assert.strictEqual(result, false);
assert.calledWith(scm.scmGithubGQL.getEnterpriseUserAccount, config);
});
});
});

0 comments on commit 7a87dea

Please sign in to comment.