Skip to content

Commit 3463b0b

Browse files
committed
job: add is_branch and is_tag functions
1 parent e62397d commit 3463b0b

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

murdock/job.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def remove_dir(work_dir):
8080
except FileNotFoundError:
8181
LOGGER.debug(f"Directory '{work_dir}' doesn't exist, cannot remove")
8282

83+
def is_branch(self) -> bool:
84+
return self.ref is not None and self.ref.startswith("refs/heads/")
85+
86+
def is_tag(self) -> bool:
87+
return self.ref is not None and self.ref.startswith("refs/tags/")
88+
8389
@property
8490
def start_time(self) -> datetime:
8591
return self._start_time
@@ -183,9 +189,9 @@ def env(self):
183189
"CI_BUILD_REPO": GITHUB_CONFIG.repo,
184190
}
185191
)
186-
if self.ref.startswith("refs/tags"):
192+
if self.is_tag():
187193
_env.update({"CI_BUILD_TAG": self.ref[10:]})
188-
if self.ref.startswith("refs/heads"):
194+
if self.is_branch():
189195
_env.update({"CI_BUILD_BRANCH": self.ref[11:]})
190196

191197
return _env
@@ -202,9 +208,9 @@ def title(self):
202208
commit = self.commit.sha[0:7]
203209
if self.pr is not None:
204210
return f"PR #{self.pr.number} ({commit})"
205-
elif self.ref is not None and self.ref.startswith("refs/tags"):
211+
elif self.is_tag():
206212
return f"tag {self.ref[10:]} ({commit})"
207-
elif self.ref is not None and self.ref.startswith("refs/heads"):
213+
elif self.is_branch():
208214
return f"branch {self.ref[11:]} ({commit})"
209215
else:
210216
return f"commit {commit}"

murdock/job_containers.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,7 @@ def search_with_query(self, query: JobQueryModel) -> List[MurdockJob]:
7979
if query.is_branch is not None:
8080
if query.is_branch is True:
8181
is_branch_jobs = {
82-
job
83-
for job in self.jobs
84-
if (
85-
job is not None
86-
and job.ref is not None
87-
and job.ref.startswith("refs/heads/")
88-
)
82+
job for job in self.jobs if job is not None and job.is_branch
8983
}
9084
else:
9185
is_branch_jobs = {
@@ -105,13 +99,7 @@ def search_with_query(self, query: JobQueryModel) -> List[MurdockJob]:
10599
if query.is_tag is not None:
106100
if query.is_tag is True:
107101
is_tag_jobs = {
108-
job
109-
for job in self.jobs
110-
if (
111-
job is not None
112-
and job.ref is not None
113-
and job.ref.startswith("refs/tags/")
114-
)
102+
job for job in self.jobs if job is not None and job.is_tag()
115103
}
116104
else:
117105
is_tag_jobs = {

murdock/notify.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ async def notify(self, job: MurdockJob):
9999
f'(<a href="{commit_url}" target="_blank" rel="noreferrer noopener">{commit_short}</a>) '
100100
f"by {author}"
101101
)
102-
elif job.ref is not None and job.ref.startswith("refs/tags"):
102+
elif job.is_tag():
103103
tag_url = f"https://github.com/{GITHUB_CONFIG.repo}/tree/{job.ref[10:]}"
104104
job_html_description = (
105105
f'tag <a href="{tag_url}" target="_blank" rel="noreferrer noopener">{job.ref[10:]}</a> '
106106
f'(<a href="{commit_url}" target="_blank" rel="noreferrer noopener">{commit_short}</a>)'
107107
)
108-
elif job.ref is not None and job.ref.startswith("refs/heads"):
108+
elif job.is_branch():
109109
branch_url = f"https://github.com/{GITHUB_CONFIG.repo}/tree/{job.ref[11:]}"
110110
job_html_description = (
111111
f'branch <a href="{branch_url}" target="_blank" rel="noreferrer noopener">{job.ref[11:]}</a> '
@@ -167,10 +167,10 @@ async def notify(self, job: MurdockJob, db: Database):
167167
f"{job} result still successful, skipping notification"
168168
)
169169
return
170-
if job.ref.startswith("refs/heads"):
170+
if job.is_branch():
171171
for notifier_type in self.config.branch:
172172
await self._notifiers[notifier_type].notify(job)
173-
elif job.ref.startswith("refs/tags"):
173+
elif job.is_tag():
174174
for notifier_type in self.config.tag:
175175
await self._notifiers[notifier_type].notify(job)
176176
else:

0 commit comments

Comments
 (0)