Add targetBranch output indicating what is the target branch main
authorJarek Potiuk <jarek@potiuk.com>
Fri, 13 Nov 2020 09:57:32 +0000 (10:57 +0100)
committerJarek Potiuk <jarek@potiuk.com>
Fri, 13 Nov 2020 12:05:24 +0000 (13:05 +0100)
Depending on type of the "source" event target branch output
indicates either the direct target branch (for pushes)
or targetBranch for the PR (for Pull Request)

README.md
dist/index.js
src/main.ts

index 4947cd640005264489b80952f44f52fd87c6a345..8e130dc585dca89be7150b034057852b6d6f4475 100644 (file)
--- a/README.md
+++ b/README.md
@@ -50,16 +50,17 @@ be set to `${{ github.event.workflow_run.id }}` in case of the `workflow_run` ev
 
 ## Outputs
 
-| Output              | No `sourceRunId` specified                                                        | The `sourceRunId` set to `${{ github.event.workflow_run.id }}`                                       |
-|---------------------|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|
-| `sourceHeadRepo`    | Current repository. Format: `owner/repo`                                          | Repository of the run that triggered this `workflow_run`. Format: `owner/repo`                       |
-| `sourceHeadBranch`  | Current branch.                                                                   | Branch of the run that triggered this `workflow_run`. Might be forked repo, if it is a pull_request. |
-| `sourceHeadSha`     | Current commit SHA: `{{ github.sha }}`                                            | Commit sha of the run that triggered this `workflow_run`.                                            |
-| `mergeCommitSha`    | Merge commit SHA if PR-triggered event.                                           | Merge commit SHA if PR-triggered event.                                                              |
-| `targetCommitSha`   | Target commit SHA (merge if present, otherwise source).                           | Target commit SHA (merge if present, otherwise source).                                              |
-| `pullRequestNumber` | Number of the associated Pull Request (if PR triggered)                           | Number of the associated Pull Request (if PR triggered)                                              |
-| `pullRequestLabels` | Stringified JSON array of Labels of the associated Pull Request (if PR triggered) |  Stringified JSON array of Labels of the associated Pull Request (if PR triggered)                   |
-| `sourceEvent`       | Current event: ``${{ github.event }}``                                            | Event of the run that triggered this `workflow_run`                                                  |
+| Output               | No `sourceRunId` specified                                                          | The `sourceRunId` set to `${{ github.event.workflow_run.id }}`                                        |
+|----------------------|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
+| `sourceHeadRepo`     | Current repository. Format: `owner/repo`                                            | Repository of the run that triggered this `workflow_run`. Format: `owner/repo`                        |
+| `sourceHeadBranch`   | Current branch.                                                                     | Branch of the run that triggered this `workflow_run`. Might be forked repo, if it is a pull_request.  |
+| `sourceHeadSha`      | Current commit SHA: `{{ github.sha }}`                                              | Commit sha of the run that triggered this `workflow_run`.                                             |
+| `mergeCommitSha`     | Merge commit SHA if PR-triggered event.                                             | Merge commit SHA if PR-triggered event.                                                               |
+| `targetCommitSha`    | Target commit SHA (merge if present, otherwise source).                             | Target commit SHA (merge if present, otherwise source).                                               |
+| `pullRequestNumber`  | Number of the associated Pull Request (if PR triggered)                             | Number of the associated Pull Request (if PR triggered)                                               |
+| `pullRequestLabels`  | Stringified JSON array of Labels of the associated Pull Request (if PR triggered)   | Stringified JSON array of Labels of the associated Pull Request (if PR triggered)                     |
+| `targetBranch`       | Target branch of the pull request or target branch for push                         | Target branch of the pull request or target branch for push                                           |
+| `sourceEvent`        | Current event: ``${{ github.event }}``                                              | Event of the run that triggered this `workflow_run`                                                   |
 
 # Examples
 
@@ -83,6 +84,7 @@ jobs:
       targetCommitSha: ${{ steps.workflow-run-info.outputs.targetCommitSha }}
       pullRequestNumber: ${{ steps.workflow-run-info.outputs.pullRequestNumber }}
       pullRequestLabels: ${{ steps.workflow-run-info.outputs.pullRequestLabels }}
+      targetBranch: ${{ steps.source-run-info.outputs.targetBranch }}
       sourceEvent: ${{ steps.workflow-run-info.outputs.sourceEvent }}
     steps:
       - name: "Get information about the current run"
@@ -114,8 +116,8 @@ jobs:
       targetCommitSha: ${{ steps.source-run-info.outputs.targetCommitSha }}
       pullRequestNumber: ${{ steps.source-run-info.outputs.pullRequestNumber }}
       pullRequestLabels: ${{ steps.source-run-info.outputs.pullRequestLabels }}
+      targetBranch: ${{ steps.source-run-info.outputs.targetBranch }}
       sourceEvent: ${{ steps.source-run-info.outputs.sourceEvent }}
-
     steps:
       - name: "Get information about the origin 'CI' run"
         uses: potiuk/get-workflow-origin@v1_1
index 51248f65523e0a1087137cd73ebe7bac5ebbd78c..59979da85a7528d6fb3e338a3dccf786f2513861 100644 (file)
@@ -1493,7 +1493,8 @@ function findPullRequest(octokit, owner, repo, headRepo, headBranch, headSha) {
         for (const pullRequest of pullRequests) {
             core.info(`\nComparing: ${pullRequest.number} sha: ${pullRequest.head.sha} with expected: ${headSha}.\n`);
             if (pullRequest.head.sha === headSha) {
-                core.info(`\nFound PR: ${pullRequest.number}\n`);
+                core.info(`\nFound PR: ${pullRequest.number}. ` +
+                    `Url: https://api.github.com/repos/${owner}/${repo}/pulls/${pullRequest.number}\n`);
                 return pullRequest;
             }
         }
@@ -1510,6 +1511,7 @@ function getOrigin(octokit, runId, owner, repo) {
             run_id: runId
         });
         const sourceRun = reply.data;
