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}}) + + mail_content_body.match(%r{https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})[0] + 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
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
If this file is in container directory, It is better not to carry the file name extension.
Thanks, Baijing
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}})
- mail_content_body.match(%r{https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})[0]
- 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
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 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
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
Better move gitee code out to another class.
Please remove most check_ prefixes. It's meaningless when it's used everywhere. Choose more descriptive verbs.
Please replace the below English paragraph with function names so others can directly map the logic to the code. And use descriptive function names.
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
This wrapper function can be removed.
- 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}})
- mail_content_body.match(%r{https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})[0]
- 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
On Thu, Nov 05, 2020 at 10:00:43AM +0800, Wu Fengguang wrote:
Better move gitee code out to another class.
Please remove most check_ prefixes. It's meaningless when it's used everywhere. Choose more descriptive verbs.
Please replace the below English paragraph with function names so others can directly map the logic to the code. And use descriptive function names.
I got it
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
This wrapper function can be removed.
I have fixed it
- 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}})
- mail_content_body.match(%r{https?://[^/]*/[^/]*/[^/]*/commit/[\w\d]{40}})[0]
- 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
- error_message = 'No matched commit url found'
- send_error_email(error_message)
- raise error_message
That pattern is repeated in many places. How about uniformly catch error in higher level and call send_error_email()?
That is, only raise here and call send_error_email() uniformly in another place.
Thanks, Fengguang
On Thu, Nov 05, 2020 at 10:08:16AM +0800, Wu Fengguang wrote:
- error_message = 'No matched commit url found'
- send_error_email(error_message)
- raise error_message
That pattern is repeated in many places. How about uniformly catch error in higher level and call send_error_email()?
That is, only raise here and call send_error_email() uniformly in another place.
yes it can be optimize as this idea. I will do like this:
def send_account my_commit_url, my_ssh_pubkey = parse_commit_url_pub_key acct_info = apply_my_account(my_ssh_pubkey)
@my_info['my_commit_url'] = my_commit_url @my_info['my_login_name'] = acct_info['my_login_name']
store_account_info send_my_uuid rescue StandardError => e puts e.message puts e.backtrace send_error_email(e.message) end
Thanks Luan Shengde
Thanks, Fengguang