diff --git a/extra_lib_examples/job_payload/opensvc_agent_qa_run_pull_request_with_close_on_merge_skip_close_not_merged.py b/extra_lib_examples/job_payload/opensvc_agent_qa_run_pull_request_with_close_on_merge_skip_close_not_merged.py new file mode 100644 index 0000000..60ff4c5 --- /dev/null +++ b/extra_lib_examples/job_payload/opensvc_agent_qa_run_pull_request_with_close_on_merge_skip_close_not_merged.py @@ -0,0 +1,55 @@ +import yaml + +from bypass import ConditionNotMet +from context.github_pull_request import Context +from job_payload.payload_abstract import PayloadProviderAbstract + + +class JobPayloadProvider(PayloadProviderAbstract): + @staticmethod + def __call__(context: Context): + desc = context.description + pr = context.pull_request + + def first_line(text): + if not text: + return "" + else: + return text.splitlines()[0] + + job_origin = { + 'pull_request': { + 'action': desc["action"], + 'number': desc["pull_request_number"], + 'title': first_line(desc["title"]), + 'login': desc["login"], + 'url': pr["url"], + 'body': first_line(pr["body"]), + 'head': { + 'ref': pr["head"]["ref"], + 'sha': pr["head"]["sha"], + 'url': pr["head"]["repo"]["html_url"] + }, + 'base': { + 'ref': pr["base"]["ref"], + 'sha': pr["base"]["sha"], + 'url': pr["base"]["repo"]["html_url"] + } + } + } + if context.action == "closed" and pr.get("merged", False): + code_to_test = pr["base"]["ref"] + publish = "true" + elif context.action == "closed" and not pr.get("merged", False): + raise ConditionNotMet("Skipped on pull request closed but not merged") + else: + code_to_test = f"pull/{context.pull_request_number}" + publish = "false" + + return { + "options": { + "job-origin": yaml.safe_dump(job_origin, sort_keys=False), + "code-to-test": code_to_test, + "publish-release": publish, + } + } diff --git a/src/bypass/__init__.py b/src/bypass/__init__.py new file mode 100644 index 0000000..5814d22 --- /dev/null +++ b/src/bypass/__init__.py @@ -0,0 +1,3 @@ + +class ConditionNotMet(Exception): + pass diff --git a/src/runner/http_abstract.py b/src/runner/http_abstract.py index 7538bcb..307fec9 100644 --- a/src/runner/http_abstract.py +++ b/src/runner/http_abstract.py @@ -4,6 +4,7 @@ import requests +from bypass import ConditionNotMet from runner.runner_abstract import RunnerAbstract @@ -15,7 +16,13 @@ def __init__(self, verb): def execute(self, job): url = job.uri - payload = job.payload + try: + payload = job.payload + except ConditionNotMet as e: + logging.info(e) + self._status = 200 + self._data = {"message": "ConditionNotMet: %s" % e} + return verb = self.verb logging.info(f'request sending {verb} {url}') logging.info(f'request payload {json.dumps(payload, indent=4)}')