diff --git a/patch-tracking/README.md b/patch-tracking/README.md index 63ecffbd46b18872bed19c5dabd3406c916b6c3f..6fb10f624931fc8289368f45deed118a27ccc127 100644 --- a/patch-tracking/README.md +++ b/patch-tracking/README.md @@ -261,7 +261,21 @@ patch-tracking-cli query --server --table issue patch-tracking-cli query --server 127.0.0.1:5001 --table issue ``` -### 4.4 码云查看 issue 及 PR +### 4.4 删除 Tracking 跟踪项数据 + +```shell script +patch-tracking-cli delete --server SERVER --user USER --password PWD --table TABLE --repo REPO [--branch BRANCH] +``` + +可以删除指定repo和branch的单条数据;也可直接删除指定repo下所有branch的数据。 + + +例如: +```shell script +patch-tracking-cli delete --server 127.0.0.1:5001 --user admin --password Test@123 --repo testPatchTrack/testPatch1 --branch master +``` + +### 4.5 码云查看 issue 及 PR 登录Gitee上进行跟踪的软件项目,在该项目的Issues和Pull Requests页签下,可以查看到名为`[patch tracking] TIME`,例如` [patch tracking] 20200713101548`的条目。 diff --git a/patch-tracking/patch_tracking/api/constant.py b/patch-tracking/patch_tracking/api/constant.py index 0056a0924b96e44663a60b9ef47541fd7a39cba4..c2de1446e52817fd29c92f73a1db7e2eee8ad0f8 100644 --- a/patch-tracking/patch_tracking/api/constant.py +++ b/patch-tracking/patch_tracking/api/constant.py @@ -24,6 +24,7 @@ class ResponseCode: DELETE_DB_ERROR = "6001" CONFIGFILE_PATH_EMPTY = "6002" DIS_CONNECTION_DB = "6003" + DELETE_DB_NOT_FOUND = "6005" CODE_MSG_MAP = { SUCCESS: "Successful Operation!", @@ -36,7 +37,8 @@ class ResponseCode: GITEE_CONNECTION_ERROR: "Unable to connect to the gitee", DELETE_DB_ERROR: "Failed to delete database", CONFIGFILE_PATH_EMPTY: "Initialization profile does not exist or cannot be found", - DIS_CONNECTION_DB: "Unable to connect to the database, check the database configuration" + DIS_CONNECTION_DB: "Unable to connect to the database, check the database configuration", + DELETE_DB_NOT_FOUND: "The tracking you want to delete does not exist" } @classmethod diff --git a/patch-tracking/patch_tracking/api/tracking.py b/patch-tracking/patch_tracking/api/tracking.py index 85db948cd2362e264355df3f384b6db64b7d5db6..8ecd27f2fdecb49b5ef5e7e4b6e25f627c642f96 100644 --- a/patch-tracking/patch_tracking/api/tracking.py +++ b/patch-tracking/patch_tracking/api/tracking.py @@ -30,14 +30,23 @@ def delete(): try: if "branch" in keys: - if Tracking.query.filter(Tracking.repo == input_params['repo'], Tracking.branch == input_params['branch']): + if Tracking.query.filter(Tracking.repo == input_params['repo'], + Tracking.branch == input_params['branch']).first(): delete_tracking(input_params['repo'], input_params['branch']) logger.info('Delete tracking repo: %s, branch: %s', input_params['repo'], input_params['branch']) + return ResponseCode.ret_message(code=ResponseCode.SUCCESS) + else: + logger.info('Delete tracking repo: %s, branch: %s not found.', input_params['repo'], + input_params['branch']) + return ResponseCode.ret_message(code=ResponseCode.DELETE_DB_NOT_FOUND) else: - if Tracking.query.filter(Tracking.repo == input_params['repo']): + if Tracking.query.filter(Tracking.repo == input_params['repo']).first(): delete_tracking(input_params['repo']) logger.info('Delete tracking repo: %s', input_params['repo']) - return ResponseCode.ret_message(code=ResponseCode.SUCCESS) + return ResponseCode.ret_message(code=ResponseCode.SUCCESS) + else: + logger.info('Delete tracking repo: %s not found.', input_params['repo']) + return ResponseCode.ret_message(code=ResponseCode.DELETE_DB_NOT_FOUND) except SQLAlchemyError as err: return ResponseCode.ret_message(code=ResponseCode.DELETE_DB_ERROR, data=err) diff --git a/patch-tracking/patch_tracking/app.py b/patch-tracking/patch_tracking/app.py index 442fd3ee228a665c43677ff137efd3941c245730..294149c515d589b7ef708021f0b50a70a8e39ead 100644 --- a/patch-tracking/patch_tracking/app.py +++ b/patch-tracking/patch_tracking/app.py @@ -2,6 +2,7 @@ flask app """ import logging.config +import os import sys from flask import Flask from patch_tracking.api.issue import issue @@ -33,7 +34,8 @@ def check_settings_conf(): sys.exit() -app.config.from_pyfile("settings.conf") +settings_file = os.path.join(os.path.abspath(os.curdir), "settings.conf") +app.config.from_pyfile(settings_file) check_settings_conf() GITHUB_ACCESS_TOKEN = app.config['GITHUB_ACCESS_TOKEN'] diff --git a/patch-tracking/patch_tracking/cli/patch_tracking_cli.py b/patch-tracking/patch_tracking/cli/patch_tracking_cli.py index 2a8ca0192875365c79db36bb90b3d6d187673edb..9802788c679e52e52f5b6692d8489da7ee0e3e94 100755 --- a/patch-tracking/patch_tracking/cli/patch_tracking_cli.py +++ b/patch-tracking/patch_tracking/cli/patch_tracking_cli.py @@ -21,10 +21,7 @@ def query_table(args): if args.table == "tracking": url = '/'.join(['https:/', server, 'tracking']) - if args.branch and args.repo: - params = {'repo': args.repo, 'branch': args.branch} - else: - params = {'repo': args.repo} + params = {'repo': args.repo, 'branch': args.branch} try: ret = requests.get(url, params=params, verify=False) if ret.status_code == 200 and ret.json()['code'] == '2001': @@ -119,6 +116,9 @@ def params_input_track(params, file_path=None): """ load tracking from command line arguments """ + if not check_add_param(params): + return 'error', 'Check input params error' + if add_param_check_url(params, file_path) == 'error': return 'error', 'Check input params error.' @@ -156,6 +156,19 @@ def params_input_track(params, file_path=None): return 'error', 'Unexpected Error.' +def check_add_param(params): + success = True + required_params = ["repo", "branch", "scm_repo", "scm_branch", "version_control", "enabled"] + miss_params = list() + for param in required_params: + if param not in params or not params[param]: + miss_params.append(param) + success = False + if not success: + print("patch_tracking_cli add: error: the following arguments are required: --{}".format(", --".join(miss_params))) + return success + + def add(args): """ add tracking @@ -212,20 +225,19 @@ def delete(args): if ret.status_code == 200 and ret.json()['code'] == '2001': print('Tracking delete successfully.') return + elif ret.status_code == 200 and ret.json()['code'] == '6005': + print('Delete Nothing. Tracking not exist.') + return - print("Tracking delete failed. Error: %s", ret) + print("Tracking delete failed. Error: {}".format(ret.text)) except Exception as exception: - print('Error: Connect server error: %s', str(exception)) + print('Connect server error: ' + str(exception)) def query(args): """ - query table data - """ - if args.branch and not args.repo: - print(query_usage) - return - + query table data + """ status, ret = query_table(args) if status == "success": df = pandas.DataFrame.from_dict(ret.json()["data"], orient="columns") @@ -266,16 +278,16 @@ def dir_input_track(dir_path, args): load tracking from dir """ if os.path.exists(dir_path) and os.path.isdir(dir_path): - for root, _, files in os.walk(dir_path): - if not files: - print('error: dir path empty') - return - for file in files: - if os.path.splitext(file)[-1] == ".yaml": - file_path = os.path.join(root, file) - file_input_track(file_path, args) - else: - print('Please input yaml file. Error in {}'.format(file)) + dir_files = os.listdir(dir_path) + if not dir_files: + print('error: dir path empty') + return + for file in dir_files: + if os.path.isfile(os.path.join(dir_path, file)) and os.path.splitext(file)[-1] == ".yaml": + file_path = os.path.join(dir_path, file) + file_input_track(file_path, args) + else: + print('Please input yaml file. Error in {}'.format(file)) else: print('error: dir path error. Params error in {}'.format(dir_path)) @@ -304,7 +316,7 @@ add_usage = """ %(prog)s --server SERVER --user USER --password PASSWORD --file FILE %(prog)s --server SERVER --user USER --password PASSWORD --dir DIR""" parser_add = subparsers.add_parser( - 'add', parents=[common_parser, authentication_parser], help="add tracking", usage=add_usage + 'add', parents=[common_parser, authentication_parser], help="add tracking", usage=add_usage, allow_abbrev=False ) parser_add.set_defaults(func=add) parser_add.add_argument("--version_control", choices=['github'], help="upstream version control system") @@ -319,7 +331,7 @@ parser_add.add_argument('--dir', help='import patch tracking from files in direc # delete del_usage = """ %(prog)s --server SERVER --table TABLE --repo REPO [--branch BRANCH]""" -parser_delete = subparsers.add_parser('delete', parents=[common_parser, authentication_parser], help="delete tracking") +parser_delete = subparsers.add_parser('delete', parents=[common_parser, authentication_parser], help="delete tracking", allow_abbrev=False) parser_delete.set_defaults(func=delete) parser_delete.add_argument("--repo", required=True, help="source package repository") parser_delete.add_argument("--branch", help="source package branch") @@ -327,7 +339,7 @@ parser_delete.add_argument("--branch", help="source package branch") # query query_usage = """ %(prog)s --server SERVER --table {tracking,issue} [--repo REPO] [--branch BRANCH]""" -parser_query = subparsers.add_parser('query', parents=[common_parser], help="query tracking/issue") +parser_query = subparsers.add_parser('query', parents=[common_parser], help="query tracking/issue", allow_abbrev=False) parser_query.set_defaults(func=query) parser_query.add_argument("--table", required=True, choices=["tracking", "issue"], help="query tracking or issue") parser_query.add_argument("--repo", help="source package repository") diff --git a/patch-tracking/patch_tracking/tests/settings.conf b/patch-tracking/patch_tracking/tests/settings.conf new file mode 100644 index 0000000000000000000000000000000000000000..960c2d2e5061ff6628a53d5273faaca384dbd034 --- /dev/null +++ b/patch-tracking/patch_tracking/tests/settings.conf @@ -0,0 +1,17 @@ +# server settings +LISTEN = "127.0.0.1:5001" + +# GitHub API settings +GITHUB_ACCESS_TOKEN = "" + +# Gitee API settings +GITEE_ACCESS_TOKEN = "" + +# Time interval +SCAN_DB_INTERVAL = 3600 + +# username +USER = "" + +# password +PASSWORD = "" diff --git a/patch-tracking/patch_tracking/tests/tracking_test.py b/patch-tracking/patch_tracking/tests/tracking_test.py index cb7cf185831b830c7f9b5a576a3e2492f9e8a3b7..79c1962c9e55763666a129d52074b901565a3b4a 100644 --- a/patch-tracking/patch_tracking/tests/tracking_test.py +++ b/patch-tracking/patch_tracking/tests/tracking_test.py @@ -167,12 +167,12 @@ class TestTracking(unittest.TestCase): resp_dict = json.loads(resp.data) self.assertIn("code", resp_dict, msg="Error in data format return") self.assertEqual( - ResponseCode.INPUT_PARAMETERS_ERROR, resp_dict.get("code"), msg="Error in status code return" + ResponseCode.SUCCESS, resp_dict.get("code"), msg="Error in status code return" ) self.assertIn("msg", resp_dict, msg="Error in data format return") self.assertEqual( - ResponseCode.CODE_MSG_MAP.get(ResponseCode.INPUT_PARAMETERS_ERROR), + ResponseCode.CODE_MSG_MAP.get(ResponseCode.SUCCESS), resp_dict.get("msg"), msg="Error in status code return" ) @@ -395,6 +395,38 @@ class TestTracking(unittest.TestCase): self.assertIn("data", resp_dict, msg="Error in data format return") self.assertEqual(resp_dict.get("data"), None, msg="Error in data information return") + def test_delete_data(self): + """ + The POST interface inserts data + :return: + """ + data = { + "version_control": "github", + "scm_repo": "test_delete", + "scm_branch": "test_delete", + "scm_commit": "test_delete", + "repo": "test_delete1", + "branch": "test_delete1", + "enabled": 0 + } + + self.client.post("/tracking", json=data, content_type="application/json", headers=self.auth) + + resp = self.client.delete("/tracking?repo=test_delete1&branch=test_delete1", content_type="application/json", headers=self.auth) + resp_dict = json.loads(resp.data) + self.assertIn("code", resp_dict, msg="Error in data format return") + self.assertEqual(ResponseCode.SUCCESS, resp_dict.get("code"), msg="Error in status code return") + + def test_delete_not_found(self): + """ + The POST interface inserts data + :return: + """ + resp = self.client.delete("/tracking?repo=not_found1&branch=not_found1", content_type="application/json", headers=self.auth) + resp_dict = json.loads(resp.data) + self.assertIn("code", resp_dict, msg="Error in data format return") + self.assertEqual(ResponseCode.DELETE_DB_NOT_FOUND, resp_dict.get("code"), msg="Error in status code return") + if __name__ == '__main__': unittest.main()