upstream_repo_update #4
This file contains 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: 'Follow Merge: Upstream repo sync' | |
on: | |
repository_dispatch: | |
types: | |
- upstream_repo_update | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.event.client_payload.branch_name }} | |
env: | |
FERN_WORKDIR: "fern" | |
jobs: | |
open: | |
name: Sync PR | |
if: | | |
github.event.client_payload.event_action == 'opened' || | |
github.event.client_payload.event_action == 'synchronize' || | |
github.event.client_payload.event_action == 'merged' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: hmarr/debug-action@v3.0.0 | |
- name: Details | |
id: details | |
shell: bash | |
env: | |
REPO_NAME: "${{ github.event.client_payload.repo_name }}" | |
run: | | |
set -xeuo pipefail | |
case "${REPO_NAME}" in | |
*/label-studio-client-generator) | |
;; | |
*) | |
echo "::error::Repository ${REPO_NAME} is not supported" | |
exit 1 | |
;; | |
esac | |
- name: Checkout Actions Hub | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GIT_PAT }} | |
repository: HumanSignal/actions-hub | |
path: ./.github/actions-hub | |
- name: Find or Create branch | |
uses: ./.github/actions-hub/actions/github-find-or-create-branch | |
id: get-branch | |
with: | |
github-token: ${{ secrets.GIT_PAT }} | |
branch_name: "${{ github.event.client_payload.branch_name }}" | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GIT_PAT }} | |
repository: ${{ github.event.client_payload.repo_name }} | |
path: "${{ env.FERN_WORKDIR }}" | |
- name: Setup node | |
uses: actions/setup-node@v3 | |
- name: Download Fern | |
run: npm install -g fern-api | |
- name: Set Fern Generator Branch | |
env: | |
BRANCH_NAME: "${{ steps.get-branch.outputs.branch_name }}" | |
FERN_GENERATOR_PATH: "fern/generators.yml" | |
working-directory: "${{ env.FERN_WORKDIR }}" | |
run: | | |
yq e --inplace ".groups.python-sdk-staging.generators[0].github.branch |= \"${BRANCH_NAME}\"" "${FERN_GENERATOR_PATH}" | |
cat "${FERN_GENERATOR_PATH}" | |
- name: Check Fern API is valid | |
working-directory: "${{ env.FERN_WORKDIR }}" | |
run: fern check | |
- name: Generate Python SDK | |
env: | |
FERN_TOKEN: ${{ secrets.FERN_TOKEN }} | |
working-directory: "${{ env.FERN_WORKDIR }}" | |
run: fern generate --group python-sdk-staging --log-level debug | |
- name: Find or Create PR | |
uses: ./.github/actions-hub/actions/github-find-or-create-pull-request | |
id: get-pr | |
with: | |
github-token: ${{ secrets.GIT_PAT }} | |
branch_name: "${{ steps.get-branch.outputs.branch_name }}" | |
title: "${{ github.event.client_payload.title }}" | |
- name: Convert to ready for review | |
if: | | |
github.event.client_payload.event_action == 'merged' | |
id: ready-for-review-pr | |
shell: bash | |
env: | |
GIT_PAT: ${{ secrets.GIT_PAT }} | |
run: | | |
echo "$GIT_PAT" | gh auth login --with-token | |
gh api graphql -F id='${{ steps.get-pr.outputs.node_id }}' -f query=' | |
mutation($id: ID!) { | |
markPullRequestReadyForReview(input: { pullRequestId: $id }) { | |
pullRequest { | |
id | |
} | |
} | |
} | |
' | |
- name: Enable AutoMerge | |
id: enable-pr-automerge | |
if: | | |
github.event.client_payload.event_action == 'merged' | |
shell: bash | |
env: | |
GIT_PAT: ${{ secrets.GIT_PAT }} | |
run: | | |
echo "$GIT_PAT" | gh auth login --with-token | |
gh api graphql -f pull='${{ steps.get-pr.outputs.node_id }}' -f query=' | |
mutation($pull: ID!) { | |
enablePullRequestAutoMerge(input: {pullRequestId: $pull, mergeMethod: SQUASH}) { | |
pullRequest { | |
id | |
number | |
} | |
} | |
}' | |
others: | |
name: Other actions with PR | |
if: | | |
github.event.client_payload.event_action == 'converted_to_draft' || | |
github.event.client_payload.event_action == 'ready_for_review' || | |
github.event.client_payload.event_action == 'closed' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: hmarr/debug-action@v3.0.0 | |
- name: Get PR | |
uses: actions/github-script@v7 | |
id: get-pr | |
with: | |
github-token: ${{ secrets.GIT_PAT }} | |
script: | | |
const {repo, owner} = context.repo; | |
const branchName = '${{ github.event.client_payload.branch_name }}'; | |
const branchNameLowerCase = branchName.toLowerCase(); | |
const {data: listPullsResponse} = await github.rest.pulls.list({ | |
owner, | |
repo, | |
head: `${owner}:${branchName}`, | |
per_page: 1 | |
}); | |
const {data: listPullsResponseLowerCase} = await github.rest.pulls.list({ | |
owner, | |
repo, | |
head: `${owner}:${branchNameLowerCase}`, | |
per_page: 1 | |
}); | |
if (listPullsResponse.length !== 0) { | |
console.log(`Found PR for branch '${branchName}'`) | |
core.setOutput("branch-name", branchName); | |
return listPullsResponse | |
} else if (listPullsResponseLowerCase.length !== 0) { | |
console.log(`Found PR for branch '${branchNameLowerCase}'`) | |
core.setOutput("branch-name", branchNameLowerCase); | |
return listPullsResponseLowerCase | |
} else { | |
console.log(`PR for branch '${branchNameLowerCase}' is not created yet`) | |
core.setOutput("branch-name", branchNameLowerCase); | |
return listPullsResponseLowerCase | |
} | |
- name: Close PR | |
if: github.event.client_payload.event_action == 'closed' | |
id: close-pr | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GIT_PAT }} | |
script: | | |
const { repo, owner } = context.repo; | |
const listPullsResponse = ${{ steps.get-pr.outputs.result }} | |
for (let pr of listPullsResponse ) { | |
core.info(`Closing ${ pr.html_url }`) | |
github.rest.pulls.update({ | |
owner, | |
repo, | |
pull_number: pr.number, | |
state: 'close' | |
}); | |
} | |
- name: Convert to draft | |
if: github.event.client_payload.event_action == 'converted_to_draft' | |
id: convert-pr-to-draft | |
shell: bash | |
env: | |
GIT_PAT: ${{ secrets.GIT_PAT }} | |
run: | | |
echo "$GIT_PAT" | gh auth login --with-token | |
gh api graphql -F id='${{ fromJson(steps.get-pr.outputs.result)[0].node_id }}' -f query=' | |
mutation($id: ID!) { | |
convertPullRequestToDraft(input: { pullRequestId: $id }) { | |
pullRequest { | |
id | |
isDraft | |
} | |
} | |
} | |
' | |
- name: Convert to ready for review | |
if: github.event.client_payload.event_action == 'ready_for_review' | |
id: ready-for-review-pr | |
shell: bash | |
env: | |
GIT_PAT: ${{ secrets.GIT_PAT }} | |
run: | | |
echo "$GIT_PAT" | gh auth login --with-token | |
gh api graphql -F id='${{ fromJson(steps.get-pr.outputs.result)[0].node_id }}' -f query=' | |
mutation($id: ID!) { | |
markPullRequestReadyForReview(input: { pullRequestId: $id }) { | |
pullRequest { | |
id | |
} | |
} | |
} | |
' |