Skip to content

Commit

Permalink
feat: Update octokit to 16.x. BREAKING CHANGE: updating major versions (
Browse files Browse the repository at this point in the history
  • Loading branch information
tk3fftk authored and tkyi committed Mar 6, 2019
1 parent ea9c9c0 commit dbdb57e
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 331 deletions.
72 changes: 42 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,25 @@ class GithubScm extends Scm {
* @param {String} options.token Github token used for authentication of requests
* @param {Object} options.params Parameters to run with
* @param {String} [options.scopeType] Type of request to make. Default is 'repos'
* @param {String} [options.route] Route for octokit.request()
* @param {Function} callback Callback function from github API
*/
_githubCommand(options, callback) {
this.github.authenticate({
type: 'oauth',
token: options.token
});
const config = Object.assign({ auth: `token ${options.token}` }, this.octokitConfig);
const octokit = new Octokit(config);
const scopeType = options.scopeType || 'repos';

this.github[scopeType][options.action](options.params, callback);
if (scopeType === 'request') {
// for deprecation of 'octokit.repos.getById({id})'
// ref: https://github.com/octokit/rest.js/releases/tag/v16.0.1
octokit[scopeType](options.route, options.params).then(function () { // Use "function" (not "arrow function") for getting "arguments"
callback(null, ...arguments);
}).catch(err => callback(err));
} else {
octokit[scopeType][options.action](options.params).then(function () {
callback(null, ...arguments);
}).catch(err => callback(err));
}
}

/**
Expand Down Expand Up @@ -114,17 +123,17 @@ class GithubScm extends Scm {
secret: joi.string().required()
}).unknown(true), 'Invalid config for GitHub');

const githubConfig = {};
this.octokitConfig = {};

if (this.config.gheHost) {
githubConfig.baseUrl = `${this.config.gheProtocol}://${this.config.gheHost}/api/v3`;
this.octokitConfig.baseUrl =
`${this.config.gheProtocol}://${this.config.gheHost}/api/v3`;
}
this.github = new Octokit(githubConfig);

// eslint-disable-next-line no-underscore-dangle
this.breaker = new Breaker(this._githubCommand.bind(this), {
// Do not retry when there is a 4XX error
shouldRetry: err => err && !(err.code >= 400 && err.code < 500),
shouldRetry: err => err && err.status && !(err.status >= 400 && err.status < 500),
retry: this.config.fusebox.retry,
breaker: this.config.fusebox.breaker
});
Expand All @@ -134,9 +143,10 @@ class GithubScm extends Scm {
* Look up a repo by SCM URI
* @async lookupScmUri
* @param {Object} config
* @param {Object} config.scmUri The SCM URI to look up relevant info
* @param {Object} config.token Service token to authenticate with Github
* @return {Promise} Resolves to an object containing repository-related information
* @param {Object} config.scmUri The SCM URI to look up relevant info
* @param {Object} config.scmRepo The SCM repository to look up
* @param {Object} config.token Service token to authenticate with Github
* @return {Promise} Resolves to an object containing repository-related information
*/
async lookupScmUri(config) {
const [scmHost, scmId, scmBranch] = config.scmUri.split(':');
Expand All @@ -148,7 +158,8 @@ class GithubScm extends Scm {
} else {
try {
const repo = await this.breaker.runCommand({
action: 'getById',
scopeType: 'request',
route: 'GET /repositories/:id',
token: config.token,
params: { id: scmId }
});
Expand Down Expand Up @@ -183,7 +194,7 @@ class GithubScm extends Scm {
async _findWebhook(config) {
try {
const hooks = await this.breaker.runCommand({
action: 'getHooks',
action: 'listHooks',
token: config.token,
params: {
owner: config.scmInfo.owner,
Expand Down Expand Up @@ -236,7 +247,7 @@ class GithubScm extends Scm {
};

if (config.hookInfo) {
action = 'editHook';
action = 'updateHook';
Object.assign(params, { hook_id: config.hookInfo.id });
}

Expand Down Expand Up @@ -464,8 +475,8 @@ class GithubScm extends Scm {

try {
const pullRequests = await this.breaker.runCommand({
action: 'getAll',
scopeType: 'pullRequests',
action: 'list',
scopeType: 'pulls',
token: config.token,
params: {
owner: scmInfo.owner,
Expand Down Expand Up @@ -493,9 +504,10 @@ class GithubScm extends Scm {
* Get an owner's permissions on a repository
* @async _getPermissions
* @param {Object} config
* @param {String} config.scmUri The scmUri to get permissions on
* @param {String} config.token The token used to authenticate to the SCM
* @return {Promise} Resolves to the owner's repository permissions
* @param {String} config.scmUri The scmUri to get permissions on
* @param {Object} config.scmRepo The SCM repo to look up
* @param {String} config.token The token used to authenticate to the SCM
* @return {Promise} Resolves to the owner's repository permissions
*/
async _getPermissions(config) {
const lookupConfig = {
Expand Down Expand Up @@ -553,8 +565,8 @@ class GithubScm extends Scm {

try {
const permission = await this.breaker.runCommand({
action: 'getOrgMembership',
scopeType: 'users',
action: 'getMembershipForAuthenticatedUser',
scopeType: 'orgs',
token: config.token,
params: {
org: config.organization
Expand Down Expand Up @@ -661,7 +673,7 @@ class GithubScm extends Scm {

return status ? status.data : undefined;
} catch (err) {
if (err.code !== 422) {
if (err.status !== 422) {
winston.error('Failed to updateCommitStatus: ', err);
throw err;
}
Expand Down Expand Up @@ -694,7 +706,7 @@ class GithubScm extends Scm {

try {
const file = await this.breaker.runCommand({
action: 'getContent',
action: 'getContents',
token: config.token,
params: {
owner: scmInfo.owner,
Expand Down Expand Up @@ -749,7 +761,7 @@ class GithubScm extends Scm {

return repo.data.id;
} catch (err) {
if (err.code === 404) {
if (err.status === 404) {
throw new Error(`Cannot find repository ${checkoutUrl}`);
}

Expand All @@ -769,7 +781,7 @@ class GithubScm extends Scm {
async _decorateAuthor(config) {
try {
const user = await this.breaker.runCommand({
action: 'getForUser',
action: 'getByUsername',
scopeType: 'users',
token: config.token,
params: { username: config.username }
Expand Down Expand Up @@ -901,8 +913,8 @@ class GithubScm extends Scm {
if (type === 'pr') {
try {
const files = await this.breaker.runCommand({
action: 'getFiles',
scopeType: 'pullRequests',
action: 'listFiles',
scopeType: 'pulls',
token,
params: {
owner: hoek.reach(payload, 'repository.owner.login'),
Expand Down Expand Up @@ -1088,7 +1100,7 @@ class GithubScm extends Scm {
try {
const pullRequestInfo = await this.breaker.runCommand({
action: 'get',
scopeType: 'pullRequests',
scopeType: 'pulls',
token: config.token,
params: {
number: config.prNum,
Expand Down Expand Up @@ -1204,7 +1216,7 @@ class GithubScm extends Scm {
async _findBranches(config) {
try {
let branches = await this.breaker.runCommand({
action: 'getBranches',
action: 'listBranches',
token: config.token,
params: {
owner: config.scmInfo.owner,
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
"chai": "^3.5.0",
"eslint": "^4.19.1",
"eslint-config-screwdriver": "^3.0.1",
"jenkins-mocha": "^6.0.0",
"jenkins-mocha": "^7.0.0",
"mockery": "^2.0.0",
"sinon": "^1.17.5"
"sinon": "^7.2.7"
},
"dependencies": {
"@octokit/rest": "^15.18.1",
"@octokit/rest": "^16.16.3",
"circuit-fuses": "^2.0.3",
"hoek": "^5.0.4",
"hoek": "^6.1.2",
"joi": "^13.7.0",
"screwdriver-data-schema": "^18.39.1",
"screwdriver-scm-base": "^5.1.0",
Expand Down
Loading

0 comments on commit dbdb57e

Please sign in to comment.