Skip to content

Commit

Permalink
Feature/gradle implementation (#4)
Browse files Browse the repository at this point in the history
* feat: Gradle PSR support

---------

Co-authored-by: Christopher Mason <[email protected]>
Co-authored-by: Nicholas Siviglia <[email protected]>
Co-authored-by: Jimmy Liang <[email protected]>
Co-authored-by: Jay Pithadia <[email protected]>
Co-authored-by: Syed Ajaz Hussain <[email protected]>
Co-authored-by: MJDP <[email protected]>
  • Loading branch information
7 people authored Jan 10, 2025
1 parent 4f6f3dd commit 7ea5577
Show file tree
Hide file tree
Showing 19 changed files with 976 additions and 50 deletions.
1 change: 1 addition & 0 deletions docs/end-to-end.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ step-runner-config:

unit-test:
- implementer: NpmTest
- implementer: GradleTest

package:
- implementer: NpmPackage
Expand Down
14 changes: 13 additions & 1 deletion src/ploigos_step_runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@
# Required.
# Id to the artifact repository to push the artifact to.
maven-push-artifact-repo-id: ''
- implementer: GradelDeploy
config: {
# Required.
# URL to the artifact repository to push the artifact to.
# maven-push-artifact-repo-url: ''
# Required.
# Id to the artifact repository to push the artifact to.
# maven-push-artifact-repo-id: ''
}
Expand Down Expand Up @@ -733,7 +742,10 @@
# npm-envs:
# ENV_VAR1: VALUE1
# ENV_VAR2: VALUE2
- implementer: GradleTest
config: {}
push-artifacts:
# WARNING: not yet implemented
- implementer: NPM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ploigos_step_runner.step_implementers.generate_metadata.dotnet_generate_metadata import \
DotnetGenerateMetadata
from ploigos_step_runner.step_implementers.generate_metadata.git import Git
from ploigos_step_runner.step_implementers.generate_metadata.gradle import Gradle
from ploigos_step_runner.step_implementers.generate_metadata.jenkins import \
Jenkins
from ploigos_step_runner.step_implementers.generate_metadata.maven import Maven
Expand Down
119 changes: 119 additions & 0 deletions src/ploigos_step_runner/step_implementers/generate_metadata/gradle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"""`StepImplementer` for the `generate-metadata` step using Gradle.
Step Configuration
------------------
Step configuration expected as input to this step.
Could come from:
* static configuration
* runtime configuration
* previous step results
Configuration Key | Required? | Default | Description
-------------------------------------|-----------|------------------|-----------
`build-file` | Yes | `'build.gradle'` | The build file to read the app version out of
Result Artifacts
----------------
Results artifacts output by this step.
Result Artifact Key | Description
----------------------------------------|------------
`app-version` | Value to use for `version` portion of semantic version \
(https://semver.org/). Uses the version read out of the given build.gradle file.
"""# pylint: disable=line-too-long

from ploigos_step_runner.results import StepResult
from ploigos_step_runner.exceptions import StepRunnerException
from ploigos_step_runner.step_implementers.shared import GradleGeneric
from ploigos_step_runner.utils.gradle import run_gradle, GradleGroovyParser

DEFAULT_CONFIG = {
'build-file': 'app/build.gradle',
}

REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS = [
'build-file'
]


class Gradle(GradleGeneric):
"""`StepImplementer` for the `generate-metadata` step using Gradle.
"""

@staticmethod
def step_implementer_config_defaults():
"""Getter for the StepImplementer's configuration defaults.
Returns
-------
dict
Default values to use for step configuration values.
Notes
-----
These are the lowest precedence configuration values.
"""
return {**GradleGeneric.step_implementer_config_defaults(), **DEFAULT_CONFIG}

@staticmethod
def _required_config_or_result_keys():
"""Getter for step configuration or previous step result artifacts that are required before
running this step.
See Also
--------
_validate_required_config_or_previous_step_result_artifact_keys
Returns
-------
array_list
Array of configuration keys or previous step result artifacts
that are required before running the step.
"""
return REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS

def _validate_required_config_or_previous_step_result_artifact_keys(self):
"""Validates that the required configuration keys or previous step result artifacts
are set and have valid values.
Validates that:
* required configuration is given
* given 'build.gradle' exists
Raises
------
AssertionError
If step configuration or previous step result artifacts have invalid required values
"""
super()._validate_required_config_or_previous_step_result_artifact_keys()

def _run_step(self):
"""Runs the step implemented by this StepImplementer.
Returns
-------
StepResult
Object containing the dictionary results of this step.
"""
try:
step_result = StepResult.from_step_implementer(self)
groovy_parser = GradleGroovyParser( self.get_value('build-file') )

# get the version
project_version = groovy_parser.get_version()
if project_version:
step_result.add_artifact(
name='app-version',
value=project_version
)
else:
step_result.success = False
step_result.message += 'Could not get project version from given build file' \
f' ({self.get_value("build-file")})'
except StepRunnerException as error:
step_result.success = False
step_result.message = str(error)

return step_result
2 changes: 2 additions & 0 deletions src/ploigos_step_runner/step_implementers/push_artifacts/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# export USER_NAME=momot-svc-acct
# export USER_PASSWORD=7VJK520gtRE7Gu8
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from ploigos_step_runner.step_implementers.push_artifacts.maven_deploy import MavenDeploy
from ploigos_step_runner.step_implementers.push_artifacts.maven import Maven
from ploigos_step_runner.step_implementers.push_artifacts.npm_push_artifacts import NpmPushArtifacts
from ploigos_step_runner.step_implementers.push_artifacts.gradle_deploy import GradleDeploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import os
import xml.etree.ElementTree as ET
import subprocess
import yaml
import time
from pathlib import Path
from ploigos_step_runner.exceptions import StepRunnerException
from ploigos_step_runner.results.step_result import StepResult
from ploigos_step_runner.step_implementers.shared.gradle_generic import GradleGeneric

DEFAULT_CONFIG = {
"build-file": "app/build.gradle",
}

REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS = [
"build-file",
"gradle-token",
"gradle-token-alpha",
]


class GradleDeploy(GradleGeneric):
"""`StepImplementer` for the `uat` step using Gradle by invoking the 'test` gradle phase."""

def __init__(
self, workflow_result, parent_work_dir_path, config, environment=None
): # pylint: disable=too-many-arguments
super().__init__(
workflow_result=workflow_result,
parent_work_dir_path=parent_work_dir_path,
config=config,
environment=environment,
gradle_tasks=["artifactoryPublish"],
)
print(f"environment : {self.environment}")
print(f"config : {self.config}")

@staticmethod
def step_implementer_config_defaults():
"""Getter for the StepImplementer's configuration defaults.
Returns
-------
dict
Default values to use for step configuration values.
Notes
-----
These are the lowest precedence configuration values.
"""
return {**GradleGeneric.step_implementer_config_defaults(), **DEFAULT_CONFIG}

@staticmethod
def _required_config_or_result_keys():
"""Getter for step configuration or previous step result artifacts that are required before
running this step.
See Also
--------
_validate_required_config_or_previous_step_result_artifact_keys
Returns
-------
array_list
Array of configuration keys or previous step result artifacts
that are required before running the step.
"""
return REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS

def read_and_replace_password(self):
"""Read a properties file, replace the Artifactory password, and save the changes."""
properties = {}

current_path = os.path.join(os.getcwd(), "app/build")
print("current_path")
print(current_path)
files_via_Path = os.listdir(current_path)
for file in files_via_Path:
print("\n files_via_Path ::" + file)

current_working_directory = os.getcwd()
print("current_working_directory")
print(current_working_directory)
files_via_current_cwd = os.listdir(current_working_directory)
for file in files_via_current_cwd:
print("\n files_via_current_cwd ::" + file)

properties_file = os.path.join(os.getcwd(), "gradle.properties")
artifactory_password = self.get_value("gradle-token-alpha")

# # Read the properties file
with open(properties_file, "r") as file:
for line in file:
# Skip comments and empty lines
line = line.strip()
if line and not line.startswith("#"):
key, value = line.split("=", 1) # Split on the first '='
properties[key] = value

# Replace the Artifactory password value
if "artifactory_password" in properties:
properties["artifactory_password"] = artifactory_password

# Write the modified properties back to the file
with open(properties_file, "w") as file:
for key, value in properties.items():
file.write(f"{key}={value}\n")

# print out the properties file
with open(properties_file, "r") as file:
content = file.read()
print("\n build.properties file: ")
print(content)

def _run_step(self):
"""Runs the step implemented by this StepImplementer.
Returns
-------
StepResult
Object containing the dictionary results of this step.
"""

self.read_and_replace_password()
step_result = StepResult.from_step_implementer(self)

# push the artifacts
gradle_output_file_path = self.write_working_file("gradle_deploy_output.txt")

try:
# execute Gradle Artifactory publish step (params come from config)
print("Push packaged gradle artifacts")

self._run_gradle_step(gradle_output_file_path=gradle_output_file_path)

except StepRunnerException as error:
step_result.success = False
step_result.message = (
"Error running 'gradle deploy' to push artifacts. "
f"More details maybe found in 'gradle-output' report artifact: {error}"
)
step_result.message = f"environment : {self.environment}"
step_result.message = f"config : {self.config}"

finally:
step_result.add_artifact(
description="Standard out and standard error from running gradle to "
"push artifacts to repository.",
name="gradle-push-artifacts-output",
value=gradle_output_file_path,
)

return step_result
2 changes: 2 additions & 0 deletions src/ploigos_step_runner/step_implementers/shared/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from ploigos_step_runner.step_implementers.shared.container_deploy_mixin import \
ContainerDeployMixin
from ploigos_step_runner.step_implementers.shared.git_mixin import GitMixin
from ploigos_step_runner.step_implementers.shared.gradle_generic import GradleGeneric
from ploigos_step_runner.step_implementers.shared.maven_generic import \
MavenGeneric
from ploigos_step_runner.step_implementers.shared.maven_test_reporting_mixin import \
MavenTestReportingMixin
from ploigos_step_runner.step_implementers.shared.npm_generic import NpmGeneric
from ploigos_step_runner.step_implementers.shared.gradle_generic import GradleGeneric
from ploigos_step_runner.step_implementers.shared.npm_xunit_generic import \
NpmXunitGeneric
from ploigos_step_runner.step_implementers.shared.openscap_generic import \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
from ploigos_step_runner.step_implementers.unit_test.maven_test import \
MavenTest
from ploigos_step_runner.step_implementers.unit_test.npm_test import NpmTest
from ploigos_step_runner.step_implementers.unit_test.gradle_test import GradleTest
from ploigos_step_runner.step_implementers.unit_test.npm_xunit_test import NpmXunitTest
from ploigos_step_runner.step_implementers.unit_test.tox_test import ToxTest
Loading

0 comments on commit 7ea5577

Please sign in to comment.