Skip to content

Commit

Permalink
chore: Add GitHub Action (#68)
Browse files Browse the repository at this point in the history
# Pull Request

## Description

This change introduces a GitHub Action for the GitHub Stats Analyser and updates environment variable naming conventions. Key modifications include:

1. Added `action.yml` file to define the GitHub Action, specifying inputs and branding.
2. Renamed the `REPOSITORY_OWNER` environment variable to `repository_owner` for consistency with GitHub Actions naming conventions.
3. Updated references to the renamed environment variable in various files:
   - `.github/workflows/code-test.yml`
   - `.vscode/launch.json`
   - `Justfile`
   - `analyser/utils/github_interactions.py`
4. Added `.coverage` and `coverage.xml` to the list of files excluded in `.vscode/settings.json`.
5. Updated `pyproject.toml` to ignore the SIM112 rule, allowing lowercase environment variables for GitHub Actions compatibility.

These changes improve the project's integration with GitHub Actions and maintain consistent naming conventions across the codebase.

fixes #58
  • Loading branch information
JackPlowman authored Sep 29, 2024
1 parent 2710fbe commit b203441
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:

- name: Run Analyser
env:
REPOSITORY_OWNER: ${{ github.repository_owner }}
repository_owner: ${{ github.repository_owner }}
run: just run

- name: Copy generated files to github pages folder
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"cwd": "${workspaceFolder}",
"env": {
"DEBUG": "true",
"REPOSITORY_OWNER": "JackPlowman"
"repository_owner": "JackPlowman"
},
"console": "integratedTerminal",
"justMyCode": true
Expand Down
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
".pytest_cache": true,
"**/__pycache__": true,
"**/.ruff_cache": true,
"**/.pytest_cache": true
"**/.pytest_cache": true,
".coverage": true,
"coverage.xml": true
}
}
4 changes: 2 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ run:
poetry run python -m analyser

run-with-defaults:
DEBUG=true REPOSITORY_OWNER=JackPlowman poetry run python -m analyser
DEBUG=true repository_owner=JackPlowman poetry run python -m analyser

unit-test:
poetry run pytest analyser --cov=. --cov-report=xml
Expand All @@ -52,7 +52,7 @@ docker-build:

docker-run:
docker run \
--env REPOSITORY_OWNER=JackPlowman \
--env repository_owner=JackPlowman \
--volume "$(pwd)/statistics:/statistics" \
--rm jackplowman/github-stats-analyser:latest

Expand Down
18 changes: 18 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "GitHub Stats Analyser"
description: "Analyse GitHub and generate statistics for a user's repositories"
author: "Jack Plowman"
runs:
using: "docker"
image: "Dockerfile"
inputs:
github_token:
description: "The GitHub token"
required: false
default: ${{ github.token }}
repository_owner:
description: "The GitHub repository owner"
required: true

branding:
icon: "code"
color: "black"
6 changes: 3 additions & 3 deletions analyser/utils/github_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def retrieve_repositories() -> PaginatedList[Repository]:
Returns:
PaginatedList[Repository]: The list of repositories.
"""
repository_owner = getenv("REPOSITORY_OWNER", "")
repository_owner = getenv("repository_owner", "")
if repository_owner == "":
msg = "REPOSITORY_OWNER environment variable is not set."
msg = "repository_owner environment variable is not set."
raise ValueError(msg)
token = getenv("GITHUB_TOKEN", "")
token = getenv("github_token", "")
if token == "":
github = Github()
logger.debug("Using unauthenticated GitHub API")
Expand Down
8 changes: 4 additions & 4 deletions analyser/utils/tests/test_github_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_retrieve_repositories__unauthenticated(mock_getenv: MagicMock, mock_git
repositories = retrieve_repositories()
# Assert
mock_github.assert_called_once_with()
mock_getenv.assert_has_calls([call("REPOSITORY_OWNER", ""), call("GITHUB_TOKEN", "")])
mock_getenv.assert_has_calls([call("repository_owner", ""), call("github_token", "")])
assert repositories == search_return


Expand All @@ -62,7 +62,7 @@ def test_retrieve_repositories__authenticated(mock_getenv: MagicMock, mock_githu
repositories = retrieve_repositories()
# Assert
mock_github.assert_called_once_with(token)
mock_getenv.assert_has_calls([call("REPOSITORY_OWNER", ""), call("GITHUB_TOKEN", "")])
mock_getenv.assert_has_calls([call("repository_owner", ""), call("github_token", "")])
assert repositories == search_return


Expand All @@ -72,8 +72,8 @@ def test_retrieve_repositories__no_repository_owner(mock_getenv: MagicMock, mock
# Arrange
mock_getenv.return_value = ""
# Act
with pytest.raises(ValueError, match="REPOSITORY_OWNER environment variable is not set."):
with pytest.raises(ValueError, match="repository_owner environment variable is not set."):
retrieve_repositories()
# Assert
mock_github.assert_not_called()
mock_getenv.assert_called_once_with("REPOSITORY_OWNER", "")
mock_getenv.assert_called_once_with("repository_owner", "")
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ignore = [
"D104", # Ignore missing docstring in public package
"D100", # Ignore missing docstring in public module
"N999", # Ignore invalid module name
"SIM112", # Ignore Lowercase environment variables (used for GitHub actions)
]

# Allow autofix for all enabled rules (when `--fix`) is provided.
Expand Down
3 changes: 3 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ sonar.sources=.

sonar.python.version=3.12
sonar.python.coverage.reportPaths=coverage.xml

sonar.coverage.exclusions=**/tests/**
sonar.test.exclusions=**/tests/**

0 comments on commit b203441

Please sign in to comment.