upstream_repo_update #107
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: | |
UPSTREAM_REPO_WORKDIR: "upstream" | |
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/[email protected] | |
- 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: Parse Follow Merge dispatch event payload | |
uses: ./.github/actions-hub/actions/follow-merge-parse-payload | |
id: fm | |
- 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: "${{ steps.fm.outputs.branch_name }}" | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
if: steps.fm.outputs.repo_name && steps.fm.outputs.branch_name | |
with: | |
token: ${{ secrets.GIT_PAT }} | |
repository: ${{ steps.fm.outputs.repo_name }} | |
ref: ${{ steps.fm.outputs.branch_name }} | |
path: "${{ env.UPSTREAM_REPO_WORKDIR }}" | |
- name: Setup node | |
uses: actions/setup-node@v4 | |
- name: "Install poetry" | |
run: pipx install poetry | |
- name: "Set up Python" | |
id: setup_python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: '3.12' | |
# cache: 'poetry' # Disable cache since label-studio-client-generator does not have poetry.lock | |
- name: Download Fern | |
run: npm install -g fern-api | |
- name: Set Fern Generator Branch and Mode | |
env: | |
BRANCH_NAME: "${{ steps.get-branch.outputs.branch_name }}" | |
FERN_GENERATOR_PATH: "fern/generators.yml" | |
working-directory: "${{ env.UPSTREAM_REPO_WORKDIR }}" | |
run: | | |
yq e --inplace ".groups.python-sdk-staging.generators[0].github.branch |= \"${BRANCH_NAME}\"" "${FERN_GENERATOR_PATH}" | |
yq e --inplace ".groups.python-sdk-staging.generators[0].github.mode |= \"push\"" "${FERN_GENERATOR_PATH}" | |
cat "${FERN_GENERATOR_PATH}" | |
- name: Check Fern API is valid | |
working-directory: "${{ env.UPSTREAM_REPO_WORKDIR }}" | |
run: fern check | |
- name: Generate Python SDK | |
env: | |
FERN_TOKEN: ${{ secrets.FERN_TOKEN }} | |
working-directory: "${{ env.UPSTREAM_REPO_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: "${{ steps.fm.outputs.title }}" | |
description: | | |
Hi @${{ steps.fm.outputs.actor }}! | |
This PR was automaticaly generated via Follow Merge. | |
Please ensure that all linked upstream Pull Requests are merged before proceeding with this one. | |
- name: Add PR Reviewers | |
uses: ./.github/actions-hub/actions/github-add-pull-request-reviewers | |
continue-on-error: true | |
with: | |
github_token: ${{ secrets.GIT_PAT }} | |
pullrequest_number: "${{ steps.get-pr.outputs.number }}" | |
reviewers: "${{ steps.fm.outputs.actor }}" | |
- name: Link PR | |
uses: ./.github/actions-hub/actions/github-link-upstream-pull-request | |
continue-on-error: true | |
with: | |
github_token: ${{ secrets.GIT_PAT }} | |
pullrequest_number: "${{ steps.get-pr.outputs.number }}" | |
upstream_pullrequest_link: "${{ steps.fm.outputs.pr_html_url }}" | |
- name: Convert to ready for review | |
if: steps.fm.outputs.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 | |
if: steps.fm.outputs.event_action == 'merged' | |
continue-on-error: true | |
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 | |
} | |
} | |
}' | |
- name: Notify on failure | |
uses: ./.github/actions-hub/actions/github-create-comment | |
if: failure() | |
with: | |
github_token: ${{ secrets.GIT_PAT }} | |
repository: "${{ steps.fm.outputs.repo_name }}" | |
issue_number: "${{ steps.fm.outputs.pr_number }}" | |
body: | | |
Follow Merge downstream workflow has been failed. | |
> [Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) | |
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/[email protected] | |
- 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 | |
} | |
} | |
} | |
' |