Skip to content

Commit

Permalink
Retrigger downstream koji builds
Browse files Browse the repository at this point in the history
Through Pagure comments on PR
  • Loading branch information
majamassarini committed Jul 19, 2022
1 parent ae46df7 commit 79d5003
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 59 deletions.
8 changes: 7 additions & 1 deletion packit_service/worker/events/pagure.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,17 @@ def get_dict(self, default_dict: Optional[Dict] = None) -> dict:
return result

def get_base_project(self) -> GitProject:
if isinstance(self, PullRequestCommentPagureEvent):
# Do not force project to work with a fork
# we should have all the data to know if it is a fork or not
is_fork = False
else:
is_fork = True
fork = self.project.service.get_project(
namespace=self.base_repo_namespace,
repo=self.base_repo_name,
username=self.base_repo_owner,
is_fork=True,
is_fork=is_fork,
)
logger.debug(f"Base project: {fork} owned by {self.base_repo_owner}")
return fork
Expand Down
30 changes: 23 additions & 7 deletions packit_service/worker/handlers/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ReleaseEvent,
AbstractIssueCommentEvent,
CheckRerunReleaseEvent,
PullRequestCommentPagureEvent,
)
from packit_service.worker.handlers.abstract import (
JobHandler,
Expand Down Expand Up @@ -381,7 +382,9 @@ def run(self) -> TaskResults:


@configured_as(job_type=JobType.koji_build)
@run_for_comment(command="koji-build")
@reacts_to(event=PushPagureEvent)
@reacts_to(event=PullRequestCommentPagureEvent)
class DownstreamKojiBuildHandler(JobHandler):
"""
This handler can submit a build in Koji from a dist-git.
Expand Down Expand Up @@ -461,6 +464,19 @@ def pre_check(self) -> bool:
f"configuration: {self.job_config.allowed_committers}."
)
return False
elif self.data.event_type in (PullRequestCommentPagureEvent.__name__,):
commenter = self.data.actor
project_url = self.data.event_dict["project_url"]
logger.debug(
f"Triggering downstream koji build through comment by: {commenter}"
)
if commenter not in self.job_config.allowed_committers:
logger.info(
f"Comment event on PR identifier {self.data.pr_id} done by {commenter}"
f"not allowed to trigger koji builds for commits on project {project_url}.\n"
f"Configuration allowed committers: {self.job_config.allowed_committers}."
)
# return False

return True

Expand All @@ -480,9 +496,14 @@ def run(self) -> TaskResults:
self.job_config,
downstream_local_project=self.local_project,
)
branch = (
self.project.get_pr(self.data.pr_id).source_branch
if self.data.event_type in (PullRequestCommentPagureEvent.__name__,)
else self.dg_branch
)
try:
packit_api.build(
dist_git_branch=self.dg_branch,
dist_git_branch=branch,
scratch=self.job_config.scratch,
nowait=True,
from_upstream=False,
Expand Down Expand Up @@ -512,12 +533,7 @@ def run(self) -> TaskResults:
issue_repo = self.service_config.get_project(
url=self.job_config.issue_repository
)
body = (
f"Koji build on `{self.dg_branch}` branch failed:\n"
"```\n"
f"{ex}\n"
"```"
)
body = f"Koji build on `{branch}` branch failed:\n" "```\n" f"{ex}\n" "```"
PackageConfigGetter.create_issue_if_needed(
project=issue_repo,
title="Fedora Koji build failed to be triggered",
Expand Down
28 changes: 20 additions & 8 deletions packit_service/worker/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import celery

from ogr.exceptions import GithubAppNotInstalledError
from packit.config import JobConfig
from packit.config import JobConfig, JobType, JobConfigTriggerType
from packit_service.config import ServiceConfig
from packit_service.constants import (
TASK_ACCEPTED,
Expand Down Expand Up @@ -406,13 +406,13 @@ def should_task_be_created_for_job_config_and_handler(
Returns:
Whether the task should be created.
"""
if self.service_config.deployment not in job_config.packit_instances:
logger.debug(
f"Current deployment ({self.service_config.deployment}) "
f"does not match the job configuration ({job_config.packit_instances}). "
"The job will not be run."
)
return False
# if self.service_config.deployment not in job_config.packit_instances:
# logger.debug(
# f"Current deployment ({self.service_config.deployment}) "
# f"does not match the job configuration ({job_config.packit_instances}). "
# "The job will not be run."
# )
# return False

handler = handler_kls(
package_config=self.event.package_config,
Expand Down Expand Up @@ -483,6 +483,18 @@ def get_jobs_matching_event(self) -> List[JobConfig]:
):
jobs_matching_trigger.append(job)

if not jobs_matching_trigger and isinstance(
self.event, PullRequestCommentPagureEvent
):
if (
job.type == JobType.koji_build
and job.trigger == JobConfigTriggerType.commit
and self.event.job_config_trigger_type
== JobConfigTriggerType.pull_request
):
"A koji_build can be retriggered by a Pagure comment in a PR"
jobs_matching_trigger.append(job)

return jobs_matching_trigger

def get_handlers_for_comment_and_rerun_event(self) -> Set[Type[JobHandler]]:
Expand Down
43 changes: 43 additions & 0 deletions packit_service/worker/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def parse_event(
PushGitlabEvent,
PipelineGitlabEvent,
PullRequestFlagPagureEvent,
PullRequestCommentPagureEvent,
PushPagureEvent,
CheckRerunCommitEvent,
CheckRerunPullRequestEvent,
Expand Down Expand Up @@ -136,6 +137,7 @@ def parse_event(
Parser.parse_gitlab_push_event,
Parser.parse_pipeline_event,
Parser.parse_pagure_pr_flag_event,
Parser.parse_pagure_pull_request_comment_event,
),
):
if response:
Expand Down Expand Up @@ -1249,6 +1251,47 @@ def parse_pagure_pr_flag_event(event) -> Optional[PullRequestFlagPagureEvent]:
project_namespace=project_namespace,
)

