Skip to content

Commit

Permalink
Update release.py and service-status.py
Browse files Browse the repository at this point in the history
This picks up these changes:

```
d19f45b Extract bug numbers from commits and list them when using make-bug (#35)
2af6518 Update github actions workflow
76121fb Add test scaffolding
f351b04 Update linting and reformatting to use ruff
850fab2 Print url to tag information GitHub (#45)
8d0d645 fix ruff S113 Probable use of requests call without timeout
f6090eb Fix service-status.py so it works without requests (#42)
```
  • Loading branch information
willkg committed Feb 29, 2024
1 parent 0dcd53b commit 4db3218
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 44 deletions.
4 changes: 2 additions & 2 deletions bin/license-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"""
This script checks files for license headers.
This requires Python 3 to run.
This requires Python 3.8+ to run.
See https://github.com/willkg/socorro-release/#readme for details.
repo: https://github.com/willkg/socorro-release/
sha: 8c609f3a0934b5f5fc4a954bed4e0c5cce16c429
sha: d19f45bc9eedae34de2905cdd4adf7b9fd03f870
"""

Expand Down
106 changes: 83 additions & 23 deletions bin/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
This has two subcommands: ``make-bug`` and ``make-tag``. See the help text for
both.
This requires Python 3 to run.
This requires Python 3.8+ to run.
If you want to use ``pyproject.toml`` and you're using Python <3.11, this also
requires the tomli library.
Note: If you want to use ``pyproject.toml`` and you're using Python <3.11, this
also requires the tomli library.
See https://github.com/willkg/socorro-release/#readme for details.
repo: https://github.com/willkg/socorro-release/
sha: 8c609f3a0934b5f5fc4a954bed4e0c5cce16c429
sha: d19f45bc9eedae34de2905cdd4adf7b9fd03f870
"""

Expand All @@ -31,8 +31,8 @@
import shlex
import subprocess
import sys
from urllib.request import urlopen
from urllib.parse import urlencode
from urllib.request import urlopen


DESCRIPTION = """
Expand Down Expand Up @@ -61,6 +61,12 @@

LINE = "=" * 80

# Recognize "bug-NNNNNNN", "bug NNNNNNN", and multi-bug variants
BUG_RE = re.compile(r"\bbug(?:s?:?\s*|-)([\d\s,\+&#and]+)\b", re.IGNORECASE)

# Recognize "bug-NNNNNNN"
BUG_HYPHEN_PREFIX_RE = re.compile(r"bug-([\d]+)", re.IGNORECASE)


def get_config():
"""Generates configuration.
Expand Down Expand Up @@ -114,6 +120,28 @@ def get_config():
return my_config


def find_bugs(line):
"""Returns all the bug numbers from the line.
>>> get_bug_numbers("some line")
[]
>>> get_bug_numbers("bug-1111111: some line")
["1111111"]
>>> get_bug_numbers("bug 1111111, 2222222: some line")
["1111111", "2222222"]
"""
matches = BUG_RE.findall(line)
if not matches:
return []
bugs = []
for match in matches:
for part in re.findall(r"\d+", match):
if part:
bugs.append(part)
return bugs


def fetch(url, is_json=True):
"""Fetch data from a url
Expand Down Expand Up @@ -169,7 +197,14 @@ def check_https(github_user, remote_url):
raise Exception(f"Can't figure out remote name for {github_user}.")


def make_tag(bug_number, remote_name, tag_name, commits_since_tag):
def make_tag(
bug_number,
github_project,
github_user,
remote_name,
tag_name,
commits_since_tag,
):
"""Tags a release."""
if bug_number:
resp = fetch(BZ_BUG_JSON_URL + bug_number, is_json=True)
Expand All @@ -186,6 +221,7 @@ def make_tag(bug_number, remote_name, tag_name, commits_since_tag):
message = f"Tag {tag_name}\n\n" + "\n".join(commits_since_tag)

# Print out new tag information
print("")
print(">>> New tag: %s" % tag_name)
print(">>> Tag message:")
print(LINE)
Expand All @@ -194,31 +230,35 @@ def make_tag(bug_number, remote_name, tag_name, commits_since_tag):

# Create tag
input(f">>> Ready to tag {tag_name}? Ctrl-c to cancel")
print("")
print(">>> Creating tag...")
subprocess.check_call(["git", "tag", "-s", tag_name, "-m", message])

# Push tag
input(f">>> Ready to push to remote {remote_name}? Ctrl-c to cancel")
print("")
print(">>> Pushing...")
subprocess.check_call(["git", "push", "--tags", remote_name, tag_name])

