-
Notifications
You must be signed in to change notification settings - Fork 13
Add atomic PR ID job - Python tool POC #212
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
Draft
joseph-flinn
wants to merge
7
commits into
main
Choose a base branch
from
atomic-pr-id-python
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
888e577
initial commit of using a python based automation
joseph-flinn 53214d3
ran black over files
joseph-flinn f7159b3
update the pipenv installs
joseph-flinn ac905c2
Set the working directory for all of the run commands
joseph-flinn 516145c
Add test reporting summary
joseph-flinn b35650e
make workflow linter job happy
joseph-flinn 00579cc
update the tb argument
joseph-flinn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GITHUB_TOKEN='SECRET' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/checkout@v4.1.1 | ||
with: | ||
sparse-checkout: | | ||
.github/jobs-py | ||
|
||
- name: Setup Python | ||
uses: actions/setup-python@v4.7.1 | ||
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 | ||
``` | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using an empty string as the default for GITHUB_TOKEN may result in silent authentication failures. Consider checking whether GITHUB_TOKEN is provided and raising an error if it's missing to avoid ambiguous failures later.
Copilot uses AI. Check for mistakes.