|
| 1 | +name: Rename package recipe |
| 2 | +on: |
| 3 | + issue_comment: |
| 4 | + types: [created] |
| 5 | + |
| 6 | +jobs: |
| 7 | + rename_package: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + # Only run if comment is on a PR and if it contains the magic keywords |
| 10 | + # Ensure the comment starts with "@delphis-bot rename" and is made by TCLamnidis |
| 11 | + if: > |
| 12 | + contains(github.event.comment.html_url, '/pull/') && |
| 13 | + startsWith(github.event.comment.body, '@delphis-bot rename') && |
| 14 | + github.event.repository.full_name == 'poseidon-framework/minotaur-recipes' && |
| 15 | + github.event.comment.user.login == 'TCLamnidis' |
| 16 | +
|
| 17 | + steps: |
| 18 | + # indication that the process has started |
| 19 | + - name: React on comment |
| 20 | + uses: peter-evans/create-or-update-comment@v2 |
| 21 | + with: |
| 22 | + comment-id: ${{ github.event.comment.id }} |
| 23 | + reactions: rocket |
| 24 | + token: ${{ secrets.delphis_bot_org_token }} |
| 25 | + |
| 26 | + # Use the @delphis-bot token to check out so we can push later |
| 27 | + - uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + token: ${{ secrets.delphis_bot_org_token }} |
| 30 | + |
| 31 | + # Action runs on the issue comment, so we don't get the PR by default |
| 32 | + # Use the gh cli to check out the PR |
| 33 | + - name: Checkout Pull Request |
| 34 | + run: gh pr checkout ${{ github.event.issue.number }} |
| 35 | + env: |
| 36 | + GITHUB_TOKEN: ${{ secrets.delphis_bot_org_token }} |
| 37 | + |
| 38 | + - name: Get package name from comment |
| 39 | + id: get_package_name |
| 40 | + run: | |
| 41 | + if [[ "${{ github.event.comment.body }}" =~ @delphis-bot\ rename\ ([a-zA-Z0-9_-]+)\ ([a-zA-Z0-9_-]+) ]]; then |
| 42 | + echo "target_package_name=${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT |
| 43 | + echo "destination_package_name=${BASH_REMATCH[2]}" >> $GITHUB_OUTPUT |
| 44 | + else |
| 45 | + echo "No package names found in comment." |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | +
|
| 49 | + - name: Check if target package exists |
| 50 | + id: target_check |
| 51 | + run: | |
| 52 | + target_exists='true' |
| 53 | + if [ ! -d "./packages/${{ steps.get_package_name.outputs.target_package_name }}" ]; then |
| 54 | + echo "Target package does not exist." |
| 55 | + target_exists='false' |
| 56 | + fi |
| 57 | + echo "target_exists=${target_exists}" >> $GITHUB_OUTPUT |
| 58 | +
|
| 59 | +
|
| 60 | + - name: Check if destination package exists |
| 61 | + id: destination_check |
| 62 | + run: | |
| 63 | + destination_free='true' |
| 64 | + if [ -d "./packages/${{ steps.get_package_name.outputs.destination_package_name }}" ]; then |
| 65 | + echo "Destination package already exists." |
| 66 | + destination_free='false' |
| 67 | + fi |
| 68 | + echo "destination_free=${destination_free}" >> $GITHUB_OUTPUT |
| 69 | +
|
| 70 | + ## DEBUG |
| 71 | + # - name: Post debug comment |
| 72 | + # uses: peter-evans/create-or-update-comment@v2 |
| 73 | + # with: |
| 74 | + # issue-number: ${{ github.event.issue.number }} |
| 75 | + # token: ${{ secrets.delphis_bot_org_token }} |
| 76 | + # body: | |
| 77 | + # Debugging information: |
| 78 | + # - Target package name: `${{ steps.get_package_name.outputs.target_package_name }}` |
| 79 | + # - Destination package name: `${{ steps.get_package_name.outputs.destination_package_name }}` |
| 80 | + # - Target package exists: `${{ steps.target_check.outputs.target_exists }}` |
| 81 | + # - Destination package exists: `${{ steps.destination_check.outputs.destination_free }}` |
| 82 | + |
| 83 | + - name: Post error comment |
| 84 | + if: steps.target_check.outputs.target_exists == 'false' || steps.destination_check.outputs.destination_free == 'false' |
| 85 | + uses: peter-evans/create-or-update-comment@v2 |
| 86 | + with: |
| 87 | + issue-number: ${{ github.event.issue.number }} |
| 88 | + token: ${{ secrets.delphis_bot_org_token }} |
| 89 | + body: | |
| 90 | + Error: Cannot rename package. |
| 91 | + This can be caused by one of the following reasons: |
| 92 | + - Target package `${{ steps.get_package_name.outputs.target_package_name }}` does not exist. |
| 93 | + - Destination package `${{ steps.get_package_name.outputs.destination_package_name }}` already exists. |
| 94 | +
|
| 95 | + - name: Rename package directory |
| 96 | + if: > |
| 97 | + steps.get_package_name.outputs.target_package_name != steps.get_package_name.outputs.destination_package_name && |
| 98 | + steps.target_check.outputs.target_exists == 'true' && |
| 99 | + steps.destination_check.outputs.destination_free == 'true' |
| 100 | + id: rename_package |
| 101 | + run: | |
| 102 | + mv "./packages/${{ steps.get_package_name.outputs.target_package_name }}" "./packages/${{ steps.get_package_name.outputs.destination_package_name }}" |
| 103 | + for file in ./packages/${{ steps.get_package_name.outputs.destination_package_name }}/${{ steps.get_package_name.outputs.target_package_name }}*; do |
| 104 | + mv "${file}" "${file/${{ steps.get_package_name.outputs.target_package_name }}/${{ steps.get_package_name.outputs.destination_package_name }}}" |
| 105 | + done |
| 106 | +
|
| 107 | + - name: Commit & push changes |
| 108 | + id: commit_changes |
| 109 | + if: steps.rename_package.outcome == 'success' |
| 110 | + run: | |
| 111 | + git config user.email "delphis-bot@poseidon-adna.org" |
| 112 | + git config user.name "delphis-bot" |
| 113 | + git config push.default upstream |
| 114 | + git add . |
| 115 | + git status |
| 116 | + git commit -m "[automated] Rename recipe from ${{ steps.get_package_name.outputs.target_package_name }} to ${{ steps.get_package_name.outputs.destination_package_name }}" |
| 117 | + git push |
0 commit comments