how to set GitHub action to comment on new PR? - github

I want to integrate github-action-comment-pull-request from the official GitHub marketplace.
This will be my yaml:
on: [pull_request]
jobs:
build:
name: Comment a pull_request
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Comment a pull_request
uses: mb2dev/github-action-comment-pull-request#1.0.0
with:
message: "Hello, Thank you for this PR!"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
My problem is that I want the message to be shown for all new PRs except PRs raised by specific users: userA, userB.
Preferably also to exclude users for a specific GitHub team.
Is this possible?

If you want this workflow to run the job for everyone except for specific users, an option could be to add an if condition to your job to run only if the github.actor from the Github context isn't among a list of users you set.
Example with your workflow:
on: [pull_request]
jobs:
build:
name: Comment a pull_request
runs-on: ubuntu-latest
if: ${{ github.actor != 'Slava' }}
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Comment a pull_request
uses: mb2dev/github-action-comment-pull-request#1.0.0
with:
message: "Hello, Thank you for this PR!"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
This also work for many users if you add && or || inside the condition.
For example, I use this workflow with some of my repositories to add PR comments:
name: PR Comment
on:
pull_request_target:
types:
- opened
jobs:
PR-Comment:
if: github.actor != 'user1' && github.actor != 'user2' && github.actor != 'user3' && github.actor != 'user4' && github.actor != 'user5'
runs-on: ubuntu-latest
steps:
- name: PR Comment
uses: actions/github-script#v2
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
issue_number: ${{ github.event.number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: ':warning: Have you followed the [contributions guidance](<contribution_guidance_url>)? Content PRs should generally be made against the the [source repo](https://github.com/<owner>/<repo>).'
})
A full workflow example can be found in this repo
I don't think it's possible to use or inform the Github Team directly with a condition yet.

Related

Github Actions workflow not triggered although schedule should trigger the job

I'm trying to auto-assign issues and PRs in Github from a Github Actions workflow. The respective steps work fine when an issue / a PR is opened. So this trigger is fine.
Recently I added Dependabot to my repo. Since Dependabot cannot access my secrets I cannot assign any issue in a Dependabot-triggeres pipeline. So I just thought I run this pipeline with a scheduler one per day to "clean up" every issue and PR which is unassigned. But this schedule config does not trigger the pipeline. It simply does nothing, not even show as a pipeline run which does nothing (with all jobs skipped). Seems like the trigger is completely ignored.
This is my workflow file.
---
name: "Organize: Assign Issues + Pull Requests"
on:
issues:
types:
- opened
pull_request:
types:
- opened
schedule:
- cron: '0 9 * * *' # https://crontab.guru/#0_11_*_*_*
permissions:
contents: read
issues: write
pull-requests: write
jobs:
add-to-project:
name: Add to project
runs-on: ubuntu-latest
steps:
- name: Add to project (issues and PRs)
uses: actions/add-to-project#main
with:
project-url: https://github.com/users/sebastian-sommerfeld-io/projects/1
github-token: ${{ secrets.GH_TOKEN_REPO_AND_PROJECT }}
assign-to-user:
name: Assign to user
runs-on: ubuntu-latest
steps:
- name: Assign issue to user when moved into column
uses: pozil/auto-assign-issue#v1
# https://github.com/marketplace/actions/auto-assign-issue
with:
assignees: ${{ github.actor }}
numOfAssignee: 1
allowSelfAssign: true
abortIfPreviousAssignees: true
new-pull-request-chat-message:
runs-on: ubuntu-latest
needs: ['add-to-project', 'assign-to-user']
if: github.event_name == 'pull_request'
steps:
- name: Send message to Google Chat
uses: Co-qn/google-chat-notification#releases/v1
with:
name: New Pull Request "${{ github.event.pull_request.title }}" (raised by ${{ github.actor }})
url: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
status: ${{ job.status }}
on-failure:
runs-on: ubuntu-latest
needs: ['add-to-project', 'assign-to-user', 'new-pull-request-chat-message']
if: failure()
steps:
- name: Send Pipeline Status to Google Chat
if: always()
uses: Co-qn/google-chat-notification#releases/v1
with:
name: ${{ github.workflow }}
url: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
status: failure
What bugs me is that the scheduler setting is copied from another workflow where it works just fine. So I cannot think of a reason why this pipeline is not triggered at 09:00 in the morning.
Found a way :-)
I use the Github CLI to get all PRs with a certain label and assign a user. This is a new dedicated pipeline.
---
name: "Organize: Dependabot Pull Requests"
on:
schedule:
- cron: '30 * * * *' # https://crontab.guru
permissions:
contents: read
issues: write
pull-requests: write
jobs:
assign-user:
name: Aassign PRs with label 'dependencies'
runs-on: ubuntu-latest
steps:
- name: Get PR and assign user (filtered by github cli)
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_REPO_AND_PROJECT }}
label: dependencies
assignee: sebastian-sommerfeld-io
run: |
OUTPUT=""
pr_ids="$(gh pr list --repo "$GITHUB_REPOSITORY" --label "$label" --json number --jq '.[].number')"
for id in $pr_ids; do
gh pr edit "$id" --repo "$GITHUB_REPOSITORY" --add-assignee "$assignee"
done
Then I updated the triggers of my original pipeline to
on:
issues:
types:
- opened
pull_request:
types:
- opened
- assigned

