Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #121 from ESSS/pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
[pre-commit.ci] pre-commit autoupdate
  • Loading branch information
nicoddemus committed Feb 20, 2024
2 parents 5c6a16d + e28d431 commit d06da55
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 258 deletions.
36 changes: 6 additions & 30 deletions .pre-commit-config.yaml
@@ -1,39 +1,15 @@
default_language_version:
python: python3.10
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
exclude: \.data$
- id: end-of-file-fixer
exclude: \.data$
- id: debug-statements
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.12.0
hooks:
- id: reorder-python-imports
name: "reorder python imports"
args: ['--application-directories=.:src', --py36-plus]
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.1
hooks:
- id: autoflake
name: autoflake
args: ["--in-place", "--remove-unused-variables", "--remove-all-unused-imports"]
language: python
files: \.py$
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black
args: [--safe, --quiet]
language_version: python3
- repo: https://github.com/asottile/blacken-docs
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.2.2"
hooks:
- id: ruff-format
- repo: https://github.com/adamchainz/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
additional_dependencies: [black==23.1.0]
additional_dependencies: [black==24.1.1]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
Expand Down
17 changes: 17 additions & 0 deletions pyproject.toml
@@ -0,0 +1,17 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = [
"setuptools",
"setuptools-scm",
]


[tool.ruff]
src = ["src"]
line-length = 100

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.lint.isort]
force-single-line = true
4 changes: 1 addition & 3 deletions src/jobs_done10/cli.py
Expand Up @@ -18,9 +18,7 @@ def jenkins(url, username=None, password=None):

repository, jobs = GetJobsFromDirectory()
publisher = JenkinsJobPublisher(repository, jobs)
new_jobs, updated_jobs, deleted_jobs = publisher.PublishToUrl(
url, username, password
)
new_jobs, updated_jobs, deleted_jobs = publisher.PublishToUrl(url, username, password)

