From f203e6bc5e97a44479bc705f04bd25eac1b67641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Zywert?= Date: Mon, 19 Aug 2024 13:47:24 +0200 Subject: [PATCH] action for publishing public openapi into developer portal (#113) --- .github/workflows/publish_api_docs.yaml | 111 ++++++++++++++++++++++++ README.md | 35 ++++++++ 2 files changed, 146 insertions(+) create mode 100644 .github/workflows/publish_api_docs.yaml diff --git a/.github/workflows/publish_api_docs.yaml b/.github/workflows/publish_api_docs.yaml new file mode 100644 index 0000000..540790f --- /dev/null +++ b/.github/workflows/publish_api_docs.yaml @@ -0,0 +1,111 @@ +name: Publish public OpenAPI documentation +on: + workflow_call: + inputs: + repository_tag: + default: 'Repository tag to process' + required: true + type: string + +jobs: + update_api: + runs-on: ubuntu-latest + steps: + - name: Checkout source repo + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + ref: ${{ inputs.repository_tag }} + path: source-repo + + - name: Prepare repository name + id: repo-name + run: | + REPO_NAME=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]' | tr '/' '-') + echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV + + - name: Generate access token for DevelopersPortal repo + uses: PiwikPRO/actions/github-app-token-generator@master + id: get-token + with: + private-key: ${{ secrets.DEVELOPER_PORTAL_AGGREGATOR_PRIVATE_KEY }} + app-id: ${{ secrets.DEVELOPER_PORTAL_AGGREGATOR_APPLICATION_ID }} + + - name: Check for existing Pull Request + id: check-pr + env: + GITHUB_TOKEN: ${{ steps.get-token.outputs.token }} + run: | + matching_pr=$(gh pr list -R PiwikPRO/DevelopersPortal \ + --base master \ + --json url,headRefName \ + --jq ".[] | select(.headRefName | startswith(\"api/$REPO_NAME\")) | {url: .url}") + + if [ -n "$matching_pr" ]; then + pr_url=$(echo $matching_pr | jq -r .url) + echo "::error::There is already an open pull request for this repository: $pr_url" + echo "::error::Close it or merge before running this workflow again" + exit 1 + else + echo "No matching PR found. Proceeding with creation." + fi + + - name: Checkout DevelopersPortal repo + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + repository: 'PiwikPRO/DevelopersPortal' + path: developers-portal + token: ${{ steps.get-token.outputs.token }} + + - name: Use https instead of ssh for publish script + run: | + git config --global url."https://github.com/".insteadOf "git@github.com:" + git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/" + + - name: Run publish_api.sh script + working-directory: ./developers-portal + env: + GITHUB_TOKEN: ${{ steps.get-token.outputs.token }} + run: | + ./publish_api.sh ../source-repo + + - name: Check for changes + working-directory: ./developers-portal + id: git-check + run: | + if [[ -n $(git status --porcelain) ]]; then + echo "changes=true" >> $GITHUB_OUTPUT + echo "Changes detected" + else + echo "No changes detected" + fi + + - name: Create new branch and push changes + if: steps.git-check.outputs.changes == 'true' + working-directory: ./developers-portal + env: + GITHUB_TOKEN: ${{ steps.get-token.outputs.token }} + run: | + BRANCH_NAME="api/$REPO_NAME-${{ inputs.repository_tag }}-$(date +%s)" + git config user.name "${{ github.actor }}" + git config user.email "${{ github.actor }}@users.noreply.github.com" + git checkout -b $BRANCH_NAME + git add . + git commit -m "API update from ${{ github.repository }} (${{ inputs.repository_tag }})" + git push origin $BRANCH_NAME + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV + + - name: Create Pull Request + if: steps.git-check.outputs.changes == 'true' + env: + GITHUB_TOKEN: ${{ steps.get-token.outputs.token }} + run: | + gh pr create --title "API Update: ${{ github.event.repository.name }}:${{ inputs.repository_tag }}" \ + --body "This PR contains public OpenAPI spec from \ + [${{ github.repository }}:${{ inputs.repository_tag }}](https://github.com/${{ github.repository }}/releases/tag/${{ inputs.repository_tag }}). + Triggered by: @${{ github.actor }}" \ + --head ${{ env.BRANCH_NAME }} \ + --base master \ + --repo PiwikPRO/DevelopersPortal \ + --assignee ${{ github.actor }} \ + --label "openapi-update" | tee pr_url.txt + echo "::notice::Pull request created: $(cat pr_url.txt)" diff --git a/README.md b/README.md index 3718c25..aa23030 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ - [Actions](#actions) - [Dependabot](#dependabot) - [Update changelog](#update-changelog) + - [Developers Portal](#developers-portal) - [Changelog](#changelog) - [Using aws-cli with proxy](#using-aws-cli-with-proxy) - [Dtools](#dtools) @@ -64,6 +65,40 @@ jobs: Info: You should copy not only step, but also another parts above (run only on labeled pull requests with label `dependencies`) to work it correctly. +### Developers Portal + +This action adds a manually triggered workflow to publish part of OpenAPI stored in the repository. + +Add it as `publish_api_docs.yaml` to your repository `.github/workflows` directory: + +```yaml +name: Publish public OpenAPI documentation + +on: + workflow_dispatch: + inputs: + repository_tag: + description: 'Repository tag to process' + required: true + type: string + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + push-public-api-docs: + uses: PiwikPRO/actions/.github/workflows/publish_api_docs.yaml@master + secrets: inherit + with: + repository_tag: ${{ github.event.inputs.repository_tag }} +``` + +Then, you can trigger it manually from the Actions tab in your repository. As input you need to provide the tag +of the repository you want to process. + +For more details about its implementation head to [PiwikPRO/DevelopersPortal](https://github.com/PiwikPRO/DevelopersPortal#api-documentation) + ### Changelog Keep a changelog validator: