-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
110 lines (100 loc) · 3.71 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
103
104
105
106
107
108
109
110
const core = require("@actions/core");
const JiraApi = require("jira-client")
let jira, domain, username, password, versionName, versionDescription, versionArchived, issueKeys, versionReleased;
(async () => {
try {
domain = core.getInput("domain");
username = core.getInput("username");
password = core.getInput("password");
versionName = core.getInput("versionName");
issueKeys = core.getInput("issueKeys");
versionDescription = core.getInput("versionDescription") || "CD Version";
versionArchived = core.getInput("versionArchived") === "true" || core.getInput("versionArchived") === true;
versionReleased = core.getInput("versionReleased") === "true" || core.getInput("versionReleased") === true;
// Initialize
jira = new JiraApi({
protocol: "https",
host: domain,
username: username,
password: password,
});
//core.setFailed(`version is not correct: [${version}] must be "1.0.0"/"v1.0.0"/"test 1.0.0" pattern`);
createAndSetVersion(issueKeys, versionName, versionDescription, versionArchived, versionReleased)
// core.setOutput("new-version", nextVersion);
} catch (error) {
core.setFailed(error.message);
}
})();
async function createAndSetVersion(issueKeys, versionName, versionDescription, versionArchived, versionReleased) {
// from e.g. TEST-1 get the project key --> TEST
const projectKey = getProjectKey(issueKeys);
const projectId = await getProjectId(projectKey);
const versionId = await createVersion(projectId, versionName, versionDescription);
const issueKeyArr = issueKeys.split(",");
for (let i = 0; i < issueKeyArr.length; i++) {
const issueKey = issueKeyArr[i];
const issueId = await getIssueId(issueKey);
await setVersion(issueId, versionId);
}
// archive version (passing it as argument while creating version doesn't work
if (versionArchived) {
await jira.updateVersion({
id: versionId,
archived: true,
projectId: projectId
});
}
// publish version (passing it as argument while creating version doesn't work
if (versionReleased) {
const date = new Date().toISOString().substring(0,10);
await jira.updateVersion({
id: versionId,
released: true,
projectId: projectId,
releaseDate: date
});
}
}
function getProjectKey(issueKey) {
return issueKey.substring(0, issueKey.indexOf("-"));
}
async function getProjectId(projectKey) {
const project = await jira.getProject(projectKey);
return project.id
}
async function getIssueId(issueKey) {
const issue = await jira.findIssue(issueKey);
return issue.id;
}
async function createVersion(projectId, versionName, versionDescription) {
const date = new Date().toISOString().substring(0,10);
let version = await jira.createVersion({
description: versionDescription,
name: versionName,
released: false,
startDate: date,
projectId: projectId
});
if (!!version.errors) {
// version exists already
version = await getVersion(projectId, versionName);
}
return version.id;
}
async function getVersion(projectId, versionName) {
const versions = await jira.getVersions(projectId);
for (let i = 0; i < versions.length; i++) {
const version = versions[i];
if (version.name === versionName) {
return version;
}
}
return undefined;
}
async function setVersion(issueId, versionId) {
await jira.updateIssue(issueId, {
update: {
fixVersions: [{"add": {id: versionId}}]
}
});
}