|
| 1 | +name: Get PR commit SHA |
| 2 | +on: |
| 3 | + workflow_call: |
| 4 | + inputs: |
| 5 | + pr_number: |
| 6 | + required: true |
| 7 | + type: string |
| 8 | + outputs: |
| 9 | + PR_HEAD_REPO_FULL_NAME: |
| 10 | + description: "The full name of the repository from which the pull request is created" |
| 11 | + value: ${{ jobs.get-pr-info.outputs.PR_HEAD_REPO_FULL_NAME }} |
| 12 | + PR_BASE_REPO_FULL_NAME: |
| 13 | + description: "The full name of the repository to which the pull request is created" |
| 14 | + value: ${{ jobs.get-pr-info.outputs.PR_BASE_REPO_FULL_NAME }} |
| 15 | + PR_HEAD_REPO_OWNER: |
| 16 | + description: "The owner of the repository from which the pull request is created" |
| 17 | + value: ${{ jobs.get-pr-info.outputs.PR_HEAD_REPO_OWNER }} |
| 18 | + PR_BASE_REPO_OWNER: |
| 19 | + description: "The owner of the repository to which the pull request is created" |
| 20 | + value: ${{ jobs.get-pr-info.outputs.PR_BASE_REPO_OWNER }} |
| 21 | + PR_HEAD_REPO_NAME: |
| 22 | + description: "The name of the repository from which the pull request is created" |
| 23 | + value: ${{ jobs.get-pr-info.outputs.PR_HEAD_REPO_NAME }} |
| 24 | + PR_BASE_REPO_NAME: |
| 25 | + description: "The name of the repository to which the pull request is created" |
| 26 | + value: ${{ jobs.get-pr-info.outputs.PR_BASE_REPO_NAME }} |
| 27 | + PR_HEAD_REF: |
| 28 | + description: "The branch name of the pull request in the head repository" |
| 29 | + value: ${{ jobs.get-pr-info.outputs.PR_HEAD_REF }} |
| 30 | + PR_BASE_REF: |
| 31 | + description: "The branch name in the base repository (to merge into)" |
| 32 | + value: ${{ jobs.get-pr-info.outputs.PR_BASE_REF }} |
| 33 | + PR_HEAD_SHA: |
| 34 | + description: "The head sha of the pull request branch in the head repository" |
| 35 | + value: ${{ jobs.get-pr-info.outputs.PR_HEAD_SHA }} |
| 36 | + PR_BASE_SHA: |
| 37 | + description: "The head sha of the target branch in the base repository" |
| 38 | + value: ${{ jobs.get-pr-info.outputs.PR_BASE_SHA }} |
| 39 | + PR_MERGE_COMMIT_SHA: |
| 40 | + description: "The sha of the merge commit for the pull request (created by GitHub) in the base repository" |
| 41 | + value: ${{ jobs.get-pr-info.outputs.PR_MERGE_COMMIT_SHA }} |
| 42 | + PR_HEAD_COMMIT_DATE: |
| 43 | + description: "The date of the head sha of the pull request branch in the head repository" |
| 44 | + value: ${{ jobs.get-pr-info.outputs.PR_HEAD_COMMIT_DATE }} |
| 45 | + PR_MERGE_COMMIT_DATE: |
| 46 | + description: "The date of the merge commit for the pull request (created by GitHub) in the base repository" |
| 47 | + value: ${{ jobs.get-pr-info.outputs.PR_MERGE_COMMIT_DATE }} |
| 48 | + PR_HEAD_COMMIT_TIMESTAMP: |
| 49 | + description: "The timestamp of the head sha of the pull request branch in the head repository" |
| 50 | + value: ${{ jobs.get-pr-info.outputs.PR_HEAD_COMMIT_TIMESTAMP }} |
| 51 | + PR_MERGE_COMMIT_TIMESTAMP: |
| 52 | + description: "The timestamp of the merge commit for the pull request (created by GitHub) in the base repository" |
| 53 | + value: ${{ jobs.get-pr-info.outputs.PR_MERGE_COMMIT_TIMESTAMP }} |
| 54 | + PR: |
| 55 | + description: "The PR" |
| 56 | + value: ${{ jobs.get-pr-info.outputs.PR }} |
| 57 | + PR_FILES: |
| 58 | + description: "The files touched in the PR" |
| 59 | + value: ${{ jobs.get-pr-info.outputs.PR_FILES }} |
| 60 | + |
| 61 | + |
| 62 | +jobs: |
| 63 | + get-pr-info: |
| 64 | + runs-on: ubuntu-22.04 |
| 65 | + name: Get PR commit SHA better |
| 66 | + outputs: |
| 67 | + PR_HEAD_REPO_FULL_NAME: ${{ steps.pr_info.outputs.head_repo_full_name }} |
| 68 | + PR_BASE_REPO_FULL_NAME: ${{ steps.pr_info.outputs.base_repo_full_name }} |
| 69 | + PR_HEAD_REPO_OWNER: ${{ steps.pr_info.outputs.head_repo_owner }} |
| 70 | + PR_BASE_REPO_OWNER: ${{ steps.pr_info.outputs.base_repo_owner }} |
| 71 | + PR_HEAD_REPO_NAME: ${{ steps.pr_info.outputs.head_repo_name }} |
| 72 | + PR_BASE_REPO_NAME: ${{ steps.pr_info.outputs.base_repo_name }} |
| 73 | + PR_HEAD_REF: ${{ steps.pr_info.outputs.head_ref }} |
| 74 | + PR_BASE_REF: ${{ steps.pr_info.outputs.base_ref }} |
| 75 | + PR_HEAD_SHA: ${{ steps.pr_info.outputs.head_sha }} |
| 76 | + PR_BASE_SHA: ${{ steps.pr_info.outputs.base_sha }} |
| 77 | + PR_MERGE_COMMIT_SHA: ${{ steps.pr_info.outputs.merge_commit_sha }} |
| 78 | + PR_HEAD_COMMIT_DATE: ${{ steps.pr_info.outputs.head_commit_date }} |
| 79 | + PR_MERGE_COMMIT_DATE: ${{ steps.pr_info.outputs.merge_commit_date }} |
| 80 | + PR_HEAD_COMMIT_TIMESTAMP: ${{ steps.get_timestamps.outputs.head_commit_timestamp }} |
| 81 | + PR_MERGE_COMMIT_TIMESTAMP: ${{ steps.get_timestamps.outputs.merge_commit_timestamp }} |
| 82 | + PR: ${{ steps.pr_info.outputs.pr }} |
| 83 | + PR_FILES: ${{ steps.pr_info.outputs.files }} |
| 84 | + if: ${{ inputs.pr_number != '' }} |
| 85 | + steps: |
| 86 | + - name: Extract PR details |
| 87 | + id: pr_info |
| 88 | + uses: actions/github-script@v6 |
| 89 | + with: |
| 90 | + script: | |
| 91 | + const { data: pr } = await github.rest.pulls.get({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + pull_number: ${{ inputs.pr_number }} |
| 95 | + }); |
| 96 | +
|
| 97 | + const { data: head_commit } = await github.rest.repos.getCommit({ |
| 98 | + owner: pr.head.repo.owner.login, |
| 99 | + repo: pr.head.repo.name, |
| 100 | + ref: pr.head.ref |
| 101 | + }); |
| 102 | +
|
| 103 | + const { data: merge_commit } = await github.rest.repos.getCommit({ |
| 104 | + owner: pr.base.repo.owner.login, |
| 105 | + repo: pr.base.repo.name, |
| 106 | + ref: pr.merge_commit_sha, |
| 107 | + }); |
| 108 | +
|
| 109 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 110 | + owner: context.repo.owner, |
| 111 | + repo: context.repo.repo, |
| 112 | + pull_number: ${{ inputs.pr_number }} |
| 113 | + }); |
| 114 | +
|
| 115 | + core.setOutput('head_repo_full_name', pr.head.repo.full_name); |
| 116 | + core.setOutput('base_repo_full_name', pr.base.repo.full_name); |
| 117 | + core.setOutput('head_repo_owner', pr.head.repo.owner.login); |
| 118 | + core.setOutput('base_repo_owner', pr.base.repo.owner.login); |
| 119 | + core.setOutput('head_repo_name', pr.head.repo.name); |
| 120 | + core.setOutput('base_repo_name', pr.base.repo.name); |
| 121 | + core.setOutput('head_ref', pr.head.ref); |
| 122 | + core.setOutput('base_ref', pr.base.ref); |
| 123 | + core.setOutput('head_sha', pr.head.sha); |
| 124 | + core.setOutput('base_sha', pr.base.sha); |
| 125 | + core.setOutput('merge_commit_sha', pr.merge_commit_sha); |
| 126 | + core.setOutput('pr', pr); |
| 127 | +
|
| 128 | + core.setOutput('head_commit_date', head_commit.commit.committer.date); |
| 129 | + core.setOutput('merge_commit_date', merge_commit.commit.committer.date); |
| 130 | + |
| 131 | + core.setOutput('files', files); |
| 132 | + |
| 133 | + console.log('PR head commit:', { |
| 134 | + head_commit: head_commit, |
| 135 | + commit: head_commit.commit, |
| 136 | + date: head_commit.commit.committer.date |
| 137 | + }); |
| 138 | +
|
| 139 | + console.log('PR merge commit:', { |
| 140 | + merge_commit: merge_commit, |
| 141 | + commit: merge_commit.commit, |
| 142 | + date: merge_commit.commit.committer.date |
| 143 | + }); |
| 144 | +
|
| 145 | + - name: Convert dates to timestamps |
| 146 | + id: get_timestamps |
| 147 | + run: | |
| 148 | + head_commit_date=${{ steps.pr_info.outputs.head_commit_date }} |
| 149 | + merge_commit_date=${{ steps.pr_info.outputs.merge_commit_date }} |
| 150 | + echo $head_commit_date |
| 151 | + echo $merge_commit_date |
| 152 | + head_commit_timestamp=$(date -d "$head_commit_date" +%s) |
| 153 | + merge_commit_timestamp=$(date -d "$merge_commit_date" +%s) |
| 154 | + echo $head_commit_timestamp |
| 155 | + echo $merge_commit_timestamp |
| 156 | + echo "head_commit_timestamp=$head_commit_timestamp" >> $GITHUB_OUTPUT |
| 157 | + echo "merge_commit_timestamp=$merge_commit_timestamp" >> $GITHUB_OUTPUT |
0 commit comments