-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (110 loc) · 4.01 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
async function run() {
try {
// Configuration
const config = {
shortThreshold: parseInt(core.getInput('short_threshold')) || 7,
mediumThreshold: parseInt(core.getInput('medium_threshold')) || 30,
colors: {
short: core.getInput('short_color') || '00FF00',
medium: core.getInput('medium_color') || 'FFA500',
long: core.getInput('long_color') || 'FF0000'
},
thresholdedUpdate: core.getInput('thresholded_update') === 'true'
};
const token = core.getInput('github-token');
const octokit = github.getOctokit(token);
// Main function to process issues
async function processIssues() {
const { data: issues } = await octokit.rest.issues.listForRepo({
...github.context.repo,
state: 'all'
});
for (const issue of issues) {
const duration = calculateDuration(issue);
const { label, color } = getLabelAndColorForDuration(duration, config);
if (!config.thresholdedUpdate || (config.thresholdedUpdate && label)) {
await removeOldDurationLabels(issue, octokit);
if (label) {
await createOrUpdateLabel(label, color, octokit);
await addLabelToIssue(issue, label, octokit);
core.info(`Updated issue #${issue.number} with label: ${label} (color: ${color})`);
}
}
}
}
// Calculate duration of an issue
function calculateDuration(issue) {
const createdAt = new Date(issue.created_at);
const endDate = issue.closed_at ? new Date(issue.closed_at) : new Date();
return Math.ceil((endDate - createdAt) / (1000 * 60 * 60 * 24));
}
// Determine label and color based on duration and config
function getLabelAndColorForDuration(duration, config) {
if (duration <= config.shortThreshold) {
return { label: `Duration: 1-${config.shortThreshold} days`, color: config.colors.short };
} else if (duration <= config.mediumThreshold) {
return { label: `Duration: ${config.shortThreshold + 1}-${config.mediumThreshold} days`, color: config.colors.medium };
} else {
return { label: 'Duration: >1 month', color: config.colors.long };
}
}
// Remove old duration labels from an issue
async function removeOldDurationLabels(issue, octokit) {
const oldLabels = issue.labels.filter(label => label.name.startsWith('Duration:'));
for (const label of oldLabels) {
try {
await octokit.rest.issues.removeLabel({
...github.context.repo,
issue_number: issue.number,
name: label.name
});
} catch (error) {
core.warning(`Failed to remove old label from issue #${issue.number}: ${error.message}`);
}
}
}
// Create or update a label
async function createOrUpdateLabel(name, color, octokit) {
try {
await octokit.rest.issues.createLabel({
...github.context.repo,
name: name,
color: color
});
} catch (error) {
if (error.status === 422) {
try {
await octokit.rest.issues.updateLabel({
...github.context.repo,
name: name,
color: color
});
} catch (updateError) {
core.warning(`Failed to update label: ${updateError.message}`);
}
} else {
core.warning(`Failed to create label: ${error.message}`);
}
}
}
// Add a label to an issue
async function addLabelToIssue(issue, label, octokit) {
try {
await octokit.rest.issues.addLabels({
...github.context.repo,
issue_number: issue.number,
labels: [label]
});
} catch (error) {
core.warning(`Failed to add label to issue #${issue.number}: ${error.message}`);
}
}
// Run the main function
await processIssues();
} catch (error) {
core.setFailed(error.message);
}
}
run();