Skip to content

Commit 087d694

Browse files
committed
test fail on error next
1 parent c96ff0b commit 087d694

File tree

4 files changed

+128
-4
lines changed

4 files changed

+128
-4
lines changed

.github/workflows/test_action.yaml

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Commitlint
22

33
on:
44
push:
5-
branches: ["main"]
5+
branches: ["main", "fail_on_error"]
66
pull_request:
77

88
jobs:
@@ -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 if commitlintrun status is equal to "failure"
29+
if [ "${{ steps.commitlintrun.outputs.status }}" = "failure" ]; then
30+
echo "Failing the job manually because Commitlint status is failure."
31+
exit 1 # This line manually fails the job
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** | String | 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

+83-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
This script contains actions to be taken based on GitHub events,
33
specifically for push and pull_request events.
44
"""
5+
6+
import os
57
import subprocess
68
import sys
9+
from typing import Optional, Union
710

811
from event import GithubEvent
912

13+
# Events
1014
EVENT_PUSH = "push"
1115
EVENT_PULL_REQUEST = "pull_request"
1216

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

1425
def _handle_pr_event(event: GithubEvent) -> None:
1526
"""
@@ -55,6 +66,67 @@ def _handle_push_event(event: GithubEvent) -> None:
5566
raise EnvironmentError("Unable to retrieve From hash and To hash") from None
5667

5768

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

81162

82163
def main() -> None:

0 commit comments

Comments
 (0)