Skip to content

Commit a241e8f

Browse files
authored
Merge pull request #1 from lestex/initial
adds code, workflows and tests
2 parents 1d5400e + c0a5792 commit a241e8f

24 files changed

+1509
-2
lines changed

.editorconfig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
# Unix-style newlines with a newline ending every file
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
indent_size = 4
10+
indent_style = space
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
# Docstrings and comments use max_line_length = 79
15+
[*.py]
16+
max_line_length = 119
17+
18+
# Makefiles always use tabs for indentation
19+
[Makefile]
20+
indent_style = tab
21+
22+
[*.md]
23+
max_line_length = 0
24+
trim_trailing_whitespace = false
25+
26+
[*.{yaml,yml}]
27+
indent_size = 2

.github/workflows/lint.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Lint
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
paths:
7+
- "**.py"
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version:
15+
- '3.10'
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
- uses: actions/setup-python@v2
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
24+
- name: Install dependencies
25+
run: |
26+
pip install --upgrade pip setuptools wheel
27+
pip install flake8==4.0.1
28+
pip install --upgrade isort blue mypy types-PyYAML bandit safety
29+
30+
- name: Run lint
31+
run: isort .
32+
33+
- name: Run blue formatter
34+
run: make format
35+
36+
- name: Run flake8
37+
run: |
38+
flake8 . --count --show-source --statistics
39+
flake8 . --count --exit-zero --max-complexity=4
40+
41+
- name: Run mypy / static types
42+
run: make static-check
43+
44+
- name: Run bandit
45+
run: bandit pygithubactions
46+
47+
- name: Run safety
48+
run: safety check

.github/workflows/main.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: CI
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches:
7+
- 'main'
8+
push:
9+
paths-ignore: [ 'docs/**', '*.md', '*.yaml', '*.yml', '*.toml' ]
10+
branches:
11+
- 'main'
12+
13+
env:
14+
GH_TOKEN: ${{ github.token }}
15+
16+
jobs:
17+
test:
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
python-version:
22+
- '3.10'
23+
os: [ubuntu-latest]
24+
25+
runs-on: ${{ matrix.os }}
26+
27+
steps:
28+
- uses: actions/checkout@v2
29+
30+
- uses: actions/setup-python@v2
31+
with:
32+
python-version: ${{ matrix.python-version }}
33+
34+
- name: Install dependencies
35+
run: |
36+
python -m pip install --upgrade pip setuptools wheel
37+
python -m pip install pytest pytest-cov
38+
make install-local
39+
40+
- name: Run tests
41+
run: make test

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM python:3.10 as builder
2+
3+
ENV PYTHONDONTWRITEBYTECODE 1
4+
ENV PYTHONUNBUFFERED 1
5+
6+
RUN apt update && apt upgrade -y
7+
8+
WORKDIR /app
9+
COPY . /app/
10+
11+
# install app
12+
RUN pip install .
13+
14+
ENV HOME="/home/nonadmin"
15+
RUN groupadd -r -g 1001 nonadmin && useradd -r -d $HOME -u 1001 -g nonadmin nonadmin
16+
RUN mkdir -p $HOME
17+
RUN chown nonadmin $HOME
18+
USER nonadmin
19+
20+
CMD [ "python", "-V"]

