Github-Actions: link pull request to issue - github-actions

I'm quite a rookie with GitHub Actions so this might be a stupid question: Is there a way to link pull requests with issues (in the UI linked PRs are shown under Development) in Github Actions using Github CLI or octokit/rest.js via actions/github-script?
enter image description here
Background: there is a workflow that creates pull requests. That works fine, only thing missing is the link between issues and corresponding pull requests. I would prefer not to use keywords nor other custom actions from the marketplace.
I've searched in the octokit/rest.js documentation https://octokit.github.io/rest.js/v19 under Issues and Pulls as well as in the GitHub cli documentation https://cli.github.com/manual/gh but couldn't find a solution.
I would like to have a solution either using GitHub Script https://github.com/marketplace/actions/github-script
or the command line.

You can use the Add an issue link Action for that.
This Action allows linking issues to Pull Requests. For example:
name: 'Issue Links'
on:
pull_request:
types: [opened]
jobs:
issue-links:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: tkt-actions/add-issue-links#v1.8.1
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}' # required
branch-prefix: 'issue-' # required
This workflow will trigger on Pull Request open and link related issues mentioned in the PR body.
Make sure to set the corresponding permission for the job:
permissions:
pull-requests: write
For more about these permissions visit the Permissions for the GITHUB_TOKEN.

Related

Replace env var in uses: block in GitHub Action

I am trying to re-use a workflow in my GitHub repo and want to do something like this:
name: deploy
on:
push:
branches:
- 'dev'
- 'main'
jobs:
run-other-workflow:
uses: MyRepo/.github/workflows/some-other-workflow.yml#${{env.GITHUB_REF_NAME}}
I can't get this to work in any way that I am trying. Is this possible? Not being able to do this leads to duplicate templates.
EDIT:
Also tried:
jobs:
run-other-workflow:
uses: MyRepo/.github/workflows/some-other-workflow.yml#${{github.ref_name}}
I figured it out. The documentation uses the following example:
uses: octo-org/example-repo/.github/workflows/reusable-workflow.yml#main
This implies that you need to reference an org and a repo. This also seems to lead to the requirement of adding #main or #v1.
However, you can also reference your repo without the org like so:
uses: ./.github/workflows/reusable-workflow.yml
If you do it like this adding an #ref is not required. This was not immediately clear from the documentation.

Name of Github action run in list

I have a Github action that sets a name and is run on pull requests:
name: Code Quality
on:
workflow_dispatch:
pull_request:
branches: [ main, develop ]
When I trigger the run manually (because workflow_dispatch is also set), the run will get the title “Code Quality” in the list of runs.
But when the action is run on a pull request, the run name in the list is set to the name of the PR. That may or may not be a good title, very much depending on the PR’s author. Is there a way to influence the title of the action in the list?
Since Sept. 2022, there might be a way to set the title of the run itself:
GitHub Actions: Dynamic names for workflow runs (Sep. 2022)
GitHub Actions customers can now dynamically name their workflow runs.
The new run-name feature will accept expressions and be displayed on the list of workflow runs.
For more information on how to use run-name, visit the documentation.
For questions, visit the GitHub Actions community.
To see what's next for Actions, visit our public roadmap.
You now have:
The name for workflow runs generated from the workflow.
GitHub displays the workflow run name in the list of workflow runs on your repository's "Actions" tab.
If you omit run-name, the run name is set to event-specific information for the workflow run.
For example, for a workflow triggered by a push or pull_request event, it is set as the commit message.
This value can include expressions and can reference the github and inputs contexts.
Example
run-name: Deploy to ${{ inputs.deploy_target }} by #${{ github.actor }}
It does not seem like there is a way to set the title of the run itself (reference).
Scheduled and manual runs will get the title of the workflow, and runs triggered by commit / PR will get the commit message or PR title.
However, notice that the commit / PR title is displayed in addition to the name of the workflow, which appears in two places:

GitHub Action not triggering on push

I can't for the life of me figure out why my GitHub Action isn't triggering on push. For context: I have a data file that is updated daily and pushed to the test branch with a timestamp commit message. I am trying to use this timestamp in a Dynamic Badge for my README. Everything works fine when the workflow is run manually (except, of course, I don't get the event data I am hoping to obtain when the action runs on the trigger.)
on:
push:
branches:
- test
paths:
- 'data/Sales.csv'
env:
BADGE_MESSAGE: ${{ github.event.commits[0].message }}
jobs:
create-badge-test:
runs-on: ubuntu-latest
steps:
- name: Create Dynamic Badge
uses: schneegans/dynamic-badges-action#v1.1.0
with:
auth: ${{ secrets.GIST_PAT }}
gistID: 0123456789 #Not actual gist ID
filename: test.json
label: Last Refresh
message: $BADGE_MESSAGE
color: orange
And yes, I've tried putting the branch name in quotes and updating the paths: to - '**.csv' and the action still does not trigger.
There must be something else wrong - all you have here in this workflows is just fine.
You can see it working here:
https://github.com/grzegorzkrukowski/stackoverflow_tests/actions/runs/1860382530
For this commit:
https://github.com/grzegorzkrukowski/stackoverflow_tests/commit/f3f8dd20fa780746441c7b6623b6a2a9929aa70d
It's exact copy of workflow from your question.
I would expect you are pushing a file with different name - keep in mind some systems will be case-sensitive - so pushing data/sales.csv won't work properly.
Another idea is that you pushing it to wrong branch or you have wrong path to the file.
You have to push to test branch and with data/Sales.csv - it only triggers workflow if both are true.
Short answers is - workflow is fine as it is - no brackets needed.
I could help more seeing the repository with this workflow and exact commits being done.
The action is not running because you also need to satisfy the paths condition as explained on GitHub docs
Note: If you use both the branches filter and the paths filter, the workflow will only run when both filters are satisfied.
If you want the action to run when you push to test you have to remove the paths condition
on:
push:
branches:
- 'test'

Determining the origin repo of pull request in workflow YAML

I am modifying a workflow file so that a job doesn't run when a pull request originates from another repo.
So, I am looking for something like this:
build_and_deploy_job:
if: github.repository == github.pullrequest.repo
github.pullrequest.repo is just something I made up, but the idea would be that it would return the (full) name of the repo where the pull request came from.
I've tried outputting the environment variables to see if they are somehow different if a PR comes from a different branch in the same repo, or from a different repo. Nothing stood out to me as being useful.
Is something like that possible?
Background: I am trying to avoid a build job failure because it can't access a repo secret during a pull_request event when the pull request comes from another repo.
Use the following to check if the PR is coming from the same repo. This only works when the workflow is triggered by a pull request.
on: [pull_request]
jobs:
build_and_deploy_job:
if: github.event.pull_request.head.repo.name == github.repository
Bonus: Use this to check if the repo is a fork.
on: [pull_request]
jobs:
build_and_deploy_job:
if: github.event.pull_request.head.repo.fork == true
Source: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-object-34

Trigger a GitHub action on pull request approval and path

I want to build a GitHub Action that triggers on Pull Request (PR) Approval, but only when the PR contains modification to a particular path.
Currently, I have the following implementation:
on:
pull_request_review:
types: [submitted]
paths: ['mypath/**']
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout#v2
- name: job name
if: github.event.review.state == 'approved'
[Reference: https://github.community/t/github-actions-manual-trigger-approvals]
However, the build job triggers on Approval, and seems to ignore the path. The build runs on any Approval regardless of what files have been modified in the PR.
Is it possible to trigger a GitHub Action only when a PR modifies a particular path and it is approved?
I know fuzzi found an alternative using another solution with labels, but here is my contribution if someone wants to resolve the question issue keeping the original premisses:
Context
The ON trigger conditions work as OR and not as AND. Therefore, in the question sample, the workflow will trigger when a pull_request_review is submitted OR when one of the updated file(s) path is the one informed.
Workaround
It's not possible to check both condition (yet?) through the on workflow level field alone.
Therefore, if you want to check both conditions, you would have to do it separately.
A solution could be to check the submitted PR in the ON trigger first, then checking the folder path in a job step.
Example
Here is an example of what the solution suggested above looks like using the paths-changes-filter action:
on:
pull_request_review:
types: [submitted]
jobs:
build:
runs-on: self-hosted #or any other runner
steps:
- uses: actions/checkout#v2
- uses: dorny/paths-filter#v2
id: changes
with:
filters: |
mypath:
- 'mypath/**'
# run only if some file in 'src' folder was changed
- if: steps.changes.outputs.mypath == 'true' && github.event.review.state == 'approved'
run: ...
I found a solution to this using GitHub Labels instead, rather than file paths.
I implemented a GitHub Action for automatically adding a Label to the PR, based on the updated file paths.
(https://github.com/actions/labeler)
I then modified my existing GH Action to check the Label value. My condition is now:
if: github.event.label.name == 'project' && github.event.review.state == 'approved'