get_prev_version_refs.py 2.0 KB

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