+        core.debug(JSON.stringify(reply.data));
         core.info(`Source workflow: Head repo: ${sourceRun.head_repository.full_name}, ` +
             `Head branch: ${sourceRun.head_branch} ` +
             `Event: ${sourceRun.event}, Head sha: ${sourceRun.head_sha}, url: ${sourceRun.url}`);
@@ -1524,6 +1526,7 @@ function getOrigin(octokit, runId, owner, repo) {
             reply.data.event,
             reply.data.head_sha,
             pullRequest ? pullRequest.merge_commit_sha : '',
+            pullRequest ? pullRequest.base.ref : reply.data.head_branch,
             pullRequest
         ];
     });
@@ -1541,13 +1544,13 @@ function run() {
         const eventName = getRequiredEnv('GITHUB_EVENT_NAME');
         const sourceRunId = parseInt(core.getInput('sourceRunId')) || selfRunId;
         const [owner, repo] = repository.split('/');
-        // core.info(`\nPayload: ${JSON.stringify(github.context.payload)}\n`)
+        core.debug(`\nPayload: ${JSON.stringify(github.context.payload)}\n`);
         core.info(`\nGetting workflow id for source run id: ${sourceRunId}, owner: ${owner}, repo: ${repo}\n`);
         const sourceWorkflowId = yield getWorkflowId(octokit, sourceRunId, owner, repo);
         core.info(`Repository: ${repository}, Owner: ${owner}, Repo: ${repo}, ` +
             `Event name: ${eventName},` +
             `sourceWorkflowId: ${sourceWorkflowId}, sourceRunId: ${sourceRunId}, selfRunId: ${selfRunId}, `);
-        const [headRepo, headBranch, sourceEventName, headSha, mergeCommitSha, pullRequest] = yield getOrigin(octokit, sourceRunId, owner, repo);
+        const [headRepo, headBranch, sourceEventName, headSha, mergeCommitSha, targetBranch, pullRequest] = yield getOrigin(octokit, sourceRunId, owner, repo);
         verboseOutput('sourceHeadRepo', headRepo);
         verboseOutput('sourceHeadBranch', headBranch);
         verboseOutput('sourceHeadSha', headSha);
@@ -1557,6 +1560,7 @@ function run() {
         verboseOutput('pullRequestLabels', JSON.stringify(labelNames));
         verboseOutput('mergeCommitSha', mergeCommitSha);
         verboseOutput('targetCommitSha', pullRequest ? mergeCommitSha : headSha);
+        verboseOutput('targetBranch', targetBranch);
     });
 }
 run()
index 46b06eddb678c37ec354632f86174f7b103de2c7..96d016a3d652b82725020ba66232390018a40e9f 100644 (file)
@@ -52,7 +52,10 @@ async function findPullRequest(
       `\nComparing: ${pullRequest.number} sha: ${pullRequest.head.sha} with expected: ${headSha}.\n`
     )
     if (pullRequest.head.sha === headSha) {
-      core.info(`\nFound PR: ${pullRequest.number}\n`)
+      core.info(
+        `\nFound PR: ${pullRequest.number}. ` +
+          `Url: https://api.github.com/repos/${owner}/${repo}/pulls/${pullRequest.number}\n`
+      )
       return pullRequest
     }
   }
@@ -66,7 +69,15 @@ async function getOrigin(
   owner: string,
   repo: string
 ): Promise<
-  [string, string, string, string, string, rest.PullsListResponseItem | null]
+  [
+    string,
+    string,
+    string,
+    string,
+    string,
+    string,
+    rest.PullsListResponseItem | null
+  ]
 > {
   const reply = await octokit.actions.getWorkflowRun({
     owner,
@@ -75,6 +86,7 @@ async function getOrigin(
     run_id: runId
   })
   const sourceRun = reply.data
+  core.debug(JSON.stringify(reply.data))
   core.info(
     `Source workflow: Head repo: ${sourceRun.head_repository.full_name}, ` +
       `Head branch: ${sourceRun.head_branch} ` +
@@ -101,6 +113,7 @@ async function getOrigin(
     reply.data.event,
     reply.data.head_sha,
     pullRequest ? pullRequest.merge_commit_sha : '',
+    pullRequest ? pullRequest.base.ref : reply.data.head_branch,
     pullRequest
   ]
 }
@@ -119,7 +132,7 @@ async function run(): Promise<void> {
   const sourceRunId = parseInt(core.getInput('sourceRunId')) || selfRunId
   const [owner, repo] = repository.split('/')
 
-  // core.info(`\nPayload: ${JSON.stringify(github.context.payload)}\n`)
+  core.debug(`\nPayload: ${JSON.stringify(github.context.payload)}\n`)
 
   core.info(
     `\nGetting workflow id for source run id: ${sourceRunId}, owner: ${owner}, repo: ${repo}\n`
@@ -142,6 +155,7 @@ async function run(): Promise<void> {
     sourceEventName,
     headSha,
     mergeCommitSha,
+    targetBranch,
     pullRequest
   ] = await getOrigin(octokit, sourceRunId, owner, repo)
 
@@ -157,6 +171,7 @@ async function run(): Promise<void> {
   verboseOutput('pullRequestLabels', JSON.stringify(labelNames))
   verboseOutput('mergeCommitSha', mergeCommitSha)
   verboseOutput('targetCommitSha', pullRequest ? mergeCommitSha : headSha)
+  verboseOutput('targetBranch', targetBranch)
 }
 
 run()