status: switch to GitHub Checks API

This commit is contained in:
liushuyu 2020-05-02 14:23:28 -06:00
parent 3f282a9c19
commit 1344f23221
No known key found for this signature in database
GPG Key ID: 23D1CE4534419437

View File

@ -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...')
});