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

Custom Pr_number after merge to master not working #282

Closed
starverdiyev opened this issue Sep 5, 2023 · 3 comments
Closed

Custom Pr_number after merge to master not working #282

starverdiyev opened this issue Sep 5, 2023 · 3 comments

Comments

@starverdiyev
Copy link

starverdiyev commented Sep 5, 2023

Hello!

I have got simple workflow:

on:
  workflow_dispatch:
    inputs:
      action:
        required: true
        default: "deploy"
        type: choice
        options:
        - test1
        - test

  push:
    branches:
      - master

      - name: Comment to PR with action link 
        uses: thollander/actions-comment-pull-request@v2
        with:
          message: |
             Deploy Begins! Actions URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
          pr_number: ${{ github.event.pull_request.number }} 
          GITHUB_TOKEN: ${{ secrets.COMMENT_TOKEN }}

After merge to master, I need to post commit on PR, which was merged to master. In action log I see, that it's now pr_number env:


Run thollander/actions-comment-pull-request@v2
  with:
    message: Deploy Begins! Actions URL: https://github.com/*]
  
    GITHUB_TOKEN: ***
    mode: upsert
    create_if_not_exists: true
Error: No issue/pull request in input neither in current context.

Is any ideas, why pr_number did not set?

Thank you!

@starverdiyev
Copy link
Author

It was a problem to get PR number after merge in master with push event. Found this way, it works well.

Thanks!

@RDhar
Copy link

RDhar commented Sep 6, 2023

Here's a working snippet to get the issue number in both push and pull_request events within a GitHub Actions workflow by leveraging actions/github-script:

steps:
  - uses: actions/github-script@v6
    id: get_issue_number
    with:
      script: |
        if (context.issue.number) {
          // Return issue number if present
          return context.issue.number;
        } else {
          // Otherwise return issue number from commit
          return (
            await github.rest.repos.listPullRequestsAssociatedWithCommit({
              commit_sha: context.sha,
              owner: context.repo.owner,
              repo: context.repo.repo,
            })
          ).data[0].number;
        }
      result-encoding: string
  - name: Issue number
    run: echo '${{steps.get_issue_number.outputs.result}}'

The script queries the list labels for an issue REST API endpoint via octokit/rest.js client.

@nelsonprsousa
Copy link

If anyone is wondering how to get a complete, yet working, yml file, here it is:

name: 'Your awesome new GA'

on:
  push:
    branches:
      - main
jobs:
  pr-number:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
    steps:
      - uses: actions/github-script@v7
        id: get_pr_data
        with:
          script: |
              return (
                await github.rest.repos.listPullRequestsAssociatedWithCommit({
                  commit_sha: context.sha,
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                })
              ).data[0];
      - name: Pull Request data
        run: |
          echo '${{ fromJson(steps.get_pr_data.outputs.result).number }}'
          echo '${{ fromJson(steps.get_pr_data.outputs.result).title }}'

Please notice that both permissions (contents: read and pull-requests: read) are mandatory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants