forked from langchain-ai/deepagents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneeds_response_author_reply.yml
More file actions
181 lines (163 loc) · 6.19 KB
/
Copy pathneeds_response_author_reply.yml
File metadata and controls
181 lines (163 loc) · 6.19 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
180
181
# Clear `needs-response` after an author reply recorded by needs_response.yml.
#
# `workflow_run` executes this definition from the default branch and receives
# a write-capable token for fork-origin events. The artifact is untrusted input:
# this workflow uses it only as an opaque response ID, then fetches the comment
# or review and the current item directly from GitHub before removing a label.
# It never checks out or executes contributor-controlled code.
name: Clear Needs Response After Author Reply
on:
workflow_run:
workflows: ["Needs Response"]
types: [completed]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}
cancel-in-progress: false
jobs:
remove-label:
if: >-
github.repository == 'langchain-ai/deepagents' &&
github.event.workflow_run.conclusion == 'success' &&
contains(
fromJSON('["issue_comment", "pull_request_review", "pull_request_review_comment"]'),
github.event.workflow_run.event
)
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
actions: read
issues: write
pull-requests: write
steps:
- name: Download response identifier
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
name: author-response
path: response
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Verify author response and remove label
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const fs = require('fs');
const response = JSON.parse(
fs.readFileSync('response/author-response.json', 'utf8'),
);
const { owner, repo } = context.repo;
function numberFromUrl(url, resource) {
const match = new URL(url).pathname.match(
new RegExp(`/${resource}/(\\d+)$`),
);
return match ? Number(match[1]) : undefined;
}
let item;
let responseData;
let responseTime;
let issueNumber;
if (response.eventName === 'issue_comment') {
responseData = (
await github.rest.issues.getComment({
owner,
repo,
comment_id: response.responseId,
})
).data;
issueNumber = numberFromUrl(responseData.issue_url, 'issues');
responseTime = responseData.created_at;
} else if (response.eventName === 'pull_request_review_comment') {
responseData = (
await github.rest.pulls.getReviewComment({
owner,
repo,
comment_id: response.responseId,
})
).data;
issueNumber = numberFromUrl(
responseData.pull_request_url,
'pulls',
);
responseTime = responseData.created_at;
} else if (
response.eventName === 'pull_request_review' &&
Number.isInteger(response.pullNumber)
) {
responseData = (
await github.rest.pulls.getReview({
owner,
repo,
pull_number: response.pullNumber,
review_id: response.responseId,
})
).data;
issueNumber = response.pullNumber;
responseTime = responseData.submitted_at;
} else {
core.warning('Ignoring an artifact with an unsupported response.');
return;
}
if (!issueNumber || !responseTime) {
core.warning('Ignoring a response with no item number or timestamp.');
return;
}
item = (
await github.rest.issues.get({
owner,
repo,
issue_number: issueNumber,
})
).data;
if (
item.state !== 'open' ||
!item.labels.some((label) => label.name === 'needs-response') ||
item.user.type === 'Bot' ||
responseData.user.type === 'Bot' ||
responseData.user.login !== item.user.login
) {
console.log(`Response on #${issueNumber} is not eligible.`);
return;
}
// Do not clear a label that was applied after this response. The
// event history is the source of truth rather than the untrusted
// artifact or a race-prone current-label-only check.
const events = await github.paginate(github.rest.issues.listEvents, {
owner,
repo,
issue_number: issueNumber,
per_page: 100,
});
const latestLabelEvent = events
.filter(
(event) =>
event.event === 'labeled' &&
event.label?.name === 'needs-response' &&
event.created_at,
)
.sort((a, b) => Date.parse(b.created_at) - Date.parse(a.created_at))[0];
if (!latestLabelEvent) {
core.warning(
`No needs-response label event found for #${issueNumber}; leaving it intact.`,
);
return;
}
if (Date.parse(responseTime) <= Date.parse(latestLabelEvent.created_at)) {
console.log(
`Response on #${issueNumber} predates its current needs-response label.`,
);
return;
}
try {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: issueNumber,
name: 'needs-response',
});
} catch (error) {
if (error.status !== 404) throw error;
console.log(`needs-response was already absent from #${issueNumber}.`);
return;
}
console.log(`Removed needs-response from #${issueNumber}.`);