build: bump the minimal group across 1 directory with 3 updates #1559
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
| # ------------------------------------------------------------------------------------ | |
| # Fork PR Handler GitHub Action | |
| # | |
| # Description: | |
| # This workflow migrates pull requests (PRs) from forks to branches within the main | |
| # repository, enabling workflows that require repository secrets to run. It is triggered | |
| # by comments containing "@pyansys-ci-bot migrate" or "@pyansys-ci-bot sync" on PRs, | |
| # or manually via workflow dispatch. | |
| # | |
| # Usage: | |
| # - Comment "@pyansys-ci-bot migrate" or "@pyansys-ci-bot sync" on a forked PR. | |
| # - Optionally specify "theirs" or "ours" to resolve merge conflicts automatically. | |
| # - Can also be triggered manually from the Actions tab with required inputs. | |
| # | |
| # Intended Output: | |
| # - Migrates the PR to a new branch in the main repository. | |
| # - Optionally resolves merge conflicts using the specified strategy. | |
| # - Opens a new PR in the main repository and notifies the user. | |
| # - Reacts to the triggering comment to indicate success or failure. | |
| # | |
| # Inputs (for workflow_dispatch/dev mode): | |
| # - issue_number: The PR/issue number to migrate (required). | |
| # - comment_body: The comment body to simulate (optional, default: "@pyansys-ci-bot migrate"). | |
| # ------------------------------------------------------------------------------------ | |
| name: Fork PR Handler | |
| on: | |
| issue_comment: | |
| types: [created] | |
| workflow_dispatch: | |
| inputs: | |
| issue_number: | |
| description: 'Issue number' | |
| required: true | |
| type: number | |
| comment_body: | |
| description: 'Comment body' | |
| required: false | |
| default: '@pyansys-ci-bot migrate' | |
| type: string | |
| comment_id: | |
| description: 'Comment ID' | |
| required: true | |
| type: number | |
| actor: | |
| description: 'Actor triggering the workflow' | |
| required: true | |
| type: string | |
| permissions: {} | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| migrate: | |
| name: Migrate PR from fork to main repo | |
| if: | | |
| ( | |
| github.event.issue.pull_request != null && | |
| (contains(github.event.comment.body, '@pyansys-ci-bot migrate') || contains(github.event.comment.body, '@pyansys-ci-bot sync') ) | |
| ) || ( github.event_name == 'workflow_dispatch' ) | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write # Needed to create and push migration branches | |
| pull-requests: write # Needed to create pull requests and add comments | |
| steps: | |
| - name: Setup the configuration | |
| id: pr_number | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0 | |
| env: | |
| EVENTNAME: ${{ github.event_name }} | |
| DISPATCH_PR_NUMBER: ${{ github.event.inputs.issue_number }} | |
| DISPATCH_USER: ${{ github.event.inputs.actor }} | |
| DISPATCH_COMMENT_BODY: ${{ github.event.inputs.comment_body }} | |
| DISPATCH_COMMENT_ID: ${{ github.event.inputs.comment_id }} | |
| # | |
| COMMENT_PR_NUMBER: ${{ github.event.issue.number }} | |
| COMMENT_USER: ${{ github.event.comment.user.login }} | |
| COMMENT_BODY: ${{ github.event.comment.body }} | |
| COMMENT_ID: ${{ github.event.comment.id }} | |
| with: | |
| github-token: ${{ secrets.PYANSYS_CI_BOT_TOKEN }} | |
| script: | | |
| const eventName = process.env.EVENTNAME | |
| core.debug(`Event name: ${eventName}`); | |
| let prNumber, userTriggering, commentBody, commentId; | |
| if (eventName === "workflow_dispatch") { | |
| core.info('Running in "workflow_dispatch" mode'); | |
| prNumber = process.env.DISPATCH_PR_NUMBER; | |
| userTriggering = process.env.DISPATCH_USER; | |
| commentBody = process.env.DISPATCH_COMMENT_BODY; | |
| commentId = process.env.DISPATCH_COMMENT_ID; | |
| } else { | |
| core.info('Running in "issue_comment" mode'); | |
| prNumber = process.env.COMMENT_PR_NUMBER; | |
| userTriggering = process.env.COMMENT_USER; | |
| commentBody = process.env.COMMENT_BODY; | |
| commentId = process.env.COMMENT_ID; | |
| } | |
| core.debug(`PR Number: ${prNumber}`); | |
| core.debug(`User triggering: ${userTriggering}`); | |
| core.debug(`Comment body: ${commentBody}`); | |
| core.debug(`Comment ID: ${commentId}`); | |
| // Export variables for later steps | |
| core.exportVariable('PR_NUMBER', prNumber); | |
| core.exportVariable('USER_TRIGGERING', userTriggering); | |
| core.exportVariable('COMMENT_ID', commentId); | |
| // Three modes of operation: | |
| // 1. auto - automatically resolves conflicts if possible, otherwise exits | |
| // 2. theirs - resolves conflicts by taking changes from the head branch | |
| // 3. ours - resolves conflicts by taking changes from the base branch | |
| let mode = ''; | |
| if ( | |
| commentBody.includes('pyansys-ci-bot sync theirs') || | |
| commentBody.includes('pyansys-ci-bot migrate theirs') | |
| ) { | |
| core.info("Resolving conflicts by taking 'theirs' changes"); | |
| mode = '--theirs'; | |
| } else if ( | |
| commentBody.includes('pyansys-ci-bot sync ours') || | |
| commentBody.includes('pyansys-ci-bot migrate ours') | |
| ) { | |
| core.info("Resolving conflicts by taking 'ours' changes"); | |
| mode = '--ours'; | |
| } else { | |
| core.info("No specific sync mode provided, defaulting to 'auto' which will exit if there are conflicts."); | |
| } | |
| core.exportVariable('MODE', mode); | |
| - id: is_organization_member | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0 | |
| with: | |
| github-token: ${{ secrets.TOKEN_TEAMS_USER_READ }} | |
| script: | | |
| try { | |
| const { data } = await github.rest.teams.getMembershipForUserInOrg({ | |
| org: 'ansys', | |
| team_slug: 'pymapdl-maintainers', | |
| username: process.env.USER_TRIGGERING, | |
| }); | |
| if (core.isDebug()) { | |
| core.debug(data); | |
| } | |
| // Check if the user is a member or maintainer of the team | |
| if (data && data.state === 'active' && (data.role === 'member' || data.role === 'maintainer')) { | |
| core.setOutput('is_member', true); | |
| core.exportVariable('CONTINUE', 'true'); | |
| } else { | |
| core.setOutput('is_member', false); | |
| core.exportVariable('CONTINUE', 'false'); | |
| } | |
| } catch (error) { | |
| core.error(`Error fetching team membership: ${error.message}`); | |
| core.exportVariable('CONTINUE', 'false'); | |
| return; | |
| } | |
| - name: 'Delete previous reactions' | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0 | |
| with: | |
| github-token: ${{ secrets.PYANSYS_CI_BOT_TOKEN }} | |
| script: | | |
| commentId = process.env.COMMENT_ID; | |
| // Remove previous reaction if it exists | |
| data = await github.rest.reactions.listForIssueComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| comment_id: commentId, | |
| }); | |
| if (data && data.data && data.data.length > 0) { | |
| core.info(`Found ${data.data.length} reactions for comment ${commentId}`); | |
| for (const reaction of data.data) { | |
| if (reaction.user.login === 'pyansys-ci-bot') { | |
| // Remove the reaction | |
| await github.rest.reactions.deleteForIssueComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| comment_id: commentId, | |
| reaction_id: reaction.id, | |
| }); | |
| } | |
| } | |
| } else { | |
| core.debug(`No reactions found for comment ${commentId}`); | |
| } | |
| - name: 'Reply to comment' | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0 | |
| with: | |
| github-token: ${{ secrets.PYANSYS_CI_BOT_TOKEN }} | |
| script: | | |
| // React negatively to the comment if the user is NOT a team member | |
| const CONTINUE = process.env.CONTINUE; | |
| if (CONTINUE == 'true') { | |
| core.info('User is a member of the PyMAPDL maintainers team.'); | |
| github.rest.reactions.createForIssueComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| comment_id: process.env.COMMENT_ID, | |
| content: '+1', | |
| }); | |
| } else { | |
| // React negatively to the comment if the user is NOT a team member | |
| core.warning('User is \u001b[1mNOT a member of the PyMAPDL maintainers team!'); | |
| github.rest.reactions.createForIssueComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| comment_id: process.env.COMMENT_ID, | |
| content: '-1', | |
| }); | |
| // Create a comment to notify the user about insufficient permissions | |
| github.rest.issues.createComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| issue_number: process.env.PR_NUMBER, | |
| body: `**:stop_sign: Not appropiated permissions! :stop_sign:** | |
| You are not a member of the PyMAPDL maintainers team. Please contact a team member to migrate this PR.`, | |
| }); | |
| core.exportVariable('CONTINUE', 'false'); | |
| } | |
| - name: Get pull request details | |
| if : ${{ env.CONTINUE == 'true' }} | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0 | |
| with: | |
| github-token: ${{ secrets.PYANSYS_CI_BOT_TOKEN }} | |
| script: | | |
| const { data } = await github.rest.pulls.get({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| pull_number: process.env.PR_NUMBER, | |
| }); | |
| if (!data || !data.head || !data.base) { | |
| throw new Error('Pull request data is incomplete'); | |
| } | |
| if (core.isDebug()) { | |
| core.debug(data); | |
| } | |
| if (data.head.repo.full_name === 'ansys/pymapdl') { | |
| core.warning('The PR is already in the main repository. No migration needed. Exiting...'); | |
| core.exportVariable('CONTINUE', 'false'); | |
| // React neutrally to the comment | |
| github.rest.reactions.createForIssueComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| comment_id: process.env.COMMENT_ID, | |
| content: 'confused', | |
| }); | |
| // Create a comment to notify the user about insufficient permissions | |
| github.rest.issues.createComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| issue_number: process.env.PR_NUMBER, | |
| body: `**:question: Wrong command? :confused:** | |
| You are trying to migrate a PR that is already in the main repository. No migration needed.`, | |
| }); | |
| return; | |
| } | |
| // Set the PR head and base branches as environment variables | |
| if (data && data.head && data.base) { | |
| core.exportVariable('PR_HEAD_BRANCH', data.head.ref); | |
| core.exportVariable('PR_HEAD_REPO', data.head.repo.full_name); | |
| core.exportVariable('PR_BASE_BRANCH', `migration/pr-${process.env.PR_NUMBER}`); | |
| core.exportVariable('PR_BASE_REPO', data.base.repo.full_name); | |
| } else { | |
| throw new Error('Failed to retrieve pull request details'); | |
| } | |
| - name: Checkout repo | |
| if : ${{ env.CONTINUE == 'true'}} | |
| uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 #v6.0.1 | |
| with: | |
| ref: main | |
| persist-credentials: true # zizmor: ignore[artipacked] | |
| - name: Clone head repo and checkout branch. Resolve conflicts if needed. | |
| if : ${{ env.CONTINUE == 'true' }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.PYANSYS_CI_BOT_TOKEN }} | |
| PYANSYS_CI_BOT_USERNAME: ${{ secrets.PYANSYS_CI_BOT_USERNAME }} | |
| PYANSYS_CI_BOT_EMAIL: ${{ secrets.PYANSYS_CI_BOT_EMAIL }} | |
| run: | | |
| echo "Setting up git configuration" | |
| git config --global user.name "${PYANSYS_CI_BOT_USERNAME}" | |
| git config --global user.email "${PYANSYS_CI_BOT_EMAIL}" | |
| git config pull.rebase true | |
| echo "\033[1;92m[INFO]Adding \"${PR_HEAD_REPO}\" as remote \033[0m" | |
| git remote add head_repo https://x-access-token:${GITHUB_TOKEN}@github.com/${PR_HEAD_REPO}.git | |
| echo "\033[1;92m[INFO]Fetching \"${PR_HEAD_BRANCH}\" branch from \"${PR_HEAD_REPO}\" \033[0m" | |
| git fetch head_repo ${PR_HEAD_BRANCH} | |
| echo "\033[1;92m[INFO] Checking out \"${PR_BASE_BRANCH}\" branch from \"${PR_HEAD_REPO}/${PR_HEAD_BRANCH}\" \033[0m" | |
| git checkout -b ${PR_BASE_BRANCH} head_repo/${PR_HEAD_BRANCH} | |
| echo "\033[1;92m[INFO] Pulling \"${PR_HEAD_BRANCH}\" from \"${PR_BASE_REPO}\" \033[0m" | |
| git pull head_repo ${PR_HEAD_BRANCH} | |
| echo "\033[1;92m[INFO] Merging 'main' branch into \"${PR_BASE_BRANCH}\" \033[0m" | |
| git merge origin/main | |
| # Check for merge conflicts | |
| CONFLICTS=$(git ls-files -u | wc -l) | |
| echo "Merge conflicts:" | |
| echo "${CONFLICTS}" | |
| if [[ "$CONFLICTS" -gt 0 && -n "${MODE}" ]]; then | |
| echo "::warning:: Conflicts trying to solve using mode ${MODE}." | |
| # Show conflicting files | |
| echo "Conflicting files:" | |
| git status | |
| echo "" | |
| # Resolve conflicts by taking "theirs" changes (optional, depending on your strategy) | |
| echo "Resolving conflicts by taking 'theirs' changes" | |
| git checkout ${MODE} . | |
| git add . | |
| # Verify if conflicts are resolved | |
| REMAINING_CONFLICTS=$(git ls-files -u | wc -l) | |
| if [ "$REMAINING_CONFLICTS" -gt 0 ]; then | |
| echo "::error:: Conflicts remain after resolution. Aborting." | |
| exit 1 | |
| fi | |
| # Continue the merge | |
| git merge --continue || { echo "::error:: Merge failed. Aborting."; exit 1; } | |
| else | |
| echo "No merge conflicts detected." | |
| fi | |
| echo "Pushing changes to \"${PR_BASE_REPO}\" repo" | |
| git push origin ${PR_BASE_BRANCH} --force-with-lease || git fetch --all && git push origin ${PR_BASE_BRANCH} --force-with-lease || { echo "::error:: Push failed. Aborting."; exit 1; } | |
| if [[ "${MODE}" == "--ours" ]]; then | |
| echo "Sync mode is 'ours'. Pushing to head_repo/${PR_HEAD_BRANCH} with 'ours' changes" | |
| git push origin head_repo/${PR_HEAD_BRANCH} --force-with-lease || { echo "::error:: Push to head_repo/${PR_HEAD_BRANCH} failed. Aborting."; exit 1; } | |
| fi | |
| - name: Opening PR if needed. | |
| if : ${{ env.CONTINUE == 'true' }} | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0 | |
| with: | |
| github-token: ${{ secrets.PYANSYS_CI_BOT_TOKEN }} | |
| script: | | |
| const prNumber = process.env.PR_NUMBER; | |
| const prHeadBranch = process.env.PR_HEAD_BRANCH; | |
| const prHeadRepo = process.env.PR_HEAD_REPO; | |
| const prBaseBranch = process.env.PR_BASE_BRANCH; | |
| const prBaseRepo = process.env.PR_BASE_REPO; | |
| const userTriggering = process.env.USER_TRIGGERING; | |
| const comment_id = process.env.COMMENT_ID; | |
| core.debug(`Listing existing PRs for branch ${prBaseBranch}...`); | |
| const existingPrs = await github.rest.pulls.list({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| base: `${prBaseBranch}`, | |
| state: 'open', | |
| }); | |
| core.debug(existingPrs.data); | |
| if (existingPrs.data.length > 0) { | |
| core.info(`PR already exists for branch ${prBaseBranch}. Skipping creation.`); | |
| core.exportVariable('COMMENT', 'false'); | |
| return; | |
| } | |
| // Retrieving the original PR details | |
| const { data: originalPR } = await github.rest.pulls.get({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| pull_number: prNumber, | |
| }); | |
| // https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#create-a-pull-request | |
| const newPrData = await github.rest.pulls.create({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| debug: true, | |
| head: prBaseBranch, // This is the branch we just created which becomes now the head of the PR, and the base (target) branch is the main branch. | |
| base: 'main', | |
| title: `migrated (PR ${prNumber}): ${originalPR.title}`, | |
| body: `This PR is a mirror pull request created from [${originalPR.title}](https://github.com/ansys/pymapdl/pull/${originalPR.number}) to allow the code to access PyMAPDL CICD secrets. | |
| Check the [original PR](https://github.com/ansys/pymapdl/pull/${originalPR.number}) made by @${originalPR.user.login} for more details. | |
| Closes #${prNumber} | |
| ## Original pull request | |
| ### ${originalPR.title} | |
| ${originalPR.body}`, | |
| }); | |
| core.info(`New PR created: ${newPrData.data.number}`); | |
| core.info(`Assinging PR to: ${userTriggering} and ${originalPR.user.login}`); | |
| await github.request('POST /repos/{owner}/{repo}/issues/{issue_number}/assignees', { | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| issue_number: newPrData.data.number, | |
| assignees: [ | |
| userTriggering, // The user who triggered the action | |
| originalPR.user.login, // The original PR author | |
| ], | |
| headers: { | |
| 'X-GitHub-Api-Version': '2022-11-28' | |
| } | |
| }) | |
| // React positively to the comment | |
| github.rest.reactions.createForIssueComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| comment_id: comment_id, | |
| content: 'rocket', | |
| }); | |
| // Create a comment to notify the user about success. | |
| github.rest.issues.createComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| issue_number: prNumber, | |
| body: `## :rocket: Migration completed! | |
| The pull request [#${newPrData.data.number}](${newPrData.data.html_url}) has been created successfully. | |
| Thank you @${originalPR.user.login} for your contribution! Please review the new PR and make any necessary changes. | |
| This PR will be closed by the admins soon. | |
| You can now start to work in the new pull request by cloning this pull request branch in a new repository by doing: | |
| ~~~terminal | |
| git clone -b ${prBaseBranch} --single-branch https://github.com/ansys/pymapdl.git | |
| ~~~ | |
| If you have [GitHub CLI](https://cli.github.com/) installed, you can also use the following command to check out the pull request: | |
| ~~~terminal | |
| gh pr checkout https://github.com/ansys/pymapdl/pull/${prNumber} | |
| ~~~ | |
| Thank you again for your contribution @${originalPR.user.login}! | |
| `, | |
| }); | |
| - name: Create comment about failed migration | |
| # Not creating more failure comments if the workflow is retried | |
| if: ${{ failure() && github.run_attempt == 1 && github.event_name != 'workflow_dispatch' }} | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0 | |
| with: | |
| github-token: ${{ secrets.PYANSYS_CI_BOT_TOKEN }} | |
| script: | | |
| // React positively to the comment | |
| commentId = process.env.COMMENT_ID; | |
| pr_number = process.env.PR_NUMBER; | |
| github.rest.reactions.createForIssueComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| comment_id: commentId, | |
| content: '-1', | |
| }); | |
| // Create a comment to notify the user about success. | |
| github.rest.issues.createComment({ | |
| owner: 'ansys', | |
| repo: 'pymapdl', | |
| issue_number: pr_number, | |
| body: `**:x: Error :x:** | |
| An error occurred while migrating or syncing the PR. Pinging @pymapdl-maintainers for assistance.`, | |
| }); | |