Skip to content

Commit

Permalink
feat: allow github cloud context (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
pritamstyz4ever authored Aug 23, 2024
1 parent 7a5d0d8 commit 1e28c58
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 57 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ The class has a variety of knobs to tweak when interacting with GitHub.
| config.fusebox | Object | {} | [Circuit Breaker configuration][circuitbreaker] |
| config.secret | String | | Secret to validate the signature of webhook events |
| config.privateRepo | Boolean | false | Request 'repo' scope, which allows read/write access for public & private repos

| config.gheCloud | Boolean | false | Flag set to true if using Github Enterprise Cloud |
| [config.gheCloudSlug] | String | null | The Github Enterprise Cloud Slug |
| [config.gheCloudCookie] | String| null | The Github Enterprise Cloud Cookie name |
| [config.gheCloudContext] | String | null | The Github Enterprise Cloud scm context |
| config.githubGraphQLUrl | String | https://api.github.com/graphql | GraphQL endpoint for GitHub |

```js
const scm = new GithubScm({
oauthClientId: 'abcdef',
Expand Down
85 changes: 34 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class GithubScm extends Scm {
* @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 {Boolean} [config.gheCloudCookie] The Github Enterprise Cloud Cookie name
* @param {Boolean} [config.gheCloudContext] The Github Enterprise Cloud scm context
* @param {String} config.githubGraphQLUrl GraphQL endpoint for GitHub https://api.github.com/graphql
* @return {GithubScm}
*/
Expand All @@ -193,65 +195,33 @@ class GithubScm extends Scm {
joi
.object()
.keys({
privateRepo: joi
.boolean()
.optional()
.default(false),
gheProtocol: joi
.string()
.optional()
.default('https'),
gheHost: joi
.string()
.optional()
.description('GitHub Enterpise host'),
username: joi
.string()
.optional()
.default('sd-buildbot'),
email: joi
.string()
.optional()
.default('[email protected]'),
commentUserToken: joi
.string()
.optional()
.description('Token for PR comments'),
autoDeployKeyGeneration: joi
.boolean()
.optional()
.default(false),
privateRepo: joi.boolean().optional().default(false),
gheProtocol: joi.string().optional().default('https'),
gheHost: joi.string().optional().description('GitHub Enterprise host'),
username: joi.string().optional().default('sd-buildbot'),
email: joi.string().optional().default('[email protected]'),
commentUserToken: joi.string().optional().description('Token for PR comments'),
autoDeployKeyGeneration: joi.boolean().optional().default(false),
readOnly: joi
.object()
.keys({
enabled: joi.boolean().optional(),
username: joi.string().optional(),
accessToken: joi.string().optional(),
cloneType: joi
.string()
.valid('https', 'ssh')
.optional()
.default('https')
cloneType: joi.string().valid('https', 'ssh').optional().default('https')
})
.optional()
.default({}),
https: joi
.boolean()
.optional()
.default(false),
https: joi.boolean().optional().default(false),
oauthClientId: joi.string().required(),
oauthClientSecret: joi.string().required(),
fusebox: joi.object().default({}),
secret: joi.string().required(),
gheCloud: joi
.boolean()
.optional()
.default(false),
gheCloud: joi.boolean().optional().default(false),
gheCloudSlug: joi.string().optional(),
githubGraphQLUrl: joi
.string()
.optional()
.default('https://api.github.com/graphql')
gheCloudCookie: joi.string().optional(),
gheCloudContext: joi.string().optional(),
githubGraphQLUrl: joi.string().optional().default('https://api.github.com/graphql')
})
.unknown(true),
'Invalid config for GitHub'
Expand Down Expand Up @@ -786,10 +756,12 @@ class GithubScm extends Scm {
// Git clone
command.push(`echo 'Cloning external config repo ${parentCheckoutUrl}'`);
command.push(
`${'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; ' +
`${
'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; ' +
'then $SD_GIT_WRAPPER ' +
`"git clone --recursive --quiet --progress --branch '${escapedParentBranch}' ` +
'$CONFIG_URL $SD_CONFIG_DIR"; '}${shallowCloneCmd}` +
'$CONFIG_URL $SD_CONFIG_DIR"; '
}${shallowCloneCmd}` +
`--recursive --quiet --progress --branch '${escapedParentBranch}' ` +
'$CONFIG_URL $SD_CONFIG_DIR"; fi'
);
Expand Down Expand Up @@ -844,10 +816,12 @@ class GithubScm extends Scm {
// Git clone
command.push(`echo 'Cloning ${checkoutUrl}, on branch ${singleQuoteEscapedBranch}'`);
command.push(
`${'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; ' +
`${
'if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; ' +
'then $SD_GIT_WRAPPER ' +
`"git clone --recursive --quiet --progress --branch '${doubleQuoteEscapedBranch}' ` +
'$SCM_URL $SD_CHECKOUT_DIR_FINAL"; '}${shallowCloneCmd}` +
'$SCM_URL $SD_CHECKOUT_DIR_FINAL"; '
}${shallowCloneCmd}` +
`--recursive --quiet --progress --branch '${doubleQuoteEscapedBranch}' ` +
'$SCM_URL $SD_CHECKOUT_DIR_FINAL"; fi'
);
Expand Down Expand Up @@ -1709,7 +1683,12 @@ class GithubScm extends Scm {
const scmContexts = this._getScmContexts();
const scmContext = scmContexts[0];
const scope = ['admin:repo_hook', 'read:org', 'repo:status'];
const cookie = this.config.gheHost ? `github-${this.config.gheHost}` : 'github-github.com';
let cookie = this.config.gheHost ? `github-${this.config.gheHost}` : 'github-github.com';

if (this.config.gheCloudCookie) {
cookie = this.config.gheCloudCookie;
}

const bellConfig = {
provider: 'github',
cookie,
Expand Down Expand Up @@ -1886,7 +1865,11 @@ class GithubScm extends Scm {
* @return {Array} Array of scm contexts
*/
_getScmContexts() {
const contextName = this.config.gheHost ? [`github:${this.config.gheHost}`] : ['github:github.com'];
let contextName = this.config.gheHost ? [`github:${this.config.gheHost}`] : ['github:github.com'];

if (this.config.gheCloudContext) {
contextName = [this.config.gheCloudContext];
}

return contextName;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"devDependencies": {
"chai": "^4.3.6",
"eslint": "^7.32.0",
"eslint-config-screwdriver": "^5.0.1",
"eslint-config-screwdriver": "^7.0.0",
"mocha": "^8.4.0",
"mocha-multi-reporters": "^1.5.1",
"mocha-sonarqube-reporter": "^1.0.2",
Expand Down
48 changes: 44 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ sinon.assert.expose(assert, {
prefix: ''
});

describe('index', function() {
describe('index', function () {
// Time not important. Only life important
this.timeout(5000);

Expand Down Expand Up @@ -590,9 +590,7 @@ describe('index', function() {
});

it('throws error when getRef API returned unexpected type', () => {
const type = Math.random()
.toString(36)
.slice(-8);
const type = Math.random().toString(36).slice(-8);
const err = `Cannot handle ${type} type`;

githubMock.git.getRef.resolves({ data: { object: { sha: tagSha, type } } });
Expand Down Expand Up @@ -2666,6 +2664,32 @@ jobs:
});
});

it('returns configuration for github enterprise cloud', () => {
scm = new GithubScm({
oauthClientId: 'abcdefg',
oauthClientSecret: 'defghijk',
gheCloud: true,
gheCloudSlug: 'ghe-slug',
gheCloudCookie: 'github-example-github.com',
gheCloudContext: 'github:example.github.com',
secret: 'somesecret'
});

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

it('add repo scope to support private repo', () => {
scm = new GithubScm({
oauthClientId: 'abcdefg',
Expand Down Expand Up @@ -3386,6 +3410,22 @@ jobs:

return assert.deepEqual(result, ['github:github.screwdriver.cd']);
});

it('returns a scmContext for github enterprise cloud', () => {
scm = new GithubScm({
oauthClientId: 'abcdefg',
oauthClientSecret: 'hijklmno',
gheCloud: true,
gheCloudSlug: 'ghe-slug',
gheCloudCookie: 'github-example-github.com',
gheCloudContext: 'github:example.github.com',
secret: 'somesecret'
});

const result = scm.getScmContexts();

return assert.deepEqual(result, ['github:example.github.com']);
});
});

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

0 comments on commit 1e28c58

Please sign in to comment.