UNPKG

pr-deployment

Version:
134 lines (119 loc) 5.17 kB
#!/usr/bin/env node 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /** * This script will check the current list of deployments against open Pull Requests on GitHub. * If the deployment isn't linked against one, then it will shut it down. */ var _isomorphicFetch = require('isomorphic-fetch'); var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // Get all active now.sh deployments var cleanup = function cleanup(_ref) { var _ref$nowToken = _ref.nowToken, nowToken = _ref$nowToken === undefined ? null : _ref$nowToken, _ref$ghAuthTokenUsern = _ref.ghAuthTokenUsername, ghAuthTokenUsername = _ref$ghAuthTokenUsern === undefined ? null : _ref$ghAuthTokenUsern, _ref$ghAuthToken = _ref.ghAuthToken, ghAuthToken = _ref$ghAuthToken === undefined ? null : _ref$ghAuthToken, _ref$repoUsername = _ref.repoUsername, repoUsername = _ref$repoUsername === undefined ? null : _ref$repoUsername, _ref$repoName = _ref.repoName, repoName = _ref$repoName === undefined ? null : _ref$repoName, _ref$contextName = _ref.contextName, contextName = _ref$contextName === undefined ? 'pr-deployment/deployment' : _ref$contextName; if (!nowToken || !ghAuthTokenUsername || !ghAuthToken || !repoUsername || !repoName) { return Promise.reject(new Error('All required input parameters for cleanup were not provided.')); } var deployments = null; var aliases = []; var deploymentNames = {}; var nowShHeaders = { headers: { Authorization: 'Bearer ' + nowToken } }; var githubHeaders = { headers: { 'Content-Type': 'application/json; charset=utf-8', 'X-GitHub-Media-Type': 'github.v3', Authorization: 'Basic ' + Buffer.from(ghAuthTokenUsername + ':' + ghAuthToken).toString('base64') } }; return (0, _isomorphicFetch2.default)('https://api.zeit.co/now/aliases', nowShHeaders).then(function (res) { return res.json(); }).then(function (response) { aliases = response.aliases.map(function (alias) { return alias.deployment.url; }); return (0, _isomorphicFetch2.default)('https://api.zeit.co/now/deployments', nowShHeaders); }).then(function (res) { return res.json(); }).then(function (response) { deployments = response.deployments // Filter out containers which aren't part of the given repo. .filter(function (deployment) { return deployment.name === repoName; }) // Filter out containers which are still being built. .filter(function (deployment) { return typeof deployment.url !== 'undefined'; }) // Filter out deployments which are aliased. .filter(function (deployment) { return !aliases.includes(deployment.url); }); // Save into a variable by ID so we can use it as a reference later. deployments.forEach(function (deployment) { deploymentNames[deployment.uid] = deployment; }); return (0, _isomorphicFetch2.default)('https://api.github.com/repos/' + repoUsername + '/' + repoName + '/pulls?state=open', githubHeaders); }).then(function (response) { return response.json(); }).then(function (pullRequests) { return Promise.all(pullRequests // eslint-disable-next-line no-underscore-dangle .map(function (pullRequest) { return (0, _isomorphicFetch2.default)(pullRequest._links.statuses.href, githubHeaders).then(function (response) { return response.json(); }); })); }).then(function (pullRequestStatuses) { var allStatuses = []; pullRequestStatuses.forEach(function (statuses) { allStatuses = allStatuses.concat(statuses); }); allStatuses = allStatuses // Get only statuses which are for ci/deploy .filter(function (status) { return status.context === contextName; }) // Retrieve only the deployed URL .map(function (status) { return status.target_url; }) // Make sure this is a now.sh URL .filter(function (url) { return url.substr(-7) === '.now.sh'; }) // Remove https:// .map(function (url) { return url.substr(8); }); return Promise.all(deployments // Make sure this deployment isn't being used in an active pull request. .filter(function (deployment) { return allStatuses.indexOf(deployment.url) === -1; }).map(function (deployment) { return (0, _isomorphicFetch2.default)('https://api.zeit.co/now/deployments/' + deployment.uid, _extends({}, nowShHeaders, { method: 'DELETE' })).then(function (response) { return response.json(); }); })); }).then(function (responses) { return responses.map(function (response) { return _extends({}, response, { url: deploymentNames[response.uid].url }); }); }); }; exports.default = cleanup;