From 0915a19f48d236e556be4852e1ca6886b7f2dfbe Mon Sep 17 00:00:00 2001 From: Matheus Fernandes Date: Fri, 7 Apr 2017 21:42:08 -0700 Subject: [PATCH] Feature/slack builds (#432) * Upload builds to Slack after every commit * Make XO happy * chmod +x * Do not skip pull requests * Add `slackup` dependency * Use the new `slackup` API * `node` === latest * `msg2` => `msg` * Build the slack script before using it on CI * Shut up XO * chmod +x * Move the built scripts to the `build` directory * Bump `slackup` * Fix spacing * fml * Let the Slack know when the node version is being skipped --- .travis.yml | 7 ++++- build.sh | 5 ++++ package.json | 4 ++- scripts/slack.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100755 scripts/slack.js diff --git a/.travis.yml b/.travis.yml index 7beec31..71ec69c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,5 +16,10 @@ "on": { "tags": true } - } + }, + "after_success": [ + 'npm install --global slackup', + 'npm run pack', + './build/scripts/slack.js' + ] } diff --git a/build.sh b/build.sh index 7a5d360..c8a067b 100755 --- a/build.sh +++ b/build.sh @@ -8,3 +8,8 @@ find lib/** -type f -exec node_modules/.bin/async-to-gen --out-file build/{} {} find bin/** -type f -exec node_modules/.bin/async-to-gen --out-file build/{} {} \; chmod +x build/bin/now.js cp lib/utils/billing/*.json build/lib/utils/billing/ + +# CI +mkdir build/scripts +node_modules/.bin/async-to-gen --out-file build/scripts/slack.js scripts/slack.js +chmod +x build/scripts/slack.js diff --git a/package.json b/package.json index 4278b79..d596e38 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ }, "xo": { "ignores": [ - "test/_fixtures/**" + "test/_fixtures/**", + "scripts/build/**" ], "extends": "prettier" }, @@ -94,6 +95,7 @@ "husky": "0.13.3", "lint-staged": "3.4.0", "pkg": "3.0.0-beta.29", + "slackup": "1.2.0", "xo": "0.19.0" } } diff --git a/scripts/slack.js b/scripts/slack.js new file mode 100755 index 0000000..c0ddb86 --- /dev/null +++ b/scripts/slack.js @@ -0,0 +1,72 @@ +#!/usr/bin/env node + +const slackup = require('slackup') +const fetch = require('node-fetch') + +const repo = process.env.TRAVIS_REPO_SLUG +const commit = process.env.TRAVIS_COMMIT +const branch = process.env.TRAVIS_BRANCH +const apiKey = process.env.SLACK_API_KEY +const channel = process.env.SLACK_CHANNEL +const githubToken = process.env.GITHUB_API_KEY +const currentNodeVersion = process.env.TRAVIS_NODE_VERSION +const regex = /^(node|7)\.*/ + +// Skip if not on a zeit repo +if (!/^zeit\//.test(repo)) { + console.log('not a zeit repo') + process.exit(0) +} + +if (!apiKey) { + console.log('$SLACKUP_TOKEN not found') + process.exit(0) +} + +if (!channel) { + console.log('$SLACKUP_CHANNEL not found') + process.exit(0) +} + +if (!githubToken) { + console.log('$GITHUB_TOKEN not found') + process.exit(0) +} + +const opts = { + headers: { + authorization: `token ${githubToken}` + } +} + +fetch(`https://api.github.com/repos/${repo}/commits/${commit}`, opts) +.then(res => res.json()) +.then(res => ({ + message: res.commit.message, + authorName: res.commit.author.name, + authorUrl: res.author.html_url + })) +.then(async res => { + if (regex.test(currentNodeVersion)) { + const message = `:package: Here are the binaries for the branch *${branch}* of *${repo}* (commit by <${res.authorUrl}|${res.authorName}>):` + + const binaries = [ + `${__dirname}/../../packed/now-macos`, + `${__dirname}/../../packed/now-linux`, + `${__dirname}/../../packed/now-win.exe` + ] + + try { + await slackup({apiKey, channel, type: 'message', message}) + await slackup({apiKey, channel, type: 'file', filePath: binaries[0]}) + await slackup({apiKey, channel, type: 'file', filePath: binaries[1]}) + await slackup({apiKey, channel, type: 'file', filePath: binaries[2]}) + } catch (err) { + console.log(`Couldn't send messages/files to Slack`, err) + } + } else { + const message = `:white_check_mark: Build succeded on Node ${currentNodeVersion} (commit by <${res.authorUrl}|${res.authorName}>)` + await slackup({apiKey, channel, type: 'message', message}) + } +}) +.catch(console.error)