Skip to content

Commit 61acbf3

Browse files
authored
Linters reusable workflow (#37)
* Use newer pre-commit config and reusable workflow * Clean up related to the new pre-commit config * Fix unit test
1 parent 39cd7c5 commit 61acbf3

23 files changed

+325
-249
lines changed

.github/workflows/lint.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/workflows/linters.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Linters
2+
3+
on:
4+
push:
5+
branches: ["master"]
6+
pull_request:
7+
8+
jobs:
9+
lint:
10+
uses: RedHatInsights/processing-tools/.github/workflows/linters.yaml@v0.1.0

.pre-commit-config.yaml

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
11
exclude: "doc|research|demos"
22
repos:
3-
- repo: https://github.com/psf/black
4-
rev: 26.3.1
5-
hooks:
6-
- id: black
7-
args: [--safe, --quiet, --line-length, "100"]
8-
language_version: python3.11
9-
require_serial: true
10-
- repo: https://github.com/pre-commit/pre-commit-hooks
11-
rev: v6.0.0
12-
hooks:
13-
- id: trailing-whitespace
14-
language_version: python3.11
15-
- id: end-of-file-fixer
16-
language_version: python3.11
17-
- id: check-yaml
18-
language_version: python3.11
19-
- id: debug-statements
20-
language_version: python3.11
21-
- repo: https://github.com/asottile/pyupgrade
22-
rev: v3.21.2
23-
hooks:
24-
- id: pyupgrade
25-
language_version: python3.11
26-
- repo: https://github.com/PyCQA/flake8
27-
rev: 7.3.0
28-
hooks:
29-
- id: flake8
30-
language_version: python3.11
31-
args: [--extend-ignore=E501]
32-
- repo: https://gitlab.com/pycqa/pydocstyle
33-
rev: 6.1.1
34-
hooks:
35-
- id: pydocstyle
36-
language_version: python3.11
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v6.0.0
5+
hooks:
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- id: check-json
9+
- id: check-toml
10+
- id: check-yaml
11+
- id: debug-statements
12+
- id: mixed-line-ending
13+
- id: check-ast
14+
- id: check-merge-conflict
15+
- id: check-added-large-files
16+
17+
- repo: https://github.com/shellcheck-py/shellcheck-py
18+
rev: v0.11.0.1
19+
hooks:
20+
- id: shellcheck
21+
# Exclude checks that are commonly problematic in organization scripts
22+
# SC1090: Can't follow non-constant source (dynamic sourcing)
23+
# SC2086: Double quote to prevent globbing (intentional word splitting)
24+
# SC2034: Variable appears unused (used by sourced scripts)
25+
# SC1091: Not following included files (dynamic includes)
26+
args: ['--exclude=SC1090,SC2086,SC2034,SC1091']
27+
28+
- repo: https://github.com/golangci/golangci-lint
29+
rev: v2.11.3
30+
hooks:
31+
- id: golangci-lint-config-verify
32+
- id: golangci-lint-full
33+
34+
- repo: https://github.com/RedHatInsights/processing-tools
35+
rev: v0.1.0
36+
hooks:
37+
- id: abcgo
38+
args: ['--threshold=64']
39+
40+
- id: go-version-consistency
41+
42+
- repo: https://github.com/astral-sh/ruff-pre-commit
43+
rev: v0.15.5
44+
hooks:
45+
# Adding a set of rules to ruff check to match pyupgrade rules
46+
- id: ruff-check
47+
args:
48+
- '--fix'
49+
- '--line-length=100'
50+
- '--select=UP,F632,E,W,F,I,UP,N,B,C4,SIM,D'
51+
- id: ruff-format
52+
53+
# - repo: https://github.com/AleksaC/hadolint-py
54+
# rev: v2.14.0
55+
# hooks:
56+
# - id: hadolint

ccx_upgrades_data_eng/auth.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
logger = logging.getLogger(__name__)
1414

1515

16-
class SessionManagerException(Exception):
16+
class SessionManagerError(Exception):
1717
"""An exception related to the initialization of the session manager."""
1818

1919

20-
class TokenException(Exception):
20+
class TokenError(Exception):
2121
"""An exception related to the refreshment of the SSO token."""
2222

2323

@@ -40,7 +40,7 @@ def __init__(
4040
try:
4141
oidc_config = requests.get(oauth_config_uri, verify=self.verify).json()
4242
except Exception as ex:
43-
raise SessionManagerException(
43+
raise SessionManagerError(
4444
f"Error getting the oauth config from the SSO server:\n{ex}"
4545
) from ex
4646

@@ -67,15 +67,15 @@ def refresh_token(self) -> str:
6767
verify=self.verify,
6868
)
6969
except Exception as ex:
70-
raise TokenException(f"Error refreshing the token:\n{ex}") from ex
70+
raise TokenError(f"Error refreshing the token:\n{ex}") from ex
7171

7272
def get_session(self) -> OAuth2Session:
7373
"""Return the OauthSession2 after refreshing the auth token."""
7474
self.refresh_token()
7575
return self.session
7676

7777

78-
@lru_cache()
78+
@lru_cache
7979
def get_session_manager() -> Oauth2Manager:
8080
"""Oauth2Manager cache."""
8181
settings = get_settings()

ccx_upgrades_data_eng/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
from functools import lru_cache
5+
56
from pydantic import ValidationError
67
from pydantic_settings import BaseSettings
78

@@ -44,7 +45,7 @@ class Settings(BaseSettings):
4445
cache_size: int = DEFAULT_CACHE_SIZE
4546

4647

47-
@lru_cache()
48+
@lru_cache
4849
def get_settings() -> Settings:
4950
"""Create the Settings object for the cache."""
5051
try:

ccx_upgrades_data_eng/examples.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
EXAMPLE_CONSOLE_URL = "https://console-openshift-console.some_url.com"
1919

2020
URL_ALERT = (
21-
EXAMPLE_CONSOLE_URL + "/monitoring/alerts?orderBy=asc&sortBy=Severity&alert-name=" + ALERT_NAME
21+
EXAMPLE_CONSOLE_URL
22+
+ "/monitoring/alerts?orderBy=asc&sortBy=Severity&alert-name="
23+
+ ALERT_NAME
2224
)
2325

2426
URL_OPERATOR_CONDITIONS = (
25-
EXAMPLE_CONSOLE_URL + "/k8s/cluster/config.openshift.io~v1~ClusterOperator/" + FOC_NAME
27+
EXAMPLE_CONSOLE_URL
28+
+ "/k8s/cluster/config.openshift.io~v1~ClusterOperator/"
29+
+ FOC_NAME
2630
)
2731

2832
EXAMPLE_PREDICTORS = {

ccx_upgrades_data_eng/inference.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
"""Utils to interact with Inference service."""
22

33
import logging
4+
from datetime import datetime, timezone
5+
46
import requests
5-
from fastapi import HTTPException
67
from cachetools import cached
7-
from datetime import datetime, timezone
8+
from fastapi import HTTPException
89

910
from ccx_upgrades_data_eng.config import get_settings
1011
from ccx_upgrades_data_eng.models import (
12+
InferenceResponse,
1113
UpgradeApiResponse,
1214
UpgradeRisksPredictors,
13-
InferenceResponse,
1415
UpgradeRisksPredictorsWithURLs,
1516
)
1617
from ccx_upgrades_data_eng.urls import fill_urls
@@ -41,7 +42,9 @@ def get_inference_for_predictors(
4142

4243
response = UpgradeApiResponse(
4344
upgrade_recommended=calculate_upgrade_recommended(risks),
44-
upgrade_risks_predictors=UpgradeRisksPredictorsWithURLs.model_validate(risks.model_dump()),
45+
upgrade_risks_predictors=UpgradeRisksPredictorsWithURLs.model_validate(
46+
risks.model_dump()
47+
),
4548
last_checked_at=datetime.now(tz=timezone.utc),
4649
)
4750
logger.debug("Inference response is: %s", response)

ccx_upgrades_data_eng/logging_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""
16-
Utility functions to redirect logs to cloudwatch.
15+
"""Utility functions to redirect logs to cloudwatch."""
1716

18-
Copied from https://github.com/RedHatInsights/insights-ccx-messaging/blob/main/ccx_messaging/utils/logging.py # noqa: E501
19-
"""
17+
# Copied from https://github.com/RedHatInsights/insights-ccx-messaging/
18+
# blob/main/ccx_messaging/utils/logging.py
2019

21-
import os
2220
import logging
21+
import os
2322

2423
from boto3.session import Session
2524
from watchtower import CloudWatchLogHandler
@@ -35,6 +34,7 @@ def __new__(self):
3534
logging.NullHandler: if the hanlder couldn't be configured.
3635
or
3736
watchtower.CloudWatchLogHandler: if it could be configured.
37+
3838
"""
3939
enabled = os.getenv("LOGGING_TO_CW_ENABLED", "False").lower()
4040
if enabled not in ("true", "1", "t", "yes"):
@@ -52,7 +52,7 @@ def __new__(self):
5252
missing_envs = list(
5353
filter(
5454
lambda key: os.environ.get(key, "").strip() == "",
55-
[key for key in aws_config_vars],
55+
aws_config_vars,
5656
)
5757
)
5858

ccx_upgrades_data_eng/main.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
import logging
44
import os
5+
from contextlib import asynccontextmanager
56
from uuid import UUID
67

78
from fastapi import Depends, FastAPI, Request, status
89
from fastapi.responses import JSONResponse
9-
from contextlib import asynccontextmanager
10+
from prometheus_fastapi_instrumentator import Instrumentator
1011

12+
import ccx_upgrades_data_eng.metrics as metrics
1113
from ccx_upgrades_data_eng.auth import (
14+
SessionManagerError,
15+
TokenError,
1216
get_session_manager,
13-
SessionManagerException,
14-
TokenException,
1517
)
16-
from ccx_upgrades_data_eng.config import get_settings, Settings
18+
from ccx_upgrades_data_eng.config import Settings, get_settings
1719
from ccx_upgrades_data_eng.inference import get_filled_inference_for_predictors
1820
from ccx_upgrades_data_eng.models import (
19-
ClustersList,
2021
ClusterPrediction,
22+
ClustersList,
2123
MultiClusterUpgradeApiResponse,
2224
UpgradeApiResponse,
2325
)
@@ -26,14 +28,13 @@
2628
perform_rhobs_request_multi_cluster,
2729
)
2830
from ccx_upgrades_data_eng.sentry import init_sentry
29-
import ccx_upgrades_data_eng.metrics as metrics
30-
31-
from prometheus_fastapi_instrumentator import Instrumentator
3231
from ccx_upgrades_data_eng.utils import get_retry_decorator
3332

3433
logger = logging.getLogger(__name__)
3534

36-
init_sentry(os.environ.get("SENTRY_DSN", None), None, os.environ.get("SENTRY_ENVIRONMENT", None))
35+
init_sentry(
36+
os.environ.get("SENTRY_DSN", None), None, os.environ.get("SENTRY_ENVIRONMENT", None)
37+
)
3738

3839

3940
def create_lifespan_handler(instrumentator: Instrumentator):
@@ -77,13 +78,13 @@ async def refresh_sso_token(request: Request, call_next) -> JSONResponse:
7778
"""Middleware to ensure SSO token is refreshed before processing the request."""
7879
try:
7980
await get_session_and_refresh_token()
80-
except SessionManagerException as ex:
81+
except SessionManagerError as ex:
8182
logger.error("Unable to initialize SSO session: %s", ex)
8283
return JSONResponse(
8384
"Unable to initialize SSO session",
8485
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
8586
)
86-
except TokenException as ex:
87+
except TokenError as ex:
8788
logger.error("Unable to update SSO token: %s", ex)
8889
return JSONResponse(
8990
"Unable to update SSO token",
@@ -92,15 +93,22 @@ async def refresh_sso_token(request: Request, call_next) -> JSONResponse:
9293
return await call_next(request)
9394

9495

95-
@app.get("/cluster/{cluster_id}/upgrade-risks-prediction", response_model=UpgradeApiResponse)
96-
async def upgrade_risks_prediction(cluster_id: UUID, settings: Settings = Depends(get_settings)):
96+
@app.get(
97+
"/cluster/{cluster_id}/upgrade-risks-prediction", response_model=UpgradeApiResponse
98+
)
99+
async def upgrade_risks_prediction(
100+
cluster_id: UUID,
101+
settings: Settings = Depends(get_settings), # noqa: B008
102+
):
97103
"""Return the predition of an upgrade failure given a set of alerts and focs."""
98104
logger.info(f"Received cluster: {cluster_id}")
99105
logger.debug("Getting predictors from RHOBS")
100106
predictors, console_url = perform_rhobs_request(cluster_id)
101107

102108
if console_url is None or console_url == "":
103-
return JSONResponse("No data for this cluster", status_code=status.HTTP_404_NOT_FOUND)
109+
return JSONResponse(
110+
"No data for this cluster", status_code=status.HTTP_404_NOT_FOUND
111+
)
104112

105113
logger.debug("Getting inference result")
106114
inference_result = get_filled_inference_for_predictors(predictors, console_url)
@@ -113,16 +121,19 @@ async def upgrade_risks_prediction(cluster_id: UUID, settings: Settings = Depend
113121

114122
@app.post("/upgrade-risks-prediction", response_model=MultiClusterUpgradeApiResponse)
115123
async def upgrade_risks_multi_cluster_predictions(
116-
clusters_list: ClustersList, settings: Settings = Depends(get_settings)
124+
clusters_list: ClustersList,
125+
settings: Settings = Depends(get_settings), # noqa: B008
117126
):
118127
"""Return the upgrade risks predictions for the provided clusters."""
119128
logger.info("Received clusters list: %s", clusters_list)
120129
logger.debug("Getting predictors from RHOBS or cache")
121130
predictors_per_cluster = perform_rhobs_request_multi_cluster(clusters_list.clusters)
122131

123-
results = list()
132+
results = []
124133
for cluster, prediction in predictors_per_cluster.items():
125-
inference_result = get_filled_inference_for_predictors(prediction[0], prediction[1])
134+
inference_result = get_filled_inference_for_predictors(
135+
prediction[0], prediction[1]
136+
)
126137
results.append(
127138
ClusterPrediction(
128139
cluster_id=str(cluster),

ccx_upgrades_data_eng/metrics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def update_ccx_upgrades_prediction_total(response: UpgradeApiResponse):
3838

3939
def update_ccx_upgrades_risks_total(response: UpgradeApiResponse):
4040
"""Update CCX_UPGRADES_RISKS_TOTAL."""
41-
CCX_UPGRADES_RISKS_TOTAL.labels("alerts").observe(len(response.upgrade_risks_predictors.alerts))
41+
CCX_UPGRADES_RISKS_TOTAL.labels("alerts").observe(
42+
len(response.upgrade_risks_predictors.alerts)
43+
)
4244
CCX_UPGRADES_RISKS_TOTAL.labels("operator_conditions").observe(
4345
len(response.upgrade_risks_predictors.operator_conditions)
4446
)

0 commit comments

Comments
 (0)