diff --git a/advisors/review_tool.py b/advisors/review_tool.py index 1a8c404a14160f87adc75565575fc3dd17bc29d0..98cc6a383122980eb1f79ef987b1900fe8829cde 100755 --- a/advisors/review_tool.py +++ b/advisors/review_tool.py @@ -560,6 +560,7 @@ def args_parser(): default=os.path.dirname(os.path.realpath(__file__))) pars.add_argument("-e", "--edit", type=str, help="Edit items format.Format: status1:number_list1 status2:number_list2 ...") + pars.add_argument("-c", "--clean", help="Clean environment", action="store_true") return pars.parse_args() @@ -611,6 +612,12 @@ def prepare(args, group, repo_name, pull_id, branch): exec_cmd(["git", "merge", "--no-edit", "remotes/origin/" + branch]) return 0 +def cleanup_env(args, repo_name): + """ + Clena up environment, e.g. temporary directory + """ + work_dir = os.path.realpath(args.workdir) + shutil.rmtree(os.path.join(work_dir, repo_name)) def find_review_comment(user_gitee, group, repo_name, pull_id): """ @@ -711,6 +718,8 @@ def main(): chklist_path = os.path.join(cur_dir, CHECKLIST) review_comment = review(pull_request, repo_name, chklist_path, branch) user_gitee.create_pr_comment(repo_name, pull_id, review_comment, group) + if args.clean: + cleanup_env(args, repo_name) return 0 if __name__ == "__main__": diff --git a/command/review_tool b/command/review_tool index d18adcf15530f4113d99a533cecf2c15cae8bcd1..95b9b8569a981470ff250d34ec48787d4f6dd56f 100644 --- a/command/review_tool +++ b/command/review_tool @@ -19,7 +19,8 @@ from advisors.review_tool import main if __name__ == '__main__': try: - main() + sys.exit(main()) except Exception as error: print("WARNING: Command execution error") print(error.message) + sys.exit(1) diff --git a/openEuler-Advisor.spec b/openEuler-Advisor.spec index 1d99732c2f5bc512c129e149c098c7db25d4524e..5fdf4c1be130bf593c7ce9596ccbd5a77014e556 100644 --- a/openEuler-Advisor.spec +++ b/openEuler-Advisor.spec @@ -44,6 +44,7 @@ py.test-%{python3_version} -vv tests || : %attr(0755,root,root) %{_bindir}/review_tool %attr(0755,root,root) %{_bindir}/tc_reminder %attr(0755,root,root) %{_bindir}/which_archived +%attr(0755,root,root) %{_bindir}/prow_review_tool.py %changelog * Tue Dec 1 2020 smileknife - 1.0-3 diff --git a/prow/prow_review_tool.py b/prow/prow_review_tool.py index 5f8ba08d98e5b0c825abc2bb8ef8d54dd5c56551..e2a25d0d85f8f7d0016437f86e02dffe7e80d87c 100644 --- a/prow/prow_review_tool.py +++ b/prow/prow_review_tool.py @@ -31,43 +31,53 @@ import argparse import subprocess +def get_cmd(): + """ + get review_tool real path + """ + cmd_path = os.path.dirname(os.path.realpath(__file__)) + if cmd_path == "/usr/bin": + review_cmd="/usr/bin/review_tool" + else: + review_cmd = os.path.join(os.path.dirname(cmd_path), "command/review_tool") + return review_cmd + if __name__ == '__main__': - cur_path = os.path.dirname(os.path.realpath(__file__)) - advisor_dir=os.path.dirname(cur_path) parser = argparse.ArgumentParser() parser.add_argument("-t", "--event", type=str, choices=['Merge Request Hook', 'Note Hook'], help="event type") parser.add_argument("-p", "--payload", type=str, help="json string that PROW framework send") args = parser.parse_args() - ret_code = 1 + return_code = 0 if args.payload: data = json.loads(args.payload) if args.event == 'Merge Request Hook' and data['action'] == 'open' \ or data['action'] == 'update': - review_cmd=os.path.join(advisor_dir, "command/review_tool") - p = subprocess.Popen(["python3", review_cmd, "-q", - "-u", data['pull_request']['html_url'], "-w", "workdir"], + subp = subprocess.run(["python3", get_cmd(), + "-u", data['pull_request']['html_url'], "-w", "/tmp/review_dir", "-c"], stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - p.communicate() - ret_code = p.poll() + stderr=subprocess.STDOUT, + encoding="utf-8", + check=False) + return_code = subp.returncode elif args.event == 'Note Hook' and data['action'] == 'comment' \ and data['noteable_type'] == 'PullRequest': if not data['comment']['body'].startswith("/review"): - sys.exit(0) + sys.exit(2) for line in data['comment']['body'].splitlines(): if line.strip().startswith("/review"): args = line.strip().split(' ', 1) content = args[1] - review_cmd=os.path.join(advisor_dir, "command/review_tool") - p = subprocess.Popen(["python3", review_cmd, "-q", - "-u", data['pull_request']['html_url'], - "-w", "workdir", "-e", content], + subp = subprocess.run(["python3", get_cmd(), + "-u", data['pull_request']['html_url'], "-e", content], stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - p.communicate() - ret_code = p.poll() + stderr=subprocess.STDOUT, + encoding="utf-8", + check=False) + return_code = subp.returncode else: print("Event type: %s not support." % args.event) - sys.exit(ret_code) + if return_code != 0: + print(subp.stdout) + sys.exit(return_code) diff --git a/setup.py b/setup.py index b149c6141da853f0eda424067c527389238bbd7b..e4c2ca8eabdfb8c021b6ca9217e350eadfa573cc 100644 --- a/setup.py +++ b/setup.py @@ -50,5 +50,6 @@ setup( ('/usr/bin/', ['command/psrtool']), ('/usr/bin/', ['command/review_tool']), ('/usr/bin/', ['command/tc_reminder']), - ('/usr/bin/', ['command/which_archived'])] + ('/usr/bin/', ['command/which_archived']), + ('/usr/bin/', ['prow/prow_review_tool.py'])] )