Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 94 lines (79 sloc) 2.256 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
configure {
  set :root, File.dirname(__FILE__)
}

get '/foo' do
  status 200
end

#
# GET /projects.json
#
# Load the initial build screen
#
get '/projects.json' do
  load_projects
  render :rabl, :index, :format => 'json'
end

#
# GET '/:project_name'
#
# Returns a PNG corresponding to the current build status
#
get '/:project_name.png' do |project_name|
  load_projects

  project = @projects.find { |project| project.name == project_name }

  unless project.nil?
    # if the project is done, show the status, otherwise, show it's building
    project_state = project.built? ? project.last_build_status : project.current_status

    image = case project_state
      when 'passed' then 'passing'
      when 'failed' then 'failing'
      else 'unknown'
    end

    content_type 'image/png'
    File.read("./public/images/status/#{image}.png")
  else
    status 404
  end
end


private
# Load the servers and projects from the YAML file
def load_projects
  @servers ||= YAML::load(File.read('config/servers.yml'))

  @projects = @servers.collect do |server|
    server_projects = get_server(server).projects

    # if server['projects'] is defined, we only want those projects
    if server['projects']
      regexes = collect_regexes(server['projects'])
      server_projects.select { |server_project|
        regexes.any?{ |regex| server_project.name =~ regex }
      }
    elsif server['ignored_projects']
      regexes = collect_regexes(server['ignored_projects'])
      server_projects.reject { |server_project|
        regexes.any?{ |regex| server_project.name =~ regex }
      }
    else
      server_projects
    end
  end.compact.flatten
end

def collect_regexes(projects)
  projects.collect do |j|
    if j =~ %r{^/.*/$}
      Regexp.new(j[1..(j.size-2)])
    else
      Regexp.new("^#{Regexp.escape(j)}$")
    end
  end
end

# Attemps to find the class associated with a given server type.
# Throws an exception if the class is not found
def get_server(server)
  server_type = server['type'].camelize
  begin
    klass = "Stoplight::Providers::#{server_type}".constantize
  rescue NameError => e
    raise Stoplight::Exceptions::UnknownProviderError, "Could not load provider with class name `#{server_type}`"
  end

  klass.new(server)
end
Something went wrong with that request. Please try again.