- Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathgithub_issues.check
More file actions
Latest commit
executable file
·145 lines (117 loc) · 3.2 KB
/
Copy pathgithub_issues.check
File metadata and controls
executable file
·145 lines (117 loc) · 3.2 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
#!/usr/bin/env ruby
require"rubygems"
require"time"
require"json"
require"open-uri"
require"net/https"
classGithubIssue
definitialize(hash)
@hash=hash
end
defpull_request?
@hash["pull_request"] && @hash["pull_request"]["html_url"]
end
defcomments_count
@hash["comments"]
end
defupdated_at
@updated_at ||=
Time.parse(@hash["updated_at"])
end
defformatted_updated_at
time_ago(updated_at)
end
defformatted_title
"#{formatted_updated_at} [#{comments_count}] #{"[PR]"ifpull_request?}"
end
defformatted_description
@hash["title"].gsub(/[\r\n\t]/," ")[0..100] + "..."
end
private
# https://gist.github.com/3808180
deftime_ago(time)
casedelta=(Time.now.to_i - time.to_i)
when0..119then"-1 min"
when120..3599then"-#{delta / 60} mins"
when3600..86399then"-#{(delta / 3600).round} hours"
else"-#{(delta / 86400).round} days"
end
end
end
classGithubIssuesStatus
definitialize(issues_url,json)
@issues_url=issues_url
@issues=
Array(JSON.parse(json)) \
.map{ |e| GithubIssue.new(e)}
rescueJSON::ParserError
raiseRuntimeError,"invalid json: '#{json}'"
end
defany_issues_with_0_comments?
@issues.any?{ |i| i.comments_count.zero?}
end
defas_json(*)
{
:result=>@issues.empty?,
:changing=>any_issues_with_0_comments?,
:url=>@issues_url,
:info=>info
}
end
defto_json(*)
JSON.dump(as_json)
end
private
definfo
@issues.inject([])do |result,issue|
result.concat(issue_details(issue))
result
end[0..-2]
end
defissue_details(issue)
[[issue.formatted_title,issue.formatted_description],
["-",""]]
end
end
classGithubIssues
definitialize(repo_owner,repo_name)
raiseArgumentError"repo_owner must not be nil" \
unless@repo_owner=repo_owner
raiseArgumentError"repo_name must not be nil" \
unless@repo_name=repo_name
end
deflatest_status
GithubIssuesStatus.new(issues_url,force_utf8(http_get))
end
private
defhttp_get
# Github rate limits unauthenticated requests to the api by ip.
# To obtains client_id/client_secret just create new application on
# `https://github.com/settings/applications`.
ifENV.keys.grep(/GITHUB_ISSUES_CHECK/)
auth="client_id=#{ENV["GITHUB_ISSUES_CHECK_CLIENT_ID"]}"
auth += "&client_secret=#{ENV["GITHUB_ISSUES_CHECK_CLIENT_SECRET"]}"
end
http_opts={
"User-Agent"=>"CheckmanGithubIssues (Hostname: #{`printf "%s" "$HOSTNAME"`})"
}
open("#{api_url}?#{auth}",http_opts).read
end
defapi_url
"https://api.github.com/repos/#{@repo_owner}/#{@repo_name}/issues"
end
defissues_url
"https://github.com/#{@repo_owner}/#{@repo_name}/issues"
end
defforce_utf8(string)
ifstring.respond_to?(:encode)
# Use 'binary' as source encoding since conversion
# to the same encoding is a no-op even if there are
# invalid bytes.
string.encode(string.encoding,"binary",:invalid=>:replace,:undef=>:replace)
else
string
end
end
end
putsGithubIssues.new(*ARGV).latest_status.to_jsonif__FILE__ == $0