-
-
Notifications
You must be signed in to change notification settings - Fork 161
155 lines (131 loc) · 5.56 KB
/
auto-assign.yaml
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
name: Auto-assign issue on /attempt comment
on:
issue_comment:
types: [created]
jobs:
auto-assign:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
env:
USER_MAX_CONCURRENT_ISSUE_COUNT: ${{ vars.USER_MAX_CONCURRENT_ISSUE_COUNT }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const comment = context.payload.comment;
const issue = context.payload.issue;
const owner = 'keyshade-xyz';
const repo = 'keyshade';
const userMaxConcurrentIssueCount = process.env.USER_MAX_CONCURRENT_ISSUE_COUNT || 5;
async function listIssueEvents(issueNumber) {
const events = [];
let page = 1;
let hasNextPage = true;
while (hasNextPage) {
const eventsResponse = await github.rest.issues.listEventsForTimeline({
owner,
repo,
issue_number: issueNumber,
per_page: 100,
page
});
events.push(...eventsResponse.data);
hasNextPage = eventsResponse.headers.link && eventsResponse.headers.link.includes('rel="next"');
page++;
}
return events;
}
async function listUserOpenIssuesWithoutActivePullRequest(user) {
const issuesWithoutActivePullRequest = [];
const { data: userOpenIssues } = await github.rest.issues.listForRepo({
owner,
repo,
state: 'open',
assignee: user.login,
per_page: 100
});
for (const issue of userOpenIssues) {
const events = await listIssueEvents(issue.number);
const userPullRequestIssues = events
.filter(event => event.event === 'cross-referenced')
.map(event => event.source.issue)
.filter(issue => issue && issue.pull_request && issue.user.login === comment.user.login);
if(userPullRequestIssues.length === 0) {
issuesWithoutActivePullRequest.push(issue);
continue;
}
for (const pullRequestIssue of userPullRequestIssues) {
const { data: pullRequest } = await github.rest.pulls.get({
owner,
repo,
pull_number: pullRequestIssue.number
});
if(pullRequest.draft) {
issuesWithoutActivePullRequest.push(issue);
break;
}
}
}
return issuesWithoutActivePullRequest;
}
async function updateProjectStatus(issueNumber) {
const projectsResponse = await github.rest.projects.listForRepo({
owner,
repo,
per_page: 100,
});
for (const project of projectsResponse.data) {
const columnsResponse = await github.rest.projects.listColumns({
project_id: project.id,
per_page: 100,
});
const inProgressColumn = columnsResponse.data.find(column => column.name === 'In Progress');
if (!inProgressColumn) continue;
const cardsResponse = await github.rest.projects.listCards({
column_id: inProgressColumn.id,
per_page: 100,
});
const issueCardExists = cardsResponse.data.some(card => card.content_id === issueNumber && card.content_type === 'Issue');
if (!issueCardExists) {
await github.rest.projects.createCard({
column_id: inProgressColumn.id,
content_id: issueNumber,
content_type: 'Issue',
});
}
}
}
if (comment.body.startsWith('/attempt')) {
if (!issue.assignee) {
const userActiveIssues = await listUserOpenIssuesWithoutActivePullRequest(comment.user);
if(userActiveIssues.length < userMaxConcurrentIssueCount) {
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issue.number,
assignees: [comment.user.login],
});
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `Assigned the issue to @${comment.user.login}!`,
});
await updateProjectStatus(issue.number);
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `@${comment.user.login}, cannot concurrently work on more than ${userMaxConcurrentIssueCount} issues. Tag a maintainer if you need to take over.`,
});
}
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `@${comment.user.login}, this issue is already assigned. Tag a maintainer if you need to take over.`,
});
}
}