for job in new_jobs:
click.secho("NEW", fg="green", nl=False)
Expand Down
96 changes: 25 additions & 71 deletions src/jobs_done10/generators/jenkins.py
Expand Up @@ -3,6 +3,7 @@
This includes a generator, job publishers, constants and command line interface commands.
"""

from collections import namedtuple
from contextlib import suppress

Expand Down Expand Up @@ -143,9 +144,7 @@ def SetAdditionalRepositories(self, repositories):

# Add additional repositories
for repo in repositories:
self.SetGit(
repo["git"], git_xml=multi_scm["scms/hudson.plugins.git.GitSCM+"]
)
self.SetGit(repo["git"], git_xml=multi_scm["scms/hudson.plugins.git.GitSCM+"])

def SetAuthToken(self, auth_token):
self.xml["authToken"] = auth_token
Expand All @@ -159,9 +158,7 @@ def SetBuildBatchCommands(self, build_batch_commands):
self.SetBuildBatchCommands(command)
else:
# batch files must have \r\n as EOL or weird bugs happen (jenkins web ui add \r).
self.xml["builders/hudson.tasks.BatchFile+/command"] = command.replace(
"\n", "\r\n"
)
self.xml["builders/hudson.tasks.BatchFile+/command"] = command.replace("\n", "\r\n")

def SetBuildShellCommands(self, build_shell_commands):
for command in build_shell_commands:
Expand Down Expand Up @@ -205,9 +202,7 @@ def SetEmailNotification(self, notification_info):
mailer["dontNotifyEveryUnstableBuild"] = xmls(True)
else:
mailer["dontNotifyEveryUnstableBuild"] = xmls(False)
mailer["sendToIndividuals"] = xmls(
notification_info.pop("notify_individuals", xmls(False))
)
mailer["sendToIndividuals"] = xmls(notification_info.pop("notify_individuals", xmls(False)))

self._CheckUnknownOptions("email_notification", notification_info)

Expand Down Expand Up @@ -309,9 +304,7 @@ def SetLabelExpression(self, label_expression):
self.xml["assignedNode"] = label_expression

def SetNotifyStash(self, args):
notifier = self.xml[
"publishers/org.jenkinsci.plugins.stashNotifier.StashNotifier"
]
notifier = self.xml["publishers/org.jenkinsci.plugins.stashNotifier.StashNotifier"]

if isinstance(args, str):
# Happens when no parameter is given, we just set the URL and assume that
Expand Down Expand Up @@ -371,33 +364,25 @@ def SetScmPoll(self, schedule):
self.xml["triggers/hudson.triggers.SCMTrigger/spec"] = schedule

def SetTimeout(self, timeout):
timeout_xml = self.xml[
"buildWrappers/hudson.plugins.build__timeout.BuildTimeoutWrapper"
]
timeout_xml = self.xml["buildWrappers/hudson.plugins.build__timeout.BuildTimeoutWrapper"]
timeout_xml["timeoutMinutes"] = str(timeout)
timeout_xml["failBuild"] = xmls(True)

def SetTimeoutNoActivity(self, timeout):
timeout_xml = self.xml[
"buildWrappers/hudson.plugins.build__timeout.BuildTimeoutWrapper"
]
timeout_xml = self.xml["buildWrappers/hudson.plugins.build__timeout.BuildTimeoutWrapper"]
timeout_xml[
"strategy@class"
] = "hudson.plugins.build_timeout.impl.NoActivityTimeOutStrategy"
timeout_xml["strategy/timeoutSecondsString"] = timeout
timeout_xml[
"operationList/hudson.plugins.build__timeout.operations.FailOperation"
]
timeout_xml["operationList/hudson.plugins.build__timeout.operations.FailOperation"]

def SetCustomWorkspace(self, custom_workspace):
self.xml["customWorkspace"] = custom_workspace

def SetSlack(self, options):
room = options.get("room", "general")

properties = self.xml[
"properties/jenkins.plugins.slack.SlackNotifier_-SlackJobProperty"
]
properties = self.xml["properties/jenkins.plugins.slack.SlackNotifier_-SlackJobProperty"]
properties["@plugin"] = "slack@1.2"
properties["room"] = "#" + room
properties["startNotification"] = "true"
Expand All @@ -420,9 +405,7 @@ def SetNotification(self, options):
"properties/com.tikal.hudson.plugins.notification.HudsonNotificationProperty"
]
properties["@plugin"] = "notification@1.9"
endpoint = properties[
"endpoints/com.tikal.hudson.plugins.notification.Endpoint"
]
endpoint = properties["endpoints/com.tikal.hudson.plugins.notification.Endpoint"]
endpoint["protocol"] = options.get("protocol", "HTTP")
endpoint["format"] = options.get("format", "JSON")
endpoint["url"] = options["url"]
Expand All @@ -449,9 +432,7 @@ def SetConsoleColor(self, option):
ansi_color_wrapper["colorMapName"] = option

def SetTimestamps(self, ignored):
wrapper_xpath = (
"buildWrappers/hudson.plugins.timestamper.TimestamperBuildWrapper"
)
wrapper_xpath = "buildWrappers/hudson.plugins.timestamper.TimestamperBuildWrapper"
wrapper = self.xml[wrapper_xpath]
wrapper["@plugin"] = "timestamper@1.7.4"

Expand All @@ -460,9 +441,7 @@ def SetCoverage(self, options):
if not report_pattern:
raise ValueError("Report pattern is required by coverage")

cobertura_publisher = self.xml[
"publishers/hudson.plugins.cobertura.CoberturaPublisher"
]
cobertura_publisher = self.xml["publishers/hudson.plugins.cobertura.CoberturaPublisher"]
cobertura_publisher["@plugin"] = "cobertura@1.9.7"
# This is a file name pattern that can be used to locate the cobertura xml report files
# (for example with Maven2 use **/target/site/cobertura/coverage.xml). The path is relative
Expand All @@ -477,12 +456,8 @@ def SetCoverage(self, options):
cobertura_publisher[
"failUnhealthy"
] = "true" # fail builds if No coverage reports are found.
cobertura_publisher[
"failUnstable"
] = "true" # Unhealthy projects will be failed.
cobertura_publisher[
"autoUpdateHealth"
] = "false" # Unstable projects will be failed.
cobertura_publisher["failUnstable"] = "true" # Unhealthy projects will be failed.
cobertura_publisher["autoUpdateHealth"] = "false" # Unstable projects will be failed.
cobertura_publisher[
"autoUpdateStability"
] = "false" # Auto update threshold for health on successful build.
Expand Down Expand Up @@ -516,9 +491,7 @@ def WriteMetrics(target_name, metrics_options, default):

entry = targets["entry+"]
entry["hudson.plugins.cobertura.targets.CoverageMetric"] = "CONDITIONAL"
entry["int"] = FormatMetricValue(
metrics_options.get("conditional", default)
)
entry["int"] = FormatMetricValue(metrics_options.get("conditional", default))

healthy_options = options.get("healthy", {})
WriteMetrics("healthyTarget", healthy_options, default=80)
Expand Down Expand Up @@ -584,9 +557,7 @@ def SetWarnings(self, options):

file_parsers_xml = warnings_xml["parserConfigurations"]
for parser_options in options.get("file", []):
parser_xml = file_parsers_xml[
"hudson.plugins.warnings.ParserConfiguration+"
]
parser_xml = file_parsers_xml["hudson.plugins.warnings.ParserConfiguration+"]
parser_xml["pattern"] = parser_options.get("file_pattern")
parser_xml["parserName"] = parser_options.get("parser")

Expand All @@ -601,13 +572,9 @@ def SetTriggerJobs(self, options):
if condition not in valid_conditions:
msg = "Invalid value for condition: {!r}, expected one of {!r}"
raise RuntimeError(msg.format(condition, valid_conditions))
xml_trigger = self.xml[
"publishers/hudson.plugins.parameterizedtrigger.BuildTrigger"
]
xml_trigger = self.xml["publishers/hudson.plugins.parameterizedtrigger.BuildTrigger"]
xml_trigger["@plugin"] = "parameterized-trigger@2.33"
xml_config = xml_trigger[
"configs/hudson.plugins.parameterizedtrigger.BuildTriggerConfig"
]
xml_config = xml_trigger["configs/hudson.plugins.parameterizedtrigger.BuildTriggerConfig"]
parameters = options.get("parameters", [])
xml_configs = xml_config["configs"]
if parameters:
Expand Down Expand Up @@ -652,8 +619,7 @@ def _SetXunit(self, xunit_type, patterns):
def _CheckUnknownOptions(self, configuration_name, options_dict):
if len(options_dict) > 0:
raise RuntimeError(
"Received unknown %s options: %s"
% (configuration_name, list(options_dict.keys()))
"Received unknown %s options: %s" % (configuration_name, list(options_dict.keys()))
)


Expand Down Expand Up @@ -774,9 +740,7 @@ def PublishToDirectory(self, output_directory):
import os

for job in self.jobs.values():
with open(
os.path.join(output_directory, job.name), "w", encoding="utf-8"
) as f:
with open(os.path.join(output_directory, job.name), "w", encoding="utf-8") as f:
f.write(job.xml)

def _GetMatchingJobs(self, jenkins_api):
Expand Down Expand Up @@ -850,9 +814,7 @@ def _GetJenkinsJobBranch(self, jenkins_api, jenkins_job):

# Process them all until we find the SCM for the correct repository
for scm in scms:
url = scm.find(
"userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url"
).text.strip()
url = scm.find("userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url").text.strip()
checked_urls.append(url)

urls = [url.lower()]
Expand All @@ -861,9 +823,7 @@ def _GetJenkinsJobBranch(self, jenkins_api, jenkins_job):
urls.append(url.lower() + ".git")

if any(url in repo_urls for url in urls):
return scm.find(
"branches/hudson.plugins.git.BranchSpec/name"
).text.strip()
return scm.find("branches/hudson.plugins.git.BranchSpec/name").text.strip()

error_msg = [
'Could not find SCM for repository "%s" in job "%s"'
Expand All @@ -881,9 +841,7 @@ def _GetJenkinsJobBranch(self, jenkins_api, jenkins_job):
raise RuntimeError("\n".join(error_msg))


def UploadJobsFromFile(
repository, jobs_done_file_contents, url, username=None, password=None
):
def UploadJobsFromFile(repository, jobs_done_file_contents, url, username=None, password=None):
"""
:param repository:
.. seealso:: GetJobsFromFile
Expand Down Expand Up @@ -929,15 +887,11 @@ def GetJobsFromDirectory(directory="."):
from subprocess import check_output

