Skip to content

Instantly share code, notes, and snippets.

@kbrandwijk
Created August 18, 2021 06:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbrandwijk/124a395a33e180826544fe5518c2936d to your computer and use it in GitHub Desktop.
Save kbrandwijk/124a395a33e180826544fe5518c2936d to your computer and use it in GitHub Desktop.
Autocode Expo Slack Notifier
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const {createHmac} = await import('crypto');
const safeCompare = require('safe-compare');
// Expo request signature check
const hmac = createHmac('sha1', process.env.SECRET_WEBHOOK_KEY);
hmac.update(Buffer.from(JSON.stringify(context.params)));
const hash = `sha1=${hmac.digest('hex')}`;
console.log('context.params', context.params)
if (!safeCompare(hash, context.http.headers['expo-signature'])) {
throw new Error('Invalid request signature!');
}
// ----------------------------
const slackChannel = '#your-channel-here'
const buildDetailsUrl = `https://expo.io/accounts/${context.params.metadata.trackingContext.account_name}/builds/${context.params.id}`;
if (context.params.platform === 'ios' && context.params.status === 'finished') {
const url = `itms-services://?action=download-manifest;url=https://exp.host/--/api/v2/projects/${context.params.appId}/builds/${context.params.id}/manifest.plist`;
let attachments = [
{
color: '#49C39E',
blocks: [
{
type: 'header',
text: {
type: 'plain_text',
text:
':apple-logo: Build completed successfully for iOS :ship_it_parrot:',
emoji: true,
},
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Build Profile*: ${context.params.metadata.buildProfile}\n*Version:* ${context.params.metadata.appVersion}\n*Build*: ${context.params.metadata.appBuildVersion}`,
},
],
},
{
type: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: 'Download IPA',
},
url: context.params.artifacts.buildUrl,
},
{
type: 'button',
text: {
type: 'plain_text',
text: 'Open Build Details Page',
},
url: buildDetailsUrl,
},
],
},
{
type: 'image',
image_url: `https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(
url
)}&size=250x250&qzone=2`,
alt_text: 'qr',
},
],
},
]
const result = await lib.slack.channels['@0.7.3'].messages.create({
username: 'Expo Build Notifier',
channel: slackChannel,
attachments: attachments
});
}
if (
context.params.platform === 'android' &&
context.params.status === 'finished'
) {
let androidFinishedAttachments = [
{
color: '#49C39E',
blocks: [
{
type: 'header',
text: {
type: 'plain_text',
text:
':android: Build completed successfully for Android :ship_it_parrot:',
emoji: true,
},
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Build Profile*: ${context.params.metadata.buildProfile}\n*Version:* ${context.params.metadata.appVersion}\n*Build*: ${context.params.metadata.appBuildVersion}`,
},
],
},
{
type: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: 'Download APK',
},
url: context.params.artifacts.buildUrl,
},
{
type: 'button',
text: {
type: 'plain_text',
text: 'Open Build Details Page',
},
url: buildDetailsUrl,
},
],
},
{
type: 'image',
image_url: `https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(
context.params.artifacts.buildUrl
)}&size=250x250&qzone=2`,
alt_text: 'qr',
},
],
},
];
const result = await lib.slack.channels['@0.7.3'].messages.create({
username: 'Expo Build Notifier',
channel: slackChannel,
attachments: androidFinishedAttachments,
});
}
if (context.params.platform === 'ios' && context.params.status === 'errored') {
const result = await lib.slack.channels['@0.7.3'].messages.create({
username: 'Expo Build Notifier',
channel: slackChannel,
attachments: [
{
color: '#D40E0D',
blocks: [
{
type: 'header',
text: {
type: 'plain_text',
text: ':apple-logo: Build failed for iOS :sob:',
emoji: true,
},
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Build Profile*: ${context.params.metadata.buildProfile}\n*Version:* ${context.params.metadata.appVersion}\n*Build*: ${context.params.metadata.appBuildVersion}`,
},
],
},
{
type: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: 'Open Build Details Page',
},
url: buildDetailsUrl,
},
],
},
],
},
],
});
}
if (
context.params.platform === 'android' &&
context.params.status === 'errored'
) {
const result = await lib.slack.channels['@0.7.3'].messages.create({
username: 'Expo Build Notifier',
channel: slackChannel,
attachments: [
{
color: '#D40E0D',
blocks: [
{
type: 'header',
text: {
type: 'plain_text',
text: ':android: Build failed for Android :sob:',
emoji: true,
},
},
{
type: 'section',
fields: [
{
type: 'mrkdwn',
text: `*Build Profile*: ${context.params.metadata.buildProfile}\n*Version:* ${context.params.metadata.appVersion}\n*Build*: N/A`,
},
],
},
{
type: 'actions',
elements: [
{
type: 'button',
text: {
type: 'plain_text',
text: 'Open Build Details Page',
},
url: buildDetailsUrl,
},
],
},
],
},
],
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment