Skip to content

Commit 7f72e91

Browse files
authored
feat: enhanced actions logging with clear annotations (#61)
- 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.
1 parent 99434be commit 7f72e91

File tree

10 files changed

+499
-387
lines changed

10 files changed

+499
-387
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,11 @@ jobs:
9898
9999
#### GitHub Action Inputs
100100
101-
| # | Name | Type | Default | Description |
102-
| --- | ----------------- | ------- | ------- | --------------------------------------------------------------------- |
103-
| 1 | **fail_on_error** | Boolean | true | Determines whether the GitHub Action should fail if commitlint fails. |
104-
| 2 | **verbose** | Boolean | false | Verbose output. |
101+
| # | Name | Type | Default | Description |
102+
| --- | ----------------- | ------- | ---------------------- | --------------------------------------------------------------------- |
103+
| 1 | **fail_on_error** | Boolean | `true` | Determines whether the GitHub Action should fail if commitlint fails. |
104+
| 2 | **verbose** | Boolean | `false` | Verbose output. |
105+
| 3 | **token** | String | `secrets.GITHUB_TOKEN` | Github Token for fetching commits using Github API. |
105106

106107
#### GitHub Action Outputs
107108

action.yml

+13-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name: 'Conventional Commitlint'
22
description: 'A GitHub Action to check conventional commit message'
3+
34
inputs:
45
fail_on_error:
56
description: Whether to fail the workflow if commit messages don't follow conventions.
@@ -9,16 +10,23 @@ inputs:
910
description: Verbose output.
1011
default: 'false'
1112
required: false
13+
token:
14+
description: Token for fetching commits using Github API.
15+
default: ${{ github.token }}
16+
required: false
17+
1218
outputs:
1319
status:
1420
description: Status
1521
value: ${{ steps.commitlint.outputs.status }}
1622
exit_code:
1723
description: Exit Code
1824
value: ${{ steps.commitlint.outputs.exit_code }}
25+
1926
branding:
2027
color: 'red'
2128
icon: 'git-commit'
29+
2230
runs:
2331
using: 'composite'
2432
steps:
@@ -27,41 +35,14 @@ runs:
2735
with:
2836
python-version: '3.8'
2937

30-
- name: Install Commitlint
31-
run: python -m pip install --disable-pip-version-check -e ${{ github.action_path }}
32-
shell: bash
33-
34-
# checkout to the source code
35-
# for push event
36-
- name: Get pushed commit count
37-
if: github.event_name == 'push'
38-
id: push_commit_count
39-
run: |
40-
echo "count=$(echo '${{ toJson(github.event.commits) }}' | jq '. | length')" \
41-
>> $GITHUB_OUTPUT
42-
shell: bash
43-
44-
- name: Checkout to pushed commits
45-
if: github.event_name == 'push'
46-
uses: actions/[email protected]
47-
with:
48-
ref: ${{ github.sha }}
49-
fetch-depth: ${{ steps.push_commit_count.outputs.count }}
50-
51-
# for pull_request event
52-
- name: Checkout to PR source branch
53-
if: github.event_name == 'pull_request'
54-
uses: actions/[email protected]
55-
with:
56-
ref: ${{ github.event.pull_request.head.sha }}
57-
fetch-depth: ${{ github.event.pull_request.commits }}
58-
59-
# checking the commits (for both push and pull_request)
60-
- name: Check the commits
38+
- name: Commitlint Action
6139
id: commitlint
6240
run: |
63-
python ${{ github.action_path }}/github_actions/run.py
41+
python -m pip install --quiet --disable-pip-version-check -e ${GITHUB_ACTION_PATH}
42+
python ${{ github.action_path }}/github_actions
6443
shell: bash
6544
env:
45+
# NOTE: Remove once https://github.com/actions/runner/issues/665 is fixed.
46+
INPUT_TOKEN: ${{ inputs.token }}
6647
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}
6748
INPUT_VERBOSE: ${{ inputs.verbose }}

