A Hubot-compatible Semaphore API wrapper for Node.js
Install
npm install semaphore-api
Require
Use it in your Hubot scripts:
= semaphore = require'semaphore-api'robot
Or use it on its own:
semaphore = require'semaphore-api'
You can pass additional options to the constructor if needed.
Usage
Make any call to the Semaphore API, get the parsed JSON response:
semaphoreget "projects" consolelog projects0name semaphoreget "projects/:hash_id/branches" consolelog branches0name # Initate a deploy semaphorepost "project/:hash_id/:branch_id/builds/:build_number/deploy/:server_id" consolelog response
Authentication
Simply set HUBOT_SEMAPHORE_AUTH_TOKEN
env variable:
export HUBOT_SEMAPHORE_AUTH_TOKEN=xxxx...
Errors
Used with Hubot, errors are automatically sent to Hubot logger. If you would like to catch errors as well, define your own callback:
semaphorehandleErrors consolelog "Oh no! !"
The callback takes a response
argument with the following keys:
error
-- the error messagestatusCode
-- the status code of the API response, if present.body
-- the body of the API response, if present.
Options
Passing options
Options may be passed in three ways, in increasing order of precedence:
-
Through shell env variables
-
Through the constructor:
semaphore = require'semaphore-api'robotauthToken: 'xxx' -
Using
withOptions
, which lets you pass options to only some requests:semaphore = require'semaphore-api'robotother_provider = semaphorewithOptionsauthToken: 'xxxx'semaphoreget "projects"-> # ...other_providerget "projects"-> # ...
Available options
authToken
/HUBOT_SEMAPHORE_AUTH_TOKEN
-- semaphore API token. Required.apiRoot
/HUBOT_SEMAPHORE_API
-- base API url. Default tohttps://semaphoreci.com/api/v1
.concurrentRequests
/HUBOT_CONCURRENT_REQUESTS
-- limits the allowed number of concurrent requests to the semaphore API. Default to 20.errorHandler
-- function for custom error handling logic.
Built-in methods
Because life is too short.
Projects
# get all projects semaphoreprojects consolelog projects0name
Branches
# get all branches of given project semaphorebranches ':project_id' consolelog branches0name # get branch status of given branch semaphorebranches':project_id'status ':branch_id' consolelog response # get branch history of given branch (page 1) semaphorebranches':project_id'history ':branch_id' consolelog response # get branch history of given branch (page 2) semaphorebranches':project_id'history ':branch_id' page: 2 consolelog response
Builds
# get build info of given project and branch semaphorebuilds':project_id'info ':branch_id'':build_id' consolelog response # get build log of given project and branch semaphorebuilds':project_id'log ':branch_id'':build_id' consolelog response # rebuild given build semaphorebuilds':project_id'rebuild ':branch_id'':build_id' consolelog response # launch given build semaphorebuilds':project_id'launch ':branch_id'':build_id' consolelog response # stop given build semaphorebuilds':project_id'stop ':branch_id'':build_id' consolelog response # deploy given build on given server semaphorebuilds':project_id'deploy ':branch_id'':build_id'':server_id' consolelog response
Servers
# get all servers of given project semaphoreservers ':project_id' consolelog servers0name # get status of given server semaphoreservers':project_id'status ':server_id' consolelog response # get history of given server (apge 1) semaphoreservers':project_id'history ':server_id' consolelog response # get history of given server (apge 2) semaphoreservers':project_id'history ':server_id' page: 2 consolelog response
Deploys
# get info of given deploy semaphoredeploys':project_id'':server_id'info ':deploy_id' consolelog response # get log of given deploy semaphoredeploys':project_id'':server_id'log ':deploy_id' consolelog response # stop deploying given deploy on given server semaphoredeploys':project_id'':server_id'stop ':deploy_id' consolelog response
Contributing
Install the dependencies:
npm install
Run the tests:
make test
I'm vastly more likely to merge code that comes with tests. If you're confused by the testing process, ask and I can probably point you in the right direction.
Thanks
Thanks to the author of Githubot for his amazing work that could get me started.