diff --git a/.github/workflows/gitgraph.yml b/.github/workflows/gitgraph.yml index 222b4e0..eb1bff7 100644 --- a/.github/workflows/gitgraph.yml +++ b/.github/workflows/gitgraph.yml @@ -2,17 +2,17 @@ name: generate git graph # Controls when the workflow will run on: - # Triggers the workflow on push or pull request events but only for the main branch + # Triggers the workflow on push events and only for the main branch push: - branches: [ main ] + branches: [main] pull_request: - branches-ignore: [ main ] + branches-ignore: [main] schedule: - cron: "5 7 * * */2" # every two days at 7:05am UTC # Allows you to run this workflow manually from the Actions tab workflow_dispatch: - + repository_dispatch: # run workflow on api request # prevent concurrent jobs @@ -27,13 +27,13 @@ jobs: steps: - name: clone remote repo run: git clone https://codeberg.org/Starfish/TinyWeatherForecastGermany.git TinyWeatherForecastGermany - + - name: clone this repo run: git clone https://github.com/${{ github.repository }}.git "git-graph" - + - name: update apt packages run: timeout 120s sudo apt update || true - + - name: upgrade apt packages run: timeout 120s sudo apt upgrade -y || true @@ -41,19 +41,19 @@ jobs: uses: actions/setup-python@v4 with: python-version: 3.11 - + - name: install virtualenv run: pip install virtualenv - + - name: init virtualenv run: virtualenv venv - + - name: activate virtualenv run: source venv/bin/activate - name: upgrade pip run: python -m pip install --upgrade pip - + - name: install graphviz run: sudo apt install -y graphviz @@ -65,10 +65,10 @@ jobs: - name: generate git graph using Python and graphviz run: python git-graph/git_log2graphviz.py - + - name: list directory contents run: ls -lisha - + - name: archive generated svg uses: actions/upload-artifact@v3 with: @@ -76,7 +76,7 @@ jobs: path: | *.svg *.dot - + - name: copy artifacts to git repo directory run: | cp *.svg git-graph/ || true @@ -95,4 +95,3 @@ jobs: git commit -m "update git graph" git remote add github-pages "https://${{ github.repository_owner }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" git push -u -f github-pages - \ No newline at end of file diff --git a/.gitignore b/.gitignore index 955e054..3eebecc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.mermaid #*.dot #*.svg +TinyWeatherForecastGermany/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/.gitpod.yml b/.gitpod.yml index 906058b..ddda18e 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -1,7 +1,14 @@ + tasks: - init: > sudo apt update && sudo apt upgrade -y && sudo apt install graphviz -y && virtualenv venv && source venv/bin/activate && python -m pip install --upgrade pip && pip install -r requirements.txt git clone https://codeberg.org/Starfish/TinyWeatherForecastGermany.git "TinyWeatherForecastGermany" python git_log2graphviz.py - +vscode: + extensions: + - ms-python.python + - ms-python.black-formatter + - charliermarsh.ruff + - SonarSource.sonarlint-vscode + - eamodio.gitlens diff --git a/__init__.py b/__init__.py index 6116dd0..b422fe4 100644 --- a/__init__.py +++ b/__init__.py @@ -1,22 +1,29 @@ - import concurrent.futures -from multiprocessing import cpu_count import json +import sys +from multiprocessing import cpu_count from pprint import pprint -from pydriller import Repository - import requests +from pydriller import Repository -forks_req = requests.get(f"https://codeberg.org/api/v1/repos/Starfish/TinyWeatherForecastGermany/forks", timeout=10) -forks_req_json = forks_req.json() +forks_req = requests.get( + "https://codeberg.org/api/v1/repos/Starfish/TinyWeatherForecastGermany/forks", + timeout=10, +) +if forks_req.ok: + forks_req_json = forks_req.json() +else: + print(f"failed to request forks -> received unexpected status code {forks_req.status_code} -> text: {forks_req.text}") + sys.exit(0) -with open('forks-api.json','w+',encoding='utf-8') as fh: +with open("forks-api.json", "w+", encoding="utf-8") as fh: fh.write(str(json.dumps(forks_req_json, indent=4))) -#with open('forks-api.json','r',encoding='utf-8') as fh: +# with open('forks-api.json','r',encoding='utf-8') as fh: # forks_req_json = json.loads(str(fh.read())) + def get_commits_json(clone_url, fork_id): print(f"analyzing '{clone_url}' ... ") @@ -28,30 +35,31 @@ def get_commits_json(clone_url, fork_id): for commit in Repository(clone_url).traverse_commits(): commit_dict = {} - commit_dict['hash'] = str(commit.hash) - commit_dict['msg'] = str(commit.msg) - commit_dict['author_name'] = str(commit.author.name) - #commit_dict['branches'] = commit.branches + commit_dict["hash"] = str(commit.hash) + commit_dict["msg"] = str(commit.msg) + commit_dict["author_name"] = str(commit.author.name) + # commit_dict['branches'] = commit.branches - #commit_dict['mod_files'] = [] - #for file in commit.modified_files: + # commit_dict['mod_files'] = [] + # for file in commit.modified_files: # commit_dict['mod_files'].append(str(file.filename)) - - mermaid_str += ' commit id:"'+str(commit.hash)[0:6]+'"\n' + + mermaid_str += ' commit id:"' + str(commit.hash)[0:6] + '"\n' commits_list.append(commit_dict) del commit_dict - with open(f"fork-{fork_id}.json",'w+',encoding='utf-8') as fh: + with open(f"fork-{fork_id}.json", "w+", encoding="utf-8") as fh: fh.write(str(json.dumps(commits_list, indent=4))) del commits_list - with open(f"fork-{fork_id}.mermaid",'w+',encoding='utf-8') as fh: + with open(f"fork-{fork_id}.mermaid", "w+", encoding="utf-8") as fh: fh.write(str(mermaid_str)) + with concurrent.futures.ThreadPoolExecutor(max_workers=cpu_count()) as executor: for fork_entry in forks_req_json: - clone_url = str(fork_entry['clone_url']) + clone_url = str(fork_entry["clone_url"]) - future = executor.submit(get_commits_json, clone_url, fork_entry['id']) - #print(future.result()) + future = executor.submit(get_commits_json, clone_url, fork_entry["id"]) + # print(future.result()) diff --git a/git_log2graphviz.py b/git_log2graphviz.py index 4a23e12..64c362a 100644 --- a/git_log2graphviz.py +++ b/git_log2graphviz.py @@ -1,8 +1,7 @@ - -from pprint import pprint import json import os import re +from pprint import pprint from pydriller import Repository @@ -11,50 +10,52 @@ dot_graph_1 = """ digraph G { """ -dot_graph_2 = '' +dot_graph_2 = "" # if repo not cloned yet -> Repository("https://codeberg.org/Starfish/TinyWeatherForecastGermany.git") -for commit in Repository(f"TinyWeatherForecastGermany/").traverse_commits(): +for commit in Repository("TinyWeatherForecastGermany/").traverse_commits(): commit_dict = {} - commit_dict['hash'] = str(commit.hash) - commit_dict['msg'] = str(commit.msg).replace('"',"'") - - commit_dict['timestamp'] = str(commit.committer_date) - - commit_dict['author_name'] = str(commit.author.name) - commit_dict['branches'] = list(commit.branches) - commit_dict['parents'] = list(commit.parents) - commit_dict['merge'] = commit.merge - - c_color = '' - if commit_dict['merge']: #'merge pull' in str(commit_dict['msg']).lower(): - c_color = ',color=blue' - + commit_dict["hash"] = str(commit.hash) + commit_dict["msg"] = str(commit.msg).replace('"', "'") + + commit_dict["timestamp"] = str(commit.committer_date) + + commit_dict["author_name"] = str(commit.author.name) + commit_dict["branches"] = list(commit.branches) + commit_dict["parents"] = list(commit.parents) + commit_dict["merge"] = commit.merge + + c_color = "" + if commit_dict["merge"]: #'merge pull' in str(commit_dict['msg']).lower(): + c_color = ",color=blue" + dot_graph_1 += f"\t\"{commit_dict['hash']}\" [label=<{commit_dict['hash'][0:6]}{commit_dict['author_name']}>, comment=\"{commit_dict['timestamp']}\", tooltip=\"{commit_dict['timestamp']}\n{commit_dict['msg']}\", shape=box{c_color}, URL=\"https://codeberg.org/Starfish/TinyWeatherForecastGermany/commit/{commit_dict['hash']}\"];\n" - c_parents_len = len(commit_dict['parents']) + c_parents_len = len(commit_dict["parents"]) if c_parents_len > 0: - for c_parent in commit_dict['parents']: + for c_parent in commit_dict["parents"]: dot_graph_2 += f"\t \"{c_parent}\" -> \"{commit_dict['hash']}\";\n" commits_list.append(commit_dict) del commit_dict -#with open(f"test.json",'w+',encoding='utf-8') as fh: +# with open(f"test.json",'w+',encoding='utf-8') as fh: # fh.write(str(json.dumps(commits_list, indent=4))) dot_graph_1 += "\n\n" + dot_graph_2 + "}" -with open(f"git_graph.dot",'w+',encoding='utf-8') as fh: +with open("git_graph.dot", "w+", encoding="utf-8") as fh: fh.write(str(dot_graph_1)) os.system("dot -Tsvg -o git_graph.svg git_graph.dot") -svg_contents = '' -with open(f"git_graph.svg",'r',encoding='utf-8') as fh: +svg_contents = "" +with open("git_graph.svg", "r", encoding="utf-8") as fh: svg_contents = str(fh.read()) -svg_contents = re.sub(r'(?im)[\r\n]*\[\r\n]*','',str(svg_contents)) +svg_contents = re.sub( + r"(?im)[\r\n]*\[\r\n]*", "", str(svg_contents) +) -with open(f"git_graph.svg",'w+',encoding='utf-8') as fh: +with open("git_graph.svg", "w+", encoding="utf-8") as fh: fh.write(str(svg_contents)) diff --git a/requirements.txt b/requirements.txt index 39fb75e..67912dc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -PyDriller==2.4.1 -requests==2.30.0 \ No newline at end of file +PyDriller==2.5 +requests==2.31.0 \ No newline at end of file