Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ensure-orgs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ permissions:
contents: read

on:
schedule:
- cron: "23 6 1 * *"
workflow_dispatch:
push:
branches: [gh-pages]
paths:
Expand All @@ -26,11 +29,15 @@ jobs:

steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Set up Ruby
uses: ruby/setup-ruby@de492cf36cbab201753d720ce4f4e336cd688c94 # v1.336.0
with:
bundler-cache: true
- name: Test organization checks
run: bundle exec ruby test/ensure_orgs_test.rb
- name: Ensure data entries are GitHub Orgs
env:
ORG_BASE_REF: ${{ github.event.pull_request.base.sha || github.event.before }}
run: bundle exec script/ensure-orgs
27 changes: 26 additions & 1 deletion script/ensure-orgs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# frozen_string_literal: true

# Ensures that all organizations listed in _data are in fact, organizations
# Set ORG_BASE_REF to check only names absent from that Git revision; omit it for a full scan.

require 'typhoeus'
require 'parallel'
require 'yaml'
require 'open3'

class Cache
def initialize
Expand All @@ -28,12 +30,35 @@ puts 'Checking that all orgs listed are in fact, an org...'
data_folder_path = File.expand_path '../_data', File.dirname(__FILE__)
files = Dir["#{data_folder_path}/*.yml"]

def git_output(*)
output, error, status = Open3.capture3('git', '-C', File.expand_path('..', __dir__), *)
abort "Cannot read organization baseline: #{error}" unless status.success?

output
end

def baseline_orgs(ref)
# GitHub uses an all-zero before SHA for the first push of a branch.
return Set.new if ref.match?(/\A0{40}\z/)

paths = git_output('ls-tree', '-r', '--name-only', ref, '--', '_data')
paths.lines.map(&:chomp).grep(%r{\A_data/[^/]+\.yml\z}).each_with_object(Set.new) do |path, orgs|
data = YAML.safe_load(git_output('show', "#{ref}:#{path}"))
orgs.merge(data.values.flatten.map(&:downcase))
end
end

base_ref = ENV.fetch('ORG_BASE_REF', nil)
existing_orgs = baseline_orgs(base_ref) unless base_ref.nil? || base_ref.empty?
puts(existing_orgs ? 'Checking only added or renamed organizations.' : 'Checking all organizations.')

valid = true
response_counts = Hash.new(0)
files.each do |file|
data = YAML.load_file file
data.each do |group, orgs|
responses = Parallel.map(orgs) do |org|
candidates = existing_orgs ? orgs.reject { |org| existing_orgs.include?(org.downcase) } : orgs
responses = Parallel.map(candidates) do |org|
[org, Typhoeus.head("https://github.com/orgs/#{org}/people", accept_encoding: 'gzip')]
end
not_orgs = []
Expand Down
79 changes: 72 additions & 7 deletions test/ensure_orgs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,35 @@
require 'tmpdir'
require 'yaml'

class EnsureOrgsTest < Minitest::Test
def run_check(codes)
module EnsureOrgsFixtures
def git!(dir, *)
output, status = Open3.capture2e('git', '-C', dir, *)
raise output unless status.success?
end

def write_baseline(dir, baseline)
git!(dir, 'init', '--quiet')
baseline.each do |name, data|
File.write("#{dir}/_data/#{name}.yml", "# Organizations\n\n#{data.to_yaml.sub(/\A---\n/, '')}")
end
git!(dir, 'add', '_data')
git!(dir, '-c', 'user.name=Test', '-c', 'user.email=test@example.com', '-c', 'commit.gpgsign=false',
'commit', '--quiet', '-m', 'Baseline')
baseline.each_key { |name| File.delete("#{dir}/_data/#{name}.yml") }
end

