-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
171 lines (158 loc) · 6.18 KB
/
index.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
#!/usr/bin/env node
import { Command, program } from "commander";
import { add, parseISO } from "date-fns";
import { ConsoleWriter } from "./src/infrastructure/writer/ConsoleWriter.js";
import { CSVWriter } from "./src/infrastructure/writer/CSVWriter.js";
import { gitStatistics, StatisticsAggregate } from "./src/statistics/GitStatistics.js";
import { MergeEvent } from "./src/statistics/merge-events/MergeEvent.js";
import { GitlabEventParameters } from "./src/statistics/Gitlab.js";
import { PullRequestsStatsParameter } from "./src/statistics/Github.js";
import { ProgressBar } from "./src/infrastructure/progress-bar/ProgressBar.js";
import * as chalk from "./src/infrastructure/progress-bar/Chalk.js";
import { HTTPError } from "./src/infrastructure/repository/EventHTTPRepository.js";
import { Repositories } from "./src/statistics/Repositories.js";
import { IssueEvent } from "./src/statistics/issues/Issues.js";
import { MergedRequestHTTPGitlabRepository } from "./src/infrastructure/repository/gitlab/MergeRequestHTTPGitlabRepository.js";
import { PullRequestHTTPGithubRepository } from "./src/infrastructure/repository/github/PullRequestHTTPGithubRepository.js";
import { IssueHTTPGitlabRepository } from "./src/infrastructure/repository/gitlab/IssueHTTPGitlabRepository.js";
import { EventRepository } from "./src/statistics/EventRepository.js";
import { IssueHTTPGithubRepository } from "./src/infrastructure/repository/github/IssueHTTPGithubRepository.js";
import { HTMLWriter } from "./src/infrastructure/writer/html/HTMLWriter.js";
type Period = {
start: Date;
end: Date;
};
const commaSeparatedList = (list: string): Period => {
const period = list.split(",");
if (period.length === 1) {
return {
end: new Date(),
start: parseISO(period[0]),
};
}
return { end: add(parseISO(period[1]), { hours: 23, minutes: 59, seconds: 59 }), start: parseISO(period[0]) };
};
const writer = (format: string): Writer => {
if (format === "html") {
return new HTMLWriter("./");
}
if (format === "csv") {
return new CSVWriter("./");
}
return new ConsoleWriter();
};
export interface Writer {
write(stats: StatisticsAggregate): void;
}
export type RequestParameters = {
fromDate: Date;
toDate: Date;
};
type CommandParameters = {
requestParameters: RequestParameters;
options: any;
token: string;
};
function isHTTPError(reason: HTTPError | string | never): reason is HTTPError {
return (reason as HTTPError).rationale !== undefined;
}
const gitlabCommand = program
.command("gitlab")
.description("Provide merge requests statistics on a Gitlab project for a given period")
.argument("<token>", "your gitlab API token")
.argument("<projectId>", "gitlab project id for which you want to have statistics")
.argument(
"<period>",
"the period you want to analyse (ISO formatted date with\nthe start period, e.g: 2021-11-02\nor separated by comma, e.g: 2021-11-02,2021-11-03 to retrieve merged events between these 2 dates)",
commaSeparatedList
);
const githubCommand = program
.command("github")
.description("Provide pull requests statistics on a GitHub project for a given period")
.argument("<token>", "your github API token")
.argument("<owner>", "The account owner of the repository. The name is not case sensitive")
.argument("<repo>", "The name of the repository. The name is not case sensitive.")
.argument(
"<period>",
"the period you want to analyse (ISO formatted date with\nthe start period, e.g: 2021-11-02\nor separated by comma, e.g: 2021-11-02,2021-11-03 to retrieve merged events between these 2 dates)",
commaSeparatedList
);
const proceedCommand = (
command: Command,
commandParameters: (...args: any[]) => CommandParameters,
repositories: (token: string) => Repositories
) => {
command
.option(
"-f, --format <writer>",
"format to display the stats (default json in console), available formats are html, csv and console (default)",
writer,
new ConsoleWriter()
)
.action((...args: any[]) => {
const parameters = commandParameters(...args);
Repositories.initialize(repositories(parameters.token));
gitStatistics(parameters.requestParameters)
.then((stats) => {
parameters.options.format.write(stats);
ProgressBar.progressBar().clear();
})
.catch((reason: HTTPError | string | never) => {
ProgressBar.progressBar().clear();
const error = new chalk.chalk.Chalk().red;
let errorMessage = reason;
if (isHTTPError(reason)) {
const additionalInfo = Object.entries(reason.additionalInfo as object).reduce(
(accumulator, currentValue) => accumulator.concat(`- ${currentValue[0]}: ${currentValue[1]}\n\t`),
""
);
errorMessage = `${reason.rationale}.\nBelow are some additional information:\n\t${additionalInfo}`;
}
console.log(error(`Something wrong happened:\n${errorMessage}`));
});
});
};
proceedCommand(
gitlabCommand,
(token: string, projectId: number, period: Period, options) => ({
requestParameters: {
fromDate: period.start,
projectId: projectId,
toDate: period.end,
} as GitlabEventParameters,
options,
token,
}),
(token: string) =>
new (class extends Repositories {
protected getIssueEventRepository(): EventRepository<IssueEvent> {
return new IssueHTTPGitlabRepository(token);
}
protected getMergeEventRepository(): EventRepository<MergeEvent> {
return new MergedRequestHTTPGitlabRepository(token);
}
})()
);
proceedCommand(
githubCommand,
(token: string, owner: string, repo: string, period: Period, options) => ({
requestParameters: {
repo,
owner,
fromDate: period.start,
toDate: period.end,
} as PullRequestsStatsParameter,
options,
token,
}),
(token: string) =>
new (class extends Repositories {
protected getIssueEventRepository(): EventRepository<IssueEvent> {
return new IssueHTTPGithubRepository(token);
}
protected getMergeEventRepository(): EventRepository<MergeEvent> {
return new PullRequestHTTPGithubRepository(token);
}
})()
);
program.parse();