Skip to content

Instantly share code, notes, and snippets.

@jhanzo
Created February 5, 2018 11:08
Show Gist options
  • Save jhanzo/6fcdafd653c2bcf195d0103cb3094328 to your computer and use it in GitHub Desktop.
Save jhanzo/6fcdafd653c2bcf195d0103cb3094328 to your computer and use it in GitHub Desktop.
Hubot Builder Bot Script
# Description:
# Builder bot for sending request to Bitrise.io.
#
# Author:
# jhanzo
#
request = require 'request'
exec = require('child_process').exec
module.exports = (robot) ->
# create
robot.commands.push "create a bot for doing some tasks"
robot.respond /(.*)create bot on (.*) for (.*)/i, (res) ->
schemes = ["dev", "recette", "preprod", "prod"]
if !(res.match[2]?) || !(res.match[3]?) || !(res.match[3].toLowerCase() in schemes)
res.reply "I only understand : create bot on `branch` for `scheme`\nMy available schemes : `dev` | `recette` | `preprod` | `prod`"
res.message.done = true
else if !(process.env["BITRISE_TOKEN"]?) || !(process.env["BITRISE_APP"]?)
res.reply "BITRISE_TOKEN and BITRISE_APP env var are required"
res.message.done = true
else
env_info = ""
params = /\(env\:(.*)\)/g.exec res.match[1]
if params && params.length
env = params[0].replace /env\:|\(|\)/gi, ""
envs = env.trim().split " "
#if only one param > recette
env_info = ",\"environments\":[{\"mapped_to\":\"RECETTE_NUMBER\",\"value\":\"" + envs[0] + "\",\"is_expand\":true}]" unless envs.length is 2
# if 2 params > dev
env_info = ",\"environments\":[{\"mapped_to\":\"DEV_USERNAME\",\"value\":\"" + envs[0] + "\",\"is_expand\":true},{\"mapped_to\":\"DEV_HOSTNAME\",\"value\":\"" + envs[1] + "\",\"is_expand\":true}]" unless envs.length is 1
#construct json
message = res.match[1].replace /\(|\)/g, ""
data = " -d \'{\"hook_info\":{\"type\":\"bitrise\",\"api_token\":\"" + process.env["BITRISE_TOKEN"] + "\"},\"build_params\":{\"branch\":\"" + res.match[2] + "\",\"workflow_id\":\"" + res.match[3].toLowerCase() + "\",\"commit_message\":\"" + message + "\"" + env_info + "},\"triggered_by\":\"curl\"}\'"
headers = " -H 'cache-control: no-cache' -H 'content-type: application/json'"
cmd = "curl -X POST https://www.bitrise.io/app/" + process.env["BITRISE_APP"] + "/build/start.json" + headers + data
if res.match[3] == "dev"
res.reply ">>>" + cmd
result = exec(cmd)
result.stdout.on 'data', (data) ->
json = JSON.parse(data.toString())
if json.status == "ok"
res.reply "🔥🚀 Build on fire 👉 " + json.build_url
else
res.reply data.toString().trim()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment