Skip to content

Commit 5f001c0

Browse files
authored
Merge pull request #2 from strohganoff/ci-workflow
Add github action for running tests
2 parents 2950977 + bc97d3b commit 5f001c0

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

.github/workflows/publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
on:
2+
release:
3+
types: [published]
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
release-build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-python@v5
14+
with:
15+
python-version: "3.x"
16+
- name: Build release distributions
17+
run: |
18+
python -m pip install build
19+
python -m build
20+
- name: Publish release distributions to PyPI
21+
uses: pypa/gh-action-pypi-publish@6f7e8d9c0b1a2c3d4e5f6a7b8c9d0e1f2a3b4c5d

.github/workflows/tests.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Tests
2+
on:
3+
push:
4+
branches: [main]
5+
paths-ignore: ['*.md', '.gitignore']
6+
pull_request:
7+
types: [opened, synchronize, labeled]
8+
branches: [main]
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
if: github.event_name == 'push' ||
13+
((github.event.action == 'opened' || github.event.action == 'synchronize') && contains(github.event.pull_request.labels.*.name, 'READY TO TEST')) ||
14+
(github.event.action == 'labeled' && github.event.label.name == 'READY TO TEST')
15+
steps:
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-python@v5
18+
with:
19+
cache: 'pip'
20+
python-version: "3.x"
21+
- run: |
22+
python -m pip install --upgrade pip
23+
python -m pip install .[dev]
24+
- name: Test with pytest and python version
25+
run: pytest -p no:sugar --cov=streamdeck --cov-report=xml --cov-report=html --cov-report=term --junit-xml=reports/xunit-results.xml
26+
- name: Upload test coverage reports to artifacts
27+
uses: actions/upload-artifact@v4
28+
with:
29+
name: test-coverage-reports
30+
path: reports/
31+
if: ${{ always() }}

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
[project]
99
name = "streamdeck-plugin-sdk"
10-
version = "0.3.0"
10+
dynamic = ["version"]
1111
description = "Write Streamdeck plugins using Python"
1212
readme = "README.md"
1313
authors = [
@@ -53,6 +53,9 @@
5353
include = ["streamdeck*"]
5454
exclude = ["tests"]
5555

56+
[tool.setuptools.dynamic]
57+
version = {attr = "streamdeck.utils._version.MODULE_VERSION"}
58+
5659

5760
# Add type annotations to your Python programs, and use mypy to type check them.
5861
[tool.mypy]

streamdeck/utils/_version.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""This module provides dynamic versioning for the package, intended for use in GitHub Actions during a release event.
2+
3+
The version is extracted from the release tag in the GitHub event data.
4+
If this ins't a release, or isn't running in a GitHub runner, or something is missing,
5+
the script will fall back to version "0.0.1", to allow for testing.
6+
7+
Future Improvements:
8+
- I may either improve this to handle local building as well as merging into 'main' branch,
9+
or just go with setuptools_scm to replace all of this.
10+
"""
11+
from __future__ import annotations
12+
13+
import json
14+
import logging
15+
import os
16+
17+
18+
# NOTE: A single variable is set using this function below.
19+
def get_version():
20+
"""Retrieve the version from GitHub Actions environment variables.
21+
22+
Raises:
23+
RuntimeError: If not running in GitHub Actions or the event is not a release.
24+
FileNotFoundError: If the GitHub event path file is not found.
25+
json.JSONDecodeError: If the event data cannot be parsed as JSON.
26+
ValueError: If the release tag is not found in the event data.
27+
28+
Returns:
29+
str: The version extracted from the release tag.
30+
"""
31+
if os.getenv("GITHUB_ACTIONS") != "true":
32+
logging.error("Missing $GITHUB_ACTIONS environment variable. This script must (currently) be run in a GitHub Actions environment.")
33+
return "0.0.1"
34+
# May restrict the event type in the future.
35+
# if os.getenv("GITHUB_EVENT_NAME") != "release":
36+
# logging.error("Missing $GITHUB_EVENT_NAME environment variable or not a release event. This script must (currently) be triggered by a release event.")
37+
# return "0.0.1"
38+
39+
event_path = os.getenv("GITHUB_EVENT_PATH")
40+
41+
if not event_path:
42+
logging.error("Missing $GITHUB_EVENT_PATH environment variable. Unable to locate the GitHub event data file.")
43+
return "0.0.1"
44+
45+
try:
46+
with open(event_path) as f:
47+
event_data = json.load(f)
48+
except FileNotFoundError as e:
49+
logging.exception("Error reading event data from file.")
50+
return "0.0.1"
51+
except json.JSONDecodeError as e:
52+
logging.exception("Error parsing event data as JSON.")
53+
return "0.0.1"
54+
55+
# May restrict the action type in the future.
56+
# if event_data.get("action") != "published":
57+
# return "0.0.1"
58+
59+
tag_name = event_data.get("release", {}).get("tag_name")
60+
61+
if not tag_name:
62+
logging.error("No tag version found in event_data file.")
63+
return "0.0.1"
64+
65+
return tag_name.lstrip("v")
66+
67+
68+
MODULE_VERSION = get_version()

0 commit comments

Comments
 (0)