if bug_number:
# Show tag for adding to bug comment
print(f">>> Show tag... Copy and paste this into bug #{bug_number}.")
print(">>> %<-----------------------------------------------")
output = check_output(f"git show {tag_name}")
# Truncate the output at "diff --git"
output = output[: output.find("diff --git")].strip()
print(f"Tagged {tag_name}:")
# Show url to tag information on GitHub for bug comment
tag_url = (
f"https://github.com/{github_user}/{github_project}/releases/tag/{tag_name}"
)
print("")
print("```")
print(output)
print("```")
print(f">>> Copy and paste this tag url into bug #{bug_number}.")
print(">>> %<-----------------------------------------------")
print(f"{tag_url}")
print(">>> %<-----------------------------------------------")


def make_bug(
github_project, tag_name, commits_since_tag, bugzilla_product, bugzilla_component
github_project,
tag_name,
commits_since_tag,
bugs_referenced,
bugzilla_product,
bugzilla_component,
):
"""Creates a bug."""
summary = f"{github_project} deploy: {tag_name}"
Expand All @@ -230,10 +270,16 @@ def make_bug(
description = [
f"We want to do a deploy for `{github_project}` tagged `{tag_name}`.",
"",
"It consists of the following:",
"It consists of the following commits:",
"",
]
description.extend(commits_since_tag)
if bugs_referenced:
description.append("")
description.append("Bugs referenced:")
description.append("")
for bug in sorted(bugs_referenced):
description.append(f"* bug #{bug}")
description = "\n".join(description)

print(">>> Description")
Expand Down Expand Up @@ -350,8 +396,8 @@ def run():
first_commit = check_output("git rev-list --max-parents=0 HEAD")
resp = fetch_history_from_github(github_user, github_project, first_commit)

bugs_referenced = set()
commits_since_tag = []
bug_name_prefix_regexp = re.compile(r"bug-([\d]+)", re.IGNORECASE)
for commit in resp["commits"]:
# Skip merge commits
if len(commit["parents"]) > 1:
Expand All @@ -365,10 +411,16 @@ def run():
summary = commit["commit"]["message"]
summary = summary.splitlines()[0]
summary = summary[:80]
# Bug 1868455: While GitHub autolinking doesn't suport spaces, Bugzilla autolinking
# doesn't support hyphens.

# Bug 1868455: While GitHub autolinking doesn't suport spaces, Bugzilla
# autolinking doesn't support hyphens. When creating a bug, we want to
# use "bug NNNNNNN" form so Bugzilla autolinking works.
if args.cmd == "make-bug":
summary = bug_name_prefix_regexp.sub(r"bug \1", summary)
summary = BUG_HYPHEN_PREFIX_RE.sub(r"bug \1", summary)

bugs = find_bugs(summary)
if bugs:
bugs_referenced |= set(bugs)

# Figure out who did the commit prefering GitHub usernames
who = commit["author"]
Expand Down Expand Up @@ -401,6 +453,7 @@ def run():
github_project,
tag_name,
commits_since_tag,
bugs_referenced,
args.bugzilla_product,
args.bugzilla_component,
)
Expand All @@ -412,7 +465,14 @@ def run():
+ "specify a bug number with --with-bug."
)
return 1
make_tag(args.bug, remote_name, tag_name, commits_since_tag)
make_tag(
args.bug,
github_project,
github_user,
remote_name,
tag_name,
commits_since_tag,
)

else:
parser.print_help()
Expand Down
34 changes: 15 additions & 19 deletions bin/service-status.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
This script looks at the ``/__version__`` endpoint information and tells you
how far behind different server environments are from main tip.
This requires Python 3 to run. See help text for more.
This requires Python 3.8+ to run. See help text for more.
See https://github.com/willkg/socorro-release/#readme for details.
If you want to use ``pyproject.toml`` and you're using Python <3.11, this also
requires the tomli library.
Note: If you want to use ``pyproject.toml`` and you're using Python <3.11, this
also requires the tomli library.
repo: https://github.com/willkg/socorro-release/
sha: 8c609f3a0934b5f5fc4a954bed4e0c5cce16c429
sha: d19f45bc9eedae34de2905cdd4adf7b9fd03f870
"""

Expand All @@ -25,8 +25,7 @@
import os
import sys
from urllib.parse import urlparse

import requests
from urllib.request import urlopen


DESCRIPTION = """
Expand Down Expand Up @@ -83,20 +82,17 @@ def get_config():


def fetch(url, is_json=True):
resp = requests.get(url)
if resp.status_code != 200:
print(url)
print(f"{resp.status_code}, {resp.content}")
raise Exception("Bad return code")
"""Fetch data from a url
This raises URLError on HTTP request errors. It also raises JSONDecode
errors if it's not valid JSON.
"""
fp = urlopen(url, timeout=5)
data = fp.read()
if is_json:
try:
data = resp.content.strip()
data = data.replace(b"\n", b"")
return json.loads(data)
except json.decoder.JSONDecodeError:
print(data)
raise
return resp.content
return json.loads(data)
return data


def fetch_history_from_github(main_branch, user, repo, from_sha):
Expand Down

0 comments on commit 4db3218

Please sign in to comment.