forked from connorsmallman/github-jira-changelog-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (103 loc) · 3.52 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
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 = `
<% tickets.all.forEach((ticket) => { %>
* [<%= ticket.fields.issuetype.name %>] - [<%= ticket.key %>](<%= jira.baseUrl + '/browse/' + ticket.key %>) <%= ticket.fields.summary -%>
<% }); -%>
<% if (!tickets.all.length) {%> ~ None ~ <% } %>
`;
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('Generating Jira changelog from commit logs');
const changelog = await jira.generate(commitLogs);
console.log('Generating changelog message');
const data = await transformCommitLogs(config, changelog);
data.jira = {
baseUrl: config.jira.baseUrl,
};
const changelogMessage = ejs.render(template, data);
core.setOutput('changelog_message', changelogMessage);
} catch (error) {
core.setFailed(error.message);
}
}
main();