{"size": 1611, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "alonalon/gh-latest-contribs", "max_stars_repo_stars_event_min_datetime": "2017-10-09T20:39:12.000Z", "max_stars_repo_stars_event_max_datetime": "2017-10-10T10:19:37.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "alonalon/gh-latest-contribs", "max_issues_repo_issues_event_min_datetime": "2019-11-02T14:51:39.000Z", "max_issues_repo_issues_event_max_datetime": "2019-11-02T14:51:39.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "alonalon/gh-latest-contribs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 20.9220779221, "max_line_length": 124, "alphanum_fraction": 0.6654252017}
'use strict';
const graphqlGot = require('graphql-got');
const controlAccess = require('control-access');

const ONE_DAY = 60 * 60 * 24;
const {
	GITHUB_TOKEN,
	GITHUB_USERNAME,
	ACCESS_ALLOW_ORIGIN,
	ACCESS_MAX_COUNT = 5
} = process.env;

if (!GITHUB_TOKEN) {
	throw new Error('Please set your GitHub token in the `GITHUB_TOKEN` environment variable');
}

if (!GITHUB_USERNAME) {
	throw new Error('Please set your GitHub username in the `GITHUB_USERNAME` environment variable');
}

if (!ACCESS_ALLOW_ORIGIN) {
	throw new Error('Please set the `access-control-allow-origin` you want in the `ACCESS_ALLOW_ORIGIN` environment variable');
}

const query = `
	query {
		user(login: "${GITHUB_USERNAME}") {
			pullRequests(
				last: ${ACCESS_MAX_COUNT},
				orderBy: {
					field: CREATED_AT,
					direction: ASC
				}
			) {
				edges {
					node {
						author {
							login
						}
						url
						state
						publishedAt
						mergedAt
					}
				}
			}
		}
	}
`;

const fetchRepos = async () => {
	const {body} = await graphqlGot('api.github.com/graphql', {
		query,
		token: GITHUB_TOKEN
	});

	return body.user.pullRequests.edges;
};

module.exports = async (request, response) => {
	controlAccess()(request, response);

	try {
		const repos = await fetchRepos();

		response.setHeader('content-type', 'application/json');
		response.setHeader('cache-control', `s-maxage=${ONE_DAY}, max-age=0`);
		response.end(JSON.stringify(repos));
	} catch (error) {
		console.error(error);

		response.statusCode = 500;
		response.setHeader('content-type', 'text/plain');
		response.end('Internal server error');
	}
};
