Skip to content

Commit 1ec0f69

Browse files
committed
Add minimal jenkins plugin to display failed jobs
1 parent 998ad91 commit 1ec0f69

File tree

6 files changed

+92
-6
lines changed

6 files changed

+92
-6
lines changed

EV0002.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_relative "plugins/playstore_reviews"
1717
require_relative "plugins/twitter_webhooks"
1818
require_relative "plugins/discourse_webhooks"
19+
require_relative "plugins/jenkins_failures"
1920

2021
PWD = File.dirname(File.expand_path(__FILE__))
2122

@@ -48,7 +49,8 @@
4849
Cinch::BlogWebhooks,
4950
Cinch::PlayStoreReviews,
5051
Cinch::TwitterWebhooks,
51-
Cinch::DiscourseWebhooks
52+
Cinch::DiscourseWebhooks,
53+
Cinch::JenkinsFailures
5254
]
5355
end
5456

@@ -110,6 +112,13 @@
110112
:secret => $secrets["discourse_hooks"]["secret"]
111113
}
112114

115+
config.plugins.options[Cinch::JenkinsFailures] = {
116+
:server => "https://ci.easyrpg.org/",
117+
:view => "failing",
118+
:user => $secrets["jenkins_failures"]["user"],
119+
:pass => $secrets["jenkins_failures"]["pass"]
120+
}
121+
113122
# log to file
114123
file = File.open("#{PWD}/data/bot.log", "a")
115124
file.sync = true

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ gem "sinatra"
88
gem "chronic"
99
gem "googleauth"
1010
gem "http"
11+
gem "jenkins2-api"
1112

1213
gem "xmlrpc" if RUBY_VERSION >= "2.4.0"
1314

Gemfile.lock

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ GEM
5454
http-cookie (1.0.3)
5555
domain_name (~> 0.5)
5656
http-form_data (2.3.0)
57+
jenkins2-api (1.0.17)
58+
thor (~> 0.19)
5759
json (2.5.1)
5860
jwt (2.2.3)
5961
llhttp-ffi (0.0.1)
@@ -96,6 +98,7 @@ GEM
9698
daemons (~> 1.0, >= 1.0.9)
9799
eventmachine (~> 1.0, >= 1.0.4)
98100
rack (>= 1, < 3)
101+
thor (0.20.3)
99102
tilt (2.0.10)
100103
time (0.1.0)
101104
time-lord (1.0.1)
@@ -115,6 +118,7 @@ DEPENDENCIES
115118
cinch-seen
116119
googleauth
117120
http
121+
jenkins2-api
118122
json
119123
sinatra
120124
thin

README.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ Needed gems besides `cinch` are (see Gemfile for details):
1515
* [thin][thin]
1616
* [sinatra][sinatra]
1717
* [chronic][chronic]
18-
* [http][http]
1918
* [googleauth][googleauth]
19+
* [http][http]
20+
* [jenkins2-api][jenkins2-api]
2021
* [xmlrpc][xmlrpc] (for ruby 2.4+)
2122

22-
Deprecated/Optional gems:
23-
24-
* [cinch-identify][cinch-identify], SASL is preferred now.
25-
2623
You can use bundler to install them and their dependencies.
2724

2825
$ bundle config set --local path 'vendor/bundle'
@@ -111,6 +108,15 @@ https://github.com/Quintus/cinch-plugins
111108
happens on Twitter. This uses the webserver provided by http_server.rb and
112109
relies on the Zapier service and Twitter api.
113110

111+
* plugins/discourse_webhooks.rb:
112+
113+
Uses webhooks to provide channel notifications, when a new topic is added on the forums.
114+
115+
* plugins/jenkins_failures.rb:
116+
117+
Uses the Jenkins API (pulled with a timer) to provide channel notifications about
118+
failed builds.
119+
114120
[webchat]: https://kiwiirc.com/nextclient/#ircs://irc.libera.chat/#easyrpg?nick=rpgguest??
115121
[cinch]: https://github.com/cinchrb/cinch
116122
[ev0001]: https://github.com/EasyRPG/EV0001
@@ -122,5 +128,6 @@ https://github.com/Quintus/cinch-plugins
122128
[chronic]: https://github.com/mojombo/chronic
123129
[http]: https://github.com/httprb/http
124130
[googleauth]: https://github.com/google/google-auth-library-ruby
131+
[jenkins2-api]: https://github.com/yitsushi/jenkins2-api
125132
[xmlrpc]: https://github.com/ruby/xmlrpc
126133
[quintus]: https://github.com/Quintus

plugins/jenkins_failures.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# This cinch plugin is part of EV0002
3+
#
4+
# written by carstene1ns <dev @ f4ke . de> 2021
5+
# available under ISC license
6+
#
7+
8+
require 'jenkins2-api'
9+
10+
class Cinch::JenkinsFailures
11+
include Cinch::Plugin
12+
13+
# Every 10 minutes
14+
timer 10 * 60, method: :get_failures
15+
16+
def get_failures
17+
server = config[:server]
18+
user = config[:user]
19+
pass = config[:pass]
20+
view = config[:view]
21+
22+
# not found, ignore
23+
return if server.nil? or user.nil? or pass.nil? or view.nil?
24+
25+
client = Jenkins2API::Client.new(
26+
:server => server,
27+
:username => user,
28+
:password => pass
29+
)
30+
31+
# get failed jobs, ignoring pull requests
32+
jobs = client.job.list
33+
failed = Array.new
34+
jobs.each do |job|
35+
if job['color'] == "red" and job['name'] !~ /.*-pr/
36+
failed.push(job['name'])
37+
end
38+
end
39+
40+
return if failed.empty?
41+
42+
# filter last 10 minutes
43+
recent = Array.new
44+
failed.each do |job|
45+
build = client.build.latest(job)
46+
endtime = Time.at((build['timestamp'].to_i + build['duration'].to_i) / 1000)
47+
recent.push(job) if endtime > Time.now - (10 * 60)
48+
end
49+
50+
return if recent.empty?
51+
52+
shown = recent.join(",")
53+
message = "New failing jobs (see " + server + "view/" + view + "/ for details): "
54+
55+
# add up to 200 characters
56+
message << "\n" + Format(:red, shown[0, 200])
57+
message << "…" if shown.length > 200
58+
59+
bot.channels[0].send("[Jenkins] #{message}")
60+
end
61+
end

template.secrets.yml

+4
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ twitter_hooks:
2121

2222
discourse_hooks:
2323
secret: SomethingSecret
24+
25+
jenkins_failures:
26+
user: username
27+
pass: passorapikey

0 commit comments

Comments
 (0)