From bf19a6a48352ccfdfb3c9d7ec97b05f9cd7f2802 Mon Sep 17 00:00:00 2001 From: Aaron Blankstein Date: Thu, 18 Jun 2020 15:05:24 -0500 Subject: [PATCH] docs: add gh action for auto-opening PRs on docs.blockstack --- .github/actions/docsgen/Dockerfile.docsgen | 16 ++++ .github/workflows/docs-pr.yml | 97 ++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 .github/actions/docsgen/Dockerfile.docsgen create mode 100644 .github/workflows/docs-pr.yml diff --git a/.github/actions/docsgen/Dockerfile.docsgen b/.github/actions/docsgen/Dockerfile.docsgen new file mode 100644 index 000000000..e9d83a800 --- /dev/null +++ b/.github/actions/docsgen/Dockerfile.docsgen @@ -0,0 +1,16 @@ +FROM rust:stretch as build + +WORKDIR /src + +COPY . . + +RUN apt-get update && apt-get install -y git + +RUN cargo build + +RUN mkdir /out + +RUN /src/target/debug/blockstack-core docgen > /out/clarityRef.json + +FROM scratch AS export-stage +COPY --from=build /out/clarityRef.json / diff --git a/.github/workflows/docs-pr.yml b/.github/workflows/docs-pr.yml new file mode 100644 index 000000000..14ffb9bdb --- /dev/null +++ b/.github/workflows/docs-pr.yml @@ -0,0 +1,97 @@ +## +## Github workflow for auto-opening a PR on the docs.blockstack repo +## whenever the auto-generated documentation here changes. +## +## It does this using a robot account `kantai-robot` to create a +## _base_ for the PR, the robot doesn't need any permissions to anyone +## else's git repositories. +## + +name: Open Docs PR + +env: + ROBOT_OWNER: kantai-robot + ROBOT_REPO: docs.blockstack + TARGET_OWNER: kantai + TARGET_REPO: docs.blockstack + TARGET_REPOSITORY: kantai/docs.blockstack +on: + push: + branches: [master] + +jobs: + dist: + runs-on: ubuntu-latest + env: + ROBOT_BRANCH: ${{ format('auto/clarity-ref-{0}', github.sha) }} + steps: + - uses: actions/checkout@v2 + + - name: Build docs + env: + DOCKER_BUILDKIT: 1 + run: rm -rf docs-output && docker build -o docs-output -f ./.github/actions/docsgen/Dockerfile.docsgen . + + - name: Checkout latest docs + uses: actions/checkout@v2 + with: + token: ${{ secrets.DOCS_GITHUB_TOKEN }} + repository: ${{ env.TARGET_REPOSITORY }} + path: docs.blockstack + + - name: Branch and commit + id: push + run: | + cd docs.blockstack + git config user.email "robots@robots.actions" + git config user.name "PR Robot" + git fetch --unshallow + git checkout -b $ROBOT_BRANCH + cp ../docs-output/clarityRef.json ./_data/clarityRef.json + if $(git diff --quiet --exit-code); then + echo "No clarityRef.json changes, stopping" + echo "::set-output name=open_pr::0" + else + git remote add robot https://github.com/$ROBOT_OWNER/$ROBOT_REPO + git add _data/clarityRef.json + git commit -m "auto: update clarityRef.json from stacks-blockchain@${GITHUB_SHA}" + git push robot $ROBOT_BRANCH + echo "::set-output name=open_pr::1" + fi + - name: Open PR + if: ${{ steps.push.outputs.open_pr == '1' }} + uses: actions/github-script@v2 + with: + github-token: ${{ secrets.DOCS_GITHUB_TOKEN }} + script: | + // get env vars + const process = require("process"); + const robot_owner = process.env.ROBOT_OWNER; + const robot_branch = process.env.ROBOT_BRANCH; + const head = `${robot_owner}:${robot_branch}`; + const owner = process.env.TARGET_OWNER; + const repo = process.env.TARGET_REPO; + + console.log(`Checking PR with params: head= ${head} owner= ${owner} repo= ${repo}`); + + // check if a pull exists + const existingPulls = await github.pulls.list({ + owner, repo, state: "open" }); + const myPulls = existingPulls.data.filter( pull => pull.user.login == robot_owner ); + console.log(myPulls); + + for (myPull of myPulls) { + // close any open PRs + const pull_number = myPull.number; + console.log(`Closing PR: ${ pull_number }`); + await github.pulls.update({ owner, repo, pull_number, state: "closed" }); + } + + // Open PR if one doesn't exist + console.log("Opening the new PR."); + let result = await github.pulls.create({ + owner, repo, head, + base: "master", + title: "Auto: Update API documentation from stacks-blockchain", + body: "Update API documentation from the latest in `stacks-blockchain`", + });