This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
forked from connorsmallman/github-jira-changelog-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
159 lines (137 loc) · 4.84 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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 { SourceControl, Jira } = require('jira-changelog');
const RegExpFromString = require('regexp-from-string');
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: ['Done', 'Closed', 'Accepted'],
excludeIssueTypes: ['Sub-task'],
includeIssueTypes: [],
},
sourceControl: {
defaultRange: {
from: core.getInput('source_control_range_from'),
to: core.getInput('source_control_range_to')
}
},
};
const template = `
<% if (jira.releaseVersions && jira.releaseVersions.length) { %>
Release version: <%= jira.releaseVersions[0].name -%>
<% jira.releaseVersions.forEach((release) => { %>
* <%= release.projectKey %>: <%= jira.baseUrl + '/projects/' + release.projectKey + '/versions/' + release.id -%>
<% }); -%>
<% } %>
Jira Tickets
---------------------
<% tickets.all.forEach((ticket) => { %>
* [<%= ticket.fields.issuetype.name %>] - <<%= jira.baseUrl + '/browse/' + ticket.key %> | [<%= ticket.key %>]> <%= ticket.fields.summary -%>
<% }); -%>
<% if (!tickets.all.length) {%> ~ None ~ <% } %>
Other Changes
---------------------
<% commits.noTickets.forEach((commit) => { %>
* <%= commit.slackUser ? '@'+commit.slackUser.name : commit.authorName %> - [<%= commit.revision.substr(0, 7) %>] - <%= commit.summary -%>
<% }); -%>
<% if (!commits.noTickets.length) {%> ~ None ~ <% } %>
`;
function generateReleaseVersionName() {
const hasVersion = process.env.VERSION;
if (hasVersion) {
return process.env.VERSION;
} else {
const haikunator = new Haikunator();
return haikunator.haikunate();
}
}
function transformCommitLogs(config, logs) {
let approvalStatus = config.jira.approvalStatus;
if (!Array.isArray(approvalStatus)) {
approvalStatus = [approvalStatus];
}
// Tickets and their commits
const ticketHash = logs.reduce((all, log) => {
log.tickets.forEach((ticket) => {
all[ticket.key] = all[ticket.key] || ticket;
all[ticket.key].commits = all[ticket.key].commits || [];
all[ticket.key].commits.push(log);
});
return all;
}, {});
const ticketList = _.sortBy(Object.values(ticketHash), ticket => ticket.fields.issuetype.name);
let pendingTickets = ticketList.filter(ticket => !approvalStatus.includes(ticket.fields.status.name));
// Pending ticket owners and their tickets/commits
const reporters = {};
pendingTickets.forEach((ticket) => {
const email = ticket.fields.reporter.emailAddress;
if (!reporters[email]) {
reporters[email] = {
email,
name: ticket.fields.reporter.displayName,
slackUser: ticket.slackUser,
tickets: [ticket]
};
} else {
reporters[email].tickets.push(ticket);
}
});
const pendingByOwner = _.sortBy(Object.values(reporters), item => item.user);
// Output filtered data
return {
commits: {
all: logs,
tickets: logs.filter(commit => commit.tickets.length),
noTickets: logs.filter(commit => !commit.tickets.length)
},
tickets: {
pendingByOwner,
all: ticketList,
approved: ticketList.filter(ticket => approvalStatus.includes(ticket.fields.status.name)),
pending: pendingTickets
}
}
}
async function main() {
try {
// Get commits for a range
const source = new SourceControl(config);
const jira = new Jira(config);
const range = config.sourceControl.defaultRange;
console.log(`Getting range ${range.from}...${range.to} commit logs`);
const commitLogs = await source.getCommitLogs('./', range);
console.log('Found following commit logs:');
console.log(commitLogs);
console.log('Generating release version');
const release = generateReleaseVersionName();
console.log(`Release: ${release}`);
console.log('Generating Jira changelog from commit logs');
const changelog = await jira.generate(commitLogs, release);
console.log('Changelog entry:');
console.log(changelog);
console.log('Generating changelog message');
const data = await transformCommitLogs(config, changelog);
data.jira = {
baseUrl: config.jira.baseUrl,
releaseVersions: jira.releaseVersions,
};
const entitles = new Entities.AllHtmlEntities();
var changelogMessage = ejs.render(template, data);
console.log('Changelog message entry:');
console.log(entitles.decode(changelogMessage));
core.setOutput('changelog_message', changelogMessage);
} catch (error) {
core.setFailed(error.message);
}
}
main();