Skip to content

Commit b9ca154

Browse files
committed
Improve logic and handle extra spaces better
1 parent 1f779c5 commit b9ca154

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

.github/workflows/labeler.yml

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,56 +61,53 @@ jobs:
6161
print(f'PR labels: "{labels}"')
6262
tags_relevant = [tags[label] for label in tags if label in labels.split(",")]
6363
print("Relevant title tags:", ",".join(tags_relevant))
64-
passed = True
65-
prefix_good = ",".join(tags_relevant)
66-
prefix_good = f"[{prefix_good}] "
67-
print(f"Generated prefix: {prefix_good}")
68-
replace_title = 0
69-
title_new = title
64+
prefix_generated = ",".join(tags_relevant)
65+
prefix_generated = f"[{prefix_generated}] "
66+
print(f"Generated prefix: {prefix_generated}")
67+
found_tags = False
68+
title_new = title.strip()
7069
# If there is a prefix which contains a known tag, check it for correct tags, and reformat it if needed.
7170
# If there is a prefix which does not contain any known tag, add the tag prefix.
7271
# If there is no prefix, add the tag prefix.
73-
if match := re.match(r"\[?(\w[\w, /\+-]+)[\]:]+ ", title):
72+
if match := re.match(r" *\[?(\w[\w,/\+\- ]+)[\]: ]+ ", title):
7473
prefix_title = match.group(1)
7574
words_prefix_title = prefix_title.replace(",", " ").replace("/", " ").split()
76-
title_stripped = title[len(match.group()) :]
75+
title_stripped = title[len(match.group()) :].strip()
7776
print(f'PR title prefix: "{prefix_title}" -> tags: {words_prefix_title}')
7877
print(f'Stripped PR title: "{title_stripped}"')
7978
if any(tag in words_prefix_title for tag in tags.values()):
79+
found_tags = True
80+
passed = True
8081
for tag in tags.values():
8182
if tag in tags_relevant and tag not in words_prefix_title:
8283
print(f'::error::Relevant tag "{tag}" not found in the prefix of the PR title.')
8384
passed = False
8485
if tag not in tags_relevant and tag in words_prefix_title:
8586
print(f'::error::Irrelevant tag "{tag}" found in the prefix of the PR title.')
8687
passed = False
87-
# Format a valid prefix.
88-
if passed:
89-
prefix_good = ",".join(w for w in prefix_title.replace(",", " ").split() if w)
90-
prefix_good = f"[{prefix_good}] "
91-
print(f"::notice::Reformatted prefix: {prefix_good}")
92-
if match.group() != prefix_good:
93-
replace_title = 1
94-
title_new = prefix_good + title_stripped
88+
if not passed:
89+
print("::error::Problems were found in the PR title prefix.")
90+
print('::notice::Use the form "tags: title" or "[tags] title".')
91+
sys.exit(1)
92+
# Form a valid title with the existing prefix.
93+
prefix_good = ",".join(w for w in prefix_title.replace(",", " ").split() if w)
94+
prefix_good = f"[{prefix_good}] "
95+
print(f'::notice::Reformatted prefix: "{prefix_good}"')
96+
title_new = prefix_good + title_stripped
9597
else:
9698
print("::warning::No known tags found in the prefix.")
97-
if tags_relevant:
98-
replace_title = 1
99-
title_new = prefix_good + title
99+
title_new = " ".join((*match.group().split(), title_stripped))
100100
else:
101101
print("::warning::No valid prefix found in the PR title.")
102-
if tags_relevant:
103-
replace_title = 1
104-
title_new = prefix_good + title
105-
if not passed:
106-
print("::error::Problems were found in the PR title prefix.")
107-
print('::notice::Use the form "tags: title" or "[tags] title".')
108-
sys.exit(1)
109-
if replace_title:
110-
print("::warning::The PR title prefix with tags needs to be added or adjusted.")
111-
print(f'::warning::New title: "{title_new}".')
102+
if not found_tags and tags_relevant:
103+
title_new = prefix_generated + title_new.strip()
104+
replace_title = 0
105+
if title_new == title:
106+
print("::notice::The PR title is fine.")
112107
else:
113-
print("::notice::The PR title prefix is fine.")
108+
replace_title = 1
109+
print("::warning::The PR title needs to be adjusted.")
110+
print(f'::warning::New title: "{title_new}".')
114111
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as fh:
115112
print(f"replace={replace_title}", file=fh)
116113
print(f"title={title_new}", file=fh)

0 commit comments

Comments
 (0)