-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_github_metadata
More file actions
executable file
·178 lines (156 loc) · 4.87 KB
/
Copy pathget_github_metadata
File metadata and controls
executable file
·178 lines (156 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/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