Skip to content

Commit

Permalink
feat: Updated scripts/check_changelog_format.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Feb 1, 2024
1 parent b5cbf4e commit d509dbc
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions scripts/check_changelog_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,46 +57,59 @@ def get_referenced_issues(pr_number):

# The above returns {'data': {'repository': {'pullRequest': {'closingIssuesReferences': {'edges': [{'node': {'number': 4944}}]}}}}}

ref_edges = ref_result["data"]["repository"]["pullRequest"][
"closingIssuesReferences"
]["edges"]

if not ref_edges:
return []

return [edge["node"]["number"] for edge in ref_edges if edge]

with open(file_name, "r", encoding="utf-8") as file:
for line in file:
line = line.strip()
if not is_valid_line(line):
print(f'Invalid entry in change log: "{line}"')
sys.exit(1)

# Check if a line matches any of the specified patterns
def is_valid_line(line):
patterns = [r"^Fixes:\s*.*$", r"^Implements:\s*.*$", r"^Thanks:\s*.*$"]
for pattern in patterns:
if re.match(pattern, line):
return True
return False
# Handle specific error related to QUOTED_LITERAL token
if "Unexpected QUOTED_LITERAL token" in line:
print("Error: Unexpected QUOTED_LITERAL token in change log")
sys.exit(1)

# The referenced issue number should be valid.
for issue_number in re.findall("#([0-9]+)", line):
issue_number = int(issue_number)
try:
issue = repo.get_issue(number=issue_number)
except github.UnknownObjectException:
print(
f"The changelog entry references an invalid issue #{issue_number}:\n{line}"
)
sys.exit(1)

def main():
github_token = os.environ.get("GITHUB_TOKEN")
as_pr = None
try:
as_pr = issue.as_pull_request()
except github.UnknownObjectException:
# Not a pull request
pass

if not github_token:
print("Please populate the GITHUB_TOKEN environment variable.")
sys.exit(1)
# Accept references to PR itself.
if as_pr:
if issue_number != this_pr_number:
print(
f"The changelog for PR #{this_pr_number} references another PR #{issue_number}"
)
sys.exit(1)
changelog_issues = pr_issues
else:
changelog_issues.add(issue_number)

github_obj = github.Github(github_token)
repo = github_obj.get_repo("timescale/timescaledb")
# Get the file name from the command line argument
if len(sys.argv) != 2:
print("Please provide a file name as a command-line argument.")
if changelog_issues != pr_issues:
print(
"Instead of "
+ (f"the issues {pr_issues}" if pr_issues else "no issues")
+ f" linked to the PR #{this_pr_number}, the changelog references "
+ (f"the issues {changelog_issues}" if changelog_issues else "no issues")
)
sys.exit(1)

file_name = sys.argv[1]
this_pr_number = int(os.environ["PR_NUMBER"])
pr_issues = set(get_referenced_issues(this_pr_number))

# Read the file and check non-empty lines
changelog_issues = set()
with open(file_name, "r", encoding="utf-8") as file:
if __name__ == "__main__":
main()
for line in file:
line = line.strip()
if not is_valid_line(line):
Expand Down

0 comments on commit d509dbc

Please sign in to comment.