Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
wip(octokit): replaced the related PR fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Mar 3, 2018
1 parent 47c3852 commit 3ad2b85
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 91 deletions.
29 changes: 8 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"sinon": "4.4.2"
},
"dependencies": {
"@octokit/rest": "14.0.8",
"boom": "7.2.0",
"btoa": "1.1.2",
"delay": "2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default {
'delay',
'joi',
'hoek',
'boom'
'boom',
'@octokit/rest'
],
plugins: [
nodeResolve({
Expand Down
20 changes: 16 additions & 4 deletions src/github/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {minutes} from 'milliseconds';
import octokitFactory from './octokit-factory-wrapper';
import clientFactory from './request-methods';
import poll from './poller';
import {
Expand All @@ -14,6 +15,9 @@ function determineMergeMethodFrom(acceptAction, squash) {
}

export default function (githubCredentials) {
const octokit = octokitFactory();
const {token} = githubCredentials;
octokit.authenticate({type: 'token', token});
const {get, post, put, del} = clientFactory(githubCredentials);

function ensureAcceptability({repo, ref, url, pollWhenPending}, log, timeout = minutes(1)) {
Expand Down Expand Up @@ -77,16 +81,24 @@ export default function (githubCredentials) {
return post(url, {body: `:x: greenkeeper-keeper failed to merge the pull-request \n> ${error.message}`});
}

function getPullRequestsForCommit({repo, ref}) {
return get(`https://api.github.com/repos/${repo.full_name}/pulls?head=${repo.owner.login}:${ref}`)
.then(response => response.body);
async function getPullRequestsForCommit({ref}) {
const response = await octokit.search.issues({q: `${ref}+type:pr`});

return response.data.items;
}

async function getPullRequest(repository, number) {
const response = await octokit.pullRequests.get({owner: repository.owner.login, repo: repository.name, number});

return response.data;
}

return {
ensureAcceptability,
acceptPR,
deleteBranch,
postErrorComment,
getPullRequestsForCommit
getPullRequestsForCommit,
getPullRequest
};
}
3 changes: 3 additions & 0 deletions src/github/octokit-factory-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import octokitFactory from '@octokit/rest';

export default octokitFactory;
14 changes: 7 additions & 7 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function successfulStatusCouldBeForGreenkeeperPR(event, state, branches) {
return 'status' === event && 'success' === state && 1 === branches.length && 'master' !== branches[0].name;
}

export default function (request, reply, settings) {
const {action, sender, state, repository, branches} = request.payload;
export default async function (request, reply, settings) {
const {action, sender, state, repository, pull_request, branches} = request.payload; // eslint-disable-line camelcase
const event = request.headers['x-github-event'];

if ('ping' === event) {
Expand All @@ -31,19 +31,19 @@ export default function (request, reply, settings) {
if (isValidGreenkeeperUpdate({event, action, sender})) {
reply('ok').code(ACCEPTED);

return process(request, request.payload.pull_request, {...settings, pollWhenPending: true});
return process(request, pull_request, {...settings, pollWhenPending: true});
}

if (successfulStatusCouldBeForGreenkeeperPR(event, state, branches)) {
const {getPullRequestsForCommit} = createActions(settings.github);
const {getPullRequestsForCommit, getPullRequest} = createActions(settings.github);

return getPullRequestsForCommit({repo: repository, ref: branches[0].name})
.then(pullRequests => {
return getPullRequestsForCommit({ref: branches[0].name})
.then(async pullRequests => {
if (!pullRequests.length) reply('no PRs for this commit').code(BAD_REQUEST);
else if (1 < pullRequests.length) reply(boom.internal('too many PRs exist for this commit'));
else if (openedByGreenkeeperBot(pullRequests[0].user.html_url)) {
reply('ok').code(ACCEPTED);
process(request, pullRequests[0], settings);
process(request, await getPullRequest(repository, pullRequests[0].number), settings);
} else reply('PR is not from greenkeeper').code(BAD_REQUEST);
})
.catch(e => reply(boom.internal('failed to fetch PRs', e)));
Expand Down
53 changes: 34 additions & 19 deletions test/integration/features/step_definitions/github-api-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import nock from 'nock';
import any from '@travi/any';
import {OK, METHOD_NOT_ALLOWED, INTERNAL_SERVER_ERROR} from 'http-status-codes';
import {World} from '../support/world';
import {GREENKEEPER_INTEGRATION_GITHUB_URL} from '../../../../src/greenkeeper';

const debug = require('debug')('test');

Expand Down Expand Up @@ -40,38 +41,52 @@ defineSupportCode(({Before, After, Given, setWorldConstructor}) => {
});

Given('an open PR exists for the commit', function (callback) {
if (GREENKEEPER_INTEGRATION_GITHUB_URL === this.prSender) {
githubScope
.matchHeader('Authorization', authorizationHeader)
.get(`/repos/${this.repoFullName}/pulls/${this.prNumber}`)
.reply(OK, {
url: 'https://api.github.com/123',
user: {html_url: this.prSender || any.url()},
number: this.prNumber,
head: {
sha: this.sha,
ref: this.ref,
repo: {
full_name: this.repoFullName,
name: 'temp-name',
owner: {login: this.repoOwner}
}
}
});
}
githubScope
.matchHeader('Authorization', authorizationHeader)
.get(`/repos/${this.repo}/pulls?head=${this.repoOwner}:${this.commitBranches[0]}`)
.reply(OK, [{
url: 'https://api.github.com/123',
user: {html_url: this.prSender || any.url()},
number: this.prNumber,
head: {
sha: this.sha,
ref: this.ref,
repo: {
full_name: this.repo
}
}
}]);
.get(`/search/issues?q=${this.commitBranches[0]}+type%3Apr`)
.reply(OK, {
items: [{
url: 'https://api.github.com/123',
user: {html_url: this.prSender || any.url()},
number: this.prNumber
}]
});

callback();
});

Given('no open PRs exist for the commit', function (callback) {
githubScope
.matchHeader('Authorization', authorizationHeader)
.get(`/repos/${this.repo}/pulls?head=${this.repoOwner}:${this.commitBranches[0]}`)
.reply(OK, []);
.get(`/search/issues?q=${this.commitBranches[0]}+type%3Apr`)
.reply(OK, {items: []});

callback();
});

Given(/^statuses exist for the PR$/, function (callback) {
githubScope
.matchHeader('Authorization', authorizationHeader)
.get(`/repos/${this.repo}/commits/${this.ref}/status`)
.get(`/repos/${this.repoFullName}/commits/${this.ref}/status`)
.reply(OK, {
state: 'success'
});
Expand Down Expand Up @@ -121,7 +136,7 @@ defineSupportCode(({Before, After, Given, setWorldConstructor}) => {
this.comments = `/${any.word()}`;
githubScope
.matchHeader('Authorization', authorizationHeader)
.get(`/repos/${this.repo}/commits/${this.ref}/status`)
.get(`/repos/${this.repoFullName}/commits/${this.ref}/status`)
.reply(OK, {
state: status
});
Expand All @@ -148,7 +163,7 @@ defineSupportCode(({Before, After, Given, setWorldConstructor}) => {
this.prProcessed = new Promise(resolve => {
githubScope
.matchHeader('Authorization', authorizationHeader)
.delete(`/repos/${this.repo}/git/refs/heads/${this.ref}`)
.delete(`/repos/${this.repoFullName}/git/refs/heads/${this.ref}`)
.reply(OK, resolve);
});

Expand All @@ -159,7 +174,7 @@ defineSupportCode(({Before, After, Given, setWorldConstructor}) => {
this.comments = `/${any.word()}`;
githubScope
.matchHeader('Authorization', authorizationHeader)
.delete(`/repos/${this.repo}/git/refs/heads/${this.ref}`)
.delete(`/repos/${this.repoFullName}/git/refs/heads/${this.ref}`)
.reply(INTERNAL_SERVER_ERROR, {});
stubTheCommentsEndpoint.call(this, githubScope, authorizationHeader);

Expand Down
7 changes: 1 addition & 6 deletions test/integration/features/step_definitions/webhook-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import {World} from '../support/world';
defineSupportCode(({Given, Then, setWorldConstructor}) => {
setWorldConstructor(World);

Given(/^the webhook is for a (.*) event and a (.*) action$/, function (
event,
action,
callback
) {
Given(/^the webhook is for a (.*) event and a (.*) action$/, function (event, action, callback) {
this.webhookAction = action;
this.webhookEventName = event;

Expand All @@ -21,7 +17,6 @@ defineSupportCode(({Given, Then, setWorldConstructor}) => {
Given(/^the webhook is for a status event and a (.*) state$/, function (state, callback) {
this.webhookEventName = 'status';
this.statusEventState = state;
this.repoOwner = any.word();

callback();
});
Expand Down
Loading

0 comments on commit 3ad2b85

Please sign in to comment.