Skip to content
Snippets Groups Projects
Unverified Commit 465c0915 authored by Casey's avatar Casey :recycle:
Browse files

mrhlpr: use Part-of trailer instead of adding MR ID to subject


We are planning to migrate to marge-bot for handling merging commits,
and with it adopt a new way of linking commits to their source MRs.

Using a trailer with Part-of: !MR-ID results in the commits being linked
to the MR, and the GitLab web interface inserting a hyperlink in its
place making it easier to jump to the source MR.

Let's adopt this for MRs we still merge manually now.

Use the full URL to the MR rather than just the ID, this way if we ever
split or merge repos it will still be easily possible to trace commits
back to their original MRs.

Signed-off-by: default avatarCaleb Connolly <caleb@postmarketos.org>
parent 959924f2
No related branches found
No related tags found
No related merge requests found
Pipeline #216670 passed
......@@ -16,22 +16,41 @@ if not mrhlpr_msg_filter_mr_id:
exit(1)
line_number = 0
suffix = " (MR " + mrhlpr_msg_filter_mr_id + ")"
line_count = 0
empty_lines = 0
trailer = f"Part-of: {mrhlpr_msg_filter_mr_id}"
emit_trailer = True
have_trailers = False
for line in sys.stdin:
line = line.rstrip()
line_number += 1
line_count += 1
# Add the suffix in the first line
if line_number == 1:
if line.endswith(suffix):
print(line)
else:
print(line + suffix)
# Don't emit the trailer if the commit already has it
if line == trailer:
emit_trailer = False
else:
# Make sure we have an empty line after the first one
if line_number == 2 and line != "":
print()
# Count empty lines since the last non-empty line
if line == "":
empty_lines += 1
else:
empty_lines = 0
if any(
line.lower().startswith(prefix)
for prefix in [
"signed-off-by: ",
"co-developed-by: ",
"change-id: ",
"co-authoured-by: ",
"part-of: ",
]
):
have_trailers = True
print(line)
# Print all other lines without modification
print(line)
if emit_trailer:
# Print a blank line before the trailer section unless we already have trailers
if empty_lines == 0 and not have_trailers:
print()
print(trailer)
......@@ -125,6 +125,17 @@ def get_status(
)
def get_url(mr_id: int, origin: Optional[GitLabOrigin] = None) -> str:
"""Get the URL to the merge request on the GitLab server.
:param mr_id: merge request ID
:param origin: gitlab origin information, see gitlab.parse_git_origin()
:returns: URL to the merge request"""
if not origin:
origin = gitlab.parse_git_origin()
return f"https://{origin.host}/{origin.project_id}/-/merge_requests/{mr_id}"
def get_artifacts_zip(mr_id: int, no_cache: bool, origin: GitLabOrigin) -> str:
"""Download artifacts from the GitLab API.
......@@ -326,11 +337,12 @@ def commits_have_mr_id(commits: list[str], mr_id: int) -> bool:
:param commits: return value from git.commits_on_top_of()
:returns: True if the MR-ID is in each subject, False otherwise"""
mr_url = get_url(mr_id)
for commit in commits:
subject = git.run(["show", "-s", "--format=%s", commit])
if subject is None:
body = git.run(["show", "-s", "--format=%b", commit])
if body is None:
raise RuntimeError
if not subject.endswith(" (MR " + str(mr_id) + ")"):
if f"Part-of: {mr_url}" not in body:
return False
return True
......@@ -416,7 +428,7 @@ def run_git_filter_branch(
env = os.environ.copy()
env["FILTER_BRANCH_SQUELCH_WARNING"] = "1"
if mr_id:
env["MRHLPR_MSG_FILTER_MR_ID"] = str(mr_id)
env["MRHLPR_MSG_FILTER_MR_ID"] = get_url(mr_id)
if label:
env["MRHLPR_LABEL"] = ci_labels[label]
......@@ -455,7 +467,7 @@ def fixmsg(
raise RuntimeError(msg)
os.chdir(ret)
print("Appending ' (MR " + str(mr_id) + ")' to commits and signing them...") # noqa: E501
print("Adding ' Part-of: !" + str(mr_id) + "' trailer to commits and signing them...") # noqa: E501
run_git_filter_branch("msg_filter_add_commit_id.py", f"origin/{target_branch}", mr_id)
if skip_build:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment