https://docs.github.com/rest #6666
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Create a PR to add an entry to the CHANGELOG.md file in this repo | |
| # Surfaces docs changelog details publicly from authorized github-org comments. | |
| on: | |
| issue_comment: | |
| types: [created] | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| env: | |
| CHANGELOG_FILE: CHANGELOG.md | |
| CHANGELOG_FILE_URL: https://github.com/github/docs-internal/blob/main/CHANGELOG.md | |
| jobs: | |
| docs-changelog-pr: | |
| if: ${{ github.repository == 'github/docs-internal' && github.event.issue.pull_request }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| - name: 'Ensure ${{ env.CHANGELOG_FILE }} exists' | |
| run: | | |
| if [ ! -f ${{ env.CHANGELOG_FILE }} ]; then | |
| echo "${{ env.CHANGELOG_FILE }} is missing at the root of the repository." | |
| exit 1 | |
| fi | |
| - name: Check that the user belongs to the github org | |
| id: hubber_check | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 | |
| with: | |
| github-token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| script: | | |
| try { | |
| await github.rest.teams.getMembershipForUserInOrg({ | |
| org: 'github', | |
| team_slug: 'employees', | |
| username: context.payload.sender.login, | |
| }); | |
| core.exportVariable('CONTINUE_WORKFLOW', 'true'); | |
| } catch(err) { | |
| core.info("Workflow triggered by a comment, but the commenter is not a Hubber. Exiting."); | |
| core.exportVariable('CONTINUE_WORKFLOW', 'false'); | |
| } | |
| - name: Check if comment starts with '## Changelog summary' | |
| if: env.CONTINUE_WORKFLOW == 'true' | |
| id: check_summary | |
| env: | |
| COMMENT_BODY: ${{ github.event.comment.body }} | |
| run: | | |
| FIRST_LINE=$(printf "%s\n" "$COMMENT_BODY" | head -n1 | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') | |
| if [[ "$FIRST_LINE" != '## Changelog summary' ]]; then | |
| echo "FIRST_LINE=|$FIRST_LINE|" | |
| echo "The pull request comment is not a changelog summary. Exiting." | |
| echo "CONTINUE_WORKFLOW=false" >> $GITHUB_ENV | |
| fi | |
| - name: Create changelog text | |
| if: env.CONTINUE_WORKFLOW == 'true' | |
| id: create_text | |
| env: | |
| COMMENT_BODY: ${{ github.event.comment.body }} | |
| run: | | |
| set -euo pipefail | |
| DATE=$(date +"**%-d %B %Y**") | |
| BODY="$(printf "%s\n" "$COMMENT_BODY" | tail -n +2)" | |
| CHANGELOG_TEXT="$(printf "%s\n" "$BODY" | awk '/^:writing_hand:/{exit} {print}')" | |
| { | |
| echo "$DATE" | |
| echo -e "$CHANGELOG_TEXT\n<hr>" | |
| } > changelog_entry.txt | |
| - name: Set up git | |
| if: env.CONTINUE_WORKFLOW == 'true' | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| - name: Prepare branch | |
| if: env.CONTINUE_WORKFLOW == 'true' | |
| run: | | |
| BRANCH="changelog-update-$(date +%s)" | |
| echo "BRANCH=$BRANCH" >> $GITHUB_ENV | |
| git checkout -b "$BRANCH" | |
| # Insert after the first line because the changelog starts with its H1. | |
| awk 'NR==1{print; print ""; while ((getline line < "changelog_entry.txt") > 0) print line; next}1' CHANGELOG.md > tmp && mv tmp CHANGELOG.md | |
| git add CHANGELOG.md | |
| git commit -m "Update changelog for $(head -n1 changelog_entry.txt)" | |
| git push origin "$BRANCH" | |
| - name: Create a pull request | |
| if: env.CONTINUE_WORKFLOW == 'true' | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 | |
| id: create_pull_request | |
| with: | |
| github-token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| script: | | |
| const { data: pullRequest } = await github.rest.pulls.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: `Update docs changelog (for PR #${context.payload.issue.number})`, | |
| body: `### Automated docs changelog update\n\n**Purpose:** Update the <code>[${{ env.CHANGELOG_FILE }}](${{ env.CHANGELOG_FILE_URL }})</code> file with details of a recent docs change.\n\nThis PR is an automated update, generated by the <code>create-changelog-pr.yml</code> Actions workflow as a result of a "Changelog summary" comment being added to [PR #${context.payload.issue.number}](${context.payload.issue.html_url}).\n\n**Notes for reviewer**:\n- This change to the <code>[${{ env.CHANGELOG_FILE }}](${{ env.CHANGELOG_FILE_URL }})</code> file will be synced to the public docs site, so make sure that the content of the entry is appropriate for public consumption. If the content is wholly inappropriate for public consumption, then this PR can be closed.\n- Make sure the format of this changelog entry is consistent with the other entries in the file.\n\n<details><summary>Original PR comment posted by @${context.payload.comment.user.login}, using the <code>/changelog</code> slash command:</summary>\n\n${context.payload.comment.body}</details>`, | |
| head: process.env.BRANCH, | |
| base: 'main' | |
| }); | |
| core.setOutput('pull-request-number', pullRequest.number); | |
| core.setOutput('pull-request-url', pullRequest.html_url); | |
| - name: Add 'ready-for-doc-review' and 'skip FR board' labels to PR | |
| if: env.CONTINUE_WORKFLOW == 'true' | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 | |
| env: | |
| PULL_REQUEST_NUMBER: ${{ steps.create_pull_request.outputs.pull-request-number }} | |
| with: | |
| github-token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| script: | | |
| await github.rest.issues.addLabels({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: Number(process.env.PULL_REQUEST_NUMBER), | |
| labels: ['ready-for-doc-review','skip FR board'] | |
| }); | |
| - name: Assign PR to commenter | |
| if: env.CONTINUE_WORKFLOW == 'true' | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 | |
| env: | |
| PULL_REQUEST_NUMBER: ${{ steps.create_pull_request.outputs.pull-request-number }} | |
| with: | |
| github-token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| script: | | |
| try { | |
| const username = context.payload.comment.user.login; | |
| await github.rest.issues.addAssignees({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: Number(process.env.PULL_REQUEST_NUMBER), | |
| assignees: [username] | |
| }); | |
| } catch (err) { | |
| core.info(`Failed to assign PR to @${context.payload.comment.user.login}: ${err.message}`); | |
| } | |
| - uses: ./.github/actions/create-workflow-failure-issue | |
| id: create-failure-issue | |
| if: ${{ failure() && github.event_name != 'workflow_dispatch' }} | |
| with: | |
| token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| - uses: ./.github/actions/slack-alert | |
| if: ${{ failure() && github.event_name != 'workflow_dispatch' }} | |
| with: | |
| slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} | |
| issue_url: ${{ steps.create-failure-issue.outputs.issue_url }} |