-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (76 loc) · 3.09 KB
/
index.js
File metadata and controls
93 lines (76 loc) · 3.09 KB
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
import core from "@actions/core";
import { context } from "@actions/github";
import { addFileToRepo, addLabelToIssue, closeIssueWithComment, createRepoAndInviteTo, getIssue } from "./src/GithubWrapper.js";
import { getIssueLabels, getRepoDescription, getRepoName, isBlackListedRepo } from "./src/Util.js";
/**
*
* @param {string} token
* @param {string} reason
* @param {string} label
*/
const markInternalFailure = async (token, reason, label = 'internal-failure') => {
await closeIssueWithComment(token, `Failure handling issue: ${reason}`);
await addLabelToIssue(token, label);
core.error(reason);
core.setFailed(`Submission Issue Handling Failure: "${reason}"`);
};
/**
*
* @param {string} token
* @param {string} reason
* @param {string} label
*/
const markFailure = async (token, reason, label = 'invalid') => {
await closeIssueWithComment(token, reason);
await addLabelToIssue(token, label);
core.error(reason);
core.setFailed(`Submission Issue Handling Failure: "${reason}"`);
};
const main = async _ => {
const token = core.getInput('github-token');
const issue = await getIssue(token);
if (!issue)
{
await markInternalFailure(token, "No issue in current context?");
return;
}
core.debug("Got a valid issue.");
const labels = getIssueLabels(issue);
if (!labels.includes("submission"))
{
await markInternalFailure(token, "Somehow someone managed to create an issue without label?");
return;
}
core.debug("Submission label is present.");
const body = issue.body;
const repoName = getRepoName(body).replace(' ', '-');
const repoDescription = getRepoDescription(body);
core.debug(`Parsed the following { repoName: "${repoName}", repoDescription: "${repoDescription}" }`);
if (repoName.includes('\n') || repoName.includes('\r'))
{
await markFailure(token, "Repository name includes newlines, make sure you fill only a single line for the repository name.");
return;
}
core.debug("Repo name doesn't contain any newlines.");
if (isBlackListedRepo(repoName))
{
await markFailure(token, "Repository name is on the blacklist or containst blacklisted words, please use a different name.");
return;
}
core.debug("Repo name does not contain any blacklisted names.");
const result = await createRepoAndInviteTo(token, issue.user.login, repoName, repoDescription);
if (!result)
{
await markInternalFailure("Failed to create repo and invite collaborator.");
return;
}
core.debug("Created repo and invited user as collaborator.");
const metadata = {
creator: issue.user.login,
name: repoName,
description: repoDescription
};
await addFileToRepo(token, repoName, 'metadata.json', 'Create metadata.json', JSON.stringify(metadata, null, 4));
await closeIssueWithComment(token, `Success, your repository has been created!\n\nYou can accept the invite by checking your email or clicking [here](https://github.com/${context.repo.owner}/${repoName}/invitations).`);
};
main();