diff --git a/.github/actions/artifact-size-metrics/show-results/action.yml b/.github/actions/artifact-size-metrics/show-results/action.yml new file mode 100644 index 0000000..088ba5f --- /dev/null +++ b/.github/actions/artifact-size-metrics/show-results/action.yml @@ -0,0 +1,76 @@ +name: Artifact Size Metrics - Show Results +description: Posts a new artifact analysis comment on the PR + +runs: + using: "composite" + steps: + - name: Post Artifact Analysis Comment + uses: actions/github-script@v7 + with: + script: | + const fs = require('node:fs') + const prNumber = context.issue.number ?? process.env.SDK_PR + + const prInfo = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: prNumber + }) + + const hasAcknowledgeLabel = prInfo.data.labels.some(label => label.name === 'acknowledge-artifact-size-increase') + + const getComments = ` + query { + repository(owner:"${context.repo.owner}", name:"${context.repo.repo}"){ + pullRequest(number: ${prNumber}) { + id + comments(last:100) { + nodes { + id + body + author { + login + } + isMinimized + } + } + } + }` + + const response = await github.graphql(getComments) + const comments = response.repository.pullRequest.comments.nodes + + // Minimize outdated artifact-size comments + const mutations = comments + .filter(comment => comment.author.login == 'github-actions' && !comment.isMinimized && comment.body.startsWith('Affected Artifacts')) + .map(comment => + github.graphql(`mutation { + minimizeComment(input:{subjectId:"${comment.id}", classifier:OUTDATED}){ + clientMutationId + } + }`) + ) + await Promise.all(mutations) + + const comment = fs.readFileSync('build/reports/metrics/artifact-analysis.md', 'utf8') + + // Create a new comment with the latest artifact analysis + const writeComment = `mutation { + addComment(input:{body:"""${comment}""", subjectId:"${response.repository.pullRequest.id}"}){ + commentEdge { + node { + id + } + } + }}` + const addCommentResponse = await github.graphql(writeComment) + const newCommentId = addCommentResponse.addComment.commentEdge.node.id + + // Minimize the newly-created comment if artifact size increase is acknowledged + if (hasAcknowledgeLabel) { + await github.graphql(`mutation { + minimizeComment(input:{subjectId:"${newCommentId}", classifier:RESOLVED}){ + clientMutationId + } + }`) + } \ No newline at end of file