Skip to content
Merged
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
12 changes: 6 additions & 6 deletions .github/workflows/cfpb-python-library-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
matrix:
# Add the appropriate Tox environments for the satellite here
toxenv:
# - py36
- py38
# - py36
- py38
include:
# Adjust the Python versions required for the tox environments as
# Adjust the Python versions required for the tox environments as
# needed
# - toxenv: py36
# python-version: 3.6
# python-version: 3.6
- toxenv: py38
python-version: 3.8

Expand All @@ -26,7 +26,7 @@ jobs:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -38,5 +38,5 @@ jobs:
- name: Run back-end tests
run: |
tox
env:
env:
TOXENV: ${{ matrix.toxenv }}
68 changes: 49 additions & 19 deletions changelog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This is a script to determine which PRs have been merges since the last
release, or between two releases on the same branch.
"""

from __future__ import print_function

import argparse
Expand All @@ -12,7 +13,6 @@

import requests


DEFAULT_BRANCH = "main"
PUBLIC_GITHUB_URL = "https://github.com"
PUBLIC_GITHUB_API_URL = "https://api.github.com"
Expand All @@ -26,9 +26,6 @@
# Merge commits use a double linebreak between the branch name and the title
MERGE_PR_RE = re.compile(r"^Merge pull request #([0-9]+) from .*\n\n(.*)")

# Merge commits of a release branch
MERGE_RELEASE_PR_RE = re.compile(r"^Merge pull request #([0-9]+) from .*/release/.*\n\n(.*)")

# Squash-and-merge commits use the PR title with the number in parentheses
SQUASH_PR_RE = re.compile(r"^(.*) \(#([0-9]+)\).*")

Expand Down Expand Up @@ -190,6 +187,34 @@ def get_pr_details(github_config, owner, repo, pr_number):
return PullRequestDetails(body=pr_json["body"], labels=labels)


def get_pr_for_commit(github_config, owner, repo, commit_sha):
"""Get the PR associated with a commit using GitHub API"""
# Search for PRs that contain this commit
search_url = "/".join(
[
github_config.api_url,
"repos",
owner,
repo,
"commits",
commit_sha,
"pulls",
]
)

response = requests.get(search_url, headers=github_config.headers)
if response.status_code != 200:
return None

pulls = response.json()
if not pulls:
return None

# Return the first (most relevant) PR
pr = pulls[0]
return PullRequest(number=str(pr["number"]), title=pr["title"])


def extract_changelog(pr_body):
"""Extracts the Changelog from the PR Body"""
if pr_body is None:
Expand All @@ -206,8 +231,6 @@ def is_pr(message):
"""Determine whether or not a commit message is a PR merge"""
return MERGE_PR_RE.search(message) or SQUASH_PR_RE.search(message)

def is_release_merge(message):
return MERGE_RELEASE_PR_RE.search(message)

def extract_pr(message):
"""Given a PR merge commit message, extract the PR number and title"""
Expand All @@ -231,7 +254,6 @@ def fetch_changes(
previous_tag=None,
current_tag=None,
branch=DEFAULT_BRANCH,
ignore_release_merge=False,
):
if previous_tag is None:
previous_tag = get_last_tag(github_config, owner, repo)
Expand All @@ -252,7 +274,23 @@ def fetch_changes(
)

# Process the commit list looking for PR merges
prs = [extract_pr(c.message) for c in commits_between if is_pr(c.message) and (not ignore_release_merge or not is_release_merge(c.message))]
prs = []
processed_pr_numbers = set() # Track PRs we've already processed

for commit in commits_between:
pr = None

# First try to extract PR from commit message (merge/squash patterns)
if is_pr(commit.message):
pr = extract_pr(commit.message)
else:
# For rebase merges, try to find PR via GitHub API
pr = get_pr_for_commit(github_config, owner, repo, commit.sha)

# Add PR if found and not already processed
if pr and pr.number not in processed_pr_numbers:
prs.append(pr)
processed_pr_numbers.add(pr.number)

extended_prs = [
ExtendedPullRequest(
Expand Down Expand Up @@ -318,15 +356,13 @@ def generate_changelog(
github_base_url=None,
github_api_url=None,
github_token=None,
ignore_release_merge=False,
):

github_config = get_github_config(
github_base_url, github_api_url, github_token
)

prs = fetch_changes(
github_config, owner, repo, previous_tag, current_tag, branch, ignore_release_merge
github_config, owner, repo, previous_tag, current_tag, branch
)
lines = format_changes(github_config, owner, repo, prs, markdown=markdown)

Expand Down Expand Up @@ -371,7 +407,7 @@ def main():
type=str,
action="store",
default=DEFAULT_BRANCH,
help="Override the " "target branch (defaults to main)",
help="Override the target branch (defaults to main)",
)
parser.add_argument(
"--github-base-url",
Expand All @@ -396,13 +432,7 @@ def main():
type=str,
action="store",
default=None,
help="GitHub oauth token to auth " "your Github requests with",
)

parser.add_argument(
"--ignore-release-merge",
action="store_true",
help="Override if you don't want to add release merges on the changelog",
help="GitHub oauth token to auth your Github requests with",
)

args = parser.parse_args()
Expand Down
Loading