Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atomic PR ID job - Python tool POC #212

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/jobs-py/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GITHUB_TOKEN='SECRET'
1 change: 1 addition & 0 deletions .github/jobs-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
15 changes: 15 additions & 0 deletions .github/jobs-py/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
pygithub = "*"
python-dotenv = "*"

[dev-packages]
black = "*"
pytest = "*"

[requires]
python_version = "3.11"
482 changes: 482 additions & 0 deletions .github/jobs-py/Pipfile.lock

Large diffs are not rendered by default.

76 changes: 76 additions & 0 deletions .github/jobs-py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Action Jobs - Python Scripts

## Requirements
- pipenv (with Python 3.11)

## Notes
If we go with something like this for the initial IDP client tool, something like
[PyOxidizer](https://gregoryszorc.com/docs/pyoxidizer/main/pyoxidizer_getting_started.html) would be a good for anyone
that doesn't require custom plugins (if this is something that we would want to support). This would allow us to ship a
binary (and lessen the CI pipeline impacts of setting up and install all dependencies every time we need a job)

## Development
### Initial Setup
```
cp .env.example .env

# edit .env with a GitHub token with "read public repos" permissions
```

### MacOS (brew)
```
pipenv install
pipenv shell

pytest
```


### (Nix - jflinn...)
```
nix-shell
pipenv install
pipenv shell

pytest
```


## Jobs
### Get PR ID

Get the PR ID number associated with a commit on the default branch or return `None`

**Params:** (string)
**Return:** (string) int | None

#### Example
```yaml
- name: Sparse Checkout of Jobs dir
uses: actions/[email protected]
with:
sparse-checkout: |
.github/jobs-py

- name: Setup Python
uses: actions/[email protected]
with:
python-version: '3.11'

- name: Install pipenv and setup project
run: |
pip install pipenv
pipenv install

- name: Assert
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
ID=$(python .github/jobs-py/src/get_pr_id.py "7d46c75af91adbfdfc70689f4d8b3405b26bda6b")
if [[ "$ID" != "196" ]]; then
exit 1
fi
exit 0
```


11 changes: 11 additions & 0 deletions .github/jobs-py/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
with import <nixpkgs> {};
let
pythonEnv = python311.withPackages(ps: [
]);
in mkShell {
packages = [
pythonEnv

pipenv
];
}
Empty file.
35 changes: 35 additions & 0 deletions .github/jobs-py/src/get_pr_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
import re
import sys

from dotenv import load_dotenv
from github import Auth
from github import Github
from github import GithubException

load_dotenv()

auth = Auth.Token(os.getenv("GITHUB_TOKEN", default=""))
g = Github(auth=auth)
repo = g.get_repo("bitwarden/gh-actions")


def get_pr_id(commit_sha: str) -> int | None:
try:
message = repo.get_commit(commit_sha).commit.message
result = re.search(r"(\(#[0-9]+\))", message)

if result:
return int(result.group(0)[2:-1])
return None

except GithubException:
return None


if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"[!] Please pass in commit hash: {sys.argv}")
exit(1)

print(get_pr_id(sys.argv[1]))
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions .github/jobs-py/tests/e2e/test_get_pr_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import subprocess


def test_get_pr_id_cmd():
inputs = [
{"commit": "7d46c75af91adbfdfc70689f4d8b3405b26bda6b", "expected": "196"},
{"commit": "f30be53c2a5b3d61928c0f41a2e25605a9901d6a", "expected": "None"},
{"commit": "f30be53c2a5b3d61928c0f41a2e25605a9901d6b", "expected": "None"},
]

for input in inputs:
result = subprocess.run(
["python", "src/get_pr_id.py", input["commit"]], stdout=subprocess.PIPE
)

assert result.stdout.decode().strip("\n") == input["expected"]
Empty file.
6 changes: 6 additions & 0 deletions .github/jobs-py/tests/unit/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))

import src
15 changes: 15 additions & 0 deletions .github/jobs-py/tests/unit/test_get_pr_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from .context import src

from src.get_pr_id import get_pr_id


def test_main_commit():
assert get_pr_id("7d46c75af91adbfdfc70689f4d8b3405b26bda6b") == 196


def test_feature_commit():
assert get_pr_id("f30be53c2a5b3d61928c0f41a2e25605a9901d6a") is None


def test_dne_commit():
assert get_pr_id("f30be53c2a5b3d61928c0f41a2e25605a9901d6b") is None
55 changes: 55 additions & 0 deletions .github/workflows/test_jobs_py.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
name: Test Jobs - Python

on:
pull_request:
branches:
- main
paths:
- .github/jobs-py/**
- .github/workflows/test_jobs_py.yml


jobs:
test_jobs_py:
name: Test Jobs - Python
runs-on: ubuntu-22.04
steps:
- name: Sparse Checkout of Jobs dir
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 #v4.1.1
with:
sparse-checkout: |
.github/jobs-py

- name: Setup Python
uses: actions/setup-python@65d7f2d534ac1bc67fcd62888c5f4f3d2cb2b236 #v4.7.1
with:
python-version: '3.11'

- name: Install Pipenv and Setup Project
working-directory: .github/jobs-py
run: |
mkdir reports
pip install pipenv
pipenv install --dev

- name: Run Unit Tests
working-directory: .github/jobs-py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: pipenv run pytest tests/unit --junit-xml=reports/unit.xml --tb=line

- name: Run E2E Tests
working-directory: .github/jobs-py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: pipenv run pytest tests/e2e --junit-xml=reports/e2e.xml --tb=line

- name: Report test results
uses: dorny/test-reporter@afe6793191b75b608954023a46831a3fe10048d4 # v1.7.0
if: always()
with:
name: Test Results
path: ".github/jobs-py/reports/*.xml"
reporter: java-junit
fail-on-error: true