-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
93 lines (85 loc) · 2.91 KB
/
index.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
const core = require("@actions/core");
const github = require("@actions/github");
const githubToken = core.getInput("github-token");
const octokit = github.getOctokit(githubToken);
function issueHasLabel(labelName, payload) {
return payload.issue.labels.some((label) => label.name === labelName);
}
async function process(dataMap, payload) {
for (let item in dataMap) {
if (issueHasLabel(dataMap[item].label, payload)) {
console.log(`Getting all projects.`);
const projectList = await getAllProjects(dataMap, item);
console.log(`Getting ID for project '${dataMap[item].project}'.`);
const projectId = getProjectIdByName(dataMap[item].project, projectList);
if (!projectId) {
throw new Error(`Unable to retrieve project ID.`);
}
console.log(`Getting all columns for project '${projectId}'.`);
const columnList = await getAllColumns(projectId);
console.log(`Getting ID for column '${dataMap[item].column}'.`);
const columnId = getColumnIdByName(dataMap[item].column, columnList);
if (!columnId) {
throw new Error(`Unable to retrieve column ID.`);
}
const createIssue = await octokit.rest.projects.createCard({
column_id: columnId,
content_type: "Issue",
content_id: payload.issue.id,
});
if (!createIssue) {
console.error(
"Something went wrong when attempting to create the card."
);
}
}
}
}
async function getAllProjects(dataMap, index) {
if (dataMap[index].project_type == "repo") {
console.log(`Getting all projects for repo: ${dataMap[index].repo}`);
return await octokit.rest.projects.listForRepo({
owner: github.context.payload.repository.owner.login,
repo: dataMap[index].repo,
});
} else if (dataMap[index].project_type == "org") {
console.log(`Getting all projects for organization: ${dataMap[index].org}`);
return await octokit.rest.projects.listForOrg({
org: dataMap[index].org,
});
} else {
console.error("You provided an invalid `project_type`.");
}
}
function getProjectIdByName(name, projectList) {
console.log(`Searching for project with name: ${name}.`);
console.log(`Contents of projectList: ${JSON.stringify(projectList)}`);
for (let project in projectList.data) {
if (projectList.data[project].name == name) {
return projectList.data[project].id;
}
}
return false;
}
async function getAllColumns(project_id) {
return await octokit.rest.projects.listColumns({
project_id: project_id,
});
}
function getColumnIdByName(name, columnList) {
for (let column in columnList.data) {
column = columnList.data[column];
if (column.name == name) {
return column.id;
}
}
return false;
}
try {
(async () => {
const data = JSON.parse(core.getInput("actions")).data;
await process(data, github.context.payload);
})();
} catch (error) {
core.setFailed(error.message);
}