forked from Romanitho/Winget-AutoUpdate
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGitFlow_Check-pull-request-source-branch.yml
More file actions
179 lines (156 loc) · 6.59 KB
/
GitFlow_Check-pull-request-source-branch.yml
File metadata and controls
179 lines (156 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# This workflow enforces GitFlow branch patterns by:
# - Ensuring only hotfix/* or release/* branches can target main
# - Adding labels and comments to non-compliant PRs
# - Automatically cleaning up when PRs are updated to comply
name: GitFlow | Check PR Branch Pattern
on:
pull_request_target:
types:
- opened
- reopened
- synchronize
- edited
# Add explicit permissions
permissions:
pull-requests: write
issues: write
contents: read
env:
MAIN_BRANCH: "main"
DEVELOP_BRANCH: "develop"
VALID_PATTERNS: "^(release|hotfix)/"
LABEL_NAME: "invalid-branch"
ERROR_MESSAGE_IDENTIFIER: "Invalid Branch Pattern for main Branch"
jobs:
check_branch:
name: Check branch pattern
runs-on: ubuntu-latest
steps:
# Step 1: Check branch pattern
- name: Check branch pattern
id: branch_check
env:
HEAD_REF: ${{ github.head_ref }}
BASE_REF: ${{ github.base_ref }}
run: |
# Les variables sont maintenant définies dans l'environnement plutôt qu'injectées directement
MAIN_BRANCH="${{ env.MAIN_BRANCH }}"
VALID_PATTERN="${{ env.VALID_PATTERNS }}"
echo "Checking PR from '$HEAD_REF' to '$BASE_REF'"
# Perform the validation
if [[ "$BASE_REF" == "$MAIN_BRANCH" ]]; then
if [[ ! "$HEAD_REF" =~ $VALID_PATTERN ]]; then
echo "::error::❌ Invalid branch! PRs to main must come from hotfix/* or release/* branches. Please target the develop branch instead."
exit 1
else
echo "::notice::✅ Branch pattern is valid: '$HEAD_REF' → '$MAIN_BRANCH'"
fi
else
echo "::notice::✅ Not targeting main branch, no pattern restrictions apply."
fi
# Step 2: If the branch pattern is invalid, add a label and comment to the PR
- name: Handle invalid branch (label + comment)
if: failure()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
const issue_number = context.payload.pull_request.number;
const label = process.env.LABEL_NAME;
const messageIdentifier = process.env.ERROR_MESSAGE_IDENTIFIER;
// Escape special characters in the message identifier for safer comparisons
const escapedMessageIdentifier = messageIdentifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const message = `❌ **${messageIdentifier}**
According to our GitFlow workflow:
- Pull requests to the \`main\` branch are only allowed from \`hotfix/*\` or \`release/*\` branches
- Regular feature development and other changes should target the \`develop\` branch
📝 **Action required:** Please update your PR to target the \`develop\` branch instead.
For more details about our contribution workflow, please refer to our [CONTRIBUTING.md](https://github.com/${owner}/${repo}/blob/main/CONTRIBUTING.md) guide.`;
// First step: Always apply the label
console.log("Adding invalid-branch label to PR");
try {
await github.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: [label]
});
} catch (e) {
// In case label already exists or other error
console.log(`Note: Could not add label: ${e.message}`);
}
// Second step: Add comment if it doesn't exist
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number
});
// Use regex test instead of includes for safer comparison
const commentExists = comments.some(comment =>
comment.body && new RegExp(escapedMessageIdentifier).test(comment.body)
);
if (!commentExists) {
console.log("Adding comment to PR");
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: message
});
} else {
console.log("Comment already exists, skipping");
}
# Step 3: If the branch pattern is corrected, remove label and comment
- name: Clean up if branch is corrected
if: success()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
const issue_number = context.payload.pull_request.number;
const label = process.env.LABEL_NAME;
const messageIdentifier = process.env.ERROR_MESSAGE_IDENTIFIER;
// Escape special characters in the message identifier
const escapedMessageIdentifier = messageIdentifier.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const messageRegex = new RegExp(escapedMessageIdentifier);
try {
// Check if the label is present and remove it
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number
});
if (labels.some(l => l.name === label)) {
console.log("Removing invalid-branch label from PR");
await github.rest.issues.removeLabel({
owner,
repo,
issue_number,
name: label
});
} else {
console.log("No label to remove");
}
// Check existing comments and remove any invalid branch comments
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number
});
// Find and delete any invalid branch comment
for (const comment of comments) {
// Use regex test instead of includes for safer comparison
if (comment.body && messageRegex.test(comment.body)) {
console.log(`Deleting comment ID: ${comment.id}`);
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id
});
}
}
} catch (error) {
console.log(`Error in cleanup: ${error}`);
}