-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_github_prs.10m.rb
executable file
·77 lines (66 loc) · 2.16 KB
/
open_github_prs.10m.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
#!/usr/bin/env ruby
#
# <bitbar.title>Open GitHub PRs for users</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Tim Regener</bitbar.author>
# <bitbar.author.github>timlapluie</bitbar.author.github>
# <bitbar.desc>List all open Github PR's for the given users.</bitbar.desc>
# <bitbar.dependencies>ruby</bitbar.dependencies>
#
require 'date'
require 'json'
require 'net/http'
### CONFIG ###
# Create a personal access token: https://github.com/settings/tokens
GITHUB_AUTH_TOKEN = 'YOUR_TOKEN_HERE'.freeze
# Show only PR's from this users
# e.g. GITHUB_USERS = ['timlapluie']
GITHUB_USERS = []
### END CONFIG ###
def get_pr_data(author)
graphql_query = "\{user(login:\"#{author}\")\{pullRequests(states:[OPEN],last:50)\{nodes\{title,url,createdAt,mergeStateStatus,author{login},repository\{name,url\}\}\}\}\}"
uri = URI.parse("https://api.github.com/graphql")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, 'Authorization' => 'token ' + GITHUB_AUTH_TOKEN, 'Accept' => 'application/vnd.github.merge-info-preview+json')
request.body = { query: graphql_query }.to_json
response = http.request(request)
JSON.parse(response.body).dig('data', 'user', 'pullRequests', 'nodes') || []
end
def state_icon(state)
{
behind: '💕',
blocked: '💛',
clean: '💚',
dirty: '💔',
draft: '🤍',
has_hooks: '💜',
unknown: '🖤',
unstable: '💔'
}[state.downcase.to_sym]
end
def pr_line(pr)
"#{state_icon(pr['mergeStateStatus'])}#{pr['title']} (@#{pr['author']['login']}, #{Date.parse(pr['createdAt']).iso8601})|href=#{pr['url']}"
end
begin
pr_details = ''
open_prs = []
GITHUB_USERS.each do |author|
open_prs += get_pr_data(author)
end
open_prs = open_prs.compact.select do |pr|
pr['url'].include?('github.com/Sage')
end
open_prs.group_by { |pr| pr['repository']['name'] }.sort.each do |repo, prs|
pr_details += "---\n[#{repo}]\n"
prs.each do |pr|
pr_details += "#{pr_line(pr)}\n"
end
end
puts "Open PRs (#{open_prs.count})"
puts pr_details
rescue
puts '⚡️'
puts '---'
puts 'An error occurred while fetching PR data.'
end