Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INFRA-2697] Add fork report #24

Merged
merged 1 commit into from
Aug 3, 2020
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
15 changes: 15 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pipeline {
label 'docker'
}
steps {
sh 'docker build permissions-report -t fork-report'
sh 'docker build permissions-report -t permissions-report'
sh 'docker build artifactory-users-report -t artifactory-users-report'
}
Expand All @@ -39,6 +40,20 @@ pipeline {
}
}
parallel {
stage('GitHub Forks') {
agent {
label 'docker'
}
environment {
GITHUB_API = credentials('github-token')
}
steps {
sh 'docker build fork-report -t fork-report'
sh 'docker run -e GITHUB_API_TOKEN=$GITHUB_API_PSW fork-report > github-jenkinsci-fork-report.json'
archiveArtifacts 'github-jenkinsci-fork-report.json'
publishReports ([ 'github-jenkinsci-fork-report.json' ])
}
}
stage('Artifactory Permissions') {
agent {
label 'docker'
Expand Down
7 changes: 7 additions & 0 deletions fork-report/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM ruby:2.6.5-alpine3.10

RUN gem install graphql-client httparty

COPY fork-report.rb /

ENTRYPOINT ["ruby", "/fork-report.rb"]
107 changes: 107 additions & 0 deletions fork-report/fork-report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Usage: GITHUB_API_TOKEN=abcdefabcdef ruby fork-report.rb > report.json

require 'graphql/client'
require 'graphql/client/http'
require 'httparty'
require 'pp'
require 'json'

$auth = "bearer #{ENV['GITHUB_API_TOKEN']}"

module GitHubGraphQL
HTTP = GraphQL::Client::HTTP.new('https://api.github.com/graphql') do
def headers(context)
{
'Authorization' => $auth
}
end
end
Schema = GraphQL::Client.load_schema(HTTP)
Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
end


CollaboratorsQuery = GitHubGraphQL::Client.parse <<-'GRAPHQL'

query($repository_cursor: String) {
organization(login: "jenkinsci") {
repositories(first: 100, after: $repository_cursor) {
pageInfo {
startCursor
hasNextPage
endCursor
}
edges {
node {
nameWithOwner
isFork
parent {
nameWithOwner
}
}
}
}
}
rateLimit {
limit
cost
remaining
resetAt
}
}
GRAPHQL


$table_data = []

def record_repository(repo_name, source_repo)
$table_data << [ repo_name, source_repo ]
end

def ratelimit_info(rate_limit)
STDERR.puts "Rate limit: Cost: #{rate_limit.cost}, limit #{rate_limit.limit}, remaining: #{rate_limit.remaining}, reset at: #{rate_limit.reset_at}"
end

repository_cursor = nil
collaborator_cursor = nil
error_count = 0

loop do
STDERR.puts "Calling with cursors: repository #{repository_cursor}"
result = GitHubGraphQL::Client.query(CollaboratorsQuery, variables: {repository_cursor: repository_cursor })

if !result.errors[:data].empty? then
STDERR.puts result.errors[:data]
sleep 5
if error_count > 50 then
# fatal
STDERR.puts 'Consecutive error count limit reached, aborting'
abort('Too many errors')
else
error_count += 1
end
else
error_count = 0

result.data.organization.repositories.edges.each { |repo|
repo_name = repo.node.name_with_owner
STDERR.puts "Processing #{repo_name}"
if repo.node.is_fork then
record_repository(repo_name, repo.node.parent&.name_with_owner )
end
}

ratelimit_info(result.data.rate_limit)

repository_paging = result.data.organization.repositories.page_info
if repository_paging.has_next_page
collaborator_cursor = nil
repository_cursor = repository_paging.end_cursor
STDERR.puts "Next page of repositories, from #{repository_cursor}"
else
break
end
end
end

puts JSON.generate($table_data)