-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
102 lines (95 loc) · 2.87 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
94
95
96
97
98
99
100
101
102
const { default: axios } = require("axios");
const core = require("@actions/core");
const { inspect } = require("util");
async function moveCardToPipeline(
repoId,
workspaceId,
issueId,
targetPipelineId
) {
const url = `https://api.zenhub.com/p2/workspaces/${workspaceId}/repositories/${repoId}/issues/${issueId}/moves`;
const response = await axios.post(url, {
pipeline_id: targetPipelineId,
position: "top"
});
console.log(`POST ${url} -- [${response.status}]`);
}
async function getIdOfPipelineByName(repoId, workspaceId, pipelineName) {
const url = `https://api.zenhub.com/p2/workspaces/${workspaceId}/repositories/${repoId}/board`;
const response = await axios.get(url);
console.log(`GET ${url} -- [${response.status}]`);
const pipelines = response.data.pipelines;
const pipeline = pipelines.find(
(pipeline) => pipeline.name.indexOf(pipelineName) !== -1
);
if (pipeline) {
return pipeline.id;
} else {
return null;
}
}
function extractIssueFromPattern(message) {
let res = [
/#(?<number>\d+)/i,
/(?<owner>\w+)\/(?<name>issues)#(?<number>\d+)/i,
/https?:\/\/github.com\/(?<owner>\w+)\/(?<name>\w+)\/issues\/(?<number>\d+)/i,
];
for (const re of res) {
const match = re.exec(message);
if (match) {
return {
number: match.groups.number,
};
}
}
}
(async function () {
try {
const inputs = {
zhToken: core.getInput("zh-token"),
zhWorkspaceId: core.getInput("zh-workspace-id"),
zhRepoId: core.getInput("zh-repository-id"),
commitMessage: core.getInput("commit-message"),
pipelineId: core.getInput("zh-target-pipeline-id"),
pipelineName: core.getInput("zh-target-pipeline-name"),
};
core.debug(`Inputs: ${inspect(inputs)}`);
if (!inputs.pipelineId && !inputs.pipelineName) {
core.setFailed(
"one of zh-target-pipeline-id and zh-target-pipeline-name is required"
);
return;
}
let issuePattern = extractIssueFromPattern(inputs.commitMessage);
if (!issuePattern) {
core.info("Failed to extract issue number, action skipped");
return;
}
let issueNumber = issuePattern.number;
axios.defaults.headers.common["X-Authentication-Token"] = inputs.zhToken;
let pipelineId;
if (!inputs.pipelineId && inputs.pipelineName) {
pipelineId = await getIdOfPipelineByName(
inputs.zhRepoId,
inputs.zhWorkspaceId,
inputs.pipelineName
);
if (!pipelineId) {
core.setFailed("No pipeline name of " + pipelineName + " found");
return;
}
} else {
pipelineId = inputs.pipelineId;
}
core.info(`move issue ${issueNumber} to ${pipelineId}`);
await moveCardToPipeline(
inputs.zhRepoId,
inputs.zhWorkspaceId,
issueNumber,
pipelineId
);
} catch (err) {
core.debug(inspect(err));
core.setFailed(err.message);
}
})();