Skip to content

Commit

Permalink
add credentials for result hook
Browse files Browse the repository at this point in the history
  • Loading branch information
reschandreas committed Dec 25, 2023
1 parent 91862ce commit ef67e8b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
3 changes: 3 additions & 0 deletions cli/classes/generated/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ class WindfileMetadata(BaseModel):
description='The result hook that is called after the execution of the actions, always. This can be used to send the results to a server.',
examples=['https://example.com/hey-i-got-news-for-you'],
)
resultHookCredentials: Optional[str] = Field(
None, description='The credentials that are used to call the result hook', examples=['credentials-id']
)
moveResultsTo: Optional[str] = Field(
None,
description='The path where the results are moved to after the execution of the actions, always. This can be used to move the results to a deterministic location.',
Expand Down
77 changes: 43 additions & 34 deletions cli/generators/jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ def add_docker_config(self, config: Optional[Docker], indentation: int) -> None:
self.add_line(indentation=indentation, line=" }")
self.add_line(indentation=indentation, line="}")

def add_lifecycle_parameter(self, indentation: int) -> None:
self.add_line(indentation=indentation, line="parameters {")
# we set it to working_time by default, as this is the most common case, and we want to avoid
# that the job does not execute stages only meant to be executed during evaluation (e.g. hidden tests)
indentation += 2
self.add_line(
indentation=indentation,
line="string(name: 'current_lifecycle', defaultValue: 'working_time', description: 'The current stage')",
)
indentation -= 2
self.add_line(indentation=indentation, line="}")

def add_prefix(self) -> None:
"""
Add the prefix to the pipeline, e.g. the agent,
Expand All @@ -66,19 +78,11 @@ def add_prefix(self) -> None:
if self.windfile.metadata.docker is None:
self.add_line(indentation=indentation, line="agent any")
else:
self.add_docker_config(config=self.windfile.metadata.docker, indentation=indentation + 2)
self.add_docker_config(config=self.windfile.metadata.docker, indentation=indentation)
# to respect the exclusion during different parts of the lifecycle
# we need a parameter that holds the current lifecycle
self.add_line(indentation=indentation, line="parameters {")
# we set it to working_time by default, as this is the most common case, and we want to avoid
# that the job does not execute stages only meant to be executed during evaluation (e.g. hidden tests)
indentation += 2
self.add_line(
indentation=indentation,
line="string(name: 'current_lifecycle', defaultValue: 'working_time', description: 'The current stage')",
)
indentation -= 2
self.add_line(indentation=indentation, line="}")
if self.needs_lifecycle_parameter:
self.add_lifecycle_parameter(indentation=indentation)
# to work with jenkins and bamboo, we need a way to access the repository url, as this is not possible
# in a scripted jenkins pipeline, we set it as an environment variable
self.add_repository_urls_to_environment()
Expand Down Expand Up @@ -108,12 +112,8 @@ def handle_always_step(self, name: str, step: ScriptAction, indentation: int = 4
:param indentation: indentation level
:return: CI action
"""
original_name: Optional[str] = self.metadata.get_original_name_of(name)
original_type: Optional[str] = self.metadata.get_meta_for_action(name).get("original_type")

self.add_line(indentation=indentation, line=f"// step {name}")
self.add_line(indentation=indentation, line=f"// generated from step {original_name}")
self.add_line(indentation=indentation, line=f"// original type was {original_type}")
self.add_script(
wrapper="always",
name=name,
Expand All @@ -123,6 +123,19 @@ def handle_always_step(self, name: str, step: ScriptAction, indentation: int = 4
workdir=step.workdir,
)
self.handle_step_results(workdir=step.workdir, step=step)
if self.windfile.metadata.resultHook:
script: str = "sendTestResults "
if self.windfile.metadata.resultHookCredentials:
script += f"credentialsId: '{self.windfile.metadata.resultHookCredentials}', "
script += f"notificationUrl: '{self.windfile.metadata.resultHook}'"
self.add_script(
wrapper="post",
name=name,
original_type=original_type,
script=script,
indentation=indentation,
workdir=None,
)

# pylint: disable=too-many-arguments
def add_script(
Expand All @@ -143,25 +156,25 @@ def add_script(
:param indentation: indentation level
:param workdir: workdir to use
"""
self.result.append(" " * indentation + f"{wrapper} " + "{")
self.add_line(indentation=indentation, line=f"{wrapper} " + "{")
indentation += 2
self.result.append(" " * indentation + f"echo '⚙️ executing {name}'")
self.add_line(indentation=indentation, line=f"echo '⚙️ executing {name}'")
if workdir:
self.result.append(" " * indentation + f"dir('{workdir}') " + "{")
self.add_line(indentation=indentation, line=f"dir('{workdir}') " + "{")
indentation += 2
was_script_or_file: bool = original_type in ("file", "script")
if was_script_or_file:
self.result.append(" " * indentation + "sh '''")
self.add_line(indentation=indentation, line="sh '''")
for line in script.split("\n"):
if line:
self.result.append(" " * indentation + f"{line}")
self.add_line(indentation=indentation, line=line)
if was_script_or_file:
self.result.append(" " * indentation + "'''")
self.add_line(indentation=indentation, line="'''")
if workdir:
indentation -= 2
self.result.append(" " * indentation + "}")
self.add_line(indentation=indentation, line="}")
indentation -= 2
self.result.append(" " * indentation + "}")
self.add_line(indentation=indentation, line="}")

def add_results(
self,
Expand Down Expand Up @@ -196,7 +209,6 @@ def handle_step(self, name: str, step: ScriptAction, call: bool) -> None:
:param call: whether to call the step or not
:return: CI action
"""
original_name: Optional[str] = self.metadata.get_original_name_of(name)
original_type: Optional[str] = self.metadata.get_meta_for_action(name).get("original_type")
if original_type == "platform":
if step.platform == Target.jenkins.name:
Expand All @@ -213,20 +225,17 @@ def handle_step(self, name: str, step: ScriptAction, call: bool) -> None:
self.output_settings.emoji,
)
return None
self.result.append(f" // step {name}")
self.result.append(f" // generated from step {original_name}")
self.result.append(f" // original type was {original_type}")
self.result.append(f" stage('{name}') " + "{")
self.add_line(indentation=4, line=f"stage('{name}') " + "{")
self.add_docker_config(config=step.docker, indentation=6)
self.handle_step_results(workdir=step.workdir, step=step)

if step.excludeDuring is not None:
self.result.append(" when {")
self.result.append(" anyOf {")
self.add_line(indentation=6, line="when {")
self.add_line(indentation=8, line="anyOf {")
for exclusion in step.excludeDuring:
self.result.append(f" expression {{ params.current_lifecycle != '{exclusion.name}' }}")
self.result.append(" }")
self.result.append(" }")
self.add_line(indentation=10, line=f"expression {{ params.current_lifecycle != '{exclusion.name}' }}")
self.add_line(indentation=8, line="}")
self.add_line(indentation=6, line="}")
self.add_environment_variables(step=step)
self.add_script(
wrapper="steps",
Expand All @@ -236,7 +245,7 @@ def handle_step(self, name: str, step: ScriptAction, call: bool) -> None:
indentation=6,
workdir=step.workdir,
)
self.result.append(" }")
self.add_line(indentation=4, line="}")
return None

def handle_step_results(self, workdir: Optional[str], step: ScriptAction) -> None:
Expand Down
7 changes: 7 additions & 0 deletions schemas/v0.0.1/schemas/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
"https://example.com/hey-i-got-news-for-you"
]
},
"resultHookCredentials": {
"description": "The credentials that are used to call the result hook",
"type": "string",
"examples": [
"credentials-id"
]
},
"moveResultsTo": {
"description": "The path where the results are moved to after the execution of the actions, always. This can be used to move the results to a deterministic location.",
"type": "string",
Expand Down

0 comments on commit ef67e8b

Please sign in to comment.