Unverified Commit 6e423810 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Implement a doc preview bot (#924)

* Implement a doc preview bot

* Add `aws-smithy-http-server` to the doc preview crates

* Incorporate review feedback

* Fix missing history in index generation script
parent 8136ba4a
Loading
Loading
Loading
Loading
+74 −2
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@ on:
    - synchronize
env:
  java_version: 11
  rust_version: 1.54.0

jobs:
  generate-diff:
    runs-on: ubuntu-latest
@@ -64,11 +66,81 @@ jobs:
      if: ${{ github.head_ref != null }}
      with:
        script: |
          const { DIFF_FILE_NAME } = process.env;

          await github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: '${{ steps.generate-diff.outputs.bot-message }}'
          })

  generate-doc-preview:
    runs-on: ubuntu-latest
    name: Generate rustdoc preview and upload to S3
    env:
      AWS_REGION: us-west-2
      S3_BUCKET_NAME: ${{ secrets.SMITHY_RS_PULL_REQUEST_CDN_S3_BUCKET_NAME }}
    permissions:
      id-token: write
      contents: read
      pull-requests: write
    steps:
    - uses: actions/checkout@v2
    - uses: actions/cache@v2
      name: Gradle Cache
      with:
        path: |
          ~/.gradle/caches
          ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
        restore-keys: |
          ${{ runner.os }}-gradle-
      # JDK is needed to generate code
    - name: Set up JDK
      uses: actions/setup-java@v1
      with:
        java-version: ${{ env.java_version }}
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: ${{ env.rust_version }}
        default: true
    - name: Generate doc preview
      # Only generate two of the smallest services since these get huge. One of these must be
      # STS since aws-config depends on it. STS and Transcribe Streaming were chosen below to stay
      # small while still representing most features. Combined, they are about 11 MB at time of writing.
      run: |
        ./gradlew -Paws.services=+sts,+transcribestreaming :aws:sdk:assemble

        # Copy the Server runtime crate(s) in
        cp -r rust-runtime/aws-smithy-http-server aws/sdk/build/aws-sdk/sdk

        pushd aws/sdk/build/aws-sdk

        # Remove example crates from workspace
        sed -i '/examples/d' Cargo.toml

        # Add server runtime crates to the workspace
        sed -i 's/"sdk\/sts",/"sdk\/sts","sdk\/aws-smithy-http-server",/' Cargo.toml

        cargo doc --no-deps --all-features
        popd
        ./tools/generate-doc-preview-index.sh ${{ github.event.pull_request.base.sha }}
    - uses: aws-actions/configure-aws-credentials@v1
      name: Acquire credentials for uploading to S3
      with:
        role-to-assume: ${{ secrets.SMITHY_RS_PULL_REQUEST_CDN_ROLE_ARN }}
        role-session-name: GitHubActions
        aws-region: us-west-2
    - name: Upload doc preview to S3
      run: |
        aws s3 cp target/doc "s3://${S3_BUCKET_NAME}/docs/${{ github.event.pull_request.head.sha }}" --recursive
    - uses: actions/github-script@v5
      # NOTE: if comments on each commit become bothersome, add a check that github.event.pull_request.action == "opened"
      if: ${{ github.head_ref != null }}
      with:
        script: |
          await github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: 'A [new doc preview](https://d2luzm2xt3nokh.cloudfront.net/docs/${{ github.event.pull_request.head.sha }}/index.html) is ready to view.'
          })
+1 −1
Original line number Diff line number Diff line
@@ -212,7 +212,7 @@ def run(command, shell=False):

# Returns the output from a shell command. Bails if the command failed
def get_cmd_output(command):
    result = subprocess.run(command, capture_output=True, shell=True, check=True)
    result = subprocess.run(shlex.split(command), capture_output=True, check=True)
    return result.stdout.decode("utf-8").strip()


+68 −0
Original line number Diff line number Diff line
#!/bin/bash
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
#

if [[ $# -ne 1 ]]; then
    echo "Usage: $0 <base commit hash>"
    exit 1
fi

BASE_COMMIT_SHA=$1
HEAD_COMMIT_SHA=$(git rev-parse HEAD)
DOC_TITLE_CONTEXT=$(git log -1 --oneline)
cd $(git rev-parse --show-toplevel)/target/doc

if [[ "${GITHUB_ACTIONS}" == "true" ]]; then
    echo "Fetching base revision ${BASE_COMMIT_SHA} from GitHub..."
    git fetch --no-tags --progress --no-recurse-submodules --depth=1 origin ${BASE_COMMIT_SHA}
fi

CHANGED_RUNTIME_CRATES_FILE=$(mktemp)
git diff --name-only $BASE_COMMIT_SHA -- $(git rev-parse --show-toplevel)/rust-runtime | cut -d '/' -f2 | sed 's/-/_/g' | sort | uniq > "${CHANGED_RUNTIME_CRATES_FILE}"

{
    echo '<!doctype html>'
    echo '<html>'
    echo '<head>'
    echo '  <metadata charset="utf-8">'
    echo "  <title>Doc preview: ${DOC_TITLE_CONTEXT}</title>"
    echo '</head>'
    echo '<body>'
    echo "  <h2>Doc preview: ${DOC_TITLE_CONTEXT}</h2>"
} > index.html

doc_link () {
    local crate=$1
    if [[ -d ${crate} ]]; then
        grep -q -e "^${crate}$" "${CHANGED_RUNTIME_CRATES_FILE}"
        CHANGED=$?
        if [[ $CHANGED -eq 0 ]]; then
          echo "  <li><strong><a href='${crate}/index.html'>${crate}</a></strong></li>" >> index.html
        else
          echo "  <li><a href='${crate}/index.html'>${crate}</a></li>" >> index.html
        fi
    fi
}

echo '  <h3>AWS Services</h3><ul>' >> index.html
for crate in $(ls -d aws_sdk_*); do doc_link ${crate}; done
echo '  </ul>' >> index.html

echo '  <h3>AWS Runtime Crates</h3><ul>' >> index.html
for crate in $(ls -d aws_* | grep -v '_smithy_\|_sdk_'); do doc_link ${crate}; done
echo '  </ul>' >> index.html

echo '  <h3>Smithy Runtime Crates</h3><ul>' >> index.html
for crate in $(ls -d aws_smithy_*); do doc_link ${crate}; done
echo '  </ul>' >> index.html

{
    echo "<p><strong>Bold</strong> indicates a runtime crate had changes since the base revision \
         $(git log -1 --oneline ${BASE_COMMIT_SHA}). These may or may not be documentation changes.</p>"
    echo '</body>'
    echo '</html>'
} >> index.html

rm "${CHANGED_RUNTIME_CRATES_FILE}"