renovate/lib/platform/github/user.ts
Rhys Arkins 0a28745095
fix(github): lazy fetch user and emails (#6851)
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
2020-07-26 13:51:19 +02:00

60 lines
1.3 KiB
TypeScript

import { logger } from '../../logger';
import * as githubHttp from '../../util/http/github';
const githubApi = new githubHttp.GithubHttp();
export interface UserDetails {
username: string;
name: string;
}
let userDetails: UserDetails;
export async function getUserDetails(
endpoint: string,
token: string
): Promise<UserDetails> {
if (userDetails) {
return userDetails;
}
try {
const userData = (
await githubApi.getJson<{ login: string; name: string }>(
endpoint + 'user',
{
token,
}
)
).body;
userDetails = {
username: userData.login,
name: userData.name,
};
return userDetails;
} catch (err) {
logger.debug({ err }, 'Error authenticating with GitHub');
throw new Error('Init: Authentication failure');
}
}
let userEmail: string;
export async function getUserEmail(
endpoint: string,
token: string
): Promise<string | null> {
try {
const emails = (
await githubApi.getJson<{ email: string }[]>(endpoint + 'user/emails', {
token,
})
).body;
userEmail = emails?.[0].email || null;
return userEmail;
} catch (err) {
logger.debug(
'Cannot read user/emails endpoint on GitHub to retrieve gitAuthor'
);
return null;
}
}