diff --git a/.github/workflows/import_pr_list.yml b/.github/workflows/import_pr_list.yml index e122690e3b..5e82672305 100644 --- a/.github/workflows/import_pr_list.yml +++ b/.github/workflows/import_pr_list.yml @@ -1,13 +1,13 @@ -name: Import Subrepo PRs LIST +name: Import Subrepo PRs on: workflow_dispatch: inputs: subrepo-prefix: - description: "super-repo path prefix (e.g., projects/rocblas)" + description: "Super‐repo path prefix (e.g., projects/rocblas)" required: true subrepo-pr-numbers: - description: "Comma-separated list of Subrepo PR numbers to import" + description: "Comma‐separated list of Subrepo PR numbers to import" required: true subrepo-repo: description: "Full name of subrepo repo or fork (e.g., ROCm/rocBLAS or user/rocBLAS-fork)" @@ -16,136 +16,53 @@ on: description: "Canonical subrepo repo (e.g., ROCm/rocBLAS)" required: true super-repo-target-branch: - description: "Target branch in the super-repo (default: develop)" + description: "Target branch in the super‐repo" required: false default: "develop" jobs: - generate-matrix: + import-prs: runs-on: ubuntu-latest - outputs: - matrix: ${{ steps.set-matrix.outputs.matrix }} - steps: - - name: Convert PR numbers to matrix - id: set-matrix - run: | - PRS="${{ github.event.inputs.subrepo-pr-numbers }}" - JSON_ARRAY=$(echo "$PRS" \ - | tr ',' '\n' \ - | jq -R . \ - | jq -s .) - COMPACT=$(jq -c . <<<"$JSON_ARRAY") - echo "matrix=$COMPACT" >> $GITHUB_OUTPUT + permissions: + contents: write + pull-requests: write - import: - needs: generate-matrix - runs-on: ubuntu-24.04 - strategy: - matrix: - pr_number: ${{ fromJson(needs.generate-matrix.outputs.matrix) }} - fail-fast: false - continue-on-error: true steps: - - name: Validate maintainer permissions - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - echo "Actor is: ${{ github.actor }}" - PERMISSION=$(gh api \ - repos/${{ github.repository }}/collaborators/${{ github.actor }}/permission \ - --jq .permission) - if [[ "$PERMISSION" != "admin" && "$PERMISSION" != "maintain" ]]; then - echo "❌ User ${{ github.actor }} is not authorized to run this workflow" - exit 1 - fi - - - name: Generate a token - id: generate-token - uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 + - name: Generate GitHub App token + id: gen_token + uses: actions/create-github-app-token@v2.0.6 with: app-id: ${{ secrets.APP_ID }} private-key: ${{ secrets.APP_PRIVATE_KEY }} owner: ${{ github.repository_owner }} - - name: Checkout code - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Checkout super‐repo + uses: actions/checkout@v4 with: + token: ${{ steps.gen_token.outputs.token }} + fetch-depth: 0 sparse-checkout: | - .github + .github/scripts/import_subrepo_prs.py ${{ github.event.inputs.subrepo-prefix }} sparse-checkout-cone-mode: true - token: ${{ steps.generate-token.outputs.token }} - fetch-depth: 0 # for subtree operations - - - name: Set up Git user + + - name: Set up Python 3.x + uses: actions/setup-python@v4 + with: + python-version: "3.x" + + - name: Install Python dependencies run: | - git config user.name "systems-assistant[bot]" - git config user.email "systems-assistant[bot]@users.noreply.github.com" - - - name: Fetch subrepo PR info using gh - id: prdata + python -m pip install --upgrade pip + pip install PyGithub GitPython + + - name: Import Subrepo PRs env: - GH_TOKEN: ${{ steps.generate-token.outputs.token }} + GITHUB_TOKEN: ${{ steps.gen_token.outputs.token }} + SUBPREFIX: ${{ github.event.inputs.subrepo-prefix }} + SUBREPO: ${{ github.event.inputs.subrepo-repo }} + UPSTREAM: ${{ github.event.inputs.subrepo-upstream }} + TARGET: ${{ github.event.inputs.super-repo-target-branch }} + PR_LIST: ${{ github.event.inputs.subrepo-pr-numbers }} run: | - PR_JSON=$(gh pr view ${{ matrix.pr_number }} \ - --repo ${{ github.event.inputs.subrepo-upstream }} \ - --json title,body,headRefName,headRepository,isDraft \ - --jq '{title: .title, body: .body, head_ref: .headRefName, head_repo: .headRepository.cloneUrl, is_draft: .isDraft}') - echo "$PR_JSON" > pr.json - TITLE=$(jq -r .title pr.json | sed 's/`/\\`/g' | sed 's/\r$//g') - echo "title=$TITLE" >> $GITHUB_OUTPUT - BODY_CLEAN=$(jq -r .body pr.json | sed 's/\r//g') - echo "body<> $GITHUB_OUTPUT - echo "$BODY_CLEAN" >> $GITHUB_OUTPUT - echo "EOF" >> $GITHUB_OUTPUT - echo "head_ref=$(jq -r .head_ref pr.json)" >> $GITHUB_OUTPUT - echo "head_repo=$(jq -r .head_repo pr.json)" >> $GITHUB_OUTPUT - echo "is_draft=$(jq -r .is_draft pr.json)" >> $GITHUB_OUTPUT - - - name: Create new branch for import - id: import-branch - run: | - git fetch origin ${{ github.event.inputs.super-repo-target-branch }} - git checkout ${{ github.event.inputs.super-repo-target-branch }} - SANITIZED_BASE=$(echo "${{ github.event.inputs.super-repo-target-branch }}" | sed 's|/|_|g') - SANITIZED_REPO=$(echo "${{ github.event.inputs.subrepo-repo }}" | sed 's|/|_|g') - IMPORT_BRANCH="import/${SANITIZED_BASE}/${SANITIZED_REPO}/pr-${{ matrix.pr_number }}" - echo "import_branch=$IMPORT_BRANCH" >> $GITHUB_OUTPUT - git checkout -b "$IMPORT_BRANCH" - - - name: Pull subrepo changes - run: | - git subtree pull --prefix=${{ github.event.inputs.subrepo-prefix }} https://github.com/${{ github.event.inputs.subrepo-repo }} ${{ steps.prdata.outputs.head_ref }} - git push origin ${{ steps.import-branch.outputs.import_branch }} - - - name: Create super-repo PR - env: - GH_TOKEN: ${{ steps.generate-token.outputs.token }} - run: | - IMPORT_BRANCH="${{ steps.import-branch.outputs.import_branch }}" - PR_TITLE="${{ steps.prdata.outputs.title }}" - UPSTREAM_REPO="${{ github.event.inputs.subrepo-upstream }}" - SUBREPO_PR_NUMBER="${{ matrix.pr_number }}" - SUBREPO_URL="https://github.com/$UPSTREAM_REPO/pull/$SUBREPO_PR_NUMBER" - AUTHOR=$(gh pr view "$SUBREPO_PR_NUMBER" --repo "$UPSTREAM_REPO" --json author --jq .author.login) - - echo "${{ steps.prdata.outputs.body }}" > pr_body.txt - { - echo "" - echo "---" - echo "🔁 Imported from [$UPSTREAM_REPO#$SUBREPO_PR_NUMBER]($SUBREPO_URL)" - echo "🧑‍💻 Originally authored by @$AUTHOR" - } >> pr_body.txt - - DRAFT_FLAG="" - if [[ "${{ steps.prdata.outputs.is_draft }}" == "true" ]]; then - DRAFT_FLAG="--draft" - fi - - gh pr create \ - --base "${{ github.event.inputs.super-repo-target-branch }}" \ - --head "$IMPORT_BRANCH" \ - --title "$PR_TITLE" \ - --label "imported pr" \ - $DRAFT_FLAG \ - --body-file pr_body.txt + python .github/scripts/import_subrepo_prs.py