forked from connorsmallman/github-jira-changelog-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (118 loc) · 3.96 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const core = require('@actions/core');
const github = require('@actions/github')
const _ = require('lodash');
const Entities = require('html-entities');
const ejs = require('ejs');
const Haikunator = require('haikunator');
const { Jira } = require('jira-changelog');
const RegExpFromString = require('regexp-from-string');
const git = require('simple-git')();
const config = {
jira: {
api: {
host: core.getInput('jira_host'),
email: core.getInput('jira_email'),
token: core.getInput('jira_token'),
},
baseUrl: core.getInput('jira_base_url'),
ticketIDPattern: RegExpFromString(core.getInput('jira_ticket_id_pattern')),
approvalStatus: core.getInput('approval_statuses').split(",").filter(x => x !== ""),
excludeIssueTypes: core.getInput('exclude_issue_types').split(",").filter(x => x !== ""),
includeIssueTypes: [],
},
sourceControl: {
defaultRange: {
from: core.getInput('source_control_range_from'),
to: core.getInput('source_control_range_to')
}
},
};
const template = `
Release version: <%= releaseVersionName -%>
Jira Tickets
---------------------
<% tickets.forEach((ticket) => { %>
* [<%= ticket.fields.issuetype.name %>] - [<%= ticket.key %>](<%= baseUrl + '/browse/' + ticket.key %>) <%= ticket.fields.summary -%>
<% }); -%>
<% if (!tickets.size) {%> ~ None ~ <% } %>
Unknown Tickets (not found in Jira)
---------------------
<% unknownTickets.forEach((ticket) => { %>
* <%= ticket.message -%>
<% }); -%>
<% if (!unknownTickets.size) {%> ~ None ~ <% } %>
`;
function generateReleaseVersionName() {
const hasVersion = process.env.VERSION;
if (hasVersion) {
return process.env.VERSION;
} else {
const haikunator = new Haikunator();
return haikunator.haikunate();
}
}
async function getIssues() {
// Get commits for a range
const jira = new Jira(config);
const range = config.sourceControl.defaultRange;
console.log(`Getting range ${range.from}...${range.to} commit logs`);
const commitLogs = await git.log({ from: range.from, to: range.to });
const issues = new Map();
const unknownIssues = new Map();
const commits = [];
for (const commit of commitLogs.all) {
const match = config.jira.ticketIDPattern.exec(commit.message);
if (match) {
const ticketId = match[1];
if (issues.has(ticketId) || unknownIssues.has(ticketId)) {
continue;
}
console.log(`Fetching Jira issue ${ticketId}`);
try {
const issue = await jira.getJiraIssue(ticketId);
if (issue) {
issues.set(ticketId, issue);
}
} catch (error) {
console.error(`Error while fetching Jira issue ${ticketId}: ${error.message}`);
unknownIssues.set(ticketId, commit);
}
} else {
commits.push(commit);
}
}
return {
tickets: issues,
unknownTickets: unknownIssues,
commits: commits,
};
}
async function main() {
try {
const issues = await getIssues();
const releaseVersionName = generateReleaseVersionName();
console.log(`Release version name: ${releaseVersionName}`);
console.log(`Jira tickets: ${issues.tickets.size}`);
console.log(`Unknown tickets: ${issues.unknownTickets.size}`);
console.debug("Issues: ", issues.tickets);
console.debug("Unknown Issues: ", issues.unknownTickets);
data = {
releaseVersionName: releaseVersionName,
baseUrl: config.jira.baseUrl,
tickets: issues.tickets,
unknownTickets: issues.unknownTickets,
// commits without tickets
commits: issues.commits,
};
data.includePendingApprovalSection = core.getInput('include_pending_approval_section') === 'true';
const entitles = new Entities.AllHtmlEntities();
const changelogMessage = ejs.render(template, data);
console.log('Changelog message entry:');
console.log(entitles.decode(changelogMessage));
core.setOutput('changelog_message', changelogMessage);
} catch (error) {
console.error(error);
core.setFailed(error.message);
}
}
main();