forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-open-pull-requests.template.rb
executable file
·67 lines (58 loc) · 1.63 KB
/
github-open-pull-requests.template.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
#!/usr/bin/env ruby
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Open Pull Requests
# @raycast.mode inline
# @raycast.description Display number of open pull requests
#
# Optional parameters:
# @raycast.packageName GitHub
# @raycast.icon images/github-logo.png
# @raycast.iconDark images/github-logo-iconDark.png
#
# Conditional parameters:
# @raycast.refreshTime 5m
#
# Credits
# @raycast.author Faye Sipiano
# @raycast.authorURL https://github.com/FSipiano
require 'json'
require 'net/http'
require 'uri'
# Insert a personal access token (https://github.com/settings/tokens)
API_TOKEN = ''
if API_TOKEN.empty?
puts 'No API token provided'
exit(1)
end
RED = 31
GREEN = 32
YELLOW = 33
uri = URI('https://api.github.com/graphql')
req = Net::HTTP::Post.new(uri)
req['Authorization'] = "token #{API_TOKEN}"
req.body = '{ "query": "query { viewer { pullRequests(first: 10, states:OPEN) { nodes { title number url } } } }" }'
req_options = {
use_ssl: uri.scheme == 'https'
}
res = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(req)
end
if res.code == '200'
result = JSON.parse(res.body)
pull_requests = result['data']['viewer']['pullRequests']['nodes']
if pull_requests.length == 0
message = "No open pull requests 🎉"
puts "\e[#{GREEN}m#{message}\e[0m"
else
color = (pull_requests.length <= 5) ? YELLOW : RED
message = "You have #{pull_requests.length} open pull requests"
puts "\e[#{color}m#{message}\e[0m"
pull_requests.each do |pr|
puts "##{pr['number']} #{pr['title']} (#{pr['url']})"
end
end
else
puts 'Failed loading GitHub pull requests'
exit(1)
end