Skip to content

Commit

Permalink
Merge branch 'master' into env
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer authored Jun 24, 2024
2 parents 3259484 + 5c69a30 commit d35141c
Show file tree
Hide file tree
Showing 17 changed files with 372 additions and 202 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @jelmer
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: jelmer
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: weekly
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: weekly
8 changes: 4 additions & 4 deletions .github/workflows/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}{% raw %}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
uses: docker/build-push-action@c382f710d39a5bb4e430307530a720f50c2d3318
with:
context: .
push: true
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/disperse.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: Disperse configuration

"on":
- push

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: jelmer/action-disperse-validate@v1
46 changes: 23 additions & 23 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.7, 3.8, 3.9, "3.10", "3.11"]
python-version: [3.8, 3.9, "3.10", "3.11", "3.12"]
exclude:
# There is no pyyaml wheel on macos-latest/3.9
- os: macos-latest
python-version: "3.9"
fail-fast: false

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip pyasn1 aiohttp
python setup.py develop
- name: Style checks
run: |
python -m pip install flake8
python -m flake8 prometheus_xmpp
- name: Typing checks
run: |
pip install -U mypy types-pytz types-jinja2 types-PyYAML
python -m mypy --ignore-missing-imports prometheus_xmpp
- name: Test suite run
run: |
python -m unittest tests.test_suite
env:
PYTHONHASHSEED: random
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install ".[dev]"
- name: Style checks
run: |
python -m ruff check .
- name: Typing checks
run: |
pip install -U mypy types-pytz types-jinja2 types-PyYAML
python -m mypy --ignore-missing-imports prometheus_xmpp
- name: Test suite run
run: |
python -m pip install ".[testing]"
python -m unittest tests.test_suite
env:
PYTHONHASHSEED: random
88 changes: 88 additions & 0 deletions .github/workflows/wheels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Build Python distributions

on:
push:
pull_request:
schedule:
- cron: "0 6 * * *" # Daily 6AM UTC build

jobs:
build-wheels:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
fail-fast: true

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build wheels
run: python -m build --wheel
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: artifact-${{ matrix.os }}
path: ./dist/*.whl

build-sdist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build sdist
run: python -m build --sdist
- name: Upload sdist
uses: actions/upload-artifact@v4
with:
name: artifact-source
path: ./dist/*.tar.gz

test-sdist:
needs:
- build-sdist
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v5
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install twine
- name: Download sdist
uses: actions/download-artifact@v4
with:
name: artifact-source
path: dist
- name: Test sdist
run: twine check dist/*
- name: Test installation from sdist
run: pip install dist/*.tar.gz

publish:
runs-on: ubuntu-latest
needs:
- build-wheels
- build-sdist
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
permissions:
id-token: write
environment:
name: pypi
url: https://pypi.org/p/prometheus-xmpp-alerts
steps:
- name: Download distributions
uses: actions/download-artifact@v4
with:
merge-multiple: true
pattern: artifact-*
path: dist
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ prometheus_xmpp_alerts.egg-info
__pycache__
dist
.tox
.mypy_cache
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LABEL maintainer="[email protected]"
RUN apt -y update && apt --no-install-recommends -y install prometheus-alertmanager python3-pip

COPY . .
RUN pip3 install .
RUN pip3 install --break-system-packages .

EXPOSE 9199

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
PYTHON = python3

all: check flake8
all: check ruff

check:
$(PYTHON) -m unittest tests.test_suite

flake8:
flake8 prometheus_xmpp/
ruff:
ruff check .

typing:
mypy prometheus_xmpp/

.PHONY: all flake8 check
.PHONY: all ruff check

docker:
buildah build -t docker.io/jvernooij/prometheus-xmpp-alerts -t ghcr.io/jelmer/prometheus-xmpp-alerts .
Expand Down
45 changes: 26 additions & 19 deletions prometheus_xmpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,62 @@
# $ python3 prometheus-xmpp-alerts --config=xmpp-alerts.yml.example

import json
import re
from datetime import datetime
import logging
import re
import subprocess
import traceback
from datetime import datetime


__version__ = (0, 5, 6)
version_string = '.'.join(map(str, __version__))
__version__ = (0, 5, 8)
version_string = ".".join(map(str, __version__))


def parse_timestring(ts):
# strptime doesn't understand nanoseconds, so discard the last three digits
ts = re.sub(r'\.([0-9]{6})([0-9]*)([^0-9])', r'.\1\3', ts)
return datetime.strptime(ts, '%Y-%m-%dT%H:%M:%S.%f%z')
ts = re.sub(r"\.([0-9]{6})([0-9]*)([^0-9])", r".\1\3", ts)
return datetime.strptime(ts, "%Y-%m-%dT%H:%M:%S.%f%z")


def strip_html_tags(html):
from bs4 import BeautifulSoup

soup = BeautifulSoup(html, features="html.parser")
return soup.get_text()


def render_text_template(template, alert):
from jinja2 import Template, TemplateError

try:
return Template(template, autoescape=False).render(
**alert, parse_time=parse_timestring)
except TemplateError as e:
traceback.print_exc()
logging.warning(
'Alert that failed to render: \n' + json.dumps(alert, indent=4))
logging.warning("Alert that failed to render: \n" + json.dumps(alert, indent=4))
return "Failed to render text template with jinja2: %s" % e.message


def render_html_template(template, alert):
from jinja2 import Template, TemplateError
from xml.etree import ElementTree as ET

from jinja2 import Template, TemplateError

try:
output = Template(template).render(**alert)
except TemplateError as e:
traceback.print_exc()
logging.warning(
'Alert that failed to render: \n' + json.dumps(alert, indent=4))
return (f"Failed to render HTML template <code>{template}</code> "
f"with jinja2: <code>{e.message}</code>")
logging.warning("Alert that failed to render: \n" + json.dumps(alert, indent=4))
return (
f"Failed to render HTML template <code>{template}</code> "
f"with jinja2: <code>{e.message}</code>"
)
try:
full = '<body>%s</body>' % output
full = "<body>%s</body>" % output
ET.fromstring(full)
except ET.ParseError as e:
import html
return (f"Failed to render HTML: {e} "
f"in <code>{html.escape(full)}</code>")

return f"Failed to render HTML: {e} " f"in <code>{html.escape(full)}</code>"
return output


Expand All @@ -73,6 +76,10 @@ def run_amtool(args):
# TODO(jelmer): Support setting the current user, e.g. for silence
# ownership.
ret = subprocess.run(
["amtool"] + args, shell=False, text=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
["amtool"] + args,
shell=False,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
return ret.stdout
Loading

0 comments on commit d35141c

Please sign in to comment.