diff --git a/patch-tracking/patch_tracking/app.py b/patch-tracking/patch_tracking/app.py index a48601c1d781f509897346a95342139775ba24c8..108f922badbc65ca92b3d61ef4ef747d1b8635bc 100644 --- a/patch-tracking/patch_tracking/app.py +++ b/patch-tracking/patch_tracking/app.py @@ -9,6 +9,7 @@ from patch_tracking.api.issue import issue from patch_tracking.api.tracking import tracking from patch_tracking.database import db from patch_tracking.task import task +from patch_tracking.util import github_api, gitee_api logging.config.fileConfig('logging.conf', disable_existing_loggers=False) @@ -16,6 +17,25 @@ app = Flask(__name__) logger = logging.getLogger(__name__) +def check_token(): + """ check gitee/github token """ + gitee_token = app.config['GITEE_ACCESS_TOKEN'] + github_token = app.config['GITHUB_ACCESS_TOKEN'] + token_error = False + github_ret = github_api.get_user_info(github_token) + if github_ret[0] != "success": + logger.error('github token is bad credentials.') + token_error = True + + gitee_ret = gitee_api.get_user_info(gitee_token) + if gitee_ret[0] != "success": + logger.error('gitee token is bad credentials.') + token_error = True + + if token_error: + sys.exit() + + def check_listen(listen_param): """ check LISTEN """ check_ret = True @@ -67,6 +87,7 @@ def check_settings_conf(): settings_file = os.path.join(os.path.abspath(os.curdir), "settings.conf") app.config.from_pyfile(settings_file) check_settings_conf() +check_token() app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite?check_same_thread=False' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False diff --git a/patch-tracking/patch_tracking/util/gitee_api.py b/patch-tracking/patch_tracking/util/gitee_api.py index cf798b3a06077ef3f802834998d0cfbde5f304ce..53a553f14721f01c96860832e25826865e74e0d7 100644 --- a/patch-tracking/patch_tracking/util/gitee_api.py +++ b/patch-tracking/patch_tracking/util/gitee_api.py @@ -12,6 +12,19 @@ ORG_URL = "https://gitee.com/api/v5/orgs" REPO_URL = "https://gitee.com/api/v5/repos" +def get_user_info(token): + """ + get user info + """ + url = "https://gitee.com/api/v5/user" + gitee_token = token + param = {'access_token': gitee_token} + ret = requests.get(url, params=param) + if ret.status_code == 200: + return "success", ret.text + return "error", ret.json() + + def get_path_content(repo, branch, path): """ get file content diff --git a/patch-tracking/patch_tracking/util/github_api.py b/patch-tracking/patch_tracking/util/github_api.py index c2196c5b669adc38c23f51874efac3e780b896b4..31b533c4ec35d61dd664191031e0890a4012ec89 100644 --- a/patch-tracking/patch_tracking/util/github_api.py +++ b/patch-tracking/patch_tracking/util/github_api.py @@ -10,6 +10,37 @@ from flask import current_app logger = logging.getLogger(__name__) +def get_user_info(token): + """ + get user info + """ + url = "https://api.github.com/user" + count = 30 + token = 'token ' + token + headers = { + 'User-Agent': 'Mozilla/5.0', + 'Authorization': token, + 'Content-Type': 'application/json', + 'Connection': 'close', + 'method': 'GET', + 'Accept': 'application/json' + } + while count > 0: + try: + ret = requests.get(url, headers=headers) + if ret.status_code == 200: + return 'success', ret.text + return 'error', ret.json() + except requests_connectionError as err: + logger.warning(err) + time.sleep(10) + count -= 1 + continue + if count == 0: + logger.error('Fail to connnect to github: %s after retry 30 times.', url) + return 'connect error' + + class GitHubApi: """ Encapsulates GitHub functionality