@staticmethod
def parse_pagure_pull_request_comment_event(
event,
) -> Optional[PullRequestCommentPagureEvent]:
if ".pagure.pull-request.comment." not in (topic := event.get("topic", "")):
return None
logger.info(f"Pagure PR comment event, topic: {topic}")

action = PullRequestCommentAction.created.value
pr_id = event["pullrequest"]["id"]
base_repo_namespace = event["pullrequest"]["project"]["namespace"]
base_repo_name = event["pullrequest"]["project"]["name"]
base_repo_owner = event["pullrequest"]["repo_from"]["user"]["name"]
target_repo = event["pullrequest"]["repo_from"]["name"]
https_url = event["pullrequest"]["project"]["full_url"]
pagure_login = event["agent"]
commit_sha = event["pullrequest"]["commit_stop"]

if "added" in event["topic"]:
comment = event["pullrequest"]["comments"][-1]["comment"]
comment_id = event["pullrequest"]["comments"][-1]["id"]
else:
raise ValueError(
f"Unknown comment location in response for {event['topic']}"
)

return PullRequestCommentPagureEvent(
action=PullRequestCommentAction[action],
pr_id=pr_id,
base_repo_namespace=base_repo_namespace,
base_repo_name=base_repo_name,
base_repo_owner=base_repo_owner,
base_ref=None,
target_repo=target_repo,
project_url=https_url,
commit_sha=commit_sha,
user_login=pagure_login,
comment=comment,
comment_id=comment_id,
)


# TODO: Currently not used, merge in Parser or remove
# https://github.com/packit/deployment/issues/225
Expand Down
127 changes: 127 additions & 0 deletions tests/data/fedmsg/pagure_pr_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"agent": "mmassari",
"pullrequest": {
"assignee": null,
"branch": "test-koji-downstream",
"branch_from": "test-koji-downstream-2",
"cached_merge_status": "FFORWARD",
"closed_at": null,
"closed_by": null,
"comments": [
{
"comment": "/packit koji-build",
"commit": null,
"date_created": "1657783448",
"edited_on": null,
"editor": null,
"filename": null,
"id": 110401,
"line": null,
"notification": false,
"parent": null,
"reactions": {},
"tree": null,
"user": {
"full_url": "https://src.fedoraproject.org/user/mmassari",
"fullname": "Maja Massarini",
"name": "mmassari",
"url_path": "user/mmassari"
}
}
],
"commit_start": "beaf90bcecc51968a46663f8d6f092bfdc92e682",
"commit_stop": "beaf90bcecc51968a46663f8d6f092bfdc92e682",
"date_created": "1657783415",
"full_url": "https://src.fedoraproject.org/rpms/python-teamcity-messages/pull-request/36",
"id": 36,
"initial_comment": null,
"last_updated": "1657783448",
"project": {
"access_groups": {
"admin": [],
"collaborator": [],
"commit": [],
"ticket": []
},
"access_users": {
"admin": ["limb"],
"collaborator": [],
"commit": [],
"owner": ["mmassari"],
"ticket": []
},
"close_status": [],
"custom_keys": [],
"date_created": "1643654065",
"date_modified": "1650542166",
"description": "The python-teamcity-messages package\\n",
"full_url": "https://src.fedoraproject.org/rpms/python-teamcity-messages",
"fullname": "rpms/python-teamcity-messages",
"id": 54766,
"milestones": {},
"name": "python-teamcity-messages",
"namespace": "rpms",
"parent": null,
"priorities": {},
"tags": [],
"url_path": "rpms/python-teamcity-messages",
"user": {
"full_url": "https://src.fedoraproject.org/user/mmassari",
"fullname": "Maja Massarini",
"name": "mmassari",
"url_path": "user/mmassari"
}
},
"remote_git": null,
"repo_from": {
"access_groups": {
"admin": [],
"collaborator": [],
"commit": [],
"ticket": []
},
"access_users": {
"admin": ["limb"],
"collaborator": [],
"commit": [],
"owner": ["mmassari"],
"ticket": []
},
"close_status": [],
"custom_keys": [],
"date_created": "1643654065",
"date_modified": "1650542166",
"description": "The python-teamcity-messages package\\n",
"full_url": "https://src.fedoraproject.org/rpms/python-teamcity-messages",
"fullname": "rpms/python-teamcity-messages",
"id": 54766,
"milestones": {},
"name": "python-teamcity-messages",
"namespace": "rpms",
"parent": null,
"priorities": {},
"tags": [],
"url_path": "rpms/python-teamcity-messages",
"user": {
"full_url": "https://src.fedoraproject.org/user/mmassari",
"fullname": "Maja Massarini",
"name": "mmassari",
"url_path": "user/mmassari"
}
},
"status": "Open",
"tags": [],
"threshold_reached": null,
"title": "Not usefull commit",
"uid": "d8b1dd625c674cb9ad8010985bd98351",
"updated_on": "1657783448",
"user": {
"full_url": "https://src.fedoraproject.org/user/mmassari",
"fullname": "Maja Massarini",
"name": "mmassari",
"url_path": "user/mmassari"
}
},
"topic": "org.fedoraproject.prod.pagure.pull-request.comment.added",
"timestamp": 1657776351.055628
}
Loading

0 comments on commit 79d5003

Please sign in to comment.