|
| 1 | +name: Topic PR Commenter |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'topics/**' |
| 7 | + |
| 8 | +jobs: |
| 9 | + comment: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Comment on PR with topic info |
| 14 | + uses: actions/github-script@v6 |
| 15 | + env: |
| 16 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 17 | + with: |
| 18 | + script: | |
| 19 | + // Get the PR number from the event payload |
| 20 | + const prNumber = context.payload.pull_request.number; |
| 21 | + |
| 22 | + // List the files changed in the PR |
| 23 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 24 | + owner: context.repo.owner, |
| 25 | + repo: context.repo.repo, |
| 26 | + pull_number: prNumber, |
| 27 | + }); |
| 28 | + |
| 29 | + // Extract topics from any file changed in the "topics/" folder. |
| 30 | + // Assumes the file name (e.g. "python.md") indicates the topic "python" |
| 31 | + const topics = []; |
| 32 | + for (const file of files) { |
| 33 | + if (file.filename.startsWith('topics/')) { |
| 34 | + const parts = file.filename.split('/'); |
| 35 | + const fileName = parts[parts.length - 1]; |
| 36 | + const topic = fileName.split('.')[0]; // Remove any file extension |
| 37 | + topics.push(topic); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (topics.length === 0) { |
| 42 | + console.log('No topics found in changed files.'); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // Remove duplicate topic names (in case multiple files reference the same topic) |
| 47 | + const uniqueTopics = [...new Set(topics)]; |
| 48 | + |
| 49 | + // Prepare the body of the comment |
| 50 | + let commentBody = '## Topic Information\n\n'; |
| 51 | + |
| 52 | + for (const topic of uniqueTopics) { |
| 53 | + // Query the GitHub Search API for repositories with the topic. |
| 54 | + // Note: The Search API endpoint returns a JSON with a total_count field. |
| 55 | + const searchResponse = await github.request('GET /search/repositories', { |
| 56 | + q: `topic:${topic}` |
| 57 | + }); |
| 58 | + const repoCount = searchResponse.data.total_count; |
| 59 | + |
| 60 | + // Append topic details to the comment body |
| 61 | + commentBody += `### ${topic}\n`; |
| 62 | + commentBody += `- [Topic Page](https://github.com/topics/${topic})\n`; |
| 63 | + commentBody += `- Repositories: ${repoCount}\n\n`; |
| 64 | + } |
| 65 | + |
| 66 | + // Post the comment on the PR |
| 67 | + await github.rest.issues.createComment({ |
| 68 | + owner: context.repo.owner, |
| 69 | + repo: context.repo.repo, |
| 70 | + issue_number: prNumber, |
| 71 | + body: commentBody |
| 72 | + }); |
0 commit comments