Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Automatically generate change logs for each build #52578

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ jobs:
These are the outputs for the experimental build of commit [${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }})
draft: false
prerelease: true
- name: Install python
uses: actions/setup-python@v2
with:
python-version: 3.8 #install the python needed
- name: Install python depednecies
run: |
pip install requests
- name: Generate Changelogs
run: |
python tools/changelog.py >> changelogs.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Changelogs
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{steps.create_release.outputs.upload_url}}
asset_path: changelogs.md
asset_name: changelogs.md
asset_content_type: text/plain

builds:
needs: release
if: ${{ needs.release.outputs.release_already_exists == 'false' }}
Expand Down
100 changes: 100 additions & 0 deletions tools/changelog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import requests
from datetime import datetime, timedelta
import urllib
from concurrent.futures.thread import ThreadPoolExecutor
from concurrent.futures import as_completed
import os
import sys


API_KEY = os.environ["GITHUB_TOKEN"]
REPO_API = 'https://api.github.com/repos/CleverRaven/Cataclysm-DDA/'

def github_fetch(path,parms={}):
header = {'Authorization': f'token {API_KEY}'}
querystring = urllib.parse.urlencode(parms)
r = requests.get(f"{REPO_API}{path}?{querystring}",headers=header)
return r.json()

def get_releases(page=1):
return github_fetch('releases',{'page':page,'per_page':100})

def match_commit_to_pr(hash):
json = github_fetch(f'commits/{hash}/pulls')
if len(json) == 0: # likely being rate limited
sys.stderr.write("You might be being rate limited")
raise Exception()
return json[0]

def get_cat(body):
# I tried using regular expression, but it kept throwing exceptions.
VALID_SUMMARY_CATEGORIES = (
'content',
'features',
'interface',
'mods',
'balance',
'i18n',
'bugfixes',
'performance',
'build',
'infrastructure',
'none',
)
body = body.lower().replace('\r\n',' ').replace("\n"," ").replace(':','')
s = [x for x in body.split(' ') if x != '']
for a,b in zip(s,s[1:]):
if a == 'summary':
if b in VALID_SUMMARY_CATEGORIES:
return b
return 'unable to determine category'

def get_commits_in_time_range(starting_date,ending_date):
current_timestamp = ending_date
page_num = 1

commits_hashes = []

while current_timestamp > starting_date:
releases = get_releases(page_num)
for release in releases:
current_timestamp = datetime.fromisoformat(release['published_at'][:-1])
if current_timestamp < starting_date:
break
if current_timestamp < ending_date:
commits_hashes.append(release['target_commitish'])
page_num+=1

return commits_hashes


def generate_changelogs(starting_date,ending_date=None):
if ending_date == None:
ending_date = datetime.today()

commits_hashes = get_commits_in_time_range(starting_date,ending_date)
d = dict()

with ThreadPoolExecutor() as executor: # multi threading
res = [executor.submit(match_commit_to_pr, hash) for hash in commits_hashes]
for future in as_completed(res):
info = future.result()
author = info['user']
txt_line = f"[{info['title']}]({info['html_url']}) by [{author['login']}]({author['html_url']})"
cat = get_cat(info['body'])

if cat not in d:
d[cat] = []
d[cat].append(txt_line)

sys.stdout.write(f'Logs generated from {starting_date} til {ending_date}\n')
for k in d:
sys.stdout.write(f'# {k[0].upper()}{k[1:]}\n')
for i in d[k]:
sys.stdout.write(i+'\n\n')


if __name__ =='__main__':
# for now, generate the last weeks logs
starting_time = datetime.today() - timedelta(days=7)
generate_changelogs(starting_time)