In the realm of continuous integration and deployment, GitHub Actions offers a powerful, inline software engineering tool. One of its many strengths lies in its ability to interact with pull requests, an essential part of collaborative coding. A common need is to obtain the pull request number within the context of GitHub Actions. Let's delve into how you can achieve this.

Before we explore the methods, let's understand why fetching the pull request number is crucial. Your GitHub Actions workflows might require specific actions based on the pull request. For instance, they could create a release branch pattern or generate a changelog based on the pull request number. Knowing the pull request number also helps in maintaining a structured commit history.

The GitHub Context
The GitHub context provides the necessary information for the current workflow run. It comes with predefined context objects, including the 'pull_request' object when a workflow is triggered by a pull request event.

However, when using other triggers like 'push', the 'pull_request' context object might not be available. In such cases, you can use the 'github' API to fetch the necessary information.
Direct Access to 'pull_request' Context Object

When a workflow is triggered by a pull request event, you can access the 'pull_request' context object directly. The 'number' property of this object holds the pull request number.
Here's a simple example using the 'actions-ts' library in a TypeScript workflow file: ```typescript import { context, getOctokit } from '@actions/github'; const octokit = getOctokit(process.env.GITHUB_TOKEN); const prNumber = context.issue.number; console.log(`Pull Request Number: ${prNumber}`); ```
Using 'github' API

When the workflow is triggered by a 'push' event, the 'pull_request' context object is not available. Instead, you can use the 'github' API to fetch the required information. To do this, you'll need to install the '@octocat/rest' package and use the 'pagination' to retrieve the pull request details.
Here's a basic example: ```typescript import { Octokit } from "@octocat/rest"; const token = process.env.GITHUB_TOKEN; const octokit = new Octokit(token); async function getPullRequestNumber() { const pulls = await octokit.pulls.list({ owner: context.repo.owner, repo: context.repo.repo, head: context.ref, }); const pr = pulls.data.find(p => p.head.sha === context.sha); return pr ? pr.number : null; } getPullRequestNumber().then(prNumber => { if (prNumber) { console.log(`Pull Request Number: ${prNumber}`); } else { console.log("Pull request not found"); } }); ```
Using Environmental Variables

Once you've fetched the pull request number, you can store it in an environmental variable and use it throughout your workflow. This is particularly useful when you're running complex workflows that span multiple jobs.
Here's how you can set an environmental variable using core.ts: ```typescript import { setenv } from '@actions/core'; setenv('PULL_REQUEST_NUMBER', prNumber.toString()); ```




















Extracting Pull Request Number from the Workflow File Path
In some cases, you might have structured your workflow files to contain the pull request number in their path. For instance, you could have a workflow file named 'pr-{pr_number}.yml'. In such scenarios, you can extract the pull request number from the workflow file path using regular expressions.
Here's a simple example: ```typescript import { context } from '@actions/github'; const regex = /pr-(\d+)\./; const match = context.payload.workflow_run.path.match(regex); const prNumber = match ? parseInt(match[1]) : null; if (prNumber) { console.log(`Pull Request Number: ${prNumber}`); } else { console.log("Unable to extract pull request number from workflow file path"); } ```
Remember, always ensure you're handling the case where the pull request number might not be available or might not match the expected format. This can prevent your workflows from failing unexpectedly.
In conclusion, GitHub Actions provides several ways to fetch the pull request number, catering to different workflow triggers and structures. By understanding and utilizing these methods, you can enhance your workflows' capabilities and streamline your continuous integration and deployment processes.