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

Trigger Lint Action on PR comment ? #559

Open
PrinceGupta1999 opened this issue Dec 29, 2022 · 7 comments · May be fixed by #583
Open

Trigger Lint Action on PR comment ? #559

PrinceGupta1999 opened this issue Dec 29, 2022 · 7 comments · May be fixed by #583
Labels
enhancement New feature or request in progress

Comments

@PrinceGupta1999
Copy link

I was trying to trigger this action when a specific comment is made on the PR. However I got the following error
Error: lint-action does not support "issue_comment" GitHub events

So, I would like to know which github events are supported by this action and how could I go about solving my use case ?

@ocean90
Copy link
Member

ocean90 commented Jan 2, 2023

Only push, workflow_dispatch, pull_request, and pull_request_target events are supported, see here.

Could you explain why you need to run the action on issue_comment?

@PrinceGupta1999
Copy link
Author

PrinceGupta1999 commented Jan 3, 2023

@ocean90 thanks for the reply. I wanted this action to run on issue_comment so that the lint does not run on each push of the pull request and can just be run when a dev wants to lint his PR (for ex: before putting it up for review).

Here is the way I am currently using it

name: Lint (auto fix)
on:
  issue_comment:
    types: [created]

jobs:
  check-comment:
    if: github.event.issue.pull_request
    name: Check Comment
    runs-on: ubuntu-latest
    outputs:
      status: ${{ steps.check.outputs.triggered }}
      pr: ${{ steps.get-pr.outputs.result }}
    steps:
      - uses: Khan/[email protected]
        id: check
        with:
          trigger: '@run-lint'
          reaction: eyes
        env:
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
      - uses: actions/[email protected]
        id: get-pr
        with:
          retries: 2
          script: |
            return github.rest.pulls.get({
              pull_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
            })

  run-linters:
    needs: check-comment
    name: Run linters
    runs-on: ubuntu-latest
    if: needs.check-comment.outputs.status == 'true'
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ fromJson(needs.check-comment.outputs.pr).data.head.ref }}

      - name: Set up Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 14.17.6

      - name: Install Node.js dependencies
        run: npm ci

      - name: Run linters
        run: npm run format && (npm run lint:fix || true) # Ideally I would want to use lint-action for the individual status checks for prettier / eslint

      - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Lint Fixes
          branch: ${{ fromJson(needs.check-comment.outputs.pr).data.head.ref }}

The reason for not using lint with auto_fix: true on PRs / Push is that I don't want to create potential extra commits in the PR when I am in make the code work phase.

@ocean90
Copy link
Member

ocean90 commented Jan 3, 2023

I see. Please feel free to submit a PR that adds support for the issue_comment event.

@ocean90 ocean90 added the enhancement New feature or request label Jan 3, 2023
@PrinceGupta1999
Copy link
Author

PrinceGupta1999 commented Jan 5, 2023

Sure, I will take this up in this or the next week. I am assuming the only change that needs to happed is to add support for issue_comment event in this function

/**
* Parses the name of the current branch from the GitHub webhook event
* @param {string} eventName - GitHub event type
* @param {object} event - GitHub webhook event payload
* @returns {string} - Branch name
*/
function parseBranch(eventName, event) {
if (eventName === "push" || eventName === "workflow_dispatch") {
return event.ref.substring(11); // Remove "refs/heads/" from start of string
}
if (eventName === "pull_request" || eventName === "pull_request_target") {
return event.pull_request.head.ref;
}
throw Error(`${actionName} does not support "${eventName}" GitHub events`);
}
by using some equivalent of this code from my workflow file

return github.rest.pulls.get({
  pull_number: context.issue.number,
  owner: context.repo.owner,
  repo: context.repo.repo,
}) 

@PrinceGupta1999
Copy link
Author

PrinceGupta1999 commented Jan 7, 2023

Hi @ocean90 can I use the @actions/github package or is the Rest API call (to fetch the repo branch from PR) to be made manually. The package does provide a Github client and context which should make the task easier and maybe even the current context fetching function and related logic can be done using this ?

PrinceGupta1999 added a commit to PrinceGupta1999/lint-action that referenced this issue Jan 16, 2023
@PrinceGupta1999 PrinceGupta1999 linked a pull request Jan 16, 2023 that will close this issue
@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@PrinceGupta1999
Copy link
Author

Hi @ocean90 any update on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants