|
| 1 | +# ⚠️ DO NOT EDIT THIS FILE, IT IS GENERATED BY COPIER ⚠️ |
| 2 | +# Changes here will be lost on a future update. |
| 3 | +# See: https://github.com/ingadhoc/addons-repo-template |
| 4 | + |
| 5 | +name: Delete PR branch from fork and base repo |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request: |
| 9 | + types: [closed] |
| 10 | + pull_request_target: |
| 11 | + types: [closed] |
| 12 | + |
| 13 | + # Trigger manual |
| 14 | + workflow_dispatch: |
| 15 | + inputs: |
| 16 | + pull_request_number: |
| 17 | + description: 'Pull Request number to delete the branch' |
| 18 | + required: true |
| 19 | + type: number |
| 20 | + |
| 21 | +jobs: |
| 22 | + delete-branch: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + if: > |
| 25 | + github.repository_owner == 'ingadhoc' && |
| 26 | + ( |
| 27 | + (github.event_name == 'pull_request' && github.event.pull_request.merged == false && github.event.sender.login == 'roboadhoc') || |
| 28 | + (github.event_name == 'pull_request_target' && github.event.pull_request.merged == false && github.event.sender.login == 'roboadhoc') || |
| 29 | + (github.event_name == 'workflow_dispatch') || |
| 30 | + (github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success') |
| 31 | + ) |
| 32 | + steps: |
| 33 | + - name: Delete branch from base and fork repos |
| 34 | + uses: actions/github-script@v6 |
| 35 | + id: pr_data_fetcher |
| 36 | + with: |
| 37 | + script: | |
| 38 | + // Get PR information |
| 39 | + core.info('Fetching PR data and validating conditions...'); |
| 40 | +
|
| 41 | + // Debug info |
| 42 | + const eventName = context.eventName; |
| 43 | + core.info(`El nombre del evento es: ${eventName}`); |
| 44 | + core.info(JSON.stringify(context, null, 2)) |
| 45 | + // End Debug info |
| 46 | +
|
| 47 | + let repoOwner = context.repo.owner; |
| 48 | + let repoName = context.repo.repo; |
| 49 | + let pullRequest; |
| 50 | +
|
| 51 | + if (context.eventName === 'workflow_dispatch' || context.eventName === 'deployment_status') { |
| 52 | + let prNumber = 0; |
| 53 | + if (context.eventName === 'workflow_dispatch') { |
| 54 | + prNumber = context.payload.inputs.pull_request_number; |
| 55 | + core.info(`Manual trigger for PR #${prNumber}`); |
| 56 | + } |
| 57 | +
|
| 58 | + if (context.eventName === 'deployment_status') { |
| 59 | + prNumber = context.payload.deployment_status.description.split("#")[1].split(" ")[0]; |
| 60 | + core.info(`deployment_status trigger for PR #${prNumber}`); |
| 61 | + } |
| 62 | +
|
| 63 | + // Fetch the PR data using the number |
| 64 | + pullRequest = (await github.rest.pulls.get({ |
| 65 | + owner: repoOwner, |
| 66 | + repo: repoName, |
| 67 | + pull_number: prNumber, |
| 68 | + })).data; |
| 69 | +
|
| 70 | + core.info(JSON.stringify(pullRequest, null, 2)) |
| 71 | +
|
| 72 | + if (pullRequest.merged === true) { |
| 73 | + core.info(`PR #${prNumber} was merged. No action needed.`); |
| 74 | + core.setOutput('validation_passed', 'false'); |
| 75 | + return; |
| 76 | + } |
| 77 | +
|
| 78 | + // Fetch the PR timeline to find the 'closed' event |
| 79 | + const timeline = await github.rest.issues.listEventsForTimeline({ |
| 80 | + owner: repoOwner, |
| 81 | + repo: repoName, |
| 82 | + issue_number: prNumber, |
| 83 | + }); |
| 84 | +
|
| 85 | + // Find the 'closed' event in the timeline |
| 86 | + const closeEvent = timeline.data.find(event => event.event === 'closed'); |
| 87 | +
|
| 88 | + // Get the user who closed the PR from the event |
| 89 | + const closedByLogin = closeEvent && closeEvent.actor ? closeEvent.actor.login : null; |
| 90 | +
|
| 91 | + if (closedByLogin !== 'roboadhoc') { |
| 92 | + core.info(`PR #${prNumber} was not closed by 'roboadhoc' (${closedByLogin}). No action needed.`); |
| 93 | + core.setOutput('validation_passed', 'false'); |
| 94 | + return; |
| 95 | + } |
| 96 | +
|
| 97 | + } else if (['pull_request', 'pull_request_target'].includes(context.eventName)) { |
| 98 | + pullRequest = context.payload.pull_request; |
| 99 | + } else { |
| 100 | + core.setOutput('validation_passed', 'false'); |
| 101 | + core.error(`Unsupported event type: ${context.eventName}`); |
| 102 | + return; |
| 103 | + } |
| 104 | +
|
| 105 | + // Set outputs for subsequent steps |
| 106 | + core.setOutput('validation_passed', 'true'); |
| 107 | + core.setOutput('base_repo_owner', repoOwner); |
| 108 | + core.setOutput('base_repo_name', repoName); |
| 109 | + core.setOutput('base_branch_name', pullRequest.head.ref); |
| 110 | + core.setOutput('head_repo_full_name', pullRequest.head.repo.full_name); |
| 111 | + core.setOutput('head_repo_owner', pullRequest.head.repo.owner.login); |
| 112 | + core.setOutput('head_repo_name', pullRequest.head.repo.name); |
| 113 | + core.setOutput('is_fork', pullRequest.head.repo.full_name !== context.repo.owner + '/' + context.repo.repo); |
| 114 | +
|
| 115 | + - name: Delete branch from the base repository |
| 116 | + uses: actions/github-script@v6 |
| 117 | + if: ${{ steps.pr_data_fetcher.outputs.validation_passed == 'true' }} |
| 118 | + with: |
| 119 | + github-token: ${{ github.token }} |
| 120 | + script: | |
| 121 | + const baseBranchName = `${{ steps.pr_data_fetcher.outputs.base_branch_name }}`; |
| 122 | + const baseRepoOwner = `${{ steps.pr_data_fetcher.outputs.base_repo_owner }}`; |
| 123 | + const baseRepoName = `${{ steps.pr_data_fetcher.outputs.base_repo_name }}`; |
| 124 | + try { |
| 125 | + core.info(`Attempting to delete branch '${baseBranchName}' from base repo '${baseRepoOwner}/${baseRepoName}'`); |
| 126 | + await github.rest.git.deleteRef({ |
| 127 | + owner: baseRepoOwner, |
| 128 | + repo: baseRepoName, |
| 129 | + ref: `heads/${baseBranchName}`, |
| 130 | + }); |
| 131 | + core.info(`Branch '${baseBranchName}' deleted from base repo successfully.`); |
| 132 | + } catch (error) { |
| 133 | + if (error.status === 422) { |
| 134 | + core.info(`Branch '${baseBranchName}' in base repo already deleted. No action needed.`); |
| 135 | + } else { |
| 136 | + console.error(`Error deleting branch '${baseBranchName}' from base repo: ${error.message}`); |
| 137 | + } |
| 138 | + } |
| 139 | +
|
| 140 | + - name: Delete branch from the fork repository (adhoc-dev) |
| 141 | + if: ${{ steps.pr_data_fetcher.outputs.validation_passed == 'true' }} |
| 142 | + uses: actions/github-script@v6 |
| 143 | + with: |
| 144 | + github-token: ${{ secrets.EXTERNAL_REPO_TOKEN_CLEANER_ADHOC_DEV || github.token }} |
| 145 | + script: | |
| 146 | + const baseBranchName = `${{ steps.pr_data_fetcher.outputs.base_branch_name }}`; |
| 147 | + const headRepoOwner = 'adhoc-dev'; |
| 148 | + const headRepoName = `${{ steps.pr_data_fetcher.outputs.head_repo_name }}`; |
| 149 | +
|
| 150 | + try { |
| 151 | + core.info(`PR comes from a fork. Attempting to delete branch from fork repo '${headRepoOwner}/${headRepoName}'`); |
| 152 | + await github.rest.git.deleteRef({ |
| 153 | + owner: headRepoOwner, |
| 154 | + repo: headRepoName, |
| 155 | + ref: `heads/${baseBranchName}`, |
| 156 | + }); |
| 157 | + core.info(`Branch '${baseBranchName}' deleted from fork repo successfully.`); |
| 158 | + } catch (error) { |
| 159 | + if (error.status === 422) { |
| 160 | + core.info(`Branch '${baseBranchName}' in fork repo already deleted. No action needed.`); |
| 161 | + } else { |
| 162 | + console.error(`Error deleting branch '${baseBranchName}' from fork repo: ${error.message}`); |
| 163 | + } |
| 164 | + } |
0 commit comments