forked from diplodoc-platform/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.ts
172 lines (140 loc) · 5.54 KB
/
github.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import log from '@doc-tools/transform/lib/log';
import {Octokit} from '@octokit/core';
import {join} from 'path';
import {existsSync} from 'fs';
import simpleGit, {SimpleGit} from 'simple-git';
import {ArgvService} from '../services';
import {Contributor, Contributors, ContributorsFunction, Users} from '../models';
import {ContributorDTO, GithubContributorDTO, GithubLogsDTO, SourceType, UserDTO, VCSConnector} from './models';
import {getMsgСonfigurationMustBeProvided} from '../constants';
async function getGitHubVCSConnector(): Promise<VCSConnector> {
const {contributors} = ArgvService.getConfig();
return {
getContributorsByPath: contributors
? await getGithubContributorsByPathFunction()
: () => Promise.resolve([]),
};
}
function getHttpClientByToken(): Octokit {
const {GITHUB_TOKEN, GITHUB_BASE_URL} = process.env;
const {connector} = ArgvService.getConfig();
const token = GITHUB_TOKEN || connector && connector.gitHub.token || '';
const endpoint = GITHUB_BASE_URL || connector && connector.gitHub.endpoint || '';
if (!token || !endpoint) {
log.warn(getMsgСonfigurationMustBeProvided(SourceType.GITHUB));
}
const octokit = new Octokit({auth: token, baseUrl: endpoint});
return octokit;
}
async function getGithubContributorsByPathFunction(): Promise<ContributorsFunction> {
const {contributors, rootInput} = ArgvService.getConfig();
const httpClientByToken = getHttpClientByToken();
const gitSource: SimpleGit = simpleGit(rootInput, {binary: 'git'});
const allContributors = contributors ? await getAllContributors(httpClientByToken) : {};
const getGithubContributorsFunction = async (path: string) => {
const filePath = join(rootInput, path);
return getGithubContributors(gitSource, allContributors, filePath);
};
return getGithubContributorsFunction;
}
async function getAllContributors(httpClientByToken: Octokit): Promise<Contributors> {
try {
const repoContributors = await getRepoContributors(httpClientByToken);
const promises: Promise<UserDTO | null>[] = [];
repoContributors.forEach((contributor: ContributorDTO) => {
if (contributor.login) {
promises.push(getRepoUser(httpClientByToken, contributor.login));
}
});
const repoUsers = await Promise.all(promises);
const users: Users = {};
repoUsers.forEach((user: UserDTO | null) => {
if (user) {
users[user.login] = {
name: user.name,
email: user.email,
};
}
});
const contributors: Contributors = {};
repoContributors.forEach((githubContributor: GithubContributorDTO) => {
const {login, avatar_url: avatarUrl = ''} = githubContributor;
if (login) {
const user = users[login];
if (user) {
contributors[user.email || login] = {
avatar: avatarUrl,
login: login,
name: user.name,
};
}
}
});
return contributors;
} catch (error) {
console.log(error);
log.error(`Getting of contributors has been failed. Error: ${JSON.stringify(error)}`);
throw error;
}
}
async function getRepoContributors(octokit: Octokit): Promise<ContributorDTO[]> {
const {GITHUB_OWNER, GITHUB_REPO} = process.env;
const {connector} = ArgvService.getConfig();
const owner = GITHUB_OWNER || connector && connector.gitHub.owner || '';
const repo = GITHUB_REPO || connector && connector.gitHub.repo || '';
if (!owner || !repo) {
log.warn(getMsgСonfigurationMustBeProvided(SourceType.GITHUB));
return [];
}
try {
const commits = await octokit.request('GET /repos/{owner}/{repo}/contributors', {
owner,
repo,
});
return commits.data;
} catch (error) {
log.warn('Getting contributors for GitHub has been failed. Error: ', error);
return [];
}
}
async function getRepoUser(octokit: Octokit, username: string): Promise<UserDTO | null> {
try {
const user = await octokit.request('GET /users/{username}', {
username,
});
return user.data as UserDTO;
} catch (error) {
log.warn('Getting user for GitHub has been failed. Error: ', error);
return null;
}
}
async function getGithubContributors(gitSource: SimpleGit, allContributors: Contributors, filePath: string): Promise<Contributor[]> {
if (Object.keys(allContributors).length === 0) {
return [];
}
const commits = await getGithubLogs(gitSource, filePath);
const contributors: Contributor[] = [];
if (commits) {
commits.forEach((commit: GithubLogsDTO) => {
const user = allContributors[commit.author_email];
if (user) {
contributors.push(user);
} else {
contributors.push({
avatar: '',
login: commit.author_email,
name: commit.author_name,
});
}
});
}
return contributors;
}
async function getGithubLogs(gitSource: SimpleGit, filePath: string): Promise<GithubLogsDTO[]> {
if (!existsSync(filePath)) {
return [];
}
const logs = await gitSource.log({file: filePath});
return logs.all as unknown as GithubLogsDTO[];
}
export default getGitHubVCSConnector;