optimize code: use gem resource ruby-git instead of system command
Signed-off-by: Luan Shengde shdluan@163.com --- container/mail-robot/lib/parse-apply-account-email.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/container/mail-robot/lib/parse-apply-account-email.rb b/container/mail-robot/lib/parse-apply-account-email.rb index e6c35f7..ce01fec 100755 --- a/container/mail-robot/lib/parse-apply-account-email.rb +++ b/container/mail-robot/lib/parse-apply-account-email.rb @@ -3,9 +3,13 @@ # Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. # frozen_string_literal: true
+# set default system encoding +Encoding.default_external = Encoding::UTF_8 + require 'json' require 'mail' require 'rest-client' +require 'git'
# used to parse mail_content for my_commit_url and my_ssh_pubkey # be called by AssignAccount when it needs to extract required data: @@ -110,14 +114,15 @@ class ParseApplyAccountEmail repo_dir = repo_url.split('/')[-1] commit_id = url.split('/')[-1]
- %x(/usr/bin/git -C /tmp clone --bare #{repo_url} #{repo_dir}) + Git.clone(repo_url, repo_dir, path: '/tmp/', bare: true)
# get all commit IDs and check commit id exists - repo_commits = %x(/usr/bin/git -C /tmp/#{repo_dir} log --pretty=format:'%H').split(/\n/) + repo = Git.bare("/tmp/#{repo_dir}") + repo_commits = repo.lib.log_commits check_commit_exist(commit_id, repo_commits)
# get the auther's email for the commit - author_email = %x(/usr/bin/git -C /tmp/#{repo_dir} show -s --format=%aE #{commit_id}).chomp + author_email = repo.gcommit(commit_id).author.email check_commit_email(author_email)
FileUtils.rm_rf "/tmp/#{repo_dir}"