Skip to content

Instantly share code, notes, and snippets.

@keithpitt
Created July 15, 2016 04:30
Show Gist options
  • Save keithpitt/6703c3462084ae4dcb101980b73747a8 to your computer and use it in GitHub Desktop.
Save keithpitt/6703c3462084ae4dcb101980b73747a8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Usage:
# ./flood --pipeline "my-org/bash-dumper" --api-key ... -n 20
require 'net/http'
require 'optparse'
require 'json'
options = { pipeline: "", api_key: "", endpoint: "https://api.buildkite.com", commit: "HEAD", branch: "master", number: 5 }
parser = OptionParser.new do |opts|
opts.banner = "Usage: flood [options]"
opts.on("--n [number]", "How many builds to create") do |v|
options[:number] = v.to_i
end
opts.on("--pipeline [pipeline]", "The organization and pipeline name eg. 'my-org/bash-example'") do |v|
options[:pipeline] = v
end
opts.on("--commit [commit]", "The commit to build eg. `HEAD` or `f71397c3ee22230590dada91e74c9678d6835217`") do |v|
options[:commit] = v
end
opts.on("--branch [branch]", "The branch to build eg. `master`") do |v|
options[:branch] = v
end
opts.on("--api-key [api-key]", "Your Buildkite API Key") do |v|
options[:api_key] = v
end
end
parser.parse!
if options[:pipeline].empty? || options[:api_key].empty?
puts parser.help
exit 1
end
organization, pipeline = options[:pipeline].split("/")
uri = URI.parse(URI.join(options[:endpoint], "v2/organizations/#{organization}/pipelines/#{pipeline}/builds?api_key=#{options[:api_key]}").to_s)
options[:number].times do |i|
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}", { "Content-Type" => "application/json" })
req.body = JSON.generate(commit: options[:commit], branch: options[:branch], message: "Flooded build #{i}")
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true
response = http.request(req)
if response.code != "201"
puts ""
puts "HTTP/#{response.http_version} #{response.message} #{response.code}"
headers = {}
response.each_capitalized_name do |name|
puts "#{name}: #{response[name]}"
end
puts ""
puts response.body
exit 0
else
json = JSON.parse(response.body)
puts "Created build: #{json['url']}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment