From 00f7e2ebab243c807bc2138c0a3412eae122af24 Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Wed, 29 Mar 2017 09:42:36 +0800 Subject: [PATCH 1/3] use this script to get specific branch(for instance, release branch) latest modified time and reversed sort, then dump the result to .md file. --- tools/python_branch_history.py | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tools/python_branch_history.py diff --git a/tools/python_branch_history.py b/tools/python_branch_history.py new file mode 100644 index 000000000..a3ddc8388 --- /dev/null +++ b/tools/python_branch_history.py @@ -0,0 +1,55 @@ +import base64 +import time +import os +import gitlab + +def main(): + # token authentication from config file + gl = gitlab.Gitlab.from_config(config_files=['/Users/victorzhang/.python-gitlab.cfg']) + + # projects + # Note: difference among gl.projects.list() / gl.projects.list(all=True) / gl.projects.owned() + projects = gl.projects.list(all=True) + + project_release_branches = [] + + for project in projects: + # use try/except to avoid script exit from exception when there's no 'release' branch + try: + project_release_branch = project.branches.get('release') + except Exception: + project_release_branch = None + + if project_release_branch is not None : + latest_comitted_date = project_release_branch.commit['committed_date'] + project_release_branches.append({'name':project.name, 'latest_modified':latest_comitted_date}) + + + # Sort branches by modified time + project_release_branches.sort(key=lambda r: r['latest_modified'], reverse=True) + + # print result + export(project_release_branches) + +def export(branches): + script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in + rel_path = "release_branches.md" + abs_file_path = os.path.join(script_dir, rel_path) + with open(abs_file_path, mode="w") as out_file: + out_file.write("|Project name|release branch latest modified time|\n") + out_file.write("|---|---|\n") + for branch in branches: + project_name = branch['name'] + latest_modified = branch['latest_modified'] + + print(' project name: ' + project_name + ' | ' + 'release branch latest modified time: ' + latest_modified) + + out_file.write("|") + out_file.write(project_name) + out_file.write("|") + out_file.write(latest_modified) + out_file.write("|") + out_file.write("|\n") + +if __name__ == "__main__": + main() \ No newline at end of file From ff104651874f0c56437053da6d242eb1e456725c Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Fri, 31 Mar 2017 09:22:54 +0800 Subject: [PATCH 2/3] Add several functions: 1. Add a new colume to display relese or not 2. Add an argument of specific datetime to control relese or not 3. Print all projects in console to ensure there's no missing project --- tools/python_branch_history.py | 39 ++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/tools/python_branch_history.py b/tools/python_branch_history.py index a3ddc8388..332e39966 100644 --- a/tools/python_branch_history.py +++ b/tools/python_branch_history.py @@ -1,7 +1,11 @@ +#!/usr/bin/python +# usage example: python python_branch_history.py "2017 3 22 23 40" +# import base64 -import time +import datetime import os import gitlab +import sys def main(): # token authentication from config file @@ -10,12 +14,14 @@ def main(): # projects # Note: difference among gl.projects.list() / gl.projects.list(all=True) / gl.projects.owned() projects = gl.projects.list(all=True) - + print('all projects count: ' + str(len(projects))) + print('all projects name - ') project_release_branches = [] for project in projects: # use try/except to avoid script exit from exception when there's no 'release' branch try: + print(' '+ project.name) project_release_branch = project.branches.get('release') except Exception: project_release_branch = None @@ -35,21 +41,42 @@ def export(branches): script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in rel_path = "release_branches.md" abs_file_path = os.path.join(script_dir, rel_path) + temp_argv_list = sys.argv[1].split() + year = int(temp_argv_list[0]) + month = int(temp_argv_list[1]) + day = int(temp_argv_list[2]) + hour = int(temp_argv_list[3]) + minute = int(temp_argv_list[4]) + valid_date = unicode(datetime.datetime(year,month,day,hour,minute)) + + print('\n') + print('projects (have release branch) - ') + with open(abs_file_path, mode="w") as out_file: - out_file.write("|Project name|release branch latest modified time|\n") - out_file.write("|---|---|\n") + out_file.write("|Project name|release branch latest modified time|Release or not(Yes/No)|\n") + out_file.write("|---|---|---|\n") for branch in branches: project_name = branch['name'] latest_modified = branch['latest_modified'] + release_or_not = 'Yes' - print(' project name: ' + project_name + ' | ' + 'release branch latest modified time: ' + latest_modified) + if latest_modified < valid_date: + release_or_not = 'No' + + print(' project name: ' + project_name + ' | ' + 'release branch latest modified time: ' + latest_modified + ' | ' + 'Release or not(Yes/No) : ' + release_or_not ) out_file.write("|") out_file.write(project_name) out_file.write("|") out_file.write(latest_modified) out_file.write("|") + out_file.write(release_or_not) + out_file.write("|") out_file.write("|\n") if __name__ == "__main__": - main() \ No newline at end of file + if (len(sys.argv) == 2): + main() + else: + print('wrong arguments number(2 arguments)!') + \ No newline at end of file From 552aeba4a10a5f6f34a27dff2b6bcaebb487d27e Mon Sep 17 00:00:00 2001 From: zhangsheng Date: Fri, 31 Mar 2017 09:50:10 +0800 Subject: [PATCH 3/3] test if user.email changes to victorzhangsheng@gmail.com --- tools/python_branch_history.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/python_branch_history.py b/tools/python_branch_history.py index 332e39966..1d157b3a0 100644 --- a/tools/python_branch_history.py +++ b/tools/python_branch_history.py @@ -79,4 +79,5 @@ def export(branches): main() else: print('wrong arguments number(2 arguments)!') + \ No newline at end of file