Skip to content

Commit 383e1a2

Browse files
authored
Create cleaner.yml
1 parent 97b6d52 commit 383e1a2

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

.github/workflows/cleaner.yaml

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

0 commit comments

Comments
 (0)