github-api-transactional
Version:
A transactions addon for the Github.js javascript SDK
250 lines (205 loc) • 6.57 kB
JavaScript
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
var uuid = require('uuid/v4');
var Transaction = function () {
/**
*
* @param repository
* @param parentBranch
*/
function Transaction(repository) {
var parentBranch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'master';
_classCallCheck(this, Transaction);
this.branch = null;
this.sha = null;
this.ref = null;
this.lastCommit = null;
this.elapsedSteps = 0;
this.mergedSha = null;
this.repository = repository;
this.parentBranch = parentBranch;
}
/**
*
* @return {Promise<void>}
* @private
*/
_createClass(Transaction, [{
key: '_prepareBranch',
value: function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var branchName, response;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
branchName = 'trxn-' + uuid();
_context.next = 3;
return this.repository.createBranch(this.parentBranch, branchName);
case 3:
response = _context.sent;
this.branch = branchName;
this.ref = response.data.ref;
this.sha = response.data.sha;
case 7:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function _prepareBranch() {
return _ref.apply(this, arguments);
}
return _prepareBranch;
}()
/**
*
* @param executor
* @return {Promise}
*/
}, {
key: 'step',
value: function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(executor) {
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (this.branch) {
_context2.next = 3;
break;
}
_context2.next = 3;
return this._prepareBranch();
case 3:
_context2.next = 5;
return executor(this.branch, this.ref, this.sha, ++this.elapsedSteps);
case 5:
return _context2.abrupt('return', _context2.sent);
case 6:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
function step(_x2) {
return _ref2.apply(this, arguments);
}
return step;
}()
/**
*
* @return {Promise}
*/
}, {
key: 'discard',
value: function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!this.branch) {
_context3.next = 6;
break;
}
_context3.next = 3;
return this.repository.deleteRef(this.ref);
case 3:
this.branch = null;
this.ref = null;
this.sha = null;
case 6:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function discard() {
return _ref3.apply(this, arguments);
}
return discard;
}()
/**
*
* @return {Promise}
*/
}, {
key: 'commit',
value: function () {
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var transactionPullRequest, baseSha;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return this.repository.createPullRequest({
head: this.branch,
base: this.parentBranch,
title: '[Auto] Transaction: ' + this.branch,
maintainer_can_modify: false
}).data;
case 2:
transactionPullRequest = _context4.sent;
_context4.next = 5;
return this.repository.getBranch(this.parentBranch);
case 5:
baseSha = _context4.sent.commit.sha;
_context4.next = 8;
return this.repository.mergePullRequest(transactionPullRequest.id, {
commit_title: '[Auto] Transaction: ' + this.branch,
merge_method: 'squash',
sha: baseSha
});
case 8:
this.mergedSha = _context4.sent.data.sha;
_context4.next = 11;
return this.repository.deleteRef(this.ref);
case 11:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function commit() {
return _ref4.apply(this, arguments);
}
return commit;
}()
/**
*
* @return {Promise}
*/
}, {
key: 'rollback',
value: function () {
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
throw new Error('GitHub API does not support reverting. Use GitHub UI or command line to revert. Merge commit SHA: ' + this.mergedSha + '.');
case 1:
case 'end':
return _context5.stop();
}
}
}, _callee5, this);
}));
function rollback() {
return _ref5.apply(this, arguments);
}
return rollback;
}()
}]);
return Transaction;
}();
export { Transaction };
//# sourceMappingURL=index.es.js.map