-
-
Save erik-am/cfdf021fab41cd3c7a3b1a33086df64d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const azdev = require('azure-devops-node-api'); | |
const AWS = require('aws-sdk'); | |
const spawn = require('cross-spawn'); | |
const path = require('path'); | |
AWS.config.region = 'eu-west-1'; | |
function exec(command, args, options) { | |
const { status } = spawn.sync(command, args, { | |
...options, | |
stdio: 'inherit', | |
}); | |
if (status !== 0) { | |
throw new Error('Unexpected error.'); | |
} | |
} | |
(async () => { | |
let orgUrl = process.env.SYSTEM_COLLECTIONURI; | |
let token = process.env.SYSTEM_ACCESSTOKEN; | |
let authHandler = azdev.getPersonalAccessTokenHandler(token); | |
let connection = new azdev.WebApi(orgUrl, authHandler); | |
let gitApi = await connection.getGitApi(); | |
const prList = await gitApi.getPullRequests( | |
process.env.BUILD_REPOSITORY_NAME, | |
{}, | |
process.env.SYSTEM_TEAMPROJECT, | |
); | |
const openPullRequests = prList.map(x => x.pullRequestId); | |
var cloudformation = new AWS.CloudFormation(); | |
let data = await cloudformation | |
.listStacks({ | |
StackStatusFilter: [ | |
//'CREATE_IN_PROGRESS', | |
'CREATE_FAILED', | |
'CREATE_COMPLETE', | |
//'ROLLBACK_IN_PROGRESS', | |
'ROLLBACK_FAILED', | |
'ROLLBACK_COMPLETE', | |
'DELETE_FAILED', | |
//'UPDATE_IN_PROGRESS', | |
//'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS', | |
'UPDATE_COMPLETE', | |
//'UPDATE_ROLLBACK_IN_PROGRESS', | |
'UPDATE_ROLLBACK_FAILED', | |
//'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS', | |
'UPDATE_ROLLBACK_COMPLETE', | |
//'REVIEW_IN_PROGRESS', | |
//'IMPORT_IN_PROGRESS', | |
'IMPORT_COMPLETE', | |
//'IMPORT_ROLLBACK_IN_PROGRESS', | |
'IMPORT_ROLLBACK_FAILED', | |
'IMPORT_ROLLBACK_COMPLETE', | |
], | |
}) | |
.promise(); | |
const stacksToRemove = data.StackSummaries.map(x => x.StackName) | |
.map(x => x.match(/-pr-([0-9]+)$/)) | |
.filter(x => x !== null) | |
.map(x => { | |
return { stackName: x.input, prId: parseInt(x[1]) }; | |
}) | |
.filter(x => !openPullRequests.includes(x.prId)); | |
console.log('Stacks to remove:'); | |
console.log(stacksToRemove); | |
const prNumbers = stacksToRemove.map(x => x.prId); | |
const uniquePrNumbers = [...new Set(prNumbers)]; | |
uniquePrNumbers.forEach(prId => { | |
const stage = `pr-${prId}`; | |
console.log(stage); | |
const cmd = '../node_modules/serverless/bin/serverless'; | |
['../webapp'].forEach(dir => { | |
try { | |
exec(cmd, ['remove', '-v', '--stage', stage, '--force'], { | |
cwd: path.join(dir), | |
}); | |
} catch (err) { | |
console.log(err); | |
} | |
}); | |
}); | |
})().catch(e => { | |
console.log(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment