This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
114 lines (105 loc) · 3.09 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
import { PullFormatQuery } from "../../queries/PullFormatQuery";
import { Reply, Status } from "../reply";
import { DEFAULT_SIG_INFO_FILE_EXT } from "../../config/Config";
import { ValidateFunction } from "ajv";
import {
contributorHasMultipleRoleWarning,
mustBeJSONFileMessage,
mustMatchSchemaMessage,
PullMessage,
migrateToJSONTip,
} from "../messages/PullMessage";
import { Service } from "typedi";
import { SigInfoSchema } from "../../config/SigInfoSchema";
const axios = require("axios").default;
export enum FileStatus {
Added = "added",
Renamed = "renamed",
Modified = "modified",
Deleted = "deleted",
}
@Service()
export default class PullService {
private static checkContributorHasOnlyOneRole(
sigInfo: SigInfoSchema
): string | undefined {
const contributorsMap = new Set();
const contributors = Object.values(sigInfo).reduce((a, b) => {
// FIXME: it should be checked in a more reasonable way.
if (Array.isArray(a) && Array.isArray(b)) {
return a.concat(b);
}
return [];
});
for (let contributor of contributors) {
if (!contributorsMap.has(contributor.githubId)) {
contributorsMap.add(contributor.githubId);
} else {
return contributor.githubId;
}
}
return;
}
/**
* Format the sig members file.
* @param validate
* @param pullRequestFormatQuery
*/
public async formatting(
validate: ValidateFunction,
pullRequestFormatQuery: PullFormatQuery
): Promise<Reply<null>> {
// Filter sig file name.
const files = pullRequestFormatQuery.files.filter((f) => {
return (
f.filename
.toLowerCase()
.includes(pullRequestFormatQuery.sigInfoFileName) &&
f.status !== FileStatus.Deleted // Ignore when the file deleted.
);
});
// Filter sig file extension。
const illegalFilesExt = files.filter((f) => {
return !f.filename.includes(DEFAULT_SIG_INFO_FILE_EXT);
});
if (illegalFilesExt.length > 0) {
return {
data: null,
status: Status.Problematic,
message: mustBeJSONFileMessage(pullRequestFormatQuery.sigInfoFileName),
tip: migrateToJSONTip(),
};
}
// Check each file format.
for (let i = 0; i < files.length; i++) {
const { data: sigInfo } = await axios.get(files[i].raw_url);
if (!validate(sigInfo)) {
return {
data: null,
status: Status.Problematic,
message: mustMatchSchemaMessage(
pullRequestFormatQuery.sigInfoFileName
),
tip: migrateToJSONTip(),
warning: JSON.stringify(validate.errors),
};
}
const githubId = PullService.checkContributorHasOnlyOneRole(
<SigInfoSchema>sigInfo
);
if (githubId !== undefined) {
return {
data: null,
status: Status.Problematic,
message: PullMessage.OnlyOneRole,
warning: contributorHasMultipleRoleWarning(githubId),
};
}
}
return {
data: null,
status: Status.Success,
message: PullMessage.FormatSuccess,
};
}
}