From dbf2e8de13157d425b456c24aa1162b727632254 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Wed, 19 Apr 2023 15:12:12 +0800 Subject: [PATCH 01/14] Sets up continuous deployment to `teco` and `teco-core` --- .github/workflows/build.yml | 6 ++ .github/workflows/generate.yml | 160 +++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 .github/workflows/generate.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 654e91c..2f0f101 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,3 +19,9 @@ jobs: uses: actions/checkout@v3 - name: Build run: swift build -c ${{ matrix.build-config }} + - name: Save build cache + uses: actions/cache/save@v3 + if: ${{ matrix.build-config == 'release' }} + with: + path: .build + key: ${{ runner.os }}-${{ github.sha }} diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml new file mode 100644 index 0000000..b8a2c6c --- /dev/null +++ b/.github/workflows/generate.yml @@ -0,0 +1,160 @@ +name: Generate +on: + pull_request: + branches: + - main + +env: + TECO_BRANCH: codegen-pr-${{ github.event.number }} + +jobs: + create-branch: + runs-on: ubuntu-latest + if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' }} + strategy: + matrix: + repo: + - teco + - teco-core + steps: + - name: Generate app token for bot + uses: tibdex/github-app-token@v1 + id: generate-token + with: + app_id: ${{ secrets.BOT_APP_ID }} + private_key: ${{ secrets.BOT_APP_PRIVATE_KEY }} + - name: Create synced branch + uses: actions/github-script@v6 + env: + TECO_REPO: ${{ matrix.repo }} + with: + script: | + const base_ref = context.payload.pull_request.base.ref; + const owner = context.repo.owner; + const repo = process.env.TECO_REPO; + const ref = `refs/head/${process.env.TECO_BRANCH}`; + const { data: branches } = await github.rest.repos.listBranches({ owner, repo }); + var sha = branches.find((branch) => branch.name === 'main').commit.sha; + if (branches.map((branch) => branch.name).includes(base_ref)) { + sha = branches.find((branch) => branch.name === base.ref).commit.sha; + } else { + const title = 'Base branch not found'; + const message = `Base branch "${base_ref}" doesn't exist in repository ${owner}/${repo}. Available branches: ${JSON.stringify(branches.map((branch) => branch.name))}`; + console.log(`::warning title=${title}::${message}`); + } + github.rest.git.createRef({ owner, repo, ref, sha }); + - name: Create branch protection rule + uses: octokit/request-action@v2.x + env: + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + with: + route: PUT /repos/{owner}/{repo}/branches/{branch}/protection + owner: ${{ github.repository_owner }} + repo: ${{ matrix.repo }} + branch: ${{ env.TECO_BRANCH }} + restrictions: | + users: + - ${{ github.event.pull_request.user.login }} + required_linear_history: true + allow_force_pushes: true + allow_deletions: true + generate-and-sync: + runs-on: ubuntu-latest + if: ${{ success() || github.event.action == 'synchronize' }} + needs: + - create-branch + steps: + - name: Generate app token for bot + uses: tibdex/github-app-token@v1 + id: generate-token + with: + app_id: ${{ secrets.BOT_APP_ID }} + private_key: ${{ secrets.BOT_APP_PRIVATE_KEY }} + - name: Import bot's GPG key for signing commits + id: import-gpg + uses: crazy-max/ghaction-import-gpg@v5 + with: + gpg_private_key: ${{ secrets.BOT_GPG_PRIVATE_KEY }} + passphrase: ${{ secrets.BOT_GPG_PASSPHRASE }} + git_config_global: true + git_user_signingkey: true + git_commit_gpgsign: true + - name: Checkout + uses: actions/checkout@v3 + - name: Wait for building + uses: lewagon/wait-on-check-action@v1.3.1 + with: + ref: ${{ github.ref }} + check-name: 'build (release)' + repo-token: ${{ secrets.GITHUB_TOKEN }} + wait-interval: 10 + - name: Restore build result + uses: actions/cache/restore@v3 + with: + path: .build + key: ${{ runner.os }}-${{ github.sha }} + - name: Checkout API models + uses: actions/checkout@v3 + with: + repository: ${{ github.repository_owner }}/teco-api-models + path: ./models + ref: current + - name: Checkout teco-core + uses: actions/checkout@v3 + with: + repository: ${{ github.repository_owner }}/teco-core + ref: ${{ env.TECO_BRANCH }} + path: ./teco-core + token: ${{ steps.generate-token.outputs.token }} + - name: Drop bot commits in teco-core + env: + BOT_COMMITTER_EMAIL: ${{ steps.import-gpg.outputs.email }} + working-directory: ./teco-core + run: | + git filter-repo --refs HEAD --commit-callback ' + import subprocess; import os + p = subprocess.run(["git", "merge-base", "--is-ancestor", commit.original_id, os.environ["GITHUB_BASE_REF"]]) + if p.returncode != 0 and commit.committer_email.decode() == os.environ["BOT_COMMITTER_EMAIL"]: + commit.skip() + ' + git rebase "$GITHUB_BASE_REF" --quiet + - name: Generate teco-core + run: | + swift run --skip-build teco-common-error-generator \ + --error-file ./models/zh-CN/error-codes.json \ + --output ./teco-core/Sources/TecoCore/Errors/TCCommonError.swift + swift run --skip-build teco-date-wrapper-generator \ + --output-dir './teco-core/Sources/TecoDateHelpers/Property Wrappers' + swift run --skip-build teco-region-generator \ + --output ./teco-core/Sources/TecoCore/Common/TCRegion.swift + - name: Checkout teco + uses: actions/checkout@v3 + with: + repository: ${{ github.repository_owner }}/teco + ref: ${{ env.TECO_BRANCH }} + path: ./teco + token: ${{ steps.generate-token.outputs.token }} + - name: Drop bot commits in teco + env: + BOT_COMMITTER_EMAIL: ${{ steps.import-gpg.outputs.email }} + working-directory: ./teco + run: | + git filter-repo --refs HEAD --commit-callback ' + import subprocess; import os + p = subprocess.run(["git", "merge-base", "--is-ancestor", commit.original_id, os.environ["GITHUB_BASE_REF"]]) + if p.returncode != 0 and commit.committer_email.decode() == os.environ["BOT_COMMITTER_EMAIL"]: + commit.skip() + ' + git rebase "$GITHUB_BASE_REF" --quiet + - name: Generate teco + run: | + swift run --skip-build teco-package-generator \ + --model-dir ./models/zh-CN \ + --service-generator .build/release/teco-service-generator \ + --package-dir ./teco + if ! git diff --exit-code Package.swift + then + swift package resolve + fi + # TODO: Add and commit to TecoCore. + # TODO: Add and commit to Teco. From e0110f5949b27fe393ce392c2818c4c93e0e10de Mon Sep 17 00:00:00 2001 From: YR Chen Date: Wed, 19 Apr 2023 22:55:07 +0800 Subject: [PATCH 02/14] Fix bugs and use `actions/github-script` --- .github/workflows/build.yml | 6 ---- .github/workflows/generate.yml | 63 +++++++++++++++++----------------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2f0f101..654e91c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,9 +19,3 @@ jobs: uses: actions/checkout@v3 - name: Build run: swift build -c ${{ matrix.build-config }} - - name: Save build cache - uses: actions/cache/save@v3 - if: ${{ matrix.build-config == 'release' }} - with: - path: .build - key: ${{ runner.os }}-${{ github.sha }} diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index b8a2c6c..2a6d746 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -28,15 +28,16 @@ jobs: env: TECO_REPO: ${{ matrix.repo }} with: + github-token: ${{ steps.generate-token.outputs.token }} script: | const base_ref = context.payload.pull_request.base.ref; const owner = context.repo.owner; const repo = process.env.TECO_REPO; - const ref = `refs/head/${process.env.TECO_BRANCH}`; + const ref = `refs/heads/${process.env.TECO_BRANCH}`; const { data: branches } = await github.rest.repos.listBranches({ owner, repo }); var sha = branches.find((branch) => branch.name === 'main').commit.sha; if (branches.map((branch) => branch.name).includes(base_ref)) { - sha = branches.find((branch) => branch.name === base.ref).commit.sha; + sha = branches.find((branch) => branch.name === base_ref).commit.sha; } else { const title = 'Base branch not found'; const message = `Base branch "${base_ref}" doesn't exist in repository ${owner}/${repo}. Available branches: ${JSON.stringify(branches.map((branch) => branch.name))}`; @@ -44,20 +45,28 @@ jobs: } github.rest.git.createRef({ owner, repo, ref, sha }); - name: Create branch protection rule - uses: octokit/request-action@v2.x + uses: actions/github-script@v6 env: - GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + TECO_REPO: ${{ matrix.repo }} with: - route: PUT /repos/{owner}/{repo}/branches/{branch}/protection - owner: ${{ github.repository_owner }} - repo: ${{ matrix.repo }} - branch: ${{ env.TECO_BRANCH }} - restrictions: | - users: - - ${{ github.event.pull_request.user.login }} - required_linear_history: true - allow_force_pushes: true - allow_deletions: true + github-token: ${{ steps.generate-token.outputs.token }} + script: | + github.rest.repos.updateBranchProtection({ + owner: context.repo.owner, + repo: process.env.TECO_REPO, + branch: process.env.TECO_BRANCH, + required_status_checks: null, + enforce_admins: null, + required_pull_request_reviews: null, + restrictions: { + users: [context.payload.pull_request.user.login], + teams: [], + apps: ['teco-automation'] + }, + required_linear_history: true, + allow_force_pushes: true, + allow_deletions: true + }); generate-and-sync: runs-on: ubuntu-latest if: ${{ success() || github.event.action == 'synchronize' }} @@ -79,26 +88,18 @@ jobs: git_config_global: true git_user_signingkey: true git_commit_gpgsign: true + - name: Install dependency + run: sudo apt-get install git-filter-repo -y - name: Checkout uses: actions/checkout@v3 - - name: Wait for building - uses: lewagon/wait-on-check-action@v1.3.1 - with: - ref: ${{ github.ref }} - check-name: 'build (release)' - repo-token: ${{ secrets.GITHUB_TOKEN }} - wait-interval: 10 - - name: Restore build result - uses: actions/cache/restore@v3 - with: - path: .build - key: ${{ runner.os }}-${{ github.sha }} + - name: Build + run: swift build -c release - name: Checkout API models uses: actions/checkout@v3 with: repository: ${{ github.repository_owner }}/teco-api-models path: ./models - ref: current + ref: refs/tags/current - name: Checkout teco-core uses: actions/checkout@v3 with: @@ -120,12 +121,12 @@ jobs: git rebase "$GITHUB_BASE_REF" --quiet - name: Generate teco-core run: | - swift run --skip-build teco-common-error-generator \ + swift run -c release --skip-build teco-common-error-generator \ --error-file ./models/zh-CN/error-codes.json \ --output ./teco-core/Sources/TecoCore/Errors/TCCommonError.swift - swift run --skip-build teco-date-wrapper-generator \ + swift run -c release --skip-build teco-date-wrapper-generator \ --output-dir './teco-core/Sources/TecoDateHelpers/Property Wrappers' - swift run --skip-build teco-region-generator \ + swift run -c release --skip-build teco-region-generator \ --output ./teco-core/Sources/TecoCore/Common/TCRegion.swift - name: Checkout teco uses: actions/checkout@v3 @@ -148,7 +149,7 @@ jobs: git rebase "$GITHUB_BASE_REF" --quiet - name: Generate teco run: | - swift run --skip-build teco-package-generator \ + swift run -c release --skip-build teco-package-generator \ --model-dir ./models/zh-CN \ --service-generator .build/release/teco-service-generator \ --package-dir ./teco From 31d92f3b157dffb7eff3bb6f6ce9599c7ec3d515 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Thu, 20 Apr 2023 00:37:25 +0800 Subject: [PATCH 03/14] Introduces "in sync" label --- .github/workflows/generate.yml | 42 +++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index 2a6d746..73adc1b 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -3,6 +3,10 @@ on: pull_request: branches: - main + paths: + - '.github/workflows/generate.yml' + - 'Sources/**' + - 'Package.*' env: TECO_BRANCH: codegen-pr-${{ github.event.number }} @@ -10,7 +14,7 @@ env: jobs: create-branch: runs-on: ubuntu-latest - if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' }} + if: ${{ !contains(github.event.pull_request.labels.*.name, 'in sync') }} strategy: matrix: repo: @@ -23,7 +27,28 @@ jobs: with: app_id: ${{ secrets.BOT_APP_ID }} private_key: ${{ secrets.BOT_APP_PRIVATE_KEY }} - - name: Create synced branch + - name: Check if PR author is collaborator + uses: actions/github-script@v6 + id: check-collaborator + env: + TECO_REPO: ${{ matrix.repo }} + with: + github-token: ${{ steps.generate-token.outputs.token }} + script: | + try { + await github.rest.repos.checkCollaborator({ + owner: context.repo.owner, + repo: process.env.TECO_REPO, + username: context.payload.pull_request.user.login + }); + return true; + } catch { + const title = 'Pull request author is not collaborator'; + const message = 'The author does not have push access to the sync branch at the moment. If the generated codes need patching, please contact organization owners.'; + console.log(`::warning title=${title}::${message}`); + return false; + } + - name: Create sync branch uses: actions/github-script@v6 env: TECO_REPO: ${{ matrix.repo }} @@ -67,9 +92,20 @@ jobs: allow_force_pushes: true, allow_deletions: true }); + - name: Add "in sync" label + uses: actions/github-script@v6 + with: + github-token: ${{ steps.generate-token.outputs.token }} + script: | + github.rest.issues.addLabels({ + issue_number: context.payload.pull_request.number, + owner: context.repo.owner, + repo: context.repo.repo, + labels: ['in sync'] + }); generate-and-sync: runs-on: ubuntu-latest - if: ${{ success() || github.event.action == 'synchronize' }} + if: ${{ success() || contains(github.event.pull_request.labels.*.name, 'in sync') }} needs: - create-branch steps: From 926b4f0b7df988b7df9880a79d6323abde3756b4 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Thu, 20 Apr 2023 00:40:25 +0800 Subject: [PATCH 04/14] Set concurrency group --- .github/workflows/generate.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index 73adc1b..e7ffc25 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -8,6 +8,8 @@ on: - 'Sources/**' - 'Package.*' +concurrency: codegen-sync-${{ github.event.number }} + env: TECO_BRANCH: codegen-pr-${{ github.event.number }} From dfc54eba865f94056e6f42700fa4c4f188964e8e Mon Sep 17 00:00:00 2001 From: YR Chen Date: Thu, 20 Apr 2023 01:09:13 +0800 Subject: [PATCH 05/14] Use full clone --- .github/workflows/generate.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index e7ffc25..e2c284c 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -145,6 +145,7 @@ jobs: ref: ${{ env.TECO_BRANCH }} path: ./teco-core token: ${{ steps.generate-token.outputs.token }} + fetch-depth: 0 - name: Drop bot commits in teco-core env: BOT_COMMITTER_EMAIL: ${{ steps.import-gpg.outputs.email }} @@ -152,11 +153,11 @@ jobs: run: | git filter-repo --refs HEAD --commit-callback ' import subprocess; import os - p = subprocess.run(["git", "merge-base", "--is-ancestor", commit.original_id, os.environ["GITHUB_BASE_REF"]]) + p = subprocess.run(["git", "merge-base", "--is-ancestor", commit.original_id, "origin/" + os.environ["GITHUB_BASE_REF"]]) if p.returncode != 0 and commit.committer_email.decode() == os.environ["BOT_COMMITTER_EMAIL"]: commit.skip() ' - git rebase "$GITHUB_BASE_REF" --quiet + git rebase "origin/$GITHUB_BASE_REF" --quiet - name: Generate teco-core run: | swift run -c release --skip-build teco-common-error-generator \ @@ -173,6 +174,7 @@ jobs: ref: ${{ env.TECO_BRANCH }} path: ./teco token: ${{ steps.generate-token.outputs.token }} + fetch-depth: 0 - name: Drop bot commits in teco env: BOT_COMMITTER_EMAIL: ${{ steps.import-gpg.outputs.email }} @@ -180,11 +182,11 @@ jobs: run: | git filter-repo --refs HEAD --commit-callback ' import subprocess; import os - p = subprocess.run(["git", "merge-base", "--is-ancestor", commit.original_id, os.environ["GITHUB_BASE_REF"]]) + p = subprocess.run(["git", "merge-base", "--is-ancestor", commit.original_id, "origin/" + os.environ["GITHUB_BASE_REF"]]) if p.returncode != 0 and commit.committer_email.decode() == os.environ["BOT_COMMITTER_EMAIL"]: commit.skip() ' - git rebase "$GITHUB_BASE_REF" --quiet + git rebase "origin/$GITHUB_BASE_REF" --quiet - name: Generate teco run: | swift run -c release --skip-build teco-package-generator \ From 254c0a863c3d6c6b38db42ba7e9f47be84b3a07d Mon Sep 17 00:00:00 2001 From: YR Chen Date: Thu, 20 Apr 2023 10:18:24 +0800 Subject: [PATCH 06/14] Add and commit --- .github/workflows/generate.yml | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index e2c284c..5f25b6f 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -197,5 +197,23 @@ jobs: then swift package resolve fi - # TODO: Add and commit to TecoCore. - # TODO: Add and commit to Teco. + - name: Commit and push to teco-core + uses: EndBug/add-and-commit@v9 + with: + cwd: ./teco-core + author_name: ${{ github.event.pull_request.user.name }} + author_email: ${{ github.event.pull_request.user.email }} + committer_name: ${{ steps.import-gpg.outputs.name }} + committer_email: ${{ steps.import-gpg.outputs.email }} + message: ${{ github.event.pull_request.title }} + push: true + - name: Commit and push to teco + uses: EndBug/add-and-commit@v9 + with: + cwd: ./teco + author_name: ${{ github.event.pull_request.user.name }} + author_email: ${{ github.event.pull_request.user.email }} + committer_name: ${{ steps.import-gpg.outputs.name }} + committer_email: ${{ steps.import-gpg.outputs.email }} + message: ${{ github.event.pull_request.title }} + push: true From afe8fe37c8d7ed94c03a2e9fa3d153a340c3bf4c Mon Sep 17 00:00:00 2001 From: YR Chen Date: Thu, 20 Apr 2023 10:53:57 +0800 Subject: [PATCH 07/14] Cache built products --- .github/workflows/generate.yml | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index 5f25b6f..d7a0755 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -130,7 +130,24 @@ jobs: run: sudo apt-get install git-filter-repo -y - name: Checkout uses: actions/checkout@v3 + - name: Detect Swift package information + id: swift-package + run: | + echo "BUILD_PATH=$(swift build --show-bin-path -c release)" >> $GITHUB_ENV + echo 'product-paths<<$EOF' >> $GITHUB_OUTPUT + swift package describe --type json \ + | jq '.products[].name' -r \ + | sed "s#^#$(swift build --show-bin-path -c release)/#" \ + >> $GITHUB_OUTPUT + echo '$EOF' >> $GITHUB_OUTPUT + - name: Apply cache + uses: actions/cache@v3 + id: build-cache + with: + path: ${{ steps.swift-package.outputs.product-paths }} + key: ${{ runner.os }}-${{ hashFiles('Package.*', 'Sources/**') }} - name: Build + if: ${{ !steps.build-cache.outputs.cache-hit }} run: swift build -c release - name: Checkout API models uses: actions/checkout@v3 @@ -160,12 +177,12 @@ jobs: git rebase "origin/$GITHUB_BASE_REF" --quiet - name: Generate teco-core run: | - swift run -c release --skip-build teco-common-error-generator \ + ${{ env.BUILD_PATH }}/teco-common-error-generator \ --error-file ./models/zh-CN/error-codes.json \ --output ./teco-core/Sources/TecoCore/Errors/TCCommonError.swift - swift run -c release --skip-build teco-date-wrapper-generator \ + ${{ env.BUILD_PATH }}/teco-date-wrapper-generator \ --output-dir './teco-core/Sources/TecoDateHelpers/Property Wrappers' - swift run -c release --skip-build teco-region-generator \ + ${{ env.BUILD_PATH }}/teco-region-generator \ --output ./teco-core/Sources/TecoCore/Common/TCRegion.swift - name: Checkout teco uses: actions/checkout@v3 @@ -189,9 +206,9 @@ jobs: git rebase "origin/$GITHUB_BASE_REF" --quiet - name: Generate teco run: | - swift run -c release --skip-build teco-package-generator \ + ${{ env.BUILD_PATH }}/teco-package-generator \ --model-dir ./models/zh-CN \ - --service-generator .build/release/teco-service-generator \ + --service-generator ${{ env.BUILD_PATH }}/teco-service-generator \ --package-dir ./teco if ! git diff --exit-code Package.swift then From ffc83eef2265ae830dbd3fa7133a1a5b3e6cf67c Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 25 Apr 2023 17:45:00 +0800 Subject: [PATCH 08/14] Update for test --- Sources/TecoCodeGeneratorCommons/Utils.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/TecoCodeGeneratorCommons/Utils.swift b/Sources/TecoCodeGeneratorCommons/Utils.swift index aca1e36..7ad97e7 100644 --- a/Sources/TecoCodeGeneratorCommons/Utils.swift +++ b/Sources/TecoCodeGeneratorCommons/Utils.swift @@ -76,6 +76,8 @@ extension SourceFileSyntax { // THIS FILE IS AUTOMATICALLY GENERATED by \(GeneratorContext.generator). // DO NOT EDIT. + // Test PR teco-project/teco-code-generator#23. + """ return self.with(\.leadingTrivia, .lineComment(header) + (self.leadingTrivia ?? [])) From 3da8eb3582d48cf6ff276c3954c06ad6bd31e46a Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 25 Apr 2023 20:45:39 +0800 Subject: [PATCH 09/14] Stops syncing when PR closed --- .github/workflows/desync.yml | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 .github/workflows/desync.yml diff --git a/.github/workflows/desync.yml b/.github/workflows/desync.yml new file mode 100644 index 0000000..29f41c7 --- /dev/null +++ b/.github/workflows/desync.yml @@ -0,0 +1,76 @@ +name: De-sync +on: + pull_request: + branches: + - main + types: + # Judge if PR was merged by github.event.pull_request.merged + - closed + +concurrency: codegen-sync-${{ github.event.number }} + +env: + TECO_BRANCH: codegen-pr-${{ github.event.number }} + +jobs: + delete-branch: + runs-on: ubuntu-latest + if: ${{ contains(github.event.pull_request.labels.*.name, 'in sync') }} + strategy: + matrix: + repo: + - teco + - teco-core + steps: + - name: Generate app token for bot + uses: tibdex/github-app-token@v1 + id: generate-token + with: + app_id: ${{ secrets.BOT_APP_ID }} + private_key: ${{ secrets.BOT_APP_PRIVATE_KEY }} + - name: Delete branch protection rule + uses: actions/github-script@v6 + env: + TECO_REPO: ${{ matrix.repo }} + with: + github-token: ${{ steps.generate-token.outputs.token }} + script: | + github.rest.repos.deleteBranchProtection({ + owner: context.repo.owner, + repo: process.env.TECO_REPO, + branch: process.env.TECO_BRANCH + }); + - name: Delete sync branch + uses: actions/github-script@v6 + env: + TECO_REPO: ${{ matrix.repo }} + with: + github-token: ${{ steps.generate-token.outputs.token }} + script: | + github.rest.git.deleteRef({ + owner: context.repo.owner, + repo: process.env.TECO_REPO, + ref: `heads/${process.env.TECO_BRANCH}` + }); + delete-label: + runs-on: ubuntu-latest + needs: + - delete-branch + steps: + - name: Generate app token for bot + uses: tibdex/github-app-token@v1 + id: generate-token + with: + app_id: ${{ secrets.BOT_APP_ID }} + private_key: ${{ secrets.BOT_APP_PRIVATE_KEY }} + - name: Delete "in sync" label + uses: actions/github-script@v6 + with: + github-token: ${{ steps.generate-token.outputs.token }} + script: | + github.rest.issues.removeLabel({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + name: 'in sync' + }); From 6251f0645fcd6ac604a496fef42ed86927548280 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 25 Apr 2023 20:50:27 +0800 Subject: [PATCH 10/14] Use force push --- .github/workflows/generate.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index d7a0755..275fa1a 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -223,7 +223,7 @@ jobs: committer_name: ${{ steps.import-gpg.outputs.name }} committer_email: ${{ steps.import-gpg.outputs.email }} message: ${{ github.event.pull_request.title }} - push: true + push: --force - name: Commit and push to teco uses: EndBug/add-and-commit@v9 with: @@ -233,4 +233,4 @@ jobs: committer_name: ${{ steps.import-gpg.outputs.name }} committer_email: ${{ steps.import-gpg.outputs.email }} message: ${{ github.event.pull_request.title }} - push: true + push: --force From 79273c181b39683df31b663661c6314b7813fa2c Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 25 Apr 2023 21:56:35 +0800 Subject: [PATCH 11/14] Sets commit author according to GitHub profile --- .github/workflows/generate.yml | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index 275fa1a..dc4d326 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -214,12 +214,22 @@ jobs: then swift package resolve fi + - name: Get pull request author information + uses: actions/github-script@v6 + id: author-info + with: + script: | + const { data: user } = await github.rest.users.getByUsername({ + username: context.payload.pull_request.user.login + }); + core.setOutput('name', user.name); + core.setOutput('email', user.email); - name: Commit and push to teco-core uses: EndBug/add-and-commit@v9 with: cwd: ./teco-core - author_name: ${{ github.event.pull_request.user.name }} - author_email: ${{ github.event.pull_request.user.email }} + author_name: ${{ steps.author-info.outputs.name }} + author_email: ${{ steps.author-info.outputs.email }} committer_name: ${{ steps.import-gpg.outputs.name }} committer_email: ${{ steps.import-gpg.outputs.email }} message: ${{ github.event.pull_request.title }} @@ -228,8 +238,8 @@ jobs: uses: EndBug/add-and-commit@v9 with: cwd: ./teco - author_name: ${{ github.event.pull_request.user.name }} - author_email: ${{ github.event.pull_request.user.email }} + author_name: ${{ steps.author-info.outputs.name }} + author_email: ${{ steps.author-info.outputs.email }} committer_name: ${{ steps.import-gpg.outputs.name }} committer_email: ${{ steps.import-gpg.outputs.email }} message: ${{ github.event.pull_request.title }} From 207d4b529c20de636e91ab1a5ada19a35a13b37c Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 25 Apr 2023 22:52:11 +0800 Subject: [PATCH 12/14] Adds dedicated create label job --- .github/workflows/generate.yml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/.github/workflows/generate.yml b/.github/workflows/generate.yml index dc4d326..4f9bfdb 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/generate.yml @@ -94,6 +94,17 @@ jobs: allow_force_pushes: true, allow_deletions: true }); + create-label: + runs-on: ubuntu-latest + needs: + - create-branch + steps: + - name: Generate app token for bot + uses: tibdex/github-app-token@v1 + id: generate-token + with: + app_id: ${{ secrets.BOT_APP_ID }} + private_key: ${{ secrets.BOT_APP_PRIVATE_KEY }} - name: Add "in sync" label uses: actions/github-script@v6 with: @@ -107,9 +118,10 @@ jobs: }); generate-and-sync: runs-on: ubuntu-latest - if: ${{ success() || contains(github.event.pull_request.labels.*.name, 'in sync') }} + if: ${{ success() || needs.create-branch.result == 'skipped' }} needs: - create-branch + - create-label steps: - name: Generate app token for bot uses: tibdex/github-app-token@v1 From f8f7810b0661d482c9e7951342dfb850ed24298a Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 25 Apr 2023 22:54:34 +0800 Subject: [PATCH 13/14] Renames "Generate" to "Sync" --- .github/workflows/{generate.yml => sync.yml} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename .github/workflows/{generate.yml => sync.yml} (99%) diff --git a/.github/workflows/generate.yml b/.github/workflows/sync.yml similarity index 99% rename from .github/workflows/generate.yml rename to .github/workflows/sync.yml index 4f9bfdb..7670004 100644 --- a/.github/workflows/generate.yml +++ b/.github/workflows/sync.yml @@ -1,10 +1,10 @@ -name: Generate +name: Sync on: pull_request: branches: - main paths: - - '.github/workflows/generate.yml' + - '.github/workflows/sync.yml' - 'Sources/**' - 'Package.*' @@ -116,7 +116,7 @@ jobs: repo: context.repo.repo, labels: ['in sync'] }); - generate-and-sync: + generate-and-commit: runs-on: ubuntu-latest if: ${{ success() || needs.create-branch.result == 'skipped' }} needs: From 4e01a68cf14d15216c865759c469a44ba15a63a4 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Tue, 25 Apr 2023 23:09:38 +0800 Subject: [PATCH 14/14] Improves (de-)sync workflow condition --- .github/workflows/desync.yml | 14 ++++++++++++-- .github/workflows/sync.yml | 6 +----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.github/workflows/desync.yml b/.github/workflows/desync.yml index 29f41c7..bdf4f7a 100644 --- a/.github/workflows/desync.yml +++ b/.github/workflows/desync.yml @@ -4,7 +4,6 @@ on: branches: - main types: - # Judge if PR was merged by github.event.pull_request.merged - closed concurrency: codegen-sync-${{ github.event.number }} @@ -13,9 +12,20 @@ env: TECO_BRANCH: codegen-pr-${{ github.event.number }} jobs: + create-pr: + runs-on: ubuntu-latest + if: ${{ github.event.pull_request.merged }} + strategy: + matrix: + repo: + - teco + - teco-core + steps: + - name: TODO + run: echo "Hello world!" > /dev/null delete-branch: runs-on: ubuntu-latest - if: ${{ contains(github.event.pull_request.labels.*.name, 'in sync') }} + if: ${{ !github.event.pull_request.merged }} strategy: matrix: repo: diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index 7670004..e0d7653 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -3,10 +3,6 @@ on: pull_request: branches: - main - paths: - - '.github/workflows/sync.yml' - - 'Sources/**' - - 'Package.*' concurrency: codegen-sync-${{ github.event.number }} @@ -16,7 +12,7 @@ env: jobs: create-branch: runs-on: ubuntu-latest - if: ${{ !contains(github.event.pull_request.labels.*.name, 'in sync') }} + if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' }} strategy: matrix: repo: