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'); import discord = require('discord.js');
const fetchOptions = { 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'; const repo = process.env.GITHUB_REPOSITORY || 'citra-emu/citra';
export const roles = ['Admins', 'Moderators', 'Developers']; export const roles = ['Admins', 'Moderators', 'Developers'];
export function command(message: discord.Message) { export function command(message: discord.Message) {
const pr = message.content.substr(message.content.indexOf(' ') + 1).replace(/\n/g, ''); const pr_number = message.content.substr(message.content.indexOf(' ') + 1).replace(/\n/g, '');
const url = `https://api.github.com/repos/${repo}/pulls/${pr}`; const url = `https://api.github.com/repos/${repo}/pulls/${pr_number}`;
fetch(url, fetchOptions).then(response => response.json()).then(pr => { fetch(url, fetchOptions).then(response => response.json()).then(pr => {
if (pr.documentation_url) throw new Error('PR not found'); if (!pr || pr.documentation_url || !pr.head) throw new Error('PR not found');
fetch(pr.statuses_url, fetchOptions).then(response => response.json()).then(statuses => { const headSHA = pr.head.sha;
if (statuses.length === 0) return; // use the new GitHub checks API
// Travis CI will give you multiple, identical target URLs so we might as well just check the first one... fetch(`https://api.github.com/repos/${repo}/commits/${headSHA}/check-runs`, fetchOptions).then(response => response.json()).then(statuses => {
const status = statuses[0]; if (!statuses.check_runs || statuses.total_count < 1) throw new Error('No check runs');
status.target_url = status.target_url.substr(0, status.target_url.indexOf('?')); let msg = new discord.MessageEmbed().setTitle(`Status for PR #${pr_number}`).setURL(pr.html_url);
message.channel.send(`${status.context}: ${status.target_url}: **${status.state}**`); 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(() => { }).catch(() => {
message.channel.send('I wasn\'t able to get the status of that PR...') message.channel.send('I wasn\'t able to get the status of that PR...')
}); });