Skip to content

Instantly share code, notes, and snippets.

@fmasuhr
Last active July 18, 2018 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmasuhr/3ee4dd414d90765090188978e6bc7655 to your computer and use it in GitHub Desktop.
Save fmasuhr/3ee4dd414d90765090188978e6bc7655 to your computer and use it in GitHub Desktop.
Move Travis CI required_status_checks to new Github Application integration
require 'json'
require 'octokit'
require 'restclient'
ORGANIZATION = 'lessonnine'.freeze
ACCEPT_HEADER = 'application/vnd.github.luke-cage-preview+json'.freeze
Octokit.auto_paginate = true
client = Octokit::Client.new(access_token: ENV['GITHUB_ACCESS_TOKEN'])
repositories = client.organization_repositories(ORGANIZATION)
puts repositories.size
repositories.each do |repository|
branches = client.branches(repository.full_name).map(&:name)
branches.each do |branch|
protection = client.branch_protection(repository.full_name, branch,
accept: ACCEPT_HEADER)
next unless protection
next unless protection.required_status_checks
contexts = protection.required_status_checks.contexts
next unless contexts
next if contexts.include?('Travis CI - Branch')
next unless contexts.include?('continuous-integration/travis-ci')
puts '=============================='
puts "#{repository.full_name}: #{branch}"
url = "https://api.github.com/repos/#{repository.full_name}/branches/#{branch}/protection/required_status_checks"
payload = {
strict: true,
contexts: contexts.map do |c|
c == 'continuous-integration/travis-ci' ? 'Travis CI - Branch' : c
end
}
header = {
accept: ACCEPT_HEADER,
Authorization: "Token #{ENV['GITHUB_ACCESS_TOKEN']}"
}
response = RestClient.patch url, payload.to_json, header
puts response.body if response.code != 200
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment