From 1344f232218c591479e1435c21526d6ac8c625d4 Mon Sep 17 00:00:00 2001 From: liushuyu Date: Sat, 2 May 2020 14:23:28 -0600 Subject: [PATCH] status: switch to GitHub Checks API --- src/commands/status.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/commands/status.ts b/src/commands/status.ts index 9b42249..8fe3a84 100644 --- a/src/commands/status.ts +++ b/src/commands/status.ts @@ -2,22 +2,28 @@ import fetch from 'node-fetch'; import discord = require('discord.js'); const fetchOptions = { - headers: { 'User-Agent': 'Citra-Emu/CitraBot (Node.js)' } + headers: { 'User-Agent': 'Citra-Emu/CitraBot (Node.js)', 'Accept': 'application/vnd.github.antiope-preview+json' } }; const repo = process.env.GITHUB_REPOSITORY || 'citra-emu/citra'; export const roles = ['Admins', 'Moderators', 'Developers']; export function command(message: discord.Message) { - const pr = message.content.substr(message.content.indexOf(' ') + 1).replace(/\n/g, ''); - const url = `https://api.github.com/repos/${repo}/pulls/${pr}`; + const pr_number = message.content.substr(message.content.indexOf(' ') + 1).replace(/\n/g, ''); + const url = `https://api.github.com/repos/${repo}/pulls/${pr_number}`; fetch(url, fetchOptions).then(response => response.json()).then(pr => { - if (pr.documentation_url) throw new Error('PR not found'); - fetch(pr.statuses_url, fetchOptions).then(response => response.json()).then(statuses => { - if (statuses.length === 0) return; - // Travis CI will give you multiple, identical target URLs so we might as well just check the first one... - const status = statuses[0]; - status.target_url = status.target_url.substr(0, status.target_url.indexOf('?')); - message.channel.send(`${status.context}: ${status.target_url}: **${status.state}**`); + if (!pr || pr.documentation_url || !pr.head) throw new Error('PR not found'); + const headSHA = pr.head.sha; + // use the new GitHub checks API + fetch(`https://api.github.com/repos/${repo}/commits/${headSHA}/check-runs`, fetchOptions).then(response => response.json()).then(statuses => { + if (!statuses.check_runs || statuses.total_count < 1) throw new Error('No check runs'); + let msg = new discord.MessageEmbed().setTitle(`Status for PR #${pr_number}`).setURL(pr.html_url); + let color = 'GREEN'; + statuses.check_runs.forEach((run: any) => { + msg.addField(`${run.name}`, `**[${run.status} ${run.conclusion}](${run.html_url})**`); + if (run.conclusion !== 'success') color = 'RED'; + }); + msg.setColor(color); + message.channel.send(msg); }).catch(() => { message.channel.send('I wasn\'t able to get the status of that PR...') });