Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: outputs the error messages #194

Merged
merged 11 commits into from
Sep 26, 2022
Prev Previous commit
Next Next commit
Shorten docs
  • Loading branch information
amannn committed Jul 25, 2022
commit cb5e1aeaea5c2c433ef5fe17ae40e104ee900aac
24 changes: 8 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,12 @@ There are two events that can be used as triggers for this action, each with dif

## Outputs
juninholiveira marked this conversation as resolved.
Show resolved Hide resolved

- `error_message`: The error message created by this action case the validation fails
In case the validation fails, this action will populate the `error_message` ouput.
juninholiveira marked this conversation as resolved.
Show resolved Hide resolved

This actions outputs the error message raised in the validation, so you can use it in other steps or jobs.
[An output can be used in other steps](https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs), for example to comment the error message onto the pull request.

- First, assign an ID to the action-semantic-pull-request step.
- On the next step, add an "if: always()" to force it to run. This is necessary because otherwise the whole workflow would just stop when action-semantic-pull-request throws the error.
- Get the output by using an expression pointing to the action-semantic-pull-request ID.
- Do what you want with it.

In the example below, we use the [sticky-pull-request-comment](https://github.com/marketplace/actions/sticky-pull-request-comment) action to create a comment in the PR with the error message outputed by this action.
<details>
<summary>Example</summary>

```yml
name: "Lint PR"
Expand All @@ -142,23 +138,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: amannn/action-semantic-pull-request@v4
# Assign an ID to the step.
id: lint_pr_title
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Comment on PR
# Since this action throws an error that naturaly stops the workflow execution,
# add an "if: always()" to force it to run.
# In this case, to comment on the PR.
- uses: marocchino/sticky-pull-request-comment@v2
# When the previous steps fails, the workflow would stop. By adding this
# condition you can continue the execution with the populated error message.
if: always()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juninholiveira Have you tested what happens when the validation no longer fails with this step? I guess ideally the comment would be removed. It seems like the action would provide an option for a workflow like this, maybe we can set an argument conditionally based on if there's an error_message?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amannn proper setup is:

      # Comments the error message from the above lint_pr_title action 
      - if: always()
        name: Comment on PR
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          header: pr-title-lint-error
          message: |
                   Hey there 👋🏼 thanks for opening the PR but we need you to adjust the title of the pull request. 
                   We require all PRs to follow [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/). More details 👇🏼
                   
                   ```
                   ${{ steps.lint_pr_title.outputs.error_message}}
                   ```
       # deletes the error comment if the title is correct            
      - if: ${{ steps.lint_pr_title.outputs.error_message == null }}
        name: delete the comment
        uses: marocchino/sticky-pull-request-comment@v2
        with:   
          header: pr-title-lint-error
          delete: true

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derberg Thanks a lot for sharing your configuration! That looks really good to me, I've just updated this PR accordingly and will merge once CI is green.

Thank you @juninholiveira for taking the lead with this feature! 👏

uses: marocchino/sticky-pull-request-comment@v2
with:
# Get the output using an expression, pointing to the ID you assigned.
message: ${{ steps.lint_pr_title.outputs.error_message }}
```

You can read more about outputs in the [GitHub Documentation](https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs).
</details>

## Legacy configuration

Expand Down