-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
103 lines (96 loc) · 3.03 KB
/
utils.js
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
/**
* turn
*
* {
* url: 'https://api.github.com/repos/octokit/core.js/branches/master/protection',
* required_status_checks: {
* url: 'https://api.github.com/repos/octokit/core.js/branches/master/protection/required_status_checks',
* strict: true,
* contexts: [ 'WIP', 'project-board', 'test (10)', 'test (12)', 'test (14)' ],
* contexts_url: 'https://api.github.com/repos/octokit/core.js/branches/master/protection/required_status_checks/contexts'
* },
* required_pull_request_reviews: {
* url: 'https://api.github.com/repos/octokit/core.js/branches/master/protection/required_pull_request_reviews',
* dismiss_stale_reviews: false,
* require_code_owner_reviews: false
* },
* enforce_admins: {
* url: 'https://api.github.com/repos/octokit/core.js/branches/master/protection/enforce_admins',
* enabled: false
* },
* dismissal_restrictions: {
* users: [
* {
* id: 1,
* node_id: "MDQ6VXNlcjE=",
* login: "octocat"
* },
* ],
* teams: [
* {
* name: "Team name",
* id: 1,
* node_id: "MDQ6VGVhbTE=",
* slug: "team-name",
* description: "",
* privacy: "closed",
* permission: "pull",
* },
* ],
* },
* required_linear_history: { enabled: false },
* allow_force_pushes: { enabled: false },
* allow_deletions: { enabled: false }
* }
*
* into
*
* {
* required_status_checks: {
* strict: true,
* contexts: [ 'WIP', 'project-board', 'test (10)', 'test (12)', 'test (14)' ]
* },
* required_pull_request_reviews: { dismiss_stale_reviews: false, require_code_owner_reviews: false },
* enforce_admins: false,
* required_linear_history: false,
* allow_force_pushes: false,
* allow_deletions: false
* }
*/
export function protectionResponseDataToUpdateParameters(data) {
const defaults = {
required_status_checks: null,
enforce_admins: null,
required_pull_request_reviews: null,
restrictions: null,
};
const parameters = mapValues(data, (key, value) => {
if (/(^|_)url$/.test(key)) return;
if (value === null || typeof value !== "object") return value;
if ("enabled" in value) return value.enabled;
if ("login" in value) return value.login;
if ("slug" in value) return value.slug;
return { ...value };
});
return { ...defaults, ...parameters };
}
function mapValues(object, map) {
if (typeof object !== "object") return object;
const newObject = Object.fromEntries(
Object.entries(object).map(([key, value]) => [
key,
Array.isArray(value) ? value.map((v) => map(key, v)) : map(key, value),
])
);
for (const [key, value] of Object.entries(newObject)) {
if (value === undefined || value === null) {
delete newObject[key];
}
if (Array.isArray(value)) {
newObject[key] = newObject[key].map((item) => mapValues(item, map));
} else if (typeof value === "object") {
newObject[key] = mapValues(newObject[key], map);
}
}
return newObject;
}