Skip to content

Commit

Permalink
upkeep: use refactored ci and pyright (#540)
Browse files Browse the repository at this point in the history
* upkeep: use refactored ci and pyright

* tox fmt
  • Loading branch information
lucabello authored Oct 20, 2023
1 parent 29554d0 commit 16ba0e8
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 136 deletions.
50 changes: 4 additions & 46 deletions .github/workflows/issues.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,7 @@ on: [issues]
jobs:
update:
name: Update Issue
runs-on: ubuntu-latest
steps:
- name: Dump Github Context
run: |
echo "update=false" >> $GITHUB_ENV
if [ ${{ github.event_name }} != "issues" ]; then
echo "This action only operates on issues"
exit 0
fi
echo "update=true" >> $GITHUB_ENV
- name: Determine action
run: |
if [ ${{ github.event.action }} == "opened" ]; then
echo "action=open" >> $GITHUB_ENV
fi
if [ ${{ github.event.action }} == "reopened" ]; then
echo "action=reopen" >> $GITHUB_ENV
fi
if [ ${{ github.event.action }} == "closed" ]; then
echo "action=close" >> $GITHUB_ENV
fi
- name: Determine type
run: |
if ${{ contains(github.event.*.labels.*.name, 'Type: Bug') }}; then
echo "type=bug" >> $GITHUB_ENV
else
echo "type=story" >> $GITHUB_ENV
fi
- name: Update
if: ${{ env.update == 'true' }}
env:
ID: ${{ github.event.issue.html_url }}
TITLE: ${{github.event.issue.title }}
COMPONENT: prometheus
DESCRIPTION: Opened by ${{ github.event.issue.user.login }}.
run: |
data=$(jq -n \
--arg id "$ID" \
--arg action "${{ env.action }}" \
--arg title "$TITLE" \
--arg description "$DESCRIPTION" \
--arg component "$COMPONENT" \
--arg type "${{ env.type }}" \
'{data: {id: $id, action: $action, title: $title, description: $description, component: $component, type: $type}}')
curl -X POST -H 'Content-type: application/json' --data "${data}" "${{ secrets.JIRA_URL }}"
uses: canonical/observability/.github/workflows/issues.yaml@main
secrets: inherit
with:
component: prometheus
2 changes: 1 addition & 1 deletion .github/workflows/promote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
jobs:
promote:
name: Promote
uses: canonical/observability/.github/workflows/promote-charm.yaml@main
uses: canonical/observability/.github/workflows/charm-promote.yaml@main
with:
promotion: ${{ github.event.inputs.promotion }}
secrets: inherit
6 changes: 1 addition & 5 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
name: Pull Requests

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
pull_request:
branches:
Expand All @@ -12,5 +8,5 @@ on:
jobs:
pull-request:
name: PR
uses: canonical/observability/.github/workflows/pull-request.yaml@main
uses: canonical/observability/.github/workflows/charm-pull-request.yaml@main
secrets: inherit
11 changes: 0 additions & 11 deletions .github/workflows/release-edge.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions .github/workflows/release-libs.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Release Charm to Edge and Publish Libraries

on:
push:
branches:
- main

jobs:
release:
uses: canonical/observability/.github/workflows/charm-release.yaml@main
secrets: inherit
3 changes: 2 additions & 1 deletion .github/workflows/update-libs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ on:
jobs:
update-lib:
name: Check libraries
uses: canonical/observability/.github/workflows/update-libs.yaml@main
uses: canonical/observability/.github/workflows/charm-update-libs.yaml@main
secrets: inherit

22 changes: 12 additions & 10 deletions lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def _on_scrape_targets_changed(self, event):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 42
LIBPATCH = 43

PYDEPS = ["cosl"]

Expand Down Expand Up @@ -764,7 +764,7 @@ def _validate_relation_by_interface_and_direction(
actual_relation_interface = relation.interface_name
if actual_relation_interface != expected_relation_interface:
raise RelationInterfaceMismatchError(
relation_name, expected_relation_interface, actual_relation_interface
relation_name, expected_relation_interface, actual_relation_interface or "None"
)

if expected_relation_role == RelationRole.provides:
Expand Down Expand Up @@ -857,7 +857,7 @@ class MonitoringEvents(ObjectEvents):
class MetricsEndpointConsumer(Object):
"""A Prometheus based Monitoring service."""

on = MonitoringEvents()
on = MonitoringEvents() # pyright: ignore

def __init__(self, charm: CharmBase, relation_name: str = DEFAULT_RELATION_NAME):
"""A Prometheus based Monitoring service.
Expand Down Expand Up @@ -1294,7 +1294,7 @@ def _resolve_dir_against_charm_path(charm: CharmBase, *path_elements: str) -> st
class MetricsEndpointProvider(Object):
"""A metrics endpoint for Prometheus."""

on = MetricsEndpointProviderEvents()
on = MetricsEndpointProviderEvents() # pyright: ignore

def __init__(
self,
Expand Down Expand Up @@ -1836,14 +1836,16 @@ def _set_prometheus_data(self, event):
return

jobs = [] + _type_convert_stored(
self._stored.jobs
self._stored.jobs # pyright: ignore
) # list of scrape jobs, one per relation
for relation in self.model.relations[self._target_relation]:
targets = self._get_targets(relation)
if targets and relation.app:
jobs.append(self._static_scrape_job(targets, relation.app.name))

groups = [] + _type_convert_stored(self._stored.alert_rules) # list of alert rule groups
groups = [] + _type_convert_stored(
self._stored.alert_rules # pyright: ignore
) # list of alert rule groups
for relation in self.model.relations[self._alert_rules_relation]:
unit_rules = self._get_alert_rules(relation)
if unit_rules and relation.app:
Expand Down Expand Up @@ -1895,7 +1897,7 @@ def set_target_job_data(self, targets: dict, app_name: str, **kwargs) -> None:
jobs.append(updated_job)
relation.data[self._charm.app]["scrape_jobs"] = json.dumps(jobs)

if not _type_convert_stored(self._stored.jobs) == jobs:
if not _type_convert_stored(self._stored.jobs) == jobs: # pyright: ignore
self._stored.jobs = jobs

def _on_prometheus_targets_departed(self, event):
Expand Down Expand Up @@ -1947,7 +1949,7 @@ def remove_prometheus_jobs(self, job_name: str, unit_name: Optional[str] = ""):

relation.data[self._charm.app]["scrape_jobs"] = json.dumps(jobs)

if not _type_convert_stored(self._stored.jobs) == jobs:
if not _type_convert_stored(self._stored.jobs) == jobs: # pyright: ignore
self._stored.jobs = jobs

def _job_name(self, appname) -> str:
Expand Down Expand Up @@ -2126,7 +2128,7 @@ def set_alert_rule_data(self, name: str, unit_rules: dict, label_rules: bool = T
groups.append(updated_group)
relation.data[self._charm.app]["alert_rules"] = json.dumps({"groups": groups})

if not _type_convert_stored(self._stored.alert_rules) == groups:
if not _type_convert_stored(self._stored.alert_rules) == groups: # pyright: ignore
self._stored.alert_rules = groups

def _on_alert_rules_departed(self, event):
Expand Down Expand Up @@ -2176,7 +2178,7 @@ def remove_alert_rules(self, group_name: str, unit_name: str) -> None:
json.dumps({"groups": groups}) if groups else "{}"
)

if not _type_convert_stored(self._stored.alert_rules) == groups:
if not _type_convert_stored(self._stored.alert_rules) == groups: # pyright: ignore
self._stored.alert_rules = groups

def _get_alert_rules(self, relation) -> dict:
Expand Down
10 changes: 5 additions & 5 deletions lib/charms/prometheus_k8s/v1/prometheus_remote_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 2
LIBPATCH = 3

PYDEPS = ["cosl"]

Expand Down Expand Up @@ -211,7 +211,7 @@ def _validate_relation_by_interface_and_direction(
actual_relation_interface = relation.interface_name
if actual_relation_interface != expected_relation_interface:
raise RelationInterfaceMismatchError(
relation_name, expected_relation_interface, actual_relation_interface
relation_name, expected_relation_interface, actual_relation_interface or "None"
)

if expected_relation_role == RelationRole.provides:
Expand Down Expand Up @@ -394,7 +394,7 @@ def __init__(self, *args):
```
"""

on = PrometheusRemoteWriteConsumerEvents()
on = PrometheusRemoteWriteConsumerEvents() # pyright: ignore

def __init__(
self,
Expand Down Expand Up @@ -458,7 +458,7 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
self.on.endpoints_changed.emit(relation_id=event.relation.id)

def _handle_endpoints_changed(self, event: RelationEvent) -> None:
if self._charm.unit.is_leader():
if self._charm.unit.is_leader() and event.app is not None:
ev = json.loads(event.relation.data[event.app].get("event", "{}"))

if ev:
Expand Down Expand Up @@ -591,7 +591,7 @@ def __init__(self, *args):
name to differentiate between "incoming" and "outgoing" remote write interactions is necessary.
"""

on = PrometheusRemoteWriteProviderEvents()
on = PrometheusRemoteWriteProviderEvents() # pyright: ignore

def __init__(
self,
Expand Down
30 changes: 0 additions & 30 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,6 @@ per-file-ignores = {"tests/*" = ["D100","D101","D102","D103"]}
[tool.ruff.pydocstyle]
convention = "google"

[tool.mypy]
pretty = true
python_version = 3.8
mypy_path = "$MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/lib"
follow_imports = "normal"
warn_redundant_casts = true
warn_unused_ignores = true
warn_unused_configs = true
show_traceback = true
show_error_codes = true
namespace_packages = true
explicit_package_bases = true
check_untyped_defs = true
allow_redefinition = true
no_implicit_optional = false


# Ignore libraries that do not have type hint nor stubs
[[tool.mypy.overrides]]
module = ["pytest.*", "pytest_operator.*", "prometheus_api_client.*", "deepdiff.*"]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = ["charms.grafana_k8s.*", "charms.alertmanager_k8s.*", "charms.observability_libs.*", "charms.traefik_k8s.*"]
follow_imports = "silent"

[[tool.mypy.overrides]]
module = ["ops.*"]
follow_imports = "skip"

[tool.pyright]
extraPaths = ["lib"]
pythonVersion = "3.8"
Expand Down
16 changes: 8 additions & 8 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ def _on_server_cert_changed(self, _):
def _is_cert_available(self) -> bool:
return (
self.cert_handler.enabled
and self.cert_handler.cert
and self.cert_handler.key
and self.cert_handler.ca
and (self.cert_handler.cert is not None)
and (self.cert_handler.key is not None)
and (self.cert_handler.ca is not None)
)

def _is_tls_ready(self) -> bool:
Expand All @@ -408,31 +408,31 @@ def _update_cert(self):
# Save the workload certificates
self.container.push(
CERT_PATH,
self.cert_handler.cert,
self.cert_handler.cert, # pyright: ignore
make_dirs=True,
)
self.container.push(
KEY_PATH,
self.cert_handler.key,
self.cert_handler.key, # pyright: ignore
make_dirs=True,
)
# Save the CA among the trusted CAs and trust it
self.container.push(
ca_cert_path,
self.cert_handler.ca,
self.cert_handler.ca, # pyright: ignore
make_dirs=True,
)
# FIXME with the update-ca-certificates machinery prometheus shouldn't need
# CA_CERT_PATH.
self.container.push(
CA_CERT_PATH,
self.cert_handler.ca,
self.cert_handler.ca, # pyright: ignore
make_dirs=True,
)

# Repeat for the charm container. We need it there for prometheus client requests.
ca_cert_path.parent.mkdir(exist_ok=True, parents=True)
ca_cert_path.write_text(self.cert_handler.ca)
ca_cert_path.write_text(self.cert_handler.ca) # pyright: ignore
else:
self.container.remove_path(CERT_PATH, recursive=True)
self.container.remove_path(KEY_PATH, recursive=True)
Expand Down
13 changes: 5 additions & 8 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ commands =
description = Run static analysis checks
deps =
cosl
mypy
types-dataclasses
types-PyYAML
types-requests
pyright
charm: -r{toxinidir}/requirements.txt
lib: ops
charm: responses==0.20.0
Expand All @@ -59,11 +56,11 @@ deps =
integration: {[testenv:integration]deps}
integration: pytest-operator==1.0.0b1
commands =
charm: mypy {[vars]src_path} {posargs}
lib: mypy --python-version 3.8 {[vars]lib_path} {posargs}
charm: pyright {[vars]src_path} {posargs}
lib: pyright --pythonversion 3.8 {[vars]lib_path} {posargs}
lib: /usr/bin/env sh -c 'for m in $(git diff main --name-only {[vars]lib_path}); do if ! git diff main $m | grep -q "+LIBPATCH\|+LIBAPI"; then echo "You forgot to bump the version on $m!"; exit 1; fi; done'
unit: mypy {[vars]tst_path}/unit {posargs}
integration: mypy {[vars]tst_path}/integration {posargs}
unit: pyright {[vars]tst_path}/unit {posargs}
integration: pyright {[vars]tst_path}/integration {posargs}
allowlist_externals = /usr/bin/env

[testenv:unit]
Expand Down

0 comments on commit 16ba0e8

Please sign in to comment.