How to associate status of a Github action workflow with a commit or PR

I have a Github action workflow which is set to trigger on issue_comment, for example the following
name: Testing actions
on:
issue_comment:
types: [created, edited]
jobs:
testing-actions:
if: github.event.issue.pull_request && contains(github.event.comment.body, '#test')
name: Just testing
runs-on: ubuntu-latest
steps:
- uses: xt0rted/pull-request-comment-branch#v1
id: comment-branch
- uses: actions/checkout#v3
with:
ref: ${{ steps.comment-branch.outputs.head_ref }}
- name: Print stuff
run: echo "Hello dudes!"
The workflow is working, it is getting triggered when I comment "#test" on a PR. But I have to check it from the actions tab, the workflow doesn't get associated with any commit or PR, so it doesn't get shown in the checks tab in the PR, or the commits don't show any commit status.
How do I get the workflow status and details in the PR page, so that I don't have to go to actions tab manually to see the results?
I solved this by manually setting the commit status using #myrotvorets/set-commit-status-action. The workflow file ended up being like the following
name: Testing actions
on:
issue_comment:
types: [created, edited]
jobs:
testing-actions:
if: github.event.issue.pull_request && contains(github.event.comment.body, '#test')
name: Just testing
runs-on: ubuntu-latest
steps:
- name: Get PR details
uses: xt0rted/pull-request-comment-branch#v1
id: comment-branch
- name: Set commit status as pending
uses: myrotvorets/set-commit-status-action#master
with:
sha: ${{ steps.comment-branch.outputs.head_sha }}
token: ${{ secrets.GITHUB_TOKEN }}
status: pending
- uses: actions/checkout#v3
with:
ref: ${{ steps.comment-branch.outputs.head_ref }}
- name: Print stuff
run: echo "Hello dudes!"
- name: Set final commit status
uses: myrotvorets/set-commit-status-action#master
if: always()
with:
sha: ${{ steps.comment-branch.outputs.head_sha }}
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
This works pretty well, the workflow runs get associated with the PR's head commit, and thus it shows up on the PR page as a check too. Thanks #rethab for the alpha.

github workflow only on deployment_status AND specific branch