github_actions/__main__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Main entry point for the GitHub Actions workflow."""
2+
3+
from action.run import run_action
4+
5+
run_action()

github_actions/action/__init__.py

Whitespace-only changes.

github_actions/event.py github_actions/action/event.py

+26-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This module defines the `GithubEvent` class for handling GitHub event details.
2+
This module defines the `GitHubEvent` class for handling GitHub event details.
33
44
Note:
55
This module relies on the presence of specific environment variables
@@ -12,7 +12,7 @@
1212

1313

1414
# pylint: disable=R0902; Too many instance attributes
15-
class GithubEvent:
15+
class GitHubEvent:
1616
"""Class representing GitHub events.
1717
1818
This class provides methods for loading and accessing various details of
@@ -24,6 +24,7 @@ class GithubEvent:
2424
ref (str): The Git reference (branch or tag) for the event.
2525
workflow (str): The name of the GitHub workflow.
2626
action (str): The action that triggered the event.
27+
repository (str): The GitHub repository name.
2728
actor (str): The GitHub username of the user or app that triggered the event.
2829
job (str): The name of the job associated with the event.
2930
run_attempt (str): The current attempt number for the job run.
@@ -34,20 +35,19 @@ class GithubEvent:
3435
payload (dict): The GitHub event payload.
3536
3637
Raises:
37-
EnvironmentError: If the required environment variable 'GITHUB_EVENT_PATH'
38-
is not found.
38+
EnvironmentError: If GitHub env are not set properly.
3939
4040
Example:
4141
```python
42-
github_event = GithubEvent()
42+
github_event = GitHubEvent()
4343
print(github_event.event_name)
4444
print(github_event.sha)
4545
print(github_event.payload)
4646
```
4747
"""
4848

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

5353
def __load_details(self) -> None:
@@ -58,30 +58,31 @@ def __load_details(self) -> None:
5858
environment variables set by GitHub Actions and loading the event payload
5959
from a file.
6060
"""
61-
self.event_name = os.environ.get("GITHUB_EVENT_NAME")
62-
self.sha = os.environ.get("GITHUB_SHA")
63-
self.ref = os.environ.get("GITHUB_REF")
64-
self.workflow = os.environ.get("GITHUB_WORKFLOW")
65-
self.action = os.environ.get("GITHUB_ACTION")
66-
self.actor = os.environ.get("GITHUB_ACTOR")
67-
self.job = os.environ.get("GITHUB_JOB")
68-
self.run_attempt = os.environ.get("GITHUB_RUN_ATTEMPT")
69-
self.run_number = os.environ.get("GITHUB_RUN_NUMBER")
70-
self.run_id = os.environ.get("GITHUB_RUN_ID")
71-
72-
if "GITHUB_EVENT_PATH" not in os.environ:
73-
raise EnvironmentError("GITHUB_EVENT_PATH not found on the environment.")
74-
75-
self.event_path = os.environ["GITHUB_EVENT_PATH"]
76-
with open(self.event_path, encoding="utf-8") as file:
77-
self.payload = json.load(file)
61+
try:
62+
self.event_name = os.environ["GITHUB_EVENT_NAME"]
63+
self.sha = os.environ["GITHUB_SHA"]
64+
self.ref = os.environ["GITHUB_REF"]
65+
self.workflow = os.environ["GITHUB_WORKFLOW"]
66+
self.action = os.environ["GITHUB_ACTION"]
67+
self.actor = os.environ["GITHUB_ACTOR"]
68+
self.repository = os.environ["GITHUB_REPOSITORY"]
69+
self.job = os.environ["GITHUB_JOB"]
70+
self.run_attempt = os.environ["GITHUB_RUN_ATTEMPT"]
71+
self.run_number = os.environ["GITHUB_RUN_NUMBER"]
72+
self.run_id = os.environ["GITHUB_RUN_ID"]
73+
74+
self.event_path = os.environ["GITHUB_EVENT_PATH"]
75+
with open(self.event_path, encoding="utf-8") as file:
76+
self.payload: Dict[str, Any] = json.load(file)
77+
except KeyError as ex:
78+
raise EnvironmentError("GitHub env not found.") from ex
7879

7980
def to_dict(self) -> Dict[str, Any]:
8081
"""
81-
Convert the GithubEvent instance to a dictionary.
82+
Convert the GitHubEvent instance to a dictionary.
8283
8384
Returns:
84-
dict: A dictionary containing the attributes of the GithubEvent instance.
85+
dict: A dictionary containing the attributes of the GitHubEvent instance.
8586
"""
8687
return {
8788
attr: getattr(self, attr)

0 commit comments

Comments
 (0)