url = (
check_output(
"git config --local --get remote.origin.url", shell=True, cwd=directory
)
check_output("git config --local --get remote.origin.url", shell=True, cwd=directory)
.strip()
.decode("UTF-8")
)
branches = (
check_output("git branch", shell=True, cwd=directory).strip().decode("UTF-8")
)
branches = check_output("git branch", shell=True, cwd=directory).strip().decode("UTF-8")
for branch in branches.splitlines():
branch = branch.strip()
if "*" in branch: # Current branch
Expand Down
17 changes: 6 additions & 11 deletions src/jobs_done10/job_generator.py
Expand Up @@ -82,9 +82,7 @@ def Configure(cls, generator, jobs_done_job):
try:
generator_function = getattr(generator, generator_function_name)
except AttributeError:
raise JobGeneratorAttributeError(
generator, generator_function_name, option
)
raise JobGeneratorAttributeError(generator, generator_function_name, option)

generator_function(option_value)

Expand All @@ -97,14 +95,11 @@ class JobGeneratorAttributeError(AttributeError):
"""

def __init__(self, generator, attribute, jobs_done_job_option):
message = (
'%s "%s" cannot handle option "%s" (could not find function "%s").'
% (
IJobGenerator.__name__,
generator.__class__.__name__,
jobs_done_job_option,
attribute,
)
message = '%s "%s" cannot handle option "%s" (could not find function "%s").' % (
IJobGenerator.__name__,
generator.__class__.__name__,
jobs_done_job_option,
attribute,
)

AttributeError.__init__(self, message)
13 changes: 5 additions & 8 deletions src/jobs_done10/jobs_done_job.py
Expand Up @@ -355,9 +355,7 @@ def CreateFromYAML(cls, yaml_contents, repository):

# Do not create a job if there is no match for this branch
branch_patterns = jobs_done_job.branch_patterns or [".*"]
if not any(
[re.match(pattern, repository.branch) for pattern in branch_patterns]
):
if not any([re.match(pattern, repository.branch) for pattern in branch_patterns]):
continue

jobs_done_jobs.append(jobs_done_job)
Expand Down Expand Up @@ -397,9 +395,9 @@ def _CheckAmbiguousConditions(
conditions = set(conditions)

def _IsAmbiguous():
return not previous_conditions.issubset(
conditions
) and not conditions.issubset(previous_conditions)
return not previous_conditions.issubset(conditions) and not conditions.issubset(
previous_conditions
)

if _IsAmbiguous():
import textwrap
Expand Down Expand Up @@ -630,8 +628,7 @@ def __init__(self, option):

ValueError.__init__(
self,
'Condition "%s" can never be matched based on possible matrix rows.'
% option,
'Condition "%s" can never be matched based on possible matrix rows.' % option,
)


Expand Down

0 comments on commit d06da55

Please sign in to comment.