huginn_github_autocommit_agent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb623009f09d1a7d4360101c50d62f85273c7ff07a55391a0daa5c362f832e14
4
+ data.tar.gz: f9b9e84d26bdff6f718203b9d196172d36bea1b8c7dda2b6de99208b5452c105
5
+ SHA512:
6
+ metadata.gz: 6d5d238102609b5f7349f4d0025ca882bfd9cc02b9677d317b50dad33413ad1370d5c64d2dea46b1de8d467536a054fff02e5f6d22ad66ab3bdfd32de3cd295f
7
+ data.tar.gz: 36ad7d07350a0c00f3649efe8f6c5832295b12fc2a238f26cadc9413ec1273241bd1103d19b60fee6fbd8dcf8e3aeb3274b554c547c4a69a66ffa1efc3705a36
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2023 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,273 @@
1
+ module Agents
2
+ class GithubAutocommitAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_1h'
7
+
8
+ description do
9
+ <<-MD
10
+ The Github autocommit agent updates content for repositories and creates an event if wanted.
11
+
12
+ `repository` is the name of the repository.
13
+
14
+ `version` is the version wanted.
15
+
16
+ `commit_message` is the message in the commit.
17
+
18
+ `committer_name` is the name of the committer.
19
+
20
+ `committer_email` is the email of the committer.
21
+
22
+ `token` is needed for authentication
23
+
24
+ `rules` is needed to know information about the target repository.
25
+
26
+ `emit_events` is for creating an event.
27
+
28
+ `debug` to add verbosity.
29
+
30
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
31
+ that you anticipate passing without this Agent receiving an incoming Event.
32
+ MD
33
+ end
34
+
35
+ event_description <<-MD
36
+ Events look like this:
37
+
38
+ {
39
+ "content": {
40
+ "name": "Dockerfile",
41
+ "path": "Dockerfile",
42
+ "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
43
+ "size": 541,
44
+ "url": "https://api.github.com/repos/hihouhou/docker-test/contents/Dockerfile?ref=master",
45
+ "html_url": "https://github.com/hihouhou/docker-test/blob/master/Dockerfile",
46
+ "git_url": "https://api.github.com/repos/hihouhou/docker-test/git/blobs/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
47
+ "download_url": "https://raw.githubusercontent.com/hihouhou/docker-test/master/Dockerfile",
48
+ "type": "file",
49
+ "_links": {
50
+ "self": "https://api.github.com/repos/hihouhou/docker-test/contents/Dockerfile?ref=master",
51
+ "git": "https://api.github.com/repos/hihouhou/docker-test/git/blobs/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
52
+ "html": "https://github.com/hihouhou/docker-test/blob/master/Dockerfile"
53
+ }
54
+ },
55
+ "commit": {
56
+ "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
57
+ "node_id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
58
+ "url": "https://api.github.com/repos/hihouhou/docker-test/git/commits/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
59
+ "html_url": "https://github.com/hihouhou/docker-test/commit/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
60
+ "author": {
61
+ "name": "XXXXXXXX",
62
+ "email": "XXXXXXXXXXXXXXXXXXXXX",
63
+ "date": "2023-10-12T23:42:46Z"
64
+ },
65
+ "committer": {
66
+ "name": "XXXXXXXX",
67
+ "email": "XXXXXXXXXXXXXXXXXXXXX",
68
+ "date": "2023-10-12T23:42:46Z"
69
+ },
70
+ "tree": {
71
+ "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
72
+ "url": "https://api.github.com/repos/hihouhou/docker-test/git/trees/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
73
+ },
74
+ "message": "my commit message",
75
+ "parents": [
76
+ {
77
+ "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
78
+ "url": "https://api.github.com/repos/hihouhou/docker-test/git/commits/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
79
+ "html_url": "https://github.com/hihouhou/docker-test/commit/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
80
+ }
81
+ ],
82
+ "verification": {
83
+ "verified": false,
84
+ "reason": "unsigned",
85
+ "signature": null,
86
+ "payload": null
87
+ }
88
+ }
89
+ }
90
+ MD
91
+
92
+ def default_options
93
+ {
94
+ 'repository' => '',
95
+ 'version' => '',
96
+ 'commit_message' => '',
97
+ 'committer_name' => '',
98
+ 'committer_email' => '',
99
+ 'emit_events' => 'true',
100
+ 'expected_receive_period_in_days' => '2',
101
+ 'token' => '',
102
+ 'rules' => '[{name: "", owner: "", my_repository: "", pattern: "", file: "" }'
103
+ }
104
+ end
105
+
106
+ form_configurable :repository, type: :string
107
+ form_configurable :version, type: :string
108
+ form_configurable :commit_message, type: :string
109
+ form_configurable :committer_name, type: :string
110
+ form_configurable :committer_email, type: :string
111
+ form_configurable :token, type: :string
112
+ form_configurable :rules, type: :string
113
+ form_configurable :emit_events, type: :boolean
114
+ form_configurable :debug, type: :boolean
115
+ form_configurable :expected_receive_period_in_days, type: :string
116
+
117
+ def validate_options
118
+ unless options['repository'].present?
119
+ errors.add(:base, "repository is a required field")
120
+ end
121
+
122
+ unless options['version'].present?
123
+ errors.add(:base, "version is a required field")
124
+ end
125
+
126
+ unless options['commit_message'].present?
127
+ errors.add(:base, "commit_message is a required field")
128
+ end
129
+
130
+ unless options['committer_name'].present?
131
+ errors.add(:base, "committer_name is a required field")
132
+ end
133
+
134
+ unless options['committer_email'].present?
135
+ errors.add(:base, "committer_email is a required field")
136
+ end
137
+
138
+
139
+ unless options['token'].present?
140
+ errors.add(:base, "token is a required field")
141
+ end
142
+ #
143
+ # if options[:type] == :array && (options[:values].blank? || !options[:values].is_a?(Array))
144
+ # raise ArgumentError.new('When using :array as :type you need to provide the :values as an Array')
145
+ # end
146
+
147
+ if options.has_key?('add_release_details') && boolify(options['add_release_details']).nil?
148
+ errors.add(:base, "if provided, add_release_details must be true or false")
149
+ end
150
+
151
+ if options.has_key?('debug') && boolify(options['debug']).nil?
152
+ errors.add(:base, "if provided, debug must be true or false")
153
+ end
154
+
155
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
156
+ errors.add(:base, "if provided, emit_events must be true or false")
157
+ end
158
+
159
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
160
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
161
+ end
162
+ end
163
+
164
+ def working?
165
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
166
+ end
167
+
168
+ def receive(incoming_events)
169
+ incoming_events.each do |event|
170
+ interpolate_with(event) do
171
+ log event
172
+ commit
173
+ end
174
+ end
175
+ end
176
+
177
+ def check
178
+ commit
179
+ end
180
+
181
+ private
182
+
183
+ def log_curl_output(code,body)
184
+
185
+ log "request status : #{code}"
186
+
187
+ if interpolated['debug'] == 'true'
188
+ log "body"
189
+ log body
190
+ end
191
+
192
+ end
193
+
194
+ def update_content(owner,repository,filepath,content,sha)
195
+ encoded_content = Base64.strict_encode64(content)
196
+
197
+ uri = URI.parse("https://api.github.com/repos/#{owner}/#{repository}/contents/#{filepath}")
198
+ request = Net::HTTP::Put.new(uri)
199
+ request["Content-Type"] = "application/json"
200
+ request["Accept"] = "application/vnd.github+json"
201
+ request["Authorization"] = "token #{interpolated['token']}"
202
+ request["X-Github-Api-Version"] = "2022-11-28"
203
+ request.body = JSON.dump({
204
+ "message" => "#{interpolated['commit_message']}",
205
+ "committer" => {
206
+ "name" => "#{interpolated['committer_name']}",
207
+ "email" => "#{interpolated['committer_email']}"
208
+ },
209
+ "content" => "#{encoded_content}",
210
+ "sha" => "#{sha}"
211
+ })
212
+
213
+ req_options = {
214
+ use_ssl: uri.scheme == "https",
215
+ }
216
+
217
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
218
+ http.request(request)
219
+ end
220
+
221
+ log_curl_output(response.code,response.body)
222
+
223
+ if interpolated['emit_events'] == 'true'
224
+ create_event payload: response.body
225
+ end
226
+ end
227
+
228
+ def commit
229
+ to_apply = {}
230
+ JSON.parse(interpolated['rules']).each do | rule |
231
+ if interpolated['repository'] == rule['name']
232
+ to_apply = rule
233
+ if interpolated['debug'] == 'true'
234
+ log "found"
235
+ end
236
+ else
237
+ if interpolated['debug'] == 'true'
238
+ log "not found"
239
+ end
240
+ end
241
+ end
242
+
243
+ uri = URI.parse("https://api.github.com/repos/#{to_apply['owner']}/#{to_apply['my_repository']}/contents/#{to_apply['file']}")
244
+ request = Net::HTTP::Get.new(uri)
245
+ request["Authorization"] = "token #{interpolated['token']}"
246
+ # request["Authorization"] = interpolated['token']
247
+
248
+ req_options = {
249
+ use_ssl: uri.scheme == "https",
250
+ }
251
+
252
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
253
+ http.request(request)
254
+ end
255
+
256
+ log_curl_output(response.code,response.body)
257
+
258
+ data = JSON.parse(response.body)
259
+ file_content_base64 = data['content']
260
+ sha = data['sha']
261
+
262
+ file_content = Base64.decode64(file_content_base64)
263
+ current_version = file_content.match(/^ENV #{to_apply['pattern']} (.*)/)
264
+ if current_version && current_version[1] >= interpolated['version']
265
+ log "The current version is already greater than or equal to the given version. No replacement necessary."
266
+ else
267
+ file_content.gsub!(/^ENV #{to_apply['pattern']}.*/, "ENV #{to_apply['pattern']} #{interpolated['version']}")
268
+ update_content(to_apply['owner'],to_apply['my_repository'],to_apply['file'],file_content,sha)
269
+ # log file_content
270
+ end
271
+ end
272
+ end
273
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_github_autocommit_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_github_autocommit_agent/github_autocommit_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::GithubAutocommitAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::GithubAutocommitAgent.new.default_options
7
+ @checker = Agents::GithubAutocommitAgent.new(:name => "GithubAutocommitAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_github_autocommit_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_github_autocommit_agent.rb
64
+ - lib/huginn_github_autocommit_agent/github_autocommit_agent.rb
65
+ - spec/github_autocommit_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_github_autocommit_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/github_autocommit_agent_spec.rb