Skip to content

Commit 00bf73f

Browse files
committed
feat: Add fail_on_error github action parameter
1 parent c96ff0b commit 00bf73f

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

.github/workflows/test_action.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ jobs:
1313
- uses: actions/checkout@v4
1414

1515
- name: Run commitlint
16+
id: commitlintrun
1617
uses: ./ # Uses an action in the root directory
1718
# or use a released GitHub Action
1819
# uses: opensource-nepal/[email protected]
20+
with:
21+
fail_on_error: false
22+
23+
- name: Check Output
24+
run: |
25+
echo 'Status - ${{ steps.commitlintrun.outputs.status }}'
26+
echo 'Exit Code - ${{ steps.commitlintrun.outputs.exit_code }}'
27+
28+
# Check commitlintrun status
29+
if [ "${{ steps.commitlintrun.outputs.status }}" = "failure" ]; then
30+
echo "Failing the job manually because Commitlint status is failure."
31+
exit 1
32+
fi

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ jobs:
6969
7070
> **_NOTE:_** commitlint GitHub Actions will only be triggered by "push" or "pull_request" events.
7171
72+
#### GitHub Action Inputs
73+
74+
| # | Name | Type | Default | Description |
75+
| --- | ----------------- | -----------| -----------| ----------- |
76+
| 1 | **fail_on_error** | Boolean | true | Determines whether the GitHub Action should fail when encountering an error. |
77+
78+
79+
#### GitHub Action Outputs
80+
81+
| # | Name | Type | Description |
82+
| --- | -------------- | --------------| ------------ |
83+
| 1 | **exit_code** | Integer | The exit code indicating the success or failure of the GitHub Actions workflow. |
84+
| 2 | **status** |'failure' \| 'success'| The status of the GitHub Actions workflow, indicating success or failure. |
85+
7286
## Contribution
7387
7488
We appreciate feedback and contribution to this package. To get started please see our [contribution guide](./CONTRIBUTING.md).

action.yml

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
name: "Conventional Commitlint"
22
description: "A GitHub Action to check conventional commit message"
3+
inputs:
4+
fail_on_error:
5+
description: Whether to fail the workflow if commit messages don't follow conventions.
6+
default: 'true'
7+
required: false
8+
outputs:
9+
status:
10+
description: Status
11+
value: ${{ steps.commitlint.outputs.status }}
12+
exit_code:
13+
description: Exit Code
14+
value: ${{ steps.commitlint.outputs.exit_code }}
315
branding:
416
color: "red"
517
icon: "git-commit"
@@ -42,5 +54,8 @@ runs:
4254
# checking the commits (for both push and pull_request)
4355
- name: Check the commits
4456
id: commitlint
45-
run: python ${{ github.action_path }}/github_actions/run.py
57+
run: |
58+
python ${{ github.action_path }}/github_actions/run.py
4659
shell: bash
60+
env:
61+
INPUT_FAIL_ON_ERROR: ${{ inputs.fail_on_error }}

github_actions/run.py

+82-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
This script contains actions to be taken based on GitHub events,
33
specifically for push and pull_request events.
44
"""
5+
import os
56
import subprocess
67
import sys
8+
from typing import Optional, Union
79

810
from event import GithubEvent
911

12+
# Events
1013
EVENT_PUSH = "push"
1114
EVENT_PULL_REQUEST = "pull_request"
1215

16+
# Inputs
17+
INPUT_FAIL_ON_ERROR = "INPUT_FAIL_ON_ERROR"
18+
19+
# Status
20+
STATUS_SUCCESS = "success"
21+
STATUS_FAILURE = "failure"
22+
1323

1424
def _handle_pr_event(event: GithubEvent) -> None:
1525
"""
@@ -55,6 +65,67 @@ def _handle_push_event(event: GithubEvent) -> None:
5565
raise EnvironmentError("Unable to retrieve From hash and To hash") from None
5666

5767

68+
def _write_output(name: str, value: Union[str, int]) -> None:
69+
"""
70+
Writes an output to the GitHub Actions environment.
71+
72+
Args:
73+
name (str): The name of the output variable.
74+
value: The value to be assigned to the output variable.
75+
76+
Raises:
77+
OSError: If there is an issue opening or writing to the output file.
78+
"""
79+
output_file_path = os.environ.get("GITHUB_OUTPUT", "")
80+
with open(file=output_file_path, mode="a", encoding="utf-8") as output_file:
81+
output_file.write(f"{name}={value}\n")
82+
83+
84+
def _get_input(key: str) -> Optional[str]:
85+
"""
86+
Reads the github action input
87+
88+
Args:
89+
key (str): The environment variable to parse
90+
91+
Returns:
92+
str or None: The value of the input or None if it is not set
93+
"""
94+
return os.environ.get(key)
95+
96+
97+
def _parse_boolean_input(val: Optional[str]) -> bool:
98+
"""
99+
Parses the input environment key of boolean type in the YAML 1.2
100+
"core schema" specification.
101+
Support boolean input list:
102+
`true | True | TRUE | false | False | FALSE` .
103+
ref: https://yaml.org/spec/1.2/spec.html#id2804923
104+
105+
Args:
106+
key (str, optional): The name of the environment variable to parse.
107+
108+
Returns:
109+
bool: The parsed boolean value.
110+
111+
Raises:
112+
TypeError: If the environment variable's value does not meet the
113+
YAML 1.2 "core schema" specification for booleans.
114+
"""
115+
116+
if val in {"true", "True", "TRUE"}:
117+
return True
118+
if val in {"false", "False", "FALSE"}:
119+
return False
120+
raise TypeError(
121+
"""
122+
Input does not meet YAML 1.2 "Core Schema" specification.\n'
123+
Support boolean input list:
124+
`true | True | TRUE | false | False | FALSE
125+
"""
126+
)
127+
128+
58129
def _check_commits(from_hash: str, to_hash: str) -> None:
59130
"""Check commits using commitlint.
60131
@@ -75,8 +146,17 @@ def _check_commits(from_hash: str, to_hash: str) -> None:
75146
text=True,
76147
).strip()
77148
sys.stdout.write(f"{output}\n")
78-
except subprocess.CalledProcessError:
79-
sys.exit(1)
149+
150+
_write_output("status", STATUS_SUCCESS)
151+
_write_output("exit_code", 0)
152+
153+
except subprocess.CalledProcessError as error:
154+
_write_output("status", STATUS_FAILURE)
155+
_write_output("exit_code", error.returncode)
156+
val = _get_input(INPUT_FAIL_ON_ERROR)
157+
fail_on_error = _parse_boolean_input(val)
158+
if fail_on_error:
159+
sys.exit(1)
80160

81161

82162
def main() -> None:

0 commit comments

Comments
 (0)