On Wed, Nov 04, 2020 at 07:42:15PM +0800, Liu Shaofei wrote:
On Wed, Nov 04, 2020 at 06:18:00PM +0800, Luan Shengde wrote:
parse email for apply account check out commit url check commit url exists check base url in upstream-repos check commit available hub: gitee.com clone the repo and check the commit exists hub: non-gitee.com check the feedback of curl command to check the commit url available check out pub key extract the attachment of the pub_key
Signed-off-by: Luan Shengde shdluan@163.com
.../mail-robot/parse-apply-account-email.rb | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100755 container/mail-robot/parse-apply-account-email.rb
diff --git a/container/mail-robot/parse-apply-account-email.rb b/container/mail-robot/parse-apply-account-email.rb new file mode 100755 index 0000000..1325c8f --- /dev/null +++ b/container/mail-robot/parse-apply-account-email.rb @@ -0,0 +1,135 @@ +#!/usr/bin/env ruby +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true
+require 'json' +require 'mail'
+# check whether there is a commit url and ssh pub_key in the email +# apply account on junper server +# the entry point is parse_commit_url_pub_key +class ParseApplyAccountEmail
- def initialize(mail_content)
- @mail_content = mail_content
- @my_email = mail_content.from[0]
- @my_name = mail_content.From.unparsed_value.gsub(/ <[^<>]*>/, '')
- end
- def parse_commit_url_pub_key
- commit_url = check_out_commit_url
- pub_key = check_out_pub_key
- return commit_url, pub_key
- end
- def send_error_email(error_message)
- send_error_email_info = {
'my_email' => @my_email,
'my_name' => @my_name,
'error_message' => error_message
- }
- send_err_mail = SendApplyAccountEmail.new(send_error_email_info)
- send_err_mail.send_error_email
- end
- def extract_commit_url
- mail_content_body = @mail_content.part[0].part[0].body.decoded || @mail_content.part[0].body.decoded
- mail_content_line = mail_content_body.gsub(/\n/, '')
- no_commit_url unless mail_content_line.match?(%r{my oss commit:\s*https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})
^------- it's better to give some comments for matching rule.
- mail_content_body.match(%r{https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})[0]
^-------ditto
I will add some heredocument for it
Thanks, Luan Shengde
Thanks, Liushaofei
- end
- def no_commit_url
- error_message = 'No matched commit url found'
- send_error_email(error_message)
- raise error_message
- end
- def check_out_commit_url
- url = extract_commit_url
- base_url = url.gsub(%r{/commit/[\w\d]{40}$}, '')
- check_base_url_in_upstream_repos('/c/upstream-repos', base_url)
- check_commit_available(url, base_url)
- return url
- end
- def check_base_url_in_upstream_repos(upstream_dir, base_url)
- Dir.chdir(upstream_dir)
- match_out = %x(grep -rn #{base_url})
- return unless match_out.empty?
- error_message = 'The url is not in upstream-repo list'
- send_error_email(error_message)
- raise error_message
- end
- def check_commit_available(url, base_url)
- hub_name = url.split('/')[2]
- # it requires authentication when execute curl to get the commit infomation
- # clone the repo and then validate the commit for the email address
- if hub_name.eql? 'gitee.com'
check_gitee_commit(url, base_url)
- else
check_non_gitee_commit(url)
- end
- end
- def check_non_gitee_commit(url)
- url_fdback = %x(curl #{url})
- email_index = url_fdback.index @my_email
- return unless email_index.nil?
- error_message = 'Your commit url is not a valid commit url.'
- send_error_email(error_message)
- raise error_message
- end
- def check_gitee_commit(url, base_url)
- repo_dir = url.split('/')[-3]
- repo_url = [base_url, 'git'].join('.')
- commit_id = url.split('/')[-1]
- Dir.chdir '/tmp'
- %x(/usr/bin/git clone #{repo_url} #{repo_dir})
- email_index = %x(/usr/bin/git -C #{repo_dir} show #{commit_id}).index @my_email
- FileUtils.rm_rf repo_dir
- check_gitee_commit_exist(email_index)
- end
- def check_gitee_commit_exist(email_index)
- return unless email_index.nil?
- error_message = 'Your commit url is not a valid commit url.'
- send_error_email(error_message)
- raise error_message
- end
- def check_out_pub_key
- pub_key = @mail_content.part[1].body.decoded if @mail_content.part[1].filename == 'id_rsa.pub'
- check_pub_key(pub_key)
- return pub_key
- end
- def check_pub_key(pub_key)
- return unless pub_key.nil?
- error_message = 'No pub_key found'
- send_error_email(error_message)
- raise error_message
- end
+end
2.23.0