Skip to content

Commit

Permalink
CI: Update Changelog script to clone libs in tmp dir to get history.
Browse files Browse the repository at this point in the history
Before it depended on the dependencies being already cloned in
the parent directory.
  • Loading branch information
microbit-carlos committed Oct 9, 2024
1 parent 5e258f4 commit 4465cee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
39 changes: 28 additions & 11 deletions .github/tools/gitchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
# DEALINGS IN THE SOFTWARE.

import os
import json
import re
import json
import shutil
import tempfile
import subprocess
from natsort import natsorted
from optparse import OptionParser

outFile = None
outBuffer = []
def output( data, forcePrint = False ):
outBuffer.append( data )
Expand All @@ -36,8 +40,8 @@ def output( data, forcePrint = False ):
print( data )

def writeOutput():
if options.outFile != None:
with open( options.outFile, mode="w", encoding="utf-8" ) as out:
if outFile != None:
with open( outFile, mode="w", encoding="utf-8" ) as out:
for line in outBuffer:
out.write( f"{line}\n" )
else:
Expand All @@ -49,13 +53,28 @@ def getTags():
tags = filter(lambda x: "-" not in x, tags)
return natsorted(tags,reverse=True)

def getCommitsBetween( tagA, tagB = "HEAD", path=None):
if path:
def getCommitsBetween( tagA, tagB = "HEAD", clone=None):
if clone:
# Get repo name from clone URL
repo_name = clone.split('/')[-1]
# Get a temp folder to clone the repo into (without files)
original_path = os.getcwd()
os.chdir(path)
clone_path = tempfile.mkdtemp()
os.chdir(clone_path)
try:
subprocess.check_call(
['git', 'clone', '--bare', '--filter=blob:none', '--quiet', f'{clone}', f'{repo_name}.git'],
stdout=subprocess.DEVNULL
)
os.chdir(f'{repo_name}.git')
except Exception as e:
os.chdir(original_path)
print(f"Cannot access repo: {e}")
return "\n - Unable to find commits"
commits = os.popen(f'git log --format="%s (by %an)" --no-merges --reverse {tagA}..{tagB}').read().strip().splitlines()
if path:
if clone:
os.chdir(original_path)
shutil.rmtree(clone_path)
return "\n - ".join([""] + commits)

def getRepoURL():
Expand Down Expand Up @@ -93,7 +112,7 @@ def outputTagDiff( repoUrl, tagOld, tagNew ):
# Library commits
libInfo = getOldAndNewLibCommits( tagOld, tagNew )
for libName, lib in libInfo.items():
libCommits = getCommitsBetween(lib['old'], lib['new'], path=f"../{libName}")
libCommits = getCommitsBetween(lib['old'], lib['new'], clone=lib['url'])
if libCommits:
diffUrl = f"{lib['url']}/compare/{lib['old']}...{lib['new']}"
diffUrlMarkdown = f"[{lib['old'][:7]}...{lib['new'][:7]}]({diffUrl})"
Expand Down Expand Up @@ -121,8 +140,7 @@ def outputTagDiff( repoUrl, tagOld, tagNew ):
print( f"No such tag '{options.tag}' found, unable to continue." )
exit( 1 )

if options.outFile == None:
options.outFile = options.inFile
outFile = options.inFile if options.outFile == None else options.outFile

output( '# Changelog' )
output( '*The head of this file is autogenerated and will be overwritten on the next tag.*\n' )
Expand Down Expand Up @@ -160,7 +178,6 @@ def outputTagDiff( repoUrl, tagOld, tagNew ):
else:
# Generate a new changelog from scratch
for i in range(0, len(tags)-1):
output( tags[i] )
outputTagDiff( repoUrl, tags[i+1], tags[i] )

writeOutput()
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ jobs:
run: |
git checkout master
python3 .github/tools/gitchanges.py --input Changelog.md
- name: Commit the new Changelog
run: |
git config --global user.name 'Github Actions'
git config --global user.email '[email protected]'
git add --force Changelog.md
git commit -m "Updated the Changelog"
git commit -m "Updated the Changelog (${{ github.ref_name || 'unknown ref'}})"
git push

0 comments on commit 4465cee

Please sign in to comment.