|
| 1 | +name: Label closed PR as merged and leave a comment |
| 2 | +on: |
| 3 | + push |
| 4 | + |
| 5 | +permissions: |
| 6 | + contents: read |
| 7 | + pull-requests: write |
| 8 | + |
| 9 | +jobs: |
| 10 | + comment-and-label: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - uses: actions/github-script@v6 |
| 14 | + with: |
| 15 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 16 | + script: | |
| 17 | + if(!context.payload.commits || !context.payload.commits.length) return; |
| 18 | + const sha = context.payload.commits[0].id; |
| 19 | +
|
| 20 | + const {commit, author} = (await github.rest.repos.getCommit({ |
| 21 | + ref: sha, |
| 22 | + owner: context.repo.owner, |
| 23 | + repo: context.repo.repo, |
| 24 | + })).data; |
| 25 | +
|
| 26 | + // Looking at the commit message, checks which PR number, if any, was closed by this commit |
| 27 | + const getClosedPrIfExists = (commit) => { |
| 28 | + if(!commit || !commit.message) return; |
| 29 | + const prClosingRegex = /Closes https:\/\/github.com\/facebook\/react-native\/pull\/([0-9]+)|Pull Request resolved: https:\/\/github.com\/facebook\/react-native\/pull\/([0-9]+)/; |
| 30 | + const prClosingMatch = commit.message.match(prClosingRegex); |
| 31 | + if(!prClosingMatch || (!prClosingMatch[1] && ! prClosingMatch[2])) return; |
| 32 | + return prClosingMatch[1] ?? prClosingMatch[2]; |
| 33 | + }; |
| 34 | +
|
| 35 | + const closedPrNumber = getClosedPrIfExists(commit); |
| 36 | + if(!closedPrNumber) return; |
| 37 | +
|
| 38 | + const pr = (await github.rest.pulls.get({ |
| 39 | + pull_number: closedPrNumber, |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + })).data; |
| 43 | +
|
| 44 | + // If the PR has already been processed (labeled as Merged), skip it |
| 45 | + const mergedLabel = "Merged"; |
| 46 | + if(pr.labels && pr.labels.some(label => label.name === mergedLabel)) return; |
| 47 | +
|
| 48 | + const authorName = author?.login ? `@${author.login}` : commit.author.name; |
| 49 | +
|
| 50 | + github.rest.issues.createComment({ |
| 51 | + issue_number: closedPrNumber, |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + body: `This pull request was successfully merged by ${authorName} in **${sha}**.\n\n<sup>[When will my fix make it into a release?](https://github.com/facebook/react-native/wiki/Release-FAQ#when-will-my-fix-make-it-into-a-release) | [Upcoming Releases](https://github.com/reactwg/react-native-releases/discussions/categories/releases)</sup>` |
| 55 | + }); |
| 56 | +
|
| 57 | + github.rest.issues.addLabels({ |
| 58 | + issue_number: closedPrNumber, |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + labels: [mergedLabel] |
| 62 | + }); |
0 commit comments