-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (145 loc) · 5.25 KB
/
index.js
File metadata and controls
170 lines (145 loc) · 5.25 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
const core = require('@actions/core');
const github = require('@actions/github');
async function run() {
try {
// Get inputs
const token = core.getInput('github-token', { required: true });
const smallThreshold = parseInt(core.getInput('small-threshold'));
const mediumThreshold = parseInt(core.getInput('medium-threshold'));
const largeThreshold = parseInt(core.getInput('large-threshold'));
const commentOnLarge = core.getInput('comment-on-large') === 'true';
// Get PR context
const context = github.context;
if (!context.payload.pull_request) {
core.setFailed('This action can only be run on pull_request events');
return;
}
const pullRequest = context.payload.pull_request;
const octokit = github.getOctokit(token);
// Get PR details
const { data: prData } = await octokit.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullRequest.number,
});
// Calculate total lines changed
const additions = prData.additions;
const deletions = prData.deletions;
const totalChanges = additions + deletions;
core.info(`PR #${pullRequest.number}: +${additions} -${deletions} (total: ${totalChanges} lines)`);
// Determine size label
let sizeLabel;
if (totalChanges <= smallThreshold) {
sizeLabel = 'small';
} else if (totalChanges <= mediumThreshold) {
sizeLabel = 'medium';
} else if (totalChanges <= largeThreshold) {
sizeLabel = 'large';
} else {
sizeLabel = 'extra-large';
}
core.info(`Determined size: ${sizeLabel}`);
// Get existing labels
const existingLabels = pullRequest.labels.map(label => label.name);
const sizeLabels = ['small', 'medium', 'large', 'extra-large'];
// Remove old size labels
const labelsToRemove = existingLabels.filter(label =>
sizeLabels.includes(label) && label !== sizeLabel
);
for (const label of labelsToRemove) {
try {
await octokit.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
name: label,
});
core.info(`Removed label: ${label}`);
} catch (error) {
core.warning(`Failed to remove label ${label}: ${error.message}`);
}
}
// Add new size label if not already present
if (!existingLabels.includes(sizeLabel)) {
// First, ensure the label exists in the repository
try {
await octokit.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: sizeLabel,
});
} catch (error) {
// Label doesn't exist, create it
const labelColors = {
'small': '00ff00',
'medium': 'ffff00',
'large': 'ff9900',
'extra-large': 'ff0000'
};
try {
await octokit.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: sizeLabel,
color: labelColors[sizeLabel],
description: `PR size: ${sizeLabel}`,
});
core.info(`Created label: ${sizeLabel}`);
} catch (createError) {
core.warning(`Failed to create label ${sizeLabel}: ${createError.message}`);
}
}
// Add the label to the PR
await octokit.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
labels: [sizeLabel],
});
core.info(`Added label: ${sizeLabel}`);
}
// Comment on large PRs if enabled
if (commentOnLarge && (sizeLabel === 'large' || sizeLabel === 'extra-large')) {
const commentBody = `## ⚠️ Large Pull Request Detected
This PR has **${totalChanges} lines changed** (+${additions}/-${deletions}), which is marked as **${sizeLabel}**.
### Why does PR size matter?
- Smaller PRs are easier to review
- Faster feedback cycles
- Reduced risk of bugs
- Better context for reviewers
### Suggestions:
- Consider breaking this PR into smaller, focused changes
- Each PR should ideally address a single concern
- Aim for PRs under ${mediumThreshold} lines when possible
Thank you for your contribution! 🚀`;
// Check if we already commented
const { data: comments } = await octokit.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Large Pull Request Detected')
);
if (!botComment) {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body: commentBody,
});
core.info('Posted comment about large PR');
} else {
core.info('Comment about large PR already exists, skipping');
}
}
// Set outputs
core.setOutput('size-label', sizeLabel);
core.setOutput('lines-changed', totalChanges);
core.info(`✅ PR Size Checker completed successfully`);
} catch (error) {
core.setFailed(`Action failed: ${error.message}`);
}
}
run();