huginn_github_autosync_fork_agent 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d76b8b6b6b86db78987291102fa6a5b70a3544a1ea793db484ff010aee0e01c
4
+ data.tar.gz: ec82903745a80423cb94e441bf9e548f611d9e1151d7c13c759d2a658bf3df57
5
+ SHA512:
6
+ metadata.gz: a2ebb6c5ff774b215b70d45fb180351c446e6fd69256c9f329147f93a6b7eaef995193e8b9834de75ab1643f65b6659cb5b37743b723be63f9b99dee2f069ebb
7
+ data.tar.gz: cc56b0d3e8f55bc986d46d9c5445c6a83fa23c1c96d060d7a96c1fe63cc92e2ef87221c73a0afca210e018aff399c4d049ec8d6d4c45c31b824f6d5f15f961e8
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2024 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,188 @@
1
+ module Agents
2
+ class GithubAutosyncForkAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_12h'
7
+
8
+ description do
9
+ <<-MD
10
+ The Github Autosync Fork agent checks if a refresh is needed from the source.
11
+
12
+ `repository` for the wanted repository to check.
13
+
14
+ `token` is needed for queries.
15
+
16
+ `debug` is used for verbose mode.
17
+
18
+ `src_branch` is the branch's name for the source.
19
+
20
+ `tgt_branch` is the branch's name for the target.
21
+
22
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
23
+ that you anticipate passing without this Agent receiving an incoming Event.
24
+ MD
25
+ end
26
+
27
+ event_description <<-MD
28
+ Events look like this:
29
+
30
+ {
31
+ "message": "Successfully fetched and fast-forwarded from upstream q9f:main.",
32
+ "merge_type": "fast-forward",
33
+ "base_branch": "q9f:main"
34
+ }
35
+ MD
36
+
37
+ def default_options
38
+ {
39
+ 'repository' => '',
40
+ 'src_branch' => 'master',
41
+ 'tgt_branch' => 'master',
42
+ 'debug' => 'false',
43
+ 'expected_receive_period_in_days' => '2',
44
+ 'token' => ''
45
+ }
46
+ end
47
+
48
+ form_configurable :repository, type: :string
49
+ form_configurable :src_branch, type: :string
50
+ form_configurable :tgt_branch, type: :string
51
+ form_configurable :debug, type: :boolean
52
+ form_configurable :token, type: :string
53
+ form_configurable :expected_receive_period_in_days, type: :string
54
+
55
+ def validate_options
56
+ unless options['repository'].present?
57
+ errors.add(:base, "repository is a required field")
58
+ end
59
+
60
+ unless options['src_branch'].present?
61
+ errors.add(:base, "src_branch is a required field")
62
+ end
63
+
64
+ unless options['tgt_branch'].present?
65
+ errors.add(:base, "tgt_branch is a required field")
66
+ end
67
+
68
+ if options.has_key?('debug') && boolify(options['debug']).nil?
69
+ errors.add(:base, "if provided, debug must be true or false")
70
+ end
71
+
72
+ unless options['token'].present?
73
+ errors.add(:base, "token is a required field")
74
+ end
75
+
76
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
77
+ 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")
78
+ end
79
+ end
80
+
81
+ def working?
82
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
83
+ end
84
+
85
+ def check
86
+ check_sync
87
+ end
88
+
89
+ private
90
+
91
+ def log_curl_output(code,body)
92
+
93
+ log "request status : #{code}"
94
+
95
+ if interpolated['debug'] == 'true'
96
+ log "body"
97
+ log body
98
+ end
99
+
100
+ end
101
+
102
+ def get_repos_info
103
+ uri = URI.parse("https://api.github.com/repos/#{interpolated['repository']}")
104
+ request = Net::HTTP::Get.new(uri)
105
+ request["Accept"] = "application/vnd.github+json"
106
+ request["Authorization"] = "token #{interpolated['token']}"
107
+ request["X-Github-Api-Version"] = "2022-11-28"
108
+
109
+ req_options = {
110
+ use_ssl: uri.scheme == "https",
111
+ }
112
+
113
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
114
+ http.request(request)
115
+ end
116
+
117
+ log_curl_output(response.code,response.body)
118
+
119
+ payload = JSON.parse(response.body)
120
+
121
+ compare(payload)
122
+
123
+ end
124
+
125
+ def compare(repo_info)
126
+
127
+ uri = URI.parse("#{repo_info['parent']['url']}/compare/#{interpolated['src_branch']}...#{repo_info['owner']['login']}:#{interpolated['tgt_branch']}")
128
+ request = Net::HTTP::Get.new(uri)
129
+ request["Accept"] = "application/vnd.github+json"
130
+ request["Authorization"] = "token #{interpolated['token']}"
131
+ request["X-Github-Api-Version"] = "2022-11-28"
132
+
133
+ req_options = {
134
+ use_ssl: uri.scheme == "https",
135
+ }
136
+
137
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
138
+ http.request(request)
139
+ end
140
+
141
+ log_curl_output(response.code,response.body)
142
+
143
+ payload = JSON.parse(response.body)
144
+
145
+ trigger_sync(payload)
146
+
147
+ end
148
+
149
+ def trigger_sync(compare_result)
150
+
151
+ if compare_result['behind_by'] != 0
152
+
153
+ log "we are at #{compare_result['behind_by']} commit(s) behind"
154
+ uri = URI.parse("https://api.github.com/repos/#{interpolated['repository']}/merge-upstream")
155
+ request = Net::HTTP::Post.new(uri)
156
+ request["Accept"] = "application/vnd.github+json"
157
+ request["Authorization"] = "token #{interpolated['token']}"
158
+ request["X-Github-Api-Version"] = "2022-11-28"
159
+ request.body = JSON.dump({
160
+ "branch" => interpolated['tgt_branch']
161
+ })
162
+
163
+ req_options = {
164
+ use_ssl: uri.scheme == "https",
165
+ }
166
+
167
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
168
+ http.request(request)
169
+ end
170
+
171
+ log_curl_output(response.code,response.body)
172
+
173
+ payload = JSON.parse(response.body)
174
+
175
+ create_event payload: payload
176
+ else
177
+ log "already up to date, nothing to sync"
178
+ end
179
+
180
+ end
181
+
182
+ def check_sync
183
+
184
+ get_repos_info
185
+
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_github_autosync_fork_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_github_autosync_fork_agent/github_autosync_fork_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::GithubAutosyncForkAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::GithubAutosyncForkAgent.new.default_options
7
+ @checker = Agents::GithubAutosyncForkAgent.new(:name => "GithubAutosyncForkAgent", :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_autosync_fork_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: 2024-11-22 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_autosync_fork_agent.rb
64
+ - lib/huginn_github_autosync_fork_agent/github_autosync_fork_agent.rb
65
+ - spec/github_autosync_fork_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_github_autosync_fork_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_autosync_fork_agent_spec.rb