get_prev_version_refs.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import os
  2. import re
  3. import subprocess
  4. def run_git_command(command):
  5. result = subprocess.getoutput(command)
  6. print(f"Git Command: {command}")
  7. print(f"Git Output: {result}")
  8. return result
  9. def parse_merge_commit(line):
  10. # Parse merge commit messages like:
  11. # "355dc82 Merge pull request #71 from RooVetGit/better-error-handling"
  12. pattern = r"([a-f0-9]+)\s+Merge pull request #(\d+) from (.+)"
  13. match = re.match(pattern, line)
  14. if match:
  15. sha, pr_number, branch = match.groups()
  16. return {
  17. 'sha': sha,
  18. 'pr_number': pr_number,
  19. 'branch': branch
  20. }
  21. return None
  22. def get_version_refs():
  23. # Get the merge commits with full message
  24. command = 'git log --merges --pretty=oneline -n 3'
  25. result = run_git_command(command)
  26. if result:
  27. commits = result.split('\n')
  28. if len(commits) >= 3:
  29. # Parse HEAD~1 (PR to generate notes for)
  30. head_info = parse_merge_commit(commits[1])
  31. # Parse HEAD~2 (previous PR to compare against)
  32. base_info = parse_merge_commit(commits[2])
  33. if head_info and base_info:
  34. # Set output for GitHub Actions
  35. with open(os.environ['GITHUB_OUTPUT'], 'a') as gha_outputs:
  36. gha_outputs.write(f"head_ref={head_info['sha']}\n")
  37. gha_outputs.write(f"base_ref={base_info['sha']}")
  38. print(f"Head ref (PR #{head_info['pr_number']}): {head_info['sha']}")
  39. print(f"Base ref (PR #{base_info['pr_number']}): {base_info['sha']}")
  40. return head_info, base_info
  41. print("Could not find or parse sufficient merge history")
  42. return None, None
  43. if __name__ == "__main__":
  44. head_info, base_info = get_version_refs()