-
Notifications
You must be signed in to change notification settings - Fork 1
/
changes.rb
155 lines (135 loc) · 3.66 KB
/
changes.rb
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
# frozen_string_literal: true
require "dotenv"
Dotenv.load
require "graphql/client"
require "graphql/client/http"
# https://docs.github.com/en/graphql/
module GitHub
# Configure GraphQL endpoint using the basic HTTP network adapter.
HTTP = GraphQL::Client::HTTP.new("https://api.github.com/graphql") do
def headers(context)
# Optionally set any HTTP headers
{
"Authorization" => "bearer #{ENV.fetch('GITHUB_PERSONAL_ACCESS_TOKEN')}",
"User-Agent" => "Changelogger CLI"
}
end
end
# Fetch latest schema on init, this will make a network request
Schema = GraphQL::Client.load_schema(HTTP)
Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
end
PullRequestsOnRepositoryQuery = GitHub::Client.parse <<-'GRAPHQL'
query($repo_name: String!, $owner: String!) {
repository(name: $repo_name, owner: $owner) {
milestones(first: 10, orderBy: {direction: DESC, field: DUE_DATE}) {
nodes {
dueOn
title
issues(first:100) {
nodes {
closed
title
url
labels(first: 100) {
nodes {
name
}
}
}
}
pullRequests(first: 100) {
nodes {
closed
title
url
labels(first: 100) {
nodes {
name
}
}
}
}
}
}
}
}
GRAPHQL
def find_milestone_with_title(pull_requests, title)
pull_requests.data.repository.milestones.nodes.find { |ms|
ms.title.casecmp?(title)
}
end
def recently_completed_milestone(pull_requests)
pull_requests.data.repository.milestones.nodes.find { |ms|
Time.parse(ms.due_on) < Time.now
}
end
repo = ARGV[0]
raise "Repository name must be supplied as ARGV[0]" if repo.nil?
owner, repo_name = repo.split("/")
pull_requests = GitHub::Client.query(
PullRequestsOnRepositoryQuery,
:variables => {
:owner => owner,
:repo_name => repo_name,
}
)
response = pull_requests.to_h
raise response["errors"].inspect if response["errors"]
milestone_name = ARGV[1]
milestone = if milestone_name
find_milestone_with_title(pull_requests, milestone_name)
else
recently_completed_milestone(pull_requests)
end
puts milestone.title
puts "-" * milestone.title.size
puts
changes = {
:regular => [],
:security => [],
:dependencies => [],
}
milestone.issues.nodes.each do |pull_request|
next unless pull_request.closed
labels = pull_request.labels.nodes.map(&:name)
if labels.include?("security")
changes[:security] << pull_request
elsif labels.include?("dependencies")
changes[:dependencies] << pull_request
else
changes[:regular] << pull_request
end
end
milestone.pull_requests.nodes.each do |pull_request|
next unless pull_request.closed
labels = pull_request.labels.nodes.map(&:name)
if labels.include?("security")
changes[:security] << pull_request
elsif labels.include?("dependencies")
changes[:dependencies] << pull_request
else
changes[:regular] << pull_request
end
end
[:regular, :security].each do |category|
changes[category].sort_by(&:title).each do |pull_request|
puts "* #{pull_request.title}: #{pull_request.url}"
puts
end
end
bumped_dependencies = changes[:dependencies].
map(&:title).
flat_map { |title|
singular_bump = title.scan(/Bump (.*?) from/)
multiple_bump = title.scan(/(?:,|\s)(.+?)(?:,|\s|\z)/)
if singular_bump.any?
singular_bump.flatten
elsif multiple_bump.any?
multiple_bump.flatten
end
}.
sort.
uniq
puts "* Bumped #{bumped_dependencies.join(', ')}."