chore: remove dotenv

convert to module, just pass in the auth and repo info to module
This commit is contained in:
2018-12-06 14:27:38 -07:00
parent e041412051
commit da786a6e02
5 changed files with 34 additions and 41 deletions

View File

@@ -5,15 +5,8 @@
// <bitbar.desc>Get list of pull requests from Github for multiple repositories</bitbar.desc>
// <bitbar.dependencies>node.js request co bluebird</bitbar.dependencies>
import dotenv from 'dotenv';
import nodeFetch, { FetchError } from 'node-fetch';
dotenv.config();
const username = process.env.GITHUB_USER;
const token = process.env.GITHUB_TOKEN;
const repos = process.env.GITHUB_REPOS.split(',');
const GITHUB_API = 'https://api.github.com';
// make fetch sane
@@ -23,39 +16,43 @@ const fetch = async (...args) => {
throw new FetchError(`Unsuccessful Request (${args[0]}) [${res.status}]`);
};
const getPull = async (repo, id) =>
fetch(`${GITHUB_API}/repos/${repo}/pulls/${id}`, {
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${token}`).toString('base64')}`,
},
}).then(res => res.json());
const getPulls = async repo => {
const issues = await fetch(
`${GITHUB_API}/repos/${repo}/issues?state=open&sort=updated&creator=w33ble`,
{
const api = ({ username, token }) => {
const getPull = async (repo, id) =>
fetch(`${GITHUB_API}/repos/${repo}/pulls/${id}`, {
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${token}`).toString('base64')}`,
},
}
).then(res => res.json());
}).then(res => res.json());
// remove non-pull issues
const pulls = issues.filter(issue => !!issue.pull_request);
return {
getPulls: async repo => {
const issues = await fetch(
`${GITHUB_API}/repos/${repo}/issues?state=open&sort=updated&creator=w33ble`,
{
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${token}`).toString('base64')}`,
},
}
).then(res => res.json());
// return actual pull info
return Promise.all(pulls.map(pull => getPull(repo, pull.number)));
};
// remove non-pull issues
const pulls = issues.filter(issue => !!issue.pull_request);
const getStatus = async (repo, sha) =>
fetch(`${GITHUB_API}/repos/${repo}/commits/${sha}/status`, {
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${token}`).toString('base64')}`,
// return actual pull info
return Promise.all(pulls.map(pull => getPull(repo, pull.number)));
},
}).then(res => res.json());
getStatus: async (repo, sha) =>
fetch(`${GITHUB_API}/repos/${repo}/commits/${sha}/status`, {
method: 'GET',
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${token}`).toString('base64')}`,
},
}).then(res => res.json()),
};
};
const getStatusColor = status => {
if (status === 'failure') return ' color=#aa0000';
@@ -64,7 +61,9 @@ const getStatusColor = status => {
return '';
};
export default async () => {
export default async ({ username, token, repos = [] }) => {
const { getPulls, getStatus } = api({ username, token });
let failed = false;
const tasks = await (() => {
try {