forked from aeroo/aeroo_reports
-
Notifications
You must be signed in to change notification settings - Fork 54
Create cleaner.yml #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| # ⚠️ DO NOT EDIT THIS FILE, IT IS GENERATED BY COPIER ⚠️ | ||
| # Changes here will be lost on a future update. | ||
| # See: https://github.com/ingadhoc/addons-repo-template | ||
|
|
||
| name: Delete PR branch from fork and base repo | ||
|
|
||
| on: | ||
|
|
||
| deployment_status: | ||
|
|
||
| # Trigger manual | ||
| workflow_dispatch: | ||
| inputs: | ||
| pull_request_number: | ||
| description: 'Pull Request number to delete the branch' | ||
| required: true | ||
| type: number | ||
|
|
||
| jobs: | ||
| delete-branch: | ||
| runs-on: ubuntu-latest | ||
| if: > | ||
| github.repository_owner == 'ingadhoc' && | ||
| ( | ||
| (github.event_name == 'workflow_dispatch') || | ||
| (github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success') | ||
| ) | ||
| steps: | ||
| - name: Delete branch from base and fork repos | ||
| uses: actions/github-script@v6 | ||
| id: pr_data_fetcher | ||
| with: | ||
| script: | | ||
| // Get PR information | ||
| core.info('Fetching PR data and validating conditions...'); | ||
|
|
||
| // Debug info | ||
| const eventName = context.eventName; | ||
| core.info(`El nombre del evento es: ${eventName}`); | ||
| core.info(JSON.stringify(context, null, 2)) | ||
| // End Debug info | ||
|
|
||
| let repoOwner = context.repo.owner; | ||
| let repoName = context.repo.repo; | ||
| let pullRequest; | ||
|
|
||
| if (context.eventName === 'workflow_dispatch' || context.eventName === 'deployment_status') { | ||
| let prNumber = 0; | ||
| if (context.eventName === 'workflow_dispatch') { | ||
| prNumber = context.payload.inputs.pull_request_number; | ||
| core.info(`Manual trigger for PR #${prNumber}`); | ||
| } | ||
|
|
||
| if (context.eventName === 'deployment_status') { | ||
| prNumber = context.payload.deployment_status.description.split("#")[1].split(" ")[0]; | ||
| core.info(`deployment_status trigger for PR #${prNumber}`); | ||
| } | ||
|
|
||
| // Fetch the PR data using the number | ||
| pullRequest = (await github.rest.pulls.get({ | ||
| owner: repoOwner, | ||
| repo: repoName, | ||
| pull_number: prNumber, | ||
| })).data; | ||
|
|
||
| core.info(JSON.stringify(pullRequest, null, 2)) | ||
|
|
||
| if (pullRequest.merged === true) { | ||
| core.info(`PR #${prNumber} was merged. No action needed.`); | ||
| core.setOutput('validation_passed', 'false'); | ||
| return; | ||
| } | ||
|
|
||
| // Fetch the PR timeline to find the 'closed' event | ||
| const timeline = await github.rest.issues.listEventsForTimeline({ | ||
| owner: repoOwner, | ||
| repo: repoName, | ||
| issue_number: prNumber, | ||
| }); | ||
|
|
||
| // Find the 'closed' event in the timeline | ||
| const closeEvent = timeline.data.find(event => event.event === 'closed'); | ||
|
|
||
| // Get the user who closed the PR from the event | ||
| const closedByLogin = closeEvent && closeEvent.actor ? closeEvent.actor.login : null; | ||
|
|
||
| if (closedByLogin !== 'roboadhoc') { | ||
| core.info(`PR #${prNumber} was not closed by 'roboadhoc' (${closedByLogin}). No action needed.`); | ||
| core.setOutput('validation_passed', 'false'); | ||
| return; | ||
| } | ||
|
|
||
| } else { | ||
| core.setOutput('validation_passed', 'false'); | ||
| core.error(`Unsupported event type: ${context.eventName}`); | ||
| return; | ||
| } | ||
|
|
||
| // Set outputs for subsequent steps | ||
| core.setOutput('validation_passed', 'true'); | ||
| core.setOutput('base_repo_owner', repoOwner); | ||
| core.setOutput('base_repo_name', repoName); | ||
| core.setOutput('base_branch_name', pullRequest.head.ref); | ||
| core.setOutput('head_repo_full_name', pullRequest.head.repo.full_name); | ||
| core.setOutput('head_repo_owner', pullRequest.head.repo.owner.login); | ||
| core.setOutput('head_repo_name', pullRequest.head.repo.name); | ||
| core.setOutput('is_fork', pullRequest.head.repo.full_name !== context.repo.owner + '/' + context.repo.repo); | ||
|
|
||
| - name: Delete branch from the base repository | ||
| uses: actions/github-script@v6 | ||
| if: ${{ steps.pr_data_fetcher.outputs.validation_passed == 'true' }} | ||
| with: | ||
| github-token: ${{ github.token }} | ||
| script: | | ||
| const baseBranchName = `${{ steps.pr_data_fetcher.outputs.base_branch_name }}`; | ||
| const baseRepoOwner = `${{ steps.pr_data_fetcher.outputs.base_repo_owner }}`; | ||
| const baseRepoName = `${{ steps.pr_data_fetcher.outputs.base_repo_name }}`; | ||
| try { | ||
| core.info(`Attempting to delete branch '${baseBranchName}' from base repo '${baseRepoOwner}/${baseRepoName}'`); | ||
| await github.rest.git.deleteRef({ | ||
| owner: baseRepoOwner, | ||
| repo: baseRepoName, | ||
| ref: `heads/${baseBranchName}`, | ||
| }); | ||
| core.info(`Branch '${baseBranchName}' deleted from base repo successfully.`); | ||
| } catch (error) { | ||
| if (error.status === 422) { | ||
| core.info(`Branch '${baseBranchName}' in base repo already deleted. No action needed.`); | ||
| } else { | ||
| console.error(`Error deleting branch '${baseBranchName}' from base repo: ${error.message}`); | ||
| } | ||
| } | ||
|
|
||
| - name: Delete branch from the fork repository (adhoc-dev) | ||
| if: ${{ steps.pr_data_fetcher.outputs.validation_passed == 'true' }} | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| github-token: ${{ secrets.EXTERNAL_REPO_TOKEN_CLEANER_ADHOC_DEV || github.token }} | ||
| script: | | ||
| const baseBranchName = `${{ steps.pr_data_fetcher.outputs.base_branch_name }}`; | ||
| const headRepoOwner = 'adhoc-dev'; | ||
| const headRepoName = `${{ steps.pr_data_fetcher.outputs.head_repo_name }}`; | ||
|
|
||
| try { | ||
| core.info(`PR comes from a fork. Attempting to delete branch from fork repo '${headRepoOwner}/${headRepoName}'`); | ||
| await github.rest.git.deleteRef({ | ||
| owner: headRepoOwner, | ||
| repo: headRepoName, | ||
| ref: `heads/${baseBranchName}`, | ||
| }); | ||
| core.info(`Branch '${baseBranchName}' deleted from fork repo successfully.`); | ||
| } catch (error) { | ||
| if (error.status === 422) { | ||
| core.info(`Branch '${baseBranchName}' in fork repo already deleted. No action needed.`); | ||
| } else { | ||
| console.error(`Error deleting branch '${baseBranchName}' from fork repo: ${error.message}`); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium
Copilot Autofix
AI 5 months ago
To fix the problem, add a
permissionsblock to the workflow to restrict the GITHUB_TOKEN permissions to the minimum required. In this case, the workflow deletes branches, which requirescontents: writepermission. No other permissions are needed for the described actions. The best way to fix this is to add apermissionsblock at the root level of the workflow (beforejobs:), so it applies to all jobs unless overridden. Edit.github/workflows/cleaner.ymlto insert:after the
name:and before theon:block. No additional imports or definitions are needed.