|
| 1 | +name: Route docs feedback internally |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [labeled] |
| 6 | + # Use the trusted base workflow for fork PRs without checking out contributor code. |
| 7 | + pull_request_target: |
| 8 | + types: [labeled] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: docs-feedback-internal-${{ github.repository }}-${{ github.event.issue.number || github.event.pull_request.number }}-${{ github.event.label.name }} |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + route-feedback: |
| 19 | + if: github.repository == 'github/docs' && github.event.label.name == 'docs-feedback-internal' |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + issues: write |
| 24 | + pull-requests: write |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Record feedback in the private tracker |
| 28 | + id: record |
| 29 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 30 | + with: |
| 31 | + # GITHUB_TOKEN cannot write to another repository. |
| 32 | + github-token: ${{ secrets.DOCS_BOT_PAT_BASE }} |
| 33 | + script: | |
| 34 | + const sourceParams = { ...context.repo, issue_number: context.issue.number } |
| 35 | + const { data: source } = await github.rest.issues.get(sourceParams) |
| 36 | + if (!source.labels.some(label => label.name === 'docs-feedback-internal')) { |
| 37 | + core.info('The routing label was removed, so no feedback will be recorded.') |
| 38 | + return |
| 39 | + } |
| 40 | +
|
| 41 | + const target = { owner: 'github', repo: 'technical-content', issue_number: 8227 } |
| 42 | + const { data: tracker } = await github.rest.issues.get(target) |
| 43 | + if (tracker.state !== 'open' || tracker.pull_request) { |
| 44 | + throw new Error('The feedback tracker must be an open issue. The source will not be closed.') |
| 45 | + } |
| 46 | +
|
| 47 | + const marker = `<!-- docs-feedback-internal:${context.repo.owner}/${context.repo.repo}#${source.number} -->` |
| 48 | + let recorded = false |
| 49 | + for await (const { data: comments } of github.paginate.iterator( |
| 50 | + github.rest.issues.listComments, |
| 51 | + { ...target, per_page: 100 }, |
| 52 | + )) { |
| 53 | + if (comments.some(comment => comment.body?.startsWith(`${marker}\n`))) { |
| 54 | + recorded = true |
| 55 | + break |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + if (!recorded) { |
| 60 | + const description = source.body || '_No description provided._' |
| 61 | + // Leave room for metadata within GitHub's comment size limit. |
| 62 | + const excerpt = description.length > 60000 |
| 63 | + ? `${description.slice(0, 60000)}\n\n_Description truncated. See the source for the full text._` |
| 64 | + : description |
| 65 | + const kind = context.payload.pull_request ? 'Pull request' : 'Issue' |
| 66 | + await github.rest.issues.createComment({ |
| 67 | + ...target, |
| 68 | + body: [ |
| 69 | + marker, |
| 70 | + `## ${source.title}`, |
| 71 | + '', |
| 72 | + `${kind}: ${source.html_url}`, |
| 73 | + `Author: @${source.user.login}`, |
| 74 | + `Routed by: @${context.actor}`, |
| 75 | + '', |
| 76 | + excerpt, |
| 77 | + ].join('\n'), |
| 78 | + }) |
| 79 | + } |
| 80 | +
|
| 81 | + core.setOutput('recorded', 'true') |
| 82 | +
|
| 83 | + - name: Explain the handoff and close the source |
| 84 | + if: steps.record.outputs.recorded == 'true' |
| 85 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 86 | + with: |
| 87 | + script: | |
| 88 | + const sourceParams = { ...context.repo, issue_number: context.issue.number } |
| 89 | + const { data: source } = await github.rest.issues.get(sourceParams) |
| 90 | + if (!source.labels.some(label => label.name === 'docs-feedback-internal')) { |
| 91 | + core.info('The routing label was removed, so the source will not be closed.') |
| 92 | + return |
| 93 | + } |
| 94 | +
|
| 95 | + // Do not expose the private tracker URL in this public comment. |
| 96 | + const marker = '<!-- docs-feedback-internal:handoff -->' |
| 97 | + let explained = false |
| 98 | + for await (const { data: comments } of github.paginate.iterator( |
| 99 | + github.rest.issues.listComments, |
| 100 | + { ...sourceParams, per_page: 100 }, |
| 101 | + )) { |
| 102 | + if (comments.some(comment => |
| 103 | + comment.user?.login === 'github-actions[bot]' && |
| 104 | + comment.body?.startsWith(`${marker}\n`) |
| 105 | + )) { |
| 106 | + explained = true |
| 107 | + break |
| 108 | + } |
| 109 | + } |
| 110 | +
|
| 111 | + if (!explained) { |
| 112 | + await github.rest.issues.createComment({ |
| 113 | + ...sourceParams, |
| 114 | + body: `${marker}\nThank you for the feedback! We've recorded it for internal review by the documentation team, so we're closing this here. This does not mean the underlying feedback has been resolved.`, |
| 115 | + }) |
| 116 | + } |
| 117 | +
|
| 118 | + if (source.state === 'open') { |
| 119 | + if (context.payload.pull_request) { |
| 120 | + await github.rest.pulls.update({ |
| 121 | + ...context.repo, |
| 122 | + pull_number: source.number, |
| 123 | + state: 'closed', |
| 124 | + }) |
| 125 | + } else { |
| 126 | + await github.rest.issues.update({ |
| 127 | + ...sourceParams, |
| 128 | + state: 'closed', |
| 129 | + state_reason: 'not_planned', |
| 130 | + }) |
| 131 | + } |
| 132 | + } |
0 commit comments