Skip to content

Commit

Permalink
feat: enhanced actions logging with clear annotations (#61)
Browse files Browse the repository at this point in the history
- Rewrote GitHub Actions (using Github API and events).
- Added GitHub Actions job summary.
- Added GitHub Actions error annotations.
- Added `token` for input (defaults to GITHUB_TOKEN).
- Created `--hide-input` argument specifically for GitHub Actions.
  • Loading branch information
aj3sh authored Aug 7, 2024
1 parent 99434be commit 7f72e91
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 387 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ jobs:

#### GitHub Action Inputs

| # | Name | Type | Default | Description |
| --- | ----------------- | ------- | ------- | --------------------------------------------------------------------- |
| 1 | **fail_on_error** | Boolean | true | Determines whether the GitHub Action should fail if commitlint fails. |
| 2 | **verbose** | Boolean | false | Verbose output. |
| # | Name | Type | Default | Description |
| --- | ----------------- | ------- | ---------------------- | --------------------------------------------------------------------- |
| 1 | **fail_on_error** | Boolean | `true` | Determines whether the GitHub Action should fail if commitlint fails. |
| 2 | **verbose** | Boolean | `false` | Verbose output. |
| 3 | **token** | String | `secrets.GITHUB_TOKEN` | Github Token for fetching commits using Github API. |

#### GitHub Action Outputs

Expand Down
45 changes: 13 additions & 32 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: 'Conventional Commitlint'
description: 'A GitHub Action to check conventional commit message'

inputs:
fail_on_error:
description: Whether to fail the workflow if commit messages don't follow conventions.
Expand All @@ -9,16 +10,23 @@ inputs:
description: Verbose output.
default: 'false'
required: false
token:
description: Token for fetching commits using Github API.
default: ${{ github.token }}
required: false

outputs:
status:
description: Status
value: ${{ steps.commitlint.outputs.status }}
exit_code:
description: Exit Code
value: ${{ steps.commitlint.outputs.exit_code }}

branding:
color: 'red'
icon: 'git-commit'

runs:
using: 'composite'
steps:
Expand All @@ -27,41 +35,14 @@ runs:
with:
python-version: '3.8'

- name: Install Commitlint
run: python -m pip install --disable-pip-version-check -e ${{ github.action_path }}
shell: bash

# checkout to the source code
# for push event
- name: Get pushed commit count
if: github.event_name == 'push'
id: push_commit_count
run: |
echo "count=$(echo '${{ toJson(github.event.commits) }}' | jq '. | length')" \
>> $GITHUB_OUTPUT
shell: bash

- name: Checkout to pushed commits
if: github.event_name == 'push'
uses: actions/checkout@v4.1.7
with:
ref: ${{ github.sha }}
fetch-depth: ${{ steps.push_commit_count.outputs.count }}

# for pull_request event
- name: Checkout to PR source branch
if: github.event_name == 'pull_request'
uses: actions/checkout@v4.1.7
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: ${{ github.event.pull_request.commits }}

# checking the commits (for both push and pull_request)
- name: Check the commits
- name: Commitlint Action
id: commitlint
run: |
python ${{ github.action_path }}/github_actions/run.py
python -m pip install --quiet --disable-pip-version-check -e ${GITHUB_ACTION_PATH}
python ${{ github.action_path }}/github_actions
shell: bash
env:
# NOTE: Remove once https://github.com/actions/runner/issues/665 is fixed.
INPUT_TOKEN: ${{ inputs.token }}
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
INPUT_VERBOSE: ${{ inputs.verbose }}
5 changes: 5 additions & 0 deletions github_actions/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Main entry point for the GitHub Actions workflow."""

from action.run import run_action

run_action()
Empty file.
51 changes: 26 additions & 25 deletions github_actions/event.py → github_actions/action/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
This module defines the `GithubEvent` class for handling GitHub event details.
This module defines the `GitHubEvent` class for handling GitHub event details.
Note:
This module relies on the presence of specific environment variables
Expand All @@ -12,7 +12,7 @@


# pylint: disable=R0902; Too many instance attributes
class GithubEvent:
class GitHubEvent:
"""Class representing GitHub events.
This class provides methods for loading and accessing various details of
Expand All @@ -24,6 +24,7 @@ class GithubEvent:
ref (str): The Git reference (branch or tag) for the event.
workflow (str): The name of the GitHub workflow.
action (str): The action that triggered the event.
repository (str): The GitHub repository name.
actor (str): The GitHub username of the user or app that triggered the event.
job (str): The name of the job associated with the event.
run_attempt (str): The current attempt number for the job run.
Expand All @@ -34,20 +35,19 @@ class GithubEvent:
payload (dict): The GitHub event payload.
Raises:
EnvironmentError: If the required environment variable 'GITHUB_EVENT_PATH'
is not found.
EnvironmentError: If GitHub env are not set properly.
Example:
```python
github_event = GithubEvent()
github_event = GitHubEvent()
print(github_event.event_name)
print(github_event.sha)
print(github_event.payload)
```
"""

def __init__(self) -> None:
"""Initialize a new instance of the GithubEvent class."""
"""Initialize a new instance of the GitHubEvent class."""
self.__load_details()

def __load_details(self) -> None:
Expand All @@ -58,30 +58,31 @@ def __load_details(self) -> None:
environment variables set by GitHub Actions and loading the event payload
from a file.
"""
self.event_name = os.environ.get("GITHUB_EVENT_NAME")
self.sha = os.environ.get("GITHUB_SHA")
self.ref = os.environ.get("GITHUB_REF")
self.workflow = os.environ.get("GITHUB_WORKFLOW")
self.action = os.environ.get("GITHUB_ACTION")
self.actor = os.environ.get("GITHUB_ACTOR")
self.job = os.environ.get("GITHUB_JOB")
self.run_attempt = os.environ.get("GITHUB_RUN_ATTEMPT")
self.run_number = os.environ.get("GITHUB_RUN_NUMBER")
self.run_id = os.environ.get("GITHUB_RUN_ID")

if "GITHUB_EVENT_PATH" not in os.environ:
raise EnvironmentError("GITHUB_EVENT_PATH not found on the environment.")

self.event_path = os.environ["GITHUB_EVENT_PATH"]
with open(self.event_path, encoding="utf-8") as file:
self.payload = json.load(file)
try:
self.event_name = os.environ["GITHUB_EVENT_NAME"]
self.sha = os.environ["GITHUB_SHA"]
self.ref = os.environ["GITHUB_REF"]
self.workflow = os.environ["GITHUB_WORKFLOW"]
self.action = os.environ["GITHUB_ACTION"]
self.actor = os.environ["GITHUB_ACTOR"]
self.repository = os.environ["GITHUB_REPOSITORY"]
self.job = os.environ["GITHUB_JOB"]
self.run_attempt = os.environ["GITHUB_RUN_ATTEMPT"]
self.run_number = os.environ["GITHUB_RUN_NUMBER"]
self.run_id = os.environ["GITHUB_RUN_ID"]

self.event_path = os.environ["GITHUB_EVENT_PATH"]
with open(self.event_path, encoding="utf-8") as file:
self.payload: Dict[str, Any] = json.load(file)
except KeyError as ex:
raise EnvironmentError("GitHub env not found.") from ex

def to_dict(self) -> Dict[str, Any]:
"""
Convert the GithubEvent instance to a dictionary.
Convert the GitHubEvent instance to a dictionary.
Returns:
dict: A dictionary containing the attributes of the GithubEvent instance.
dict: A dictionary containing the attributes of the GitHubEvent instance.
"""
return {
attr: getattr(self, attr)
Expand Down
Loading

0 comments on commit 7f72e91

Please sign in to comment.