Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 178 lines (156 sloc) 4.87 KB
#!/usr/bin/env ruby
# frozen_string_literal: true
require "fileutils"
require "graphql/client"
require "graphql/client/http"
require_relative "./utils"
CACHE_DIR = ".cache"
SCHEMA_PATH = File.join(CACHE_DIR, "github-schema.json")
REPO_JSON_PATH = "_data/repos.json"
JEKYLL_CONFIG = load_yaml("_config.yml")
CONFIG = JEKYLL_CONFIG["github_repos_metadata"] || {}
CONFIG.update load_yaml(CONFIG["inherit_from"]) if CONFIG["inherit_from"]
REPO_OVERRIDES = CONFIG["repos"] || {}
HTTP = GraphQL::Client::HTTP.new("https://api.github.com/graphql") do
def headers(_context)
token = ENV["JEKYLL_GITHUB_TOKEN"]
if !token && File.exist?(".envrc")
data = File.open(".envrc").read
token = $1 if data =~ /\bJEKYLL_GITHUB_TOKEN=(\S+)/
end
return { Authorization: "Bearer #{token}" } if token
warn <<~MSG
Error: Missing GitHub access token.
To fix this, set JEKYLL_GITHUB_TOKEN to an access token created at
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
MSG
exit 1
end
end
unless File.exist?(SCHEMA_PATH)
FileUtils.mkdir_p File.dirname(SCHEMA_PATH)
puts "Saving #{SCHEMA_PATH}"
GraphQL::Client.dump_schema(HTTP, SCHEMA_PATH)
end
Client = GraphQL::Client.new(schema: SCHEMA_PATH, execute: HTTP)
IndexQuery = Client.parse <<-'GRAPHQL'
query($count: Int, $cursor: String) {
viewer {
login
repositories(first: $count, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
name
nameWithOwner
owner {
login
}
createdAt
pushedAt
description
url
homepageUrl
isArchived
isFork
isPrivate
primaryLanguage {
name
}
languages(first: 100) {
edges {
node {
name
}
}
}
repositoryTopics(first:10) {
edges {
node {
topic {
name
}
}
}
}
}
}
}
}
}
GRAPHQL
# collect repos into `repos`
repos = []
repo_count = 0
cursor = nil
viewer = nil
loop do
result = Client.query(IndexQuery, variables: { count: 100, cursor: cursor })
viewer = result.data.viewer
repo_count += viewer.repositories.edges.length
repos +=
viewer
.repositories.edges
.map(&:node)
page_info = viewer.repositories.page_info
break unless page_info.has_next_page
cursor = page_info.end_cursor
end
# Filter according to config
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def include_repo?(repo, includes, excludes, viewer)
return true if includes.any? { |s| File.fnmatch(s, repo.name_with_owner) }
return false if excludes.any? { |s| File.fnmatch(s, repo.name_with_owner) }
# TODO: search REPO_OVERRIDES for repo.name_with_owner
return false if repo.is_fork && !CONFIG["include_forks"]
return false if repo.is_private && !CONFIG["include_private"]
return false if repo.owner.login != viewer.login
if repo.repository_topics.edges.empty?
puts "Skipping https://github.com/#{repo.name_with_owner}: empty topic list"
return false
end
return true
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
includes = (CONFIG["include"] || []).map { |s| s.include?("/") ? s : "*/#{s}" }
excludes = (CONFIG["exclude"] || []).map { |s| s.include?("/") ? s : "*/#{s}" }
repos = repos.select { |r| include_repo?(r, includes, excludes, viewer) }
# Convert to hashes
repo_hashes = repos.map do |repo|
h = repo.to_h.dup
h["languages"] = repo.languages.edges.map { |edge| edge.node.name }
h["primaryLanguage"] = repo.primary_language.name if repo.primary_language
h["topics"] = repo.repository_topics.edges.map { |edge| edge.node.topic.name }
h.delete "homepageUrl" if h["homepageUrl"] == ""
h.delete "isFork"
h.delete "isPrivate"
h.delete "repositoryTopics"
h["owner"] = h["owner"].dup
h["owner"].delete "__typename"
h
end
# Apply overrides
repo_hashes.each do |repo|
nwo = repo["nameWithOwner"]
REPO_OVERRIDES.each do |pattern, fields|
next unless File.fnmatch(pattern, nwo)
repo["topics"] = [fields["topics"]].flatten if fields&.key? "topics"
end
end
repo_description = ([
CONFIG["include_private"] && "public",
CONFIG["include_forks"] && "source",
repo_hashes.length == 1 ? "repo" : "repos"
] - [nil, false]).join(" ")
puts "Writing #{repo_hashes.length} #{repo_description} out of #{repo_count}"
FileUtils.mkdir_p File.dirname(REPO_JSON_PATH)
File.open(REPO_JSON_PATH, "w") do |f|
f << JSON.generate(repo_hashes, **JSON_OPTIONS)
end