Skip to content

Instantly share code, notes, and snippets.

@d4rky-pl
Last active September 29, 2017 12:03
Show Gist options
  • Save d4rky-pl/aac0e5a9eb989b65384a5ea5044b7ec3 to your computer and use it in GitHub Desktop.
Save d4rky-pl/aac0e5a9eb989b65384a5ea5044b7ec3 to your computer and use it in GitHub Desktop.
Simple Ruby script to check Codeship status
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment