webhook will send a message when user push.
try to find the git_repo when get url of the repository.
if found, push it to queue. if not found, it maybe an illegal request,
do nothing
Signed-off-by: Li Yuanchao <lyc163mail(a)163.com>
---
lib/git_mirror.rb | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/lib/git_mirror.rb b/lib/git_mirror.rb
index 65d6940..11cf3f1 100644
--- a/lib/git_mirror.rb
+++ b/lib/git_mirror.rb
@@ -88,10 +88,16 @@ class MirrorMain
@git_queue = Queue.new
@es_client = Elasticsearch::Client.new(url: "http://#{ES_HOST}:#{ES_PORT}")
load_fork_info
+ connection_init
+ handle_webhook
+ end
+
+ def connection_init
connection = Bunny.new('amqp://172.17.0.1:5672')
connection.start
channel = connection.create_channel
@message_queue = channel.queue('new_refs')
+ @webhook_queue = channel.queue('web_hook')
end
def fork_stat_init(stat_key)
@@ -287,3 +293,35 @@ class MirrorMain
es_repo_update(git_repo)
end
end
+
+# main thread
+class MirrorMain
+ def check_git_repo(git_repo, webhook_url)
+ return @git_info.key?(git_repo) && Array(@git_info[git_repo]['url'])[0] == webhook_url
+ end
+
+ def get_git_repo(webhook_url)
+ strings = webhook_url.split('/')
+ project = strings[-1]
+ fork_name = strings[-2]
+
+ git_repo = "#{project}/#{project}"
+ return git_repo if check_git_repo(git_repo, webhook_url)
+
+ git_repo = "#{project}/#{fork_name}"
+ return git_repo if check_git_repo(git_repo, webhook_url)
+
+ git_repo = "#{project[0]}/#{project}/#{project}"
+ return git_repo if check_git_repo(git_repo, webhook_url)
+ end
+
+ def handle_webhook
+ Thread.new do
+ @webhook_queue.subscribe(block: true) do |_delivery, _properties, webhook_url|
+ git_repo = get_git_repo(webhook_url)
+ do_push(git_repo) if git_repo
+ sleep(0.1)
+ end
+ end
+ end
+end
--
2.23.0