Skip to content

Commit

Permalink
action for publishing public openapi into developer portal (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
jzyinq authored Aug 19, 2024
1 parent be7ca6e commit f203e6b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/publish_api_docs.yaml
Original file line number Diff line number Diff line change
@@ -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 "[email protected]:"
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)"
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f203e6b

Please sign in to comment.