Skip to content

Commit d836ab6

Browse files
committed
feature: test issue rule
1 parent cf4e5e3 commit d836ab6

1 file changed

Lines changed: 137 additions & 37 deletions

File tree

.github/workflows/issue_label_rule.yml

Lines changed: 137 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ jobs:
1919
run: |
2020
git fetch --all
2121
22+
- name: Get Latest Tag or Default to Master
23+
id: get_latest_tag
24+
run: |
25+
# Get the latest tag (if any)
26+
LATEST_TAG=$(git tag --sort=-creatordate | head -n 1)
27+
28+
# Output the result
29+
echo "Latest tag: $LATEST_TAG"
30+
echo "LATEST_TAG=$LATEST_TAG" >> $GITHUB_ENV
31+
2232
- name: Check Issue Title Format
2333
uses: actions/github-script@v6
2434
with:
@@ -74,70 +84,163 @@ jobs:
7484
REPO_NAME="${{ github.event.repository.name }}"
7585
GITHUB_TOKEN="${{ secrets.GITHUB_TOKEN }}"
7686
77-
# Fetch the labels for the issue
7887
RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
7988
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$ISSUE_NUMBER/labels")
8089
81-
# Extract label names and store them in an array
8290
LABELS=($(echo "$RESPONSE" | jq -r '.[].name'))
8391
84-
# Display the labels
8592
echo "Labels: ${LABELS[@]}"
8693
87-
# Store labels as an environment variable for use in later steps
8894
echo "labels=${LABELS[@]}" >> $GITHUB_ENV
8995
90-
# Determinar el tipo de rama según las etiquetas
91-
BRANCH_TYPE="feature" # Por defecto, tipo de rama "feature"
96+
HOTFIX_TYPE="0"
97+
BRANCH_TYPE="feature"
9298
for LABEL in "${LABELS[@]}"; do
9399
if [[ "$LABEL" == "bug" ]]; then
94100
BRANCH_TYPE="bugfix"
95-
break
96101
elif [[ "$LABEL" == "hotfix" ]]; then
97-
BRANCH_TYPE="hotfix"
98-
break
102+
BRANCH_TYPE="bugfix"
103+
HOTFIX_TYPE="1"
99104
fi
100105
done
101106
107+
echo "HOTFIX_TYPE=$HOTFIX_TYPE" >> $GITHUB_ENV
102108
echo "BRANCH_TYPE=$BRANCH_TYPE" >> $GITHUB_ENV
103109
110+
- name: Link Hotfix Branch to Issue in Development Section
111+
if: ${{ env.labels && contains(env.labels, 'in progress') && env.LATEST_TAG != '' && env.HOTFIX_TYPE == '1' }}
112+
uses: actions/github-script@v7
113+
with:
114+
github-token: ${{ secrets.GITHUB_TOKEN }}
115+
script: |
116+
const core = require('@actions/core');
117+
118+
const tag = '${{ env.LATEST_TAG }}';
119+
console.log(`Tag: ${tag}`);
120+
121+
const incrementHotfixVersion = (version) => {
122+
const parts = version.split('.').map(Number);
123+
parts[parts.length - 1] += 1;
124+
return parts.join('.');
125+
};
126+
127+
const newVersion = incrementHotfixVersion(tag);
128+
129+
const baseBranchName = `tags/${tag}`;
130+
const newBranchName = `hotfix/${newVersion}`;
131+
132+
console.log(`Tag branch: ${baseBranchName}`);
133+
console.log(`Hotfix branch: ${newBranchName}`);
134+
135+
const repoUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/tree/${baseBranchName}`;
136+
const newRepoUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/tree/${newBranchName}`;
137+
138+
const commentBody = `🔥 Creating [${newBranchName}](${newRepoUrl}) from [${baseBranchName}](${repoUrl}).`;
139+
await github.rest.issues.createComment({
140+
owner: context.repo.owner,
141+
repo: context.repo.repo,
142+
issue_number: issueNumber,
143+
body: commentBody,
144+
});
145+
146+
const { repository } = await github.graphql(`
147+
query($repo: String!, $owner: String!, $issueNumber: Int!) {
148+
repository(name: $repo, owner: $owner) {
149+
id
150+
issue(number: $issueNumber) {
151+
id
152+
}
153+
ref(qualifiedName: "refs/${baseBranchName}") {
154+
target {
155+
... on Commit {
156+
oid
157+
}
158+
}
159+
}
160+
}
161+
}
162+
`, {
163+
repo: context.repo.repo,
164+
owner: context.repo.owner,
165+
issueNumber: parseInt(issueNumber, 10)
166+
});
167+
168+
const repositoryId = repository.id;
169+
const issueId = repository.issue.id;
170+
const branchOid = repository.ref.target.oid;
171+
172+
console.log(`Linking branch "${newBranchName}" (oid: ${branchOid}) to issue #${issueNumber}`);
173+
174+
const result = await github.graphql(`
175+
mutation($issueId: ID!, $name: String!, $repositoryId: ID!, $oid: GitObjectID!) {
176+
createLinkedBranch(input: {
177+
issueId: $issueId,
178+
name: $name,
179+
repositoryId: $repositoryId,
180+
oid: $oid,
181+
}) {
182+
linkedBranch {
183+
id
184+
ref {
185+
name
186+
}
187+
}
188+
}
189+
}
190+
`, {
191+
issueId: issueId,
192+
name: `/${newBranchName}`,
193+
repositoryId: repositoryId,
194+
oid: branchOid,
195+
});
196+
197+
console.log(`Hotfix branch successfully linked to issue: ${JSON.stringify(result)}`);
198+
core.exportVariable('HOTFIX_BRANCH', newBranchName);
199+
console.log(`env var HOTFIX_BRANCH exported: ${newBranchName}`);
200+
104201
- name: Link Branch to Issue in Development Section
105202
if: ${{ env.labels && contains(env.labels, 'in progress') }}
106203
uses: actions/github-script@v7
107204
with:
108205
github-token: ${{ secrets.GITHUB_TOKEN }}
109206
script: |
207+
const isHotfix = '${{ env.HOTFIX_TYPE }}' === '1';
208+
const hotfixBranch = '${{ env.HOTFIX_BRANCH }}';
110209
const issueNumber = '${{ env.ISSUE_NUMBER }}';
111210
const sanitizedTitle = '${{ env.SANITIZED_TITLE }}';
112211
const branchType = '${{ env.BRANCH_TYPE }}';
113212
const newBranchName = `${branchType}/${issueNumber}-${sanitizedTitle}`;
114213
115-
const branchTypes = ["feature", "bugfix", "hotfix"];
214+
const branchTypes = ["feature", "bugfix"];
116215
117216
let baseBranchName = 'master';
118217
119-
console.log(`Searching for branches related to issue #${issueNumber}...`);
120-
121-
const { data } = await github.rest.repos.listBranches({
122-
owner: context.repo.owner,
123-
repo: context.repo.repo,
124-
});
125-
126-
for (const type of branchTypes) {
127-
const prefix = `${type}/${issueNumber}-`;
128-
129-
try {
130-
const matchingBranch = data.find(branch => branch.name.indexOf(prefix) > -1);
131-
132-
if (matchingBranch) {
133-
baseBranchName = matchingBranch.name;
134-
console.log(`Found branch: ${baseBranchName}`);
135-
break;
218+
if (!isHotfix) {
219+
console.log(`Searching for branches related to issue #${issueNumber}...`);
220+
221+
const { data } = await github.rest.repos.listBranches({
222+
owner: context.repo.owner,
223+
repo: context.repo.repo,
224+
});
225+
226+
for (const type of branchTypes) {
227+
const prefix = `${type}/${issueNumber}-`;
228+
229+
try {
230+
const matchingBranch = data.find(branch => branch.name.indexOf(prefix) > -1);
231+
232+
if (matchingBranch) {
233+
baseBranchName = matchingBranch.name;
234+
console.log(`Found branch: ${baseBranchName}`);
235+
break;
236+
}
237+
} catch (error) {
238+
console.error(`Error while listing branches: ${error.message}`);
239+
throw error;
136240
}
137-
} catch (error) {
138-
console.error(`Error while listing branches: ${error.message}`);
139-
throw error;
140241
}
242+
} else {
243+
baseBranchName = hotfixBranch;
141244
}
142245

143246
console.log(`Base branch: ${baseBranchName}`);
@@ -180,11 +283,7 @@ jobs:
180283
const branchOid = repository.ref.target.oid;
181284
182285
console.log(`Linking branch "${newBranchName}" (oid: ${branchOid}) to issue #${issueNumber}`);
183-
184-
// Generar un ID único para clientMutationId (usando la fecha actual o un UUID)
185-
const clientMutationId = new Date().toISOString(); // O usa cualquier otra forma dinámica, como UUID
186-
187-
// Ejecutar la mutación para vincular la rama
286+
188287
const result = await github.graphql(`
189288
mutation($issueId: ID!, $name: String!, $repositoryId: ID!, $oid: GitObjectID!) {
190289
createLinkedBranch(input: {
@@ -210,6 +309,7 @@ jobs:
210309

211310
console.log(`Branch successfully linked to issue: ${JSON.stringify(result)}`);
212311

312+
213313
- name: Comment on Issue with Branch Link
214314
if: ${{ env.labels && contains(env.labels, 'in progress') }}
215315
uses: actions/github-script@v7
@@ -243,7 +343,7 @@ jobs:
243343
const sanitizedTitle = '${{ env.SANITIZED_TITLE }}';
244344
const branchType = '${{ env.BRANCH_TYPE }}';
245345
246-
const branchTypes = ["feature", "bugfix", "hotfix"];
346+
const branchTypes = ["feature", "bugfix"];
247347
248348
const { data } = await github.rest.repos.listBranches({
249349
owner: context.repo.owner,
@@ -314,7 +414,7 @@ jobs:
314414
const sanitizedTitle = '${{ env.SANITIZED_TITLE }}';
315415
const branchType = '${{ env.BRANCH_TYPE }}';
316416
317-
const branchTypes = ["feature", "bugfix", "hotfix"];
417+
const branchTypes = ["feature", "bugfix"];
318418
319419
const { data } = await github.rest.repos.listBranches({
320420
owner: context.repo.owner,

0 commit comments

Comments
 (0)