def run_check(codes, baseline: nil, current: nil, base_ref: nil)
Dir.mktmpdir do |dir|
FileUtils.mkdir_p("#{dir}/script")
FileUtils.mkdir_p("#{dir}/_data")
FileUtils.cp(File.expand_path('../script/ensure-orgs', __dir__), "#{dir}/script/ensure-orgs")
data_path = "#{dir}/_data/orgs.yml"
File.write(data_path, "# Organizations\n\nTest:\n#{codes.keys.map { |org| " - #{org}\n" }.join}")
if baseline
write_baseline(dir, baseline)
base_ref ||= 'HEAD'
end
current ||= { 'Test' => codes.keys }
File.write(data_path, "# Organizations\n\n#{current.to_yaml.sub(/\A---\n/, '')}")
File.write("#{dir}/responses.rb", <<~RUBY)
require 'typhoeus'
require 'json'
Expand All @@ -26,12 +47,16 @@ def run_check(codes)
end
RUBY
output, status = Open3.capture2e(
{ 'ORG_RESPONSES' => codes.to_json, 'GITHUB_ACTIONS' => 'true' },
{ 'ORG_RESPONSES' => codes.to_json, 'GITHUB_ACTIONS' => 'true', 'ORG_BASE_REF' => base_ref },
Gem.ruby, '-r', "#{dir}/responses.rb", "#{dir}/script/ensure-orgs"
)
[output, status, YAML.safe_load_file(data_path).fetch('Test')]
[output, status, YAML.safe_load_file(data_path).values.flatten]
end
end
end

class EnsureOrgsTest < Minitest::Test
include EnsureOrgsFixtures

def test_unavailable_responses_are_inconclusive_and_retained
codes = { 'org' => 200, 'forbidden' => 403, 'limited' => 429, 'server-error' => 500,
Expand All @@ -50,7 +75,7 @@ def test_unavailable_responses_are_inconclusive_and_retained
end

def test_not_found_still_fails_without_removing_inconclusive_entries
output, status, entries = run_check('org' => 200, 'user' => 404, 'limited' => 429)
output, status, entries = run_check({ 'org' => 200, 'user' => 404, 'limited' => 429 })

assert_equal 1, status.exitstatus, output
assert_equal %w[org limited], entries
Expand All @@ -61,11 +86,51 @@ def test_not_found_still_fails_without_removing_inconclusive_entries
end

def test_successful_checks_report_success
output, status, entries = run_check('org' => 200)
output, status, entries = run_check({ 'org' => 200 })

assert status.success?, output
assert_equal ['org'], entries
assert_includes output, 'All orgs check out.'
refute_includes output, 'Skipped'
end

def test_incremental_check_only_requests_added_and_renamed_orgs
baseline = { 'old' => { 'Old group' => %w[existing renamed-old removed] } }
current = { 'New group' => %w[Existing renamed-new added] }
output, status, entries = run_check(
{ 'renamed-new' => 404, 'added' => 200 }, baseline: baseline, current: current
)

assert_equal 1, status.exitstatus, output
assert_equal %w[Existing added], entries
assert_includes output, 'Responses (2 total):'
assert_includes output, "(HTTP 404):\n\nrenamed-new\n"
end

def test_moves_sorting_and_removals_do_not_request_orgs
baseline = { 'old' => { 'Old group' => %w[zebra alpha removed] } }
output, status, entries = run_check({}, baseline: baseline, current: { 'New group' => %w[alpha zebra] })

assert status.success?, output
assert_equal %w[alpha zebra], entries
assert_includes output, 'Responses (0 total):'
end

def test_unavailable_baseline_fails_explicitly
output, status, entries = run_check(
{ 'org' => 200 }, baseline: { 'orgs' => { 'Test' => ['org'] } }, base_ref: 'missing-ref'
)

refute status.success?, output
assert_equal ['org'], entries
assert_includes output, 'Cannot read organization baseline:'
end

def test_initial_push_checks_all_entries
output, status, entries = run_check({ 'org' => 200 }, base_ref: '0' * 40)

assert status.success?, output
assert_equal ['org'], entries
assert_includes output, 'Responses (1 total):'
end
end
Loading