Nightly API Update #77
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: Nightly API Update | |
| on: | |
| schedule: | |
| - cron: "0 2 * * *" | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| issues: write | |
| jobs: | |
| update-api: | |
| name: Update API and Open PR | |
| runs-on: ubuntu-latest | |
| env: | |
| BRANCH_PREFIX: automation/nightly-api-update | |
| NIGHTLY_LABEL: nightly-api-update | |
| NIGHTLY_AUTHOR: github-actions[bot] | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Generate API client | |
| run: make api | |
| - name: Check for meaningful generated changes | |
| id: changes | |
| run: | | |
| if [ -z "$(git status --short)" ]; then | |
| echo "has_changes=false" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| meaningful_changes=$(git status --porcelain=v1 --untracked-files=all | cut -c4- | grep -Ev '^(v2/api/kiota-lock\.json|v2/openapi/spec\.json)$' || true) | |
| if [ -n "$meaningful_changes" ]; then | |
| echo "has_changes=true" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| echo "Only spec or kiota lock changes detected after make api" | |
| echo "has_changes=false" >> "$GITHUB_OUTPUT" | |
| - name: Create update branch name | |
| id: branch | |
| run: | | |
| echo "name=${BRANCH_PREFIX}/$(date -u +%Y%m%d)" >> "$GITHUB_OUTPUT" | |
| - name: Close older nightly PRs | |
| id: close_older_nightly_prs | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| env: | |
| CURRENT_BRANCH_NAME: ${{ steps.branch.outputs.name }} | |
| with: | |
| script: | | |
| const branchPrefix = process.env.BRANCH_PREFIX; | |
| const nightlyLabel = process.env.NIGHTLY_LABEL; | |
| const nightlyAuthor = process.env.NIGHTLY_AUTHOR; | |
| const currentBranchName = process.env.CURRENT_BRANCH_NAME; | |
| const { owner, repo } = context.repo; | |
| const prs = await github.paginate(github.rest.pulls.list, { | |
| owner, | |
| repo, | |
| state: 'open', | |
| sort: 'created', | |
| direction: 'desc', | |
| per_page: 100, | |
| }); | |
| const nightlyPrs = prs.filter((pr) => | |
| pr.head.ref.startsWith(branchPrefix) && | |
| pr.head.repo.full_name === `${owner}/${repo}` && | |
| pr.user.login === nightlyAuthor && | |
| pr.labels.some((label) => label.name === nightlyLabel) | |
| ); | |
| if (nightlyPrs.length === 0) { | |
| return; | |
| } | |
| const prsToClose = nightlyPrs.filter((pr) => pr.head.ref !== currentBranchName); | |
| for (const pr of prsToClose) { | |
| await github.rest.pulls.update({ | |
| owner, | |
| repo, | |
| pull_number: pr.number, | |
| state: 'closed', | |
| }); | |
| } | |
| - name: Create or update pull request | |
| if: steps.changes.outputs.has_changes == 'true' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| BRANCH_NAME: ${{ steps.branch.outputs.name }} | |
| run: | | |
| git config user.name "$NIGHTLY_AUTHOR" | |
| git config user.email "$NIGHTLY_AUTHOR@users.noreply.github.com" | |
| git checkout -B "$BRANCH_NAME" | |
| git add -A | |
| git commit -m "Update generated API client" | |
| git push --force-with-lease --set-upstream origin "$BRANCH_NAME" | |
| if ! gh label create "$NIGHTLY_LABEL" --color 1D76DB --description "Automated nightly API update PRs" 2>/dev/null; then | |
| echo "Label $NIGHTLY_LABEL already exists" | |
| fi | |
| if ! gh label create no-changelog-needed --color BFDADC --description "Changes that do not require a changelog entry" 2>/dev/null; then | |
| echo "Label no-changelog-needed already exists" | |
| fi | |
| if [ "$(gh pr list --head "$BRANCH_NAME" --state open --json number --jq 'length')" -gt 0 ]; then | |
| gh pr edit "$BRANCH_NAME" \ | |
| --title "Nightly API update $(date -u +%Y-%m-%d)" \ | |
| --body "Automated nightly update generated by \`make api\`." \ | |
| --add-label "$NIGHTLY_LABEL" \ | |
| --add-label no-changelog-needed | |
| else | |
| gh pr create \ | |
| --base main \ | |
| --head "$BRANCH_NAME" \ | |
| --title "Nightly API update $(date -u +%Y-%m-%d)" \ | |
| --body "Automated nightly update generated by \`make api\`." \ | |
| --label "$NIGHTLY_LABEL" \ | |
| --label no-changelog-needed | |
| fi |