#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'time'
# Replace YOUR_API_KEY with your Codeship API key
API_KEY = 'YOUR_API_KEY'
# Change this to ['repository/name', 'repository/name2'] if you want to filter only specific projects
PROJECTS = nil
# If you want to keep the status file in someplace else (like ~/ directory), change this,
# otherwise just leave it as is
STATUS_FILE = File.join(File.dirname(__FILE__), '.codeship_status')
def projects
response = Net::HTTP.get(URI("https://codeship.com/api/v1/projects.json?api_key=#{API_KEY}"))
projects = JSON.parse(response)
if projects.has_key?('error')
raise projects['error']
else
PROJECTS.nil? ? projects : projects.select { |project| PROJECTS.include?(project['repository_name']) }
end
end
def builds
projects.map do |project|
build = project['builds'][0]
build['project_name'] = project['repository_name'].split('/')[1..-1].join('/')
build
end
end
def message(message, wait_time)
json = { "message" => message, "updateAt" => Time.now + wait_time }
File.write(STATUS_FILE, json.to_json)
puts message
end
current = File.exists?(STATUS_FILE) ? JSON.parse(File.read(STATUS_FILE)) : { "updateAt" => Time.now.to_s }
if Time.now > Time.parse(current['updateAt'])
build = builds.find { |build| build['status'] != 'success' }
if build.nil?
message "All builds have passed successfully.", 60
elsif build['status'] == 'error'
message "Build failed for #{build['project_name']}.", 60
else
message "Build in progress...", 5
end
else
puts current['message']
end