ruby 用于检查Codeship状态的简单Ruby脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于检查Codeship状态的简单Ruby脚本相关的知识,希望对你有一定的参考价值。

#!/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

以上是关于ruby 用于检查Codeship状态的简单Ruby脚本的主要内容,如果未能解决你的问题,请参考以下文章

procs 可以与 Ruby 2.0 中的 case 语句一起使用吗?

Cloud9 警告我应该忽略吗?

python 用于检查站点何时更改状态代码的简单脚本

Ruby注释

新入手一枚测试工具:rspec

新入手一枚测试工具:rspec