Similar tho this question How to run Github Actions with the 'deployment_status' kitty and only on the QAS branch?
I want to execute a workflow only on a specific branch but combined with the deployment_status trigger.
So I want to execute my end to end test only if the deployment is done and we are on the develop branch.
The problem here is: On the trigger deployment_status the github.ref variable is not set. It is also written in docs. So I can not do a manual check if I am on the right branch.
name: E2E
on:
deployment_status:
# does not work because it is ignored on deployment_status
branches:
- develop
- stage
- master
- main
jobs:
chrome:
if: github.event.deployment_status.state == 'success'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Run Cypress
uses: cypress-io/github-action#v2
with:
browser: chrome
cache-key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
env:
CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
On the other way: If i use the deployment_status trigger I can not use branches or branches-ignore in combination. This is not working at all.
name: E2E
on:
deployment_status:
jobs:
chrome:
# does not work because github.ref is not defined
if: github.event.deployment_status.state == 'success' && github.ref == 'refs/heads/develop' /
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Run Cypress
uses: cypress-io/github-action#v2
with:
browser: chrome
cache-key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
env:
CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
Any ideas how to solve this?

How to create a Github action for labeling issues created by specific users?

I'd like to create a Github action that automatically labels issues of my Patreon supporters.
I created a file with the supporters' Github usernames:
./github/supporters.json
{
usernames: [
'test1',
'test2'
]
}
(I can change the file to any other format, if that would make it easier)
How do I check if the issue was created by a user on the list?
I've been thinking about using this marketplace action:
https://github.com/marketplace/actions/super-labeler
But it seems like it only accepts a Regex pattern. Should I just list usernames in the Regex directly?
I figured out a solution, using this action https://github.com/actions-ecosystem/action-add-labels:
I'm specifying usernames in the github action .yaml file directly:
[
"some-username-1",
"some-username-2"
]
./github/workflows/label-issue.yaml
name: Label supporter
on:
issues:
types: [opened, edited]
jobs:
add_label_tier_1:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions-ecosystem/action-add-labels#v1.1.0
if:
${{
contains(
fromJson('[
"some-username-1",
"some-username-2"
]'),
github.actor
)
}}
with:
github_token: ${{ github.token }}
labels: |
supporter 💖
priority +1
add_label_tier_2:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions-ecosystem/action-add-labels#v1.1.0
if:
${{
contains(
fromJson('[
"some-username-3"
]'),
github.actor
)
}}
with:
github_token: ${{ github.token }}
labels: |
supporter 💖
priority +2

GitHub action for issue_comment doesn't shown in checks for PR

I created a GitHub action on:issue_comment, I can see the flow running only in the action tab, but not in the PR, where I made the comment.
I want to comment in a PR and trigger a check on that PR (not on master)
here is my workflow:
name: issue-comment-CI-test
on:
issue_comment:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: echo ${{ github.event.comment.body }}
currently, I'm just printing the comment body, But I plan to check the body, and if it is equal to "run integration tests" then I'll run my integration tests (maven)
Basically you need to checkout to the PR origin. For that, first make a API request to the pr url and fetch all ref.
Then do the checkout on the fetched repo and branch.
Step1
- name: Github API Request
id: request
uses: octokit/request-action#v2.0.0
with:
route: ${{ github.event.issue.pull_request.url }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Step 2
- name: Checkout PR Branch
uses: actions/checkout#v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ fromJson(steps.request.outputs.data).head.repo.full_name }}
ref: ${{ steps.pr_data.outputs.branch }}
You can follow the following example, specially the GitHub API Request part.
I've also implemented it in one of our workflows, you can take reference from that as well.
https://github.com/adrianjost/workflow-trigger-comment-example/blob/master/.github/workflows/demo.yml
https://github.com/TeamAmaze/AmazeFileManager/blob/master/.github/workflows/android-debug-artifact-ondemand.yml
You need to checkout the Pull Request. You can get the PR ID by using {{ GITHUB_REF }}.
You can checkout the PR with:
git fetch origin pull/{{ GITHUB_REF }}/head:PR
git checkout PR
See https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onevent_nametypes, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables
and https://developer.github.com/v3/pulls/ for reference.
Used built-in gh command to fetch the branch instead of using actions/checkout.
name: issue-comment-CI-test
on:
issue_comment:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- run: |
gh pr checkout ${{ github.event.issue.number }}
# do your job...
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required for gh