Skip to content

Commit 27db886

Browse files
committed
Add !buildkite agent-pause/agent-resume
1 parent a6e77a4 commit 27db886

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

cogs/buildkite.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,67 @@ async def remove(self, ctx, device: str, date: str):
242242
else:
243243
await ctx.message.reply(f'failed: ```{resp.text[:1500]}```')
244244

245+
def _agent_id(self, name: str):
246+
resp = requests.get(
247+
'https://api.buildkite.com/v2/organizations/lineageos/agents',
248+
headers={
249+
'Authorization': f'Bearer {os.environ.get("BUILDKITE_TOKEN")}'
250+
},
251+
)
252+
if resp.status_code == 200:
253+
for agent in resp.json():
254+
if agent['name'] == name:
255+
return agent['id']
256+
return None
257+
258+
@commands.has_role('Project Director')
259+
@buildkite.command(
260+
name='agent-pause',
261+
help='pause given agent. example: agent-pause build3 45',
262+
)
263+
async def agent_pause(self, ctx, name: str, timeout_minutes: int):
264+
agent_id = self._agent_id(name)
265+
if not agent_id:
266+
await ctx.message.reply(f'failed: agent id for "{name}" not found')
267+
return
268+
data = {
269+
'note': f'Paused by {ctx.message.author.name}',
270+
'timeout_in_minutes': timeout_minutes,
271+
}
272+
resp = requests.put(
273+
f'https://api.buildkite.com/v2/organizations/lineageos/agents/{agent_id}/pause',
274+
json=data,
275+
headers={
276+
'Authorization': f'Bearer {os.environ.get("BUILDKITE_TOKEN")}'
277+
},
278+
)
279+
if resp.status_code == 204:
280+
await ctx.message.add_reaction('👍')
281+
else:
282+
await ctx.message.reply(f'failed: ```{resp.text[:1500]}```')
283+
284+
@commands.has_role('Project Director')
285+
@buildkite.command(
286+
name='agent-resume',
287+
help='resume given agent. example: agent-pause build3',
288+
)
289+
async def agent_resume(self, ctx, name: str):
290+
agent_id = self._agent_id(name)
291+
if not agent_id:
292+
await ctx.message.reply(f'failed: agent id for "{name}" not found')
293+
return
294+
resp = requests.put(
295+
f'https://api.buildkite.com/v2/organizations/lineageos/agents/{agent_id}/resume',
296+
json={},
297+
headers={
298+
'Authorization': f'Bearer {os.environ.get("BUILDKITE_TOKEN")}'
299+
},
300+
)
301+
if resp.status_code == 204:
302+
await ctx.message.add_reaction('👍')
303+
else:
304+
await ctx.message.reply(f'failed: ```{resp.text[:1500]}```')
305+
245306
async def _mirror_toggle(self, ctx, env: dict, message: str):
246307
data = {
247308
'branch': 'main',

0 commit comments

Comments
 (0)