-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (84 loc) · 2.15 KB
/
script.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
//@ts-check
let templateRepositoryOptions;
/**
* Updates repository options based on a settings from a template repository
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param { {template: string} } options Custom user options passed to the CLI
*/
export async function script(octokit, repository, options) {
if (!options.template) {
throw new Error(`--template is required`);
}
try {
if (!templateRepositoryOptions) {
octokit.log.debug(
"Load Repository Options from template repository %s",
options.template
);
const [templateOwner, templateRepo] = options.template.split("/");
try {
const {
data: {
allow_merge_commit,
allow_rebase_merge,
allow_squash_merge,
delete_branch_on_merge
}
} = await octokit.request("GET /repos/{owner}/{repo}", {
owner: templateOwner,
repo: templateRepo,
});
templateRepositoryOptions = {
allow_merge_commit,
allow_rebase_merge,
allow_squash_merge,
delete_branch_on_merge
};
} catch (error) {
if (error.status === 404) {
throw new Error(`Repository Template %s not found`);
}
throw error;
}
octokit.log.info(
templateRepositoryOptions,
"Repository Options loaded from %s",
options.template
);
}
const urlParameters = {
owner: repository.owner.login,
repo: repository.name
};
await octokit.request('PATCH /repos/{owner}/{repo}',
{
...urlParameters,
...templateRepositoryOptions,
}
);
} catch (error) {
const statusCode = error.status;
if (statusCode === 401) {
octokit.log.warn(
"Token does not have permission to access repository %s.",
repository.full_name
);
return;
} else if(statusCode === 403) {
octokit.log.warn(
"Repository %s is archived.",
repository.full_name
);
return;
} else if (statusCode === 404) {
octokit.log.info(
"Respository %s not found",
repository.full_name
);
return;
}
throw error;
}
}