Makefile

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
.PHONY: help tag release install-local test
2+
.DEFAULT_GOAL := help
3+
4+
TEST_RESULTS := test_results
5+
APP_NAME := actions-repository-filtering
6+
SYSTEM_PYTHON := python
7+
8+
define VERSION_PYSCRIPT
9+
import pkg_resources
10+
version = pkg_resources.get_distribution('pygithubactions').version
11+
endef
12+
export VERSION_PYSCRIPT
13+
14+
VERSION := $(SYSTEM_PYTHON) -c "$$VERSION_PYSCRIPT"
15+
TAG := v$(VERSION)
16+
17+
define BROWSER_PYSCRIPT
18+
import os, webbrowser, sys
19+
20+
try:
21+
from urllib import pathname2url
22+
except:
23+
from urllib.request import pathname2url
24+
25+
webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
26+
endef
27+
export BROWSER_PYSCRIPT
28+
29+
BROWSER := $(SYSTEM_PYTHON) -c "$$BROWSER_PYSCRIPT"
30+
31+
help:
32+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
33+
| awk 'BEGIN {FS = ":.*?## "}; \
34+
{printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
35+
36+
tag: ## create and push a tag for the current version
37+
echo $(VERSION_PYSCRIPT)
38+
# git tag $(TAG)
39+
# git push origin $(TAG)
40+
41+
release: ## package and upload a release
42+
@echo "For further usage."
43+
44+
install-local: ## install scaffold locally
45+
@echo "Make sure you activated the virtual env with: 'source .venv/bin/activate'"
46+
python -m pip install --upgrade pip
47+
pip install -e .
48+
49+
test: ## runs tests
50+
@pytest -s -vvv
51+
52+
coverage: ## run tests with coverage
53+
@pytest --cov=pygithubactions
54+
55+
create-results: ## create test_results directory
56+
mkdir -p $(TEST_RESULTS)
57+
58+
coverage-output: ## create outputs for coverage reports
59+
@coverage html -d $(TEST_RESULTS)
60+
61+
coverage-report: create-results coverage coverage-output ## run coverage, generate report, open in the browser
62+
$(SYSTEM_PYTHON) -m webbrowser -t "file://$(PWD)/$(TEST_RESULTS)/index.html"
63+
64+
coverage-clean: ## remove the coverage directory
65+
rm -rf $(TEST_RESULTS)
66+
rm .coverage
67+
68+
isort: ## runs isort
69+
@isort .
70+
71+
format: isort ## formats python
72+
@blue .
73+
74+
lint: format ## run python flake8 linter tool
75+
@flake8 --exclude .git,.venv pygithubactions
76+
77+
static-check: ## runs static checks with mypy
78+
@mypy pygithubactions
79+
80+
precommit-install: ## install pre-commit
81+
@pre-commit install
82+
83+
precommit-run: ## run all pre-commit hooks
84+
@pre-commit run -a
85+
86+
clean: ## run clean up
87+
rm -rf .pytest_cache dist build yamlvalidator.egg-info .pytest_cache .mypy_cache test_results .coverage
88+
find . -type d -name '__pycache__' -exec rm -r {} +

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# py-githubaction
2-
Github actions core library implemented in python
1+
# py-githubactions
2+
Github actions core library implemented in python.

pygithubactions/__init__.py

Whitespace-only changes.

pygithubactions/context/__init__.py

Whitespace-only changes.

pygithubactions/context/context.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import json
2+
import os
3+
from typing import Optional
4+
5+
6+
class Context:
7+
"""Processes all env vars set by github in runner"""
8+
9+
def __init__(self) -> None:
10+
self._payload: dict = {}
11+
self._event_name: Optional[str] = os.getenv('GITHUB_EVENT_NAME')
12+
self.sha: Optional[str] = os.getenv('GITHUB_SHA')
13+
self.ref: Optional[str] = os.getenv('GITHUB_REF')
14+
self.workflow: Optional[str] = os.getenv('GITHUB_WORKFLOW')
15+
self.action: Optional[str] = os.getenv('GITHUB_ACTION')
16+
self.actor: Optional[str] = os.getenv('GITHUB_ACTOR')
17+
self.job: Optional[str] = os.getenv('GITHUB_JOB')
18+
self.run_number: Optional[str] = os.getenv('GITHUB_RUN_NUMBER')
19+
self.run_id: Optional[str] = os.getenv('GITHUB_RUN_ID')
20+
self.api_url: str = (
21+
os.getenv('GITHUB_API_URL') or 'https://api.github.com'
22+
)
23+
self.server_url: str = (
24+
os.getenv('GITHUB_SERVER_URL') or 'https://github.com'
25+
)
26+
self.graphql_url: str = (
27+
os.getenv('GITHUB_GRAPHQL_URL') or 'https://api.github.com/graphql'
28+
)
29+
self.process_payload()
30+
31+
def process_payload(self) -> None:
32+
if 'GITHUB_EVENT_PATH' in os.environ:
33+
with open(os.environ['GITHUB_EVENT_PATH'], 'r') as f:
34+
content = f.read()
35+
self._payload = json.loads(content)
36+
37+
@property
38+
def payload(self) -> dict:
39+
return self._payload
40+
41+
@property
42+
def event_name(self) -> Optional[str]:
43+
return self._event_name
44+
45+
46+
def get_context() -> Context:
47+
return Context()

pygithubactions/core/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)