Skip to content

Commit

Permalink
feat: add scopes for GQL query (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
pritamstyz4ever authored Mar 28, 2024
1 parent 34a406c commit 40d836f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,12 @@ class GithubScm extends Scm {
};
}

if (this.config.gheCloud) {
const oauthScope = bellConfig.scope;

bellConfig.scope = oauthScope.concat('read:enterprise', 'read:user');
}

return { [scmContext]: bellConfig };
}

Expand Down Expand Up @@ -1993,14 +1999,16 @@ class GithubScm extends Scm {
* 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;
}

config.slug = this.config.gheCloudSlug;

const user = await this.scmGithubGQL.getEnterpriseUserAccount(config);

return !!user && user.type === ENTERPRISE_USER;
Expand Down
36 changes: 33 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2507,6 +2507,30 @@ jobs:
});
});
});

it('adds enterprise scope to support GraphQL queries', () => {
scm = new GithubScm({
oauthClientId: 'abcdefg',
oauthClientSecret: 'hijklmno',
secret: 'somesecret',
gheCloud: true,
gheCloudSlug: 'ghe-slug'
});

return scm.getBellConfiguration().then(config => {
assert.deepEqual(config, {
'github:github.com': {
clientId: 'abcdefg',
clientSecret: 'hijklmno',
forceHttps: false,
isSecure: false,
provider: 'github',
cookie: 'github-github.com',
scope: ['admin:repo_hook', 'read:org', 'repo:status', 'read:enterprise', 'read:user']
}
});
});
});
});

describe('addWebhook', () => {
Expand Down Expand Up @@ -3381,6 +3405,7 @@ jobs:
commentUserToken: 'sometoken',
gheHost: 'github.com',
gheCloud: true,
gheCloudSlug: slug

});

Expand All @@ -3390,7 +3415,6 @@ jobs:
it('returns true if user is an enterprise user', async () => {
config = {
login,
slug,
token
};

Expand All @@ -3403,7 +3427,10 @@ jobs:
const result = await scm._isEnterpriseUser(config);

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

it('returns false if user is not an enterprise user', async () => {
Expand All @@ -3413,7 +3440,10 @@ jobs:
const result = await scm._isEnterpriseUser(config);

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

0 comments on commit 40d836f

Please sign in to comment.