Last active
December 19, 2022 16:43
-
-
Save hidakatsuya/0be7f65816a6c09f4cc02c2c1108ebb6 to your computer and use it in GitHub Desktop.
[GAS] Cloud Build Executer
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
function init(e) { | |
const props = PropertiesService.getScriptProperties().getProperties(); | |
if (props.SLACK_VERIFICATION_TOKEN != e.parameter.token) { | |
throw new Error('Invalid token'); | |
} | |
const params = parseParams(e.parameter.text); | |
return { props, params }; | |
} | |
function parseParams(paramsText = '') { | |
const params = paramsText.split(' '); | |
return { | |
subCommand: params.shift(), | |
args: params | |
}; | |
} | |
function result({ text, inChannel = false }) { | |
const data = { | |
text, | |
// See https://api.slack.com/interactivity/slash-commands | |
response_type: inChannel ? 'in_channel' : 'ephemeral' | |
}; | |
return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON); | |
} | |
function postWebhook(name, url) { | |
const response = UrlFetchApp.fetch(url, { | |
method: 'post', | |
contentType: 'application/json', | |
payload: '{}' | |
}); | |
if (response.getResponseCode() == 200) { | |
return result({ | |
text: `Successfully started ${name} build.`, | |
inChannel: true | |
}); | |
} else { | |
return result({ | |
text: `Error! ${name}: ${response.getResponseCode()} ${response.getContentText()}` | |
}); | |
} | |
} | |
function doPost(e) { | |
const { props, params } = init(e); | |
switch (params.subCommand) { | |
case 'shopping-list': | |
return postWebhook('shopping-list', props.WEBHOOK_APP_DEPLOY_URL) | |
case 'cloud-build-notifier': | |
return postWebhook('cloud-build-notifier', props.WEBHOOK_CLOUD_BUILD_NOTIFIER_DEPLOY_URL) | |
default: | |
return result({ | |
text: `Usage: ${e.parameter.command} [shopping-list|cloud-build-notifier]`, | |
inChannel: true | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment