|
1 | | -name: 'Example composite GitHub action' |
2 | | -description: 'Example composite GitHub action' |
| 1 | +name: "PR Comment on Release" |
| 2 | +description: "Comments on a (merged) PR when it is included in a GitHub release." |
3 | 3 | author: hello@cloudposse.com |
4 | 4 | branding: |
5 | | - icon: 'file' |
6 | | - color: 'white' |
| 5 | + icon: "file" |
| 6 | + color: "white" |
7 | 7 | inputs: |
8 | | - param1: |
9 | | - required: true |
10 | | - description: "Input parameter placeholder" |
11 | | - default: "true" |
12 | | -outputs: |
13 | | - result1: |
14 | | - description: "Output result placeholder" |
15 | | - value: "${{ steps.context.outputs.action-result }}" |
| 8 | + retries: |
| 9 | + required: false |
| 10 | + description: "Number of retries" |
| 11 | + default: "3" |
16 | 12 | runs: |
17 | 13 | using: "composite" |
18 | 14 | steps: |
19 | | - - id: context |
20 | | - shell: bash |
21 | | - run: | |
22 | | - echo "action-result=${{ inputs.param1 }}" >> $GITHUB_OUTPUT |
| 15 | + - uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + result-encoding: string |
| 18 | + retries: 3 |
| 19 | + script: | |
| 20 | + // Function to check if a value is unique in an array |
| 21 | + function onlyUnique(value, index, array) { |
| 22 | + return array.indexOf(value) === index; |
| 23 | + } |
23 | 24 |
|
| 25 | + // Function to create or update a comment for a pull request (PR) associated with a release |
| 26 | + async function createCommentForPR(pr_id, release) { |
| 27 | + // Parameters for fetching comments related to the PR |
| 28 | + const parameters = { |
| 29 | + owner: context.repo.owner, |
| 30 | + repo: context.repo.repo, |
| 31 | + issue_number: pr_id, |
| 32 | + per_page: 100, |
| 33 | + } |
| 34 | + |
| 35 | + // Constructing the message to be posted or updated as a comment |
| 36 | + const messageId = `<!-- release-pr-comment:${release.id} -->`; |
| 37 | + const message = ` |
| 38 | + ${messageId} |
| 39 | + These changes were released in [${release.name}](${release.html_url}). |
| 40 | + `; |
| 41 | + |
| 42 | + // Сreate a new comment |
| 43 | + await github.rest.issues.createComment({ |
| 44 | + owner: context.repo.owner, |
| 45 | + repo: context.repo.repo, |
| 46 | + issue_number: pr_id, |
| 47 | + body: message |
| 48 | + }); |
| 49 | + } |
| 50 | +
|
| 51 | + // Retrieving the ID of the current release |
| 52 | + release_id = context.payload.release.id; |
| 53 | +
|
| 54 | + // Fetching details of the current release |
| 55 | + currentReleaseResponse = await github.rest.repos.getRelease({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + release_id, |
| 59 | + }); |
| 60 | +
|
| 61 | + currentRelease = currentReleaseResponse.data; |
| 62 | +
|
| 63 | + // Extracting tag name and target branch from the current release |
| 64 | + currentTag = currentRelease.tag_name; |
| 65 | + currentBranch = currentRelease.target_commitish; |
| 66 | +
|
| 67 | + // Listing all releases of the repository |
| 68 | + releases = await github.rest.repos.listReleases({ |
| 69 | + owner: context.repo.owner, |
| 70 | + repo: context.repo.repo, |
| 71 | + }); |
| 72 | +
|
| 73 | + // Initializing variables for storing information about the previous release |
| 74 | + previousRelease = null; |
| 75 | + currentReleaseFound = false; |
| 76 | +
|
| 77 | + // Iterating through releases to find the previous release relative to the current one |
| 78 | + for (release of releases.data) { |
| 79 | + if (currentReleaseFound) { |
| 80 | + previousRelease = release; |
| 81 | + break; |
| 82 | + } else if (release.tag_name == currentTag) { |
| 83 | + currentReleaseFound = true; |
| 84 | + } |
| 85 | + } |
| 86 | +
|
| 87 | + // If no previous release is found, log a message and return |
| 88 | + if (previousRelease == null) { |
| 89 | + console.log(`No previous release found for ${currentTag}`); |
| 90 | + return; |
| 91 | + } |
| 92 | +
|
| 93 | + // Comparing commits between the current and previous releases |
| 94 | + commitsResponse = await github.rest.repos.compareCommits({ |
| 95 | + owner: context.repo.owner, |
| 96 | + repo: context.repo.repo, |
| 97 | + base: previousRelease.tag_name, |
| 98 | + head: currentRelease.tag_name, |
| 99 | + }); |
| 100 | +
|
| 101 | + commits = commitsResponse.data; |
| 102 | +
|
| 103 | + // Initializing an array to store pull request numbers associated with the commits |
| 104 | + pull_requests = []; |
| 105 | +
|
| 106 | + // Iterating through commits to find associated pull requests and extracting their numbers |
| 107 | + for (commit of commits.commits) { |
| 108 | + responseCommit = await github.rest.git.getCommit({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + commit_sha: commit.sha, |
| 112 | + }); |
| 113 | +
|
| 114 | + // GraphQL query to fetch details about the commit, including associated pull requests |
| 115 | + const query = ` |
| 116 | + { |
| 117 | + resource(url: "${context.payload.repository.html_url}/commit/${commit.sha}") { |
| 118 | + ... on Commit { |
| 119 | + messageHeadlineHTML |
| 120 | + messageBodyHTML |
| 121 | + associatedPullRequests(first: 10) { |
| 122 | + pageInfo { hasNextPage } |
| 123 | + edges { node { number } } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + `; |
| 129 | +
|
| 130 | + response = await github.graphql(query); |
| 131 | +
|
| 132 | + // Extracting pull request numbers from the GraphQL response |
| 133 | + for (edge of response.resource.associatedPullRequests.edges) { |
| 134 | + pull_requests.push(edge.node.number); |
| 135 | + } |
| 136 | + } |
| 137 | +
|
| 138 | + // Iterating through unique pull request numbers and creating or updating comments for them |
| 139 | + for (id of pull_requests.filter(onlyUnique)) { |
| 140 | + await createCommentForPR(id, currentRelease); |
| 141 | + } |
0 commit comments