@dog-ai/github-wrapper
Advanced tools
Comparing version 1.2.5 to 1.2.6
'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
@@ -9,2 +11,4 @@ | ||
const EventEmitter = require('events'); | ||
const _ = require('lodash'); | ||
@@ -18,24 +22,22 @@ | ||
const parallelLimit = (() => { | ||
var _ref = _asyncToGenerator(function* (funcList, limit = 1) { | ||
let inFlight = new Set(); | ||
const parallel = (() => { | ||
var _ref = _asyncToGenerator(function* (functions, parallelism = 1) { | ||
const queue = new Set(); | ||
return funcList.map((() => { | ||
var _ref2 = _asyncToGenerator(function* (func, i) { | ||
// Hold the loop by another loop | ||
// while the next promise resolves | ||
while (inFlight.size >= limit) { | ||
return functions.map((() => { | ||
var _ref2 = _asyncToGenerator(function* (f) { | ||
while (queue.size >= parallelism) { | ||
// eslint-disable-next-line promise/no-native | ||
yield Promise.race(inFlight); | ||
yield Promise.race(queue); | ||
} | ||
const promise = func(); | ||
// Add promise to inFlight Set | ||
inFlight.add(promise); | ||
// Delete promise from Set when it is done | ||
const promise = f(); | ||
queue.add(promise); | ||
yield promise; | ||
inFlight.delete(promise); | ||
queue.delete(promise); | ||
}); | ||
return function (_x2, _x3) { | ||
return function (_x2) { | ||
return _ref2.apply(this, arguments); | ||
@@ -46,3 +48,3 @@ }; | ||
return function parallelLimit(_x) { | ||
return function parallel(_x) { | ||
return _ref.apply(this, arguments); | ||
@@ -52,19 +54,50 @@ }; | ||
const isGreenkeeperPull = pull => { | ||
return pull.user.login === 'greenkeeper[bot]' && !!_.find(pull.combinedStatus.statuses, { | ||
context: 'greenkeeper/verify', | ||
state: 'success' | ||
}); | ||
}; | ||
const isGreenkeeperPullUpdatedByOwner = (pull, owner) => { | ||
return pull.user.login === owner && !!_.find(pull.labels, { name: 'greenkeeper' }); | ||
}; | ||
const findGreenkeeperCommentAboutLatestVersion = comments => { | ||
if (_.isEmpty(comments)) { | ||
return; | ||
} | ||
const latestComment = _.last(_.filter(comments, { user: { login: 'greenkeeper[bot]' } })); | ||
if (!latestComment) { | ||
return; | ||
} | ||
const encodedHead = _.last(/\.\.\.[\w-]+:(.*)\)/.exec(latestComment.body)); | ||
if (!encodedHead) { | ||
return; | ||
} | ||
return decodeURIComponent(encodedHead); | ||
}; | ||
const mergeGreenkeeperPullRequest = (() => { | ||
var _ref3 = _asyncToGenerator(function* (owner, repo, pull) { | ||
const isGreenkeeper = pull.user.login === 'greenkeeper[bot]'; | ||
if (!(isGreenkeeperPull(pull) || isGreenkeeperPullUpdatedByOwner(pull, owner))) { | ||
return; | ||
} | ||
const isSuccess = pull.combinedStatus.state === 'success'; | ||
const isVerified = _.find(pull.combinedStatus.statuses, { context: 'greenkeeper/verify', state: 'success' }); | ||
if (isGreenkeeper && isVerified && isSuccess) { | ||
const number = pull.number; | ||
const sha = pull.head.sha; | ||
const url = pull.url; | ||
if (isSuccess) { | ||
yield this.mergePullRequest(owner, repo, pull.number, pull.head.sha); | ||
} | ||
try { | ||
yield this.mergePullRequest(owner, repo, number, sha); | ||
if (!isSuccess && isGreenkeeperPull(pull)) { | ||
const comments = yield this.getPullRequestComments(owner, repo, pull.number); | ||
const head = findGreenkeeperCommentAboutLatestVersion(comments); | ||
return { owner, repo, url, number, sha, success: true }; | ||
} catch (error) { | ||
return { owner, repo, url, number, sha, success: false, error }; | ||
if (head) { | ||
yield updateGreenkeeperPullRequestWithLatestVersion.bind(this)(owner, repo, pull.number, head); | ||
} | ||
@@ -74,3 +107,3 @@ } | ||
return function mergeGreenkeeperPullRequest(_x4, _x5, _x6) { | ||
return function mergeGreenkeeperPullRequest(_x3, _x4, _x5) { | ||
return _ref3.apply(this, arguments); | ||
@@ -84,14 +117,12 @@ }; | ||
const mergedPulls = []; | ||
const handlePull = (() => { | ||
var _ref5 = _asyncToGenerator(function* (repo, pull) { | ||
const mergedPull = yield mergeGreenkeeperPullRequest.bind(_this)(owner, repo, pull); | ||
if (mergedPull) { | ||
mergedPulls.push(mergedPull); | ||
try { | ||
yield mergeGreenkeeperPullRequest.bind(_this)(owner, repo, pull); | ||
} catch (error) { | ||
_this.emit('error', error); | ||
} | ||
}); | ||
return function handlePull(_x10, _x11) { | ||
return function handlePull(_x9, _x10) { | ||
return _ref5.apply(this, arguments); | ||
@@ -105,3 +136,3 @@ }; | ||
const promises = yield parallelLimit(pulls.map(function (pull) { | ||
const promises = yield parallel(pulls.map(function (pull) { | ||
return handlePull.bind(_this, repo, pull); | ||
@@ -114,3 +145,3 @@ }), pullConcurrency); | ||
return function handleRepo(_x12) { | ||
return function handleRepo(_x11) { | ||
return _ref6.apply(this, arguments); | ||
@@ -120,3 +151,3 @@ }; | ||
const promises = yield parallelLimit(repos.map(function (repo) { | ||
const promises = yield parallel(repos.map(function (repo) { | ||
return handleRepo.bind(_this, repo); | ||
@@ -126,7 +157,5 @@ }), repoConcurrency); | ||
yield Promise.all(promises); | ||
return { mergedPulls }; | ||
}); | ||
return function mergeGreenkeeperPullRequests(_x7, _x8, _x9) { | ||
return function mergeGreenkeeperPullRequests(_x6, _x7, _x8) { | ||
return _ref4.apply(this, arguments); | ||
@@ -136,4 +165,43 @@ }; | ||
class GitHubWrapper { | ||
const updateGreenkeeperPullRequest = (() => { | ||
var _ref7 = _asyncToGenerator(function* (owner, repo, title, head, number) { | ||
const pull = yield this.createPullRequest(owner, repo, title, head, 'master'); | ||
try { | ||
yield this._octokit.issues.addLabels({ owner, repo, issue_number: pull.number, labels: ['greenkeeper'] }); | ||
yield this.closePullRequest(owner, repo, number); | ||
} catch (error) { | ||
yield this.closePullRequest(owner, repo, pull.number); | ||
} | ||
return pull; | ||
}); | ||
return function updateGreenkeeperPullRequest(_x12, _x13, _x14, _x15, _x16) { | ||
return _ref7.apply(this, arguments); | ||
}; | ||
})(); | ||
const updateGreenkeeperPullRequestWithLatestVersion = (() => { | ||
var _ref8 = _asyncToGenerator(function* (owner, repo, number, head) { | ||
var _$exec = /(\w+)\/(.*)-(\d.+)/.exec(head), | ||
_$exec2 = _slicedToArray(_$exec, 4); | ||
const dependency = _$exec2[2], | ||
version = _$exec2[3]; | ||
const title = `Update ${dependency} to ${version}`; | ||
return updateGreenkeeperPullRequest.bind(this)(owner, repo, title, head, number); | ||
}); | ||
return function updateGreenkeeperPullRequestWithLatestVersion(_x17, _x18, _x19, _x20) { | ||
return _ref8.apply(this, arguments); | ||
}; | ||
})(); | ||
class GitHubWrapper extends EventEmitter { | ||
constructor(options = {}) { | ||
super(); | ||
this._options = _.defaultsDeep({}, options, defaultOptions); | ||
@@ -152,5 +220,5 @@ | ||
return _asyncToGenerator(function* () { | ||
var _ref7 = yield _this2._octokit.users.getAuthenticated(); | ||
var _ref9 = yield _this2._octokit.users.getAuthenticated(); | ||
const login = _ref7.data.login; | ||
const login = _ref9.data.login; | ||
@@ -216,5 +284,5 @@ | ||
var _ref8 = yield _this6._octokit.repos.getCombinedStatusForRef({ owner, repo, ref }); | ||
var _ref10 = yield _this6._octokit.repos.getCombinedStatusForRef({ owner, repo, ref }); | ||
const data = _ref8.data; | ||
const data = _ref10.data; | ||
@@ -242,33 +310,84 @@ pull.combinedStatus = data; | ||
mergePullRequest(owner, repo, number, sha) { | ||
createPullRequest(owner, repo, title, head, base) { | ||
var _this7 = this; | ||
return _asyncToGenerator(function* () { | ||
return _this7._octokit.pulls.merge({ owner, repo, pull_number: number, sha }); | ||
try { | ||
var _ref11 = yield _this7._octokit.pulls.create({ owner, repo, title, head, base }); | ||
const data = _ref11.data; | ||
_this7.emit('pulls:create', owner, repo, title, head, base); | ||
return data; | ||
} catch (error) { | ||
_this7.emit('pulls:create:error', error, owner, repo, title, head, base); | ||
throw error; | ||
} | ||
})(); | ||
} | ||
mergeGreenkeeperPullRequests(org, options = { repoConcurrency: 1, pullConcurrency: 1 }) { | ||
closePullRequest(owner, repo, number) { | ||
var _this8 = this; | ||
return _asyncToGenerator(function* () { | ||
const repos = !org ? yield _this8.getUserRepos() : yield _this8.getOrgRepos(org); | ||
const owner = !org ? yield _this8.getUser() : org; | ||
try { | ||
yield _this8._octokit.pulls.update({ owner, repo, pull_number: number, state: 'closed' }); | ||
var _ref9 = yield mergeGreenkeeperPullRequests.bind(_this8)(owner, repos, options); | ||
_this8.emit('pulls:close', owner, repo, number); | ||
} catch (error) { | ||
_this8.emit('pulls:close:error', error, owner, repo, number); | ||
const openedPulls = _ref9.openedPulls, | ||
mergedPulls = _ref9.mergedPulls; | ||
throw error; | ||
} | ||
})(); | ||
} | ||
mergePullRequest(owner, repo, number, sha) { | ||
var _this9 = this; | ||
return { | ||
owner, | ||
availableRepos: repos.length, | ||
openedPulls, | ||
mergedPulls | ||
}; | ||
return _asyncToGenerator(function* () { | ||
try { | ||
var _ref12 = yield _this9._octokit.pulls.merge({ owner, repo, pull_number: number, sha }); | ||
const data = _ref12.data; | ||
_this9.emit('pulls:merge', owner, repo, number, sha, data); | ||
return data; | ||
} catch (error) { | ||
_this9.emit('pulls:merge:error', error, owner, repo, number, sha); | ||
throw error; | ||
} | ||
})(); | ||
} | ||
getPullRequestComments(owner, repo, number) { | ||
var _this10 = this; | ||
return _asyncToGenerator(function* () { | ||
const options = _this10._octokit.issues.listComments.endpoint.merge({ owner, repo, issue_number: number }); | ||
const comments = yield _this10._octokit.paginate(options); | ||
return comments; | ||
})(); | ||
} | ||
mergeGreenkeeperPullRequests(org, options = { repoConcurrency: 1, pullConcurrency: 1 }) { | ||
var _this11 = this; | ||
return _asyncToGenerator(function* () { | ||
const repos = !org ? yield _this11.getUserRepos() : yield _this11.getOrgRepos(org); | ||
const owner = !org ? yield _this11.getUser() : org; | ||
yield mergeGreenkeeperPullRequests.bind(_this11)(owner, repos, options); | ||
})(); | ||
} | ||
} | ||
module.exports = GitHubWrapper; |
{ | ||
"name": "@dog-ai/github-wrapper", | ||
"description": "A GitHub wrapper library", | ||
"version": "1.2.5", | ||
"version": "1.2.6", | ||
"engines": { | ||
@@ -41,3 +41,3 @@ "node": ">= 6.0.0" | ||
"semantic-release": "15.13.12", | ||
"snyk": "1.128.0" | ||
"snyk": "1.166.0" | ||
}, | ||
@@ -44,0 +44,0 @@ "config": { |
@@ -24,6 +24,6 @@ # A :octocat: GitHub :package: wrapper library | ||
const GitHub = require('github-wrapper') | ||
const github = new GitHub({ github: { token: 'my-token' } }) | ||
const github = new GitHub({ octokit: { auth: 'my-personal-token' } }) | ||
return github.getOrgRepos('dog-ai') | ||
.mapSeries((repo) => console.log(repo.name)) | ||
github.getOrgRepos('dog-ai') | ||
.then((repos) => repos.forEach(({ name }) => console.log(name))) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16106
283