@waron97/prbot 2.5.0 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/changelog.js +1 -1
- package/src/commands/export.js +2 -1
- package/src/commands/exportEmailTemplates.js +142 -0
- package/src/index.js +40 -14
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@ function buildRefString(tridents, jiras, prNumber) {
|
|
|
9
9
|
const refs = [];
|
|
10
10
|
|
|
11
11
|
if (tridents && tridents.length > 0) {
|
|
12
|
-
refs.push(`Trident ${tridents.map((t) => `#${t}`).join(',
|
|
12
|
+
refs.push(`Trident ${tridents.map((t) => `#${t}`).join(', ')}`);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
if (jiras && jiras.length > 0) {
|
package/src/commands/export.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { exportPb } from './exportPb.js';
|
|
2
2
|
import { exportImperex } from './exportImperex.js';
|
|
3
|
+
import { exportEmailTemplates } from './exportEmailTemplates.js';
|
|
3
4
|
|
|
4
5
|
function exportRip() {
|
|
5
6
|
console.log('Not implemented yet.');
|
|
6
7
|
}
|
|
7
8
|
|
|
8
|
-
export { exportPb, exportRip, exportImperex };
|
|
9
|
+
export { exportPb, exportRip, exportImperex, exportEmailTemplates };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fetch from 'node-fetch';
|
|
4
|
+
import search from '@inquirer/search';
|
|
5
|
+
import { getToken } from '../lib/auth.js';
|
|
6
|
+
import { execGit } from '../lib/git.js';
|
|
7
|
+
import { resolveAddonsPath } from '../lib/addons.js';
|
|
8
|
+
import { fuzzyMatch } from '../lib/fuzzy.js';
|
|
9
|
+
|
|
10
|
+
async function getWorkflows(token) {
|
|
11
|
+
const url = `${process.env.RIP_URL}/symple.workflow/*`;
|
|
12
|
+
const response = await fetch(url, {
|
|
13
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
14
|
+
});
|
|
15
|
+
if (!response.ok) throw new Error(await response.text());
|
|
16
|
+
return await response.json();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function getEmailTemplates(workflowId, token) {
|
|
20
|
+
const url = `${process.env.RIP_URL}/symple.workflow/get_email_templates`;
|
|
21
|
+
const response = await fetch(url, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
|
24
|
+
body: JSON.stringify({ workflow_id: workflowId }),
|
|
25
|
+
});
|
|
26
|
+
if (!response.ok) throw new Error(await response.text());
|
|
27
|
+
return await response.json();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/i;
|
|
31
|
+
|
|
32
|
+
function sanitizeBodyHtml(html) {
|
|
33
|
+
// Convert bare void-element tags to self-closing so the XML stays valid.
|
|
34
|
+
// Also replace which is not a predefined XML entity.
|
|
35
|
+
return html
|
|
36
|
+
.replace(/ /g, ' ')
|
|
37
|
+
.replace(/<([a-zA-Z]+)(\s[^>]*[^/]|[^/>]*)>/g, (match, tag, attrs) => {
|
|
38
|
+
if (!VOID_ELEMENTS.test(tag)) return match;
|
|
39
|
+
return `<${tag}${attrs ?? ''}/>`;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function toXmlId(name) {
|
|
44
|
+
return name.replace(/[^a-zA-Z0-9]+/g, '_').replace(/^_+|_+$/g, '');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function escapeXml(str) {
|
|
48
|
+
return String(str ?? '')
|
|
49
|
+
.replace(/&/g, '&')
|
|
50
|
+
.replace(/</g, '<')
|
|
51
|
+
.replace(/>/g, '>')
|
|
52
|
+
.replace(/"/g, '"');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function generateXml(templates) {
|
|
56
|
+
const records = templates
|
|
57
|
+
.map((t) => {
|
|
58
|
+
const id = toXmlId(t.name);
|
|
59
|
+
const modelRef = Object.values(t.model_id)[0];
|
|
60
|
+
return ` <record id="${id}" model="mail.template">
|
|
61
|
+
<field name="name">${escapeXml(t.name)}</field>
|
|
62
|
+
<field name="model_id" ref="${modelRef}"/>
|
|
63
|
+
<field name="template_code">${escapeXml(t.template_code)}</field>
|
|
64
|
+
<field name="subject">${escapeXml(t.subject)}</field>
|
|
65
|
+
<field name="email_from">${escapeXml(t.email_from)}</field>
|
|
66
|
+
<field name="email_to">${escapeXml(t.email_to)}</field>
|
|
67
|
+
<field name="body_html" type="html">
|
|
68
|
+
${sanitizeBodyHtml(t.body_html)}
|
|
69
|
+
</field>
|
|
70
|
+
</record>`;
|
|
71
|
+
})
|
|
72
|
+
.join('\n');
|
|
73
|
+
|
|
74
|
+
return `<?xml version="1.0" encoding="utf-8"?>
|
|
75
|
+
<odoo>
|
|
76
|
+
<data noupdate="1">
|
|
77
|
+
${records}
|
|
78
|
+
</data>
|
|
79
|
+
</odoo>
|
|
80
|
+
`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function getModuleChoices() {
|
|
84
|
+
const ADDONS_PATH = resolveAddonsPath(process.env.ADDONS_PATH);
|
|
85
|
+
const configDir = path.join(ADDONS_PATH, 'config');
|
|
86
|
+
const entries = await fs.readdir(configDir, { withFileTypes: true });
|
|
87
|
+
return entries
|
|
88
|
+
.filter((e) => e.isDirectory())
|
|
89
|
+
.map((e) => ({ name: e.name, value: e.name }));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function exportEmailTemplates(opts) {
|
|
93
|
+
const token = await getToken();
|
|
94
|
+
|
|
95
|
+
const moduleChoices = await getModuleChoices();
|
|
96
|
+
const module = await search({
|
|
97
|
+
message: 'Select module:',
|
|
98
|
+
source: async (input) => {
|
|
99
|
+
if (!input) return moduleChoices;
|
|
100
|
+
return moduleChoices.filter((c) => fuzzyMatch(c.name, input));
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
console.log('Fetching workflows...');
|
|
105
|
+
const workflows = await getWorkflows(token);
|
|
106
|
+
const choices = workflows.map((w) => ({ name: w.name, value: w.id }));
|
|
107
|
+
|
|
108
|
+
const workflowId = await search({
|
|
109
|
+
message: 'Select workflow:',
|
|
110
|
+
source: async (input) => {
|
|
111
|
+
if (!input) return choices;
|
|
112
|
+
return choices.filter((c) => fuzzyMatch(c.name, input));
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
console.log(`Fetching email templates for workflow ${workflowId}...`);
|
|
117
|
+
const templates = await getEmailTemplates(workflowId, token);
|
|
118
|
+
|
|
119
|
+
if (!templates.length) {
|
|
120
|
+
console.log('No email templates found for this workflow.');
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const ADDONS_PATH = resolveAddonsPath(process.env.ADDONS_PATH);
|
|
125
|
+
const dataDir = path.join(ADDONS_PATH, 'config', module, 'data');
|
|
126
|
+
await fs.mkdir(dataDir, { recursive: true });
|
|
127
|
+
|
|
128
|
+
const outPath = path.join(dataDir, 'mail_templates.xml');
|
|
129
|
+
await fs.writeFile(outPath, generateXml(templates), 'utf-8');
|
|
130
|
+
console.log(`Written: ${outPath}`);
|
|
131
|
+
|
|
132
|
+
if (opts.commit !== false) {
|
|
133
|
+
await execGit(['add', outPath], ADDONS_PATH);
|
|
134
|
+
await execGit(
|
|
135
|
+
['commit', '-m', `[IMP][${module}] Export email templates`],
|
|
136
|
+
ADDONS_PATH,
|
|
137
|
+
);
|
|
138
|
+
console.log('Committed.');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export { exportEmailTemplates };
|
package/src/index.js
CHANGED
|
@@ -8,34 +8,51 @@ import omelette from 'omelette';
|
|
|
8
8
|
import { autopr } from './commands/autopr.js';
|
|
9
9
|
import { changelog } from './commands/changelog.js';
|
|
10
10
|
import { commit } from './commands/commit.js';
|
|
11
|
-
import { exportPb, exportRip, exportImperex } from './commands/export.js';
|
|
11
|
+
import { exportPb, exportRip, exportImperex, exportEmailTemplates } from './commands/export.js';
|
|
12
12
|
import { init } from './commands/init.js';
|
|
13
13
|
import { main as prMain } from './commands/pr.js';
|
|
14
14
|
import { verbot } from './commands/ver.js';
|
|
15
15
|
import { CONFIG_FILE } from './config.js';
|
|
16
16
|
|
|
17
|
-
const
|
|
18
|
-
completion.on('command', ({ reply }) => {
|
|
19
|
-
reply(['pr', 'ver', 'init', 'changelog', 'autopr', 'commit']);
|
|
20
|
-
});
|
|
17
|
+
const EXPORT_SUBCOMMANDS = ['workflow', 'email-templates', 'pb', 'imperex', 'rip'];
|
|
21
18
|
|
|
22
|
-
|
|
23
|
-
if (['init', 'changelog', 'autopr'].includes(before)) {
|
|
24
|
-
reply([]);
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
19
|
+
function replyModules(reply) {
|
|
27
20
|
try {
|
|
28
21
|
const raw = readFileSync(CONFIG_FILE, 'utf-8');
|
|
29
22
|
const match = raw.match(/^ADDONS_PATH=(.+)$/m);
|
|
30
|
-
if (!match) {
|
|
31
|
-
reply([]);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
23
|
+
if (!match) { reply([]); return; }
|
|
34
24
|
const addonsPath = match[1].trim().replace(/^~/, process.env.HOME || '');
|
|
35
25
|
reply(readdirSync(path.join(addonsPath, 'config')));
|
|
36
26
|
} catch {
|
|
37
27
|
reply([]);
|
|
38
28
|
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const completion = omelette('prbot <command> <subOrModule> <module>');
|
|
32
|
+
completion.on('command', ({ reply }) => {
|
|
33
|
+
reply(['pr', 'ver', 'init', 'changelog', 'autopr', 'commit', 'export']);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// 2nd token: export subcommands or module (for non-export commands)
|
|
37
|
+
completion.on('subOrModule', ({ before, reply }) => {
|
|
38
|
+
if (before === 'export') {
|
|
39
|
+
reply(EXPORT_SUBCOMMANDS);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (['init', 'changelog', 'autopr'].includes(before)) {
|
|
43
|
+
reply([]);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
replyModules(reply);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// 3rd token: module (only relevant for export subcommands that take one)
|
|
50
|
+
completion.on('module', ({ before, reply }) => {
|
|
51
|
+
if (['workflow'].includes(before)) {
|
|
52
|
+
replyModules(reply);
|
|
53
|
+
} else {
|
|
54
|
+
reply([]);
|
|
55
|
+
}
|
|
39
56
|
});
|
|
40
57
|
|
|
41
58
|
completion.init();
|
|
@@ -147,6 +164,15 @@ exportCmd
|
|
|
147
164
|
});
|
|
148
165
|
});
|
|
149
166
|
|
|
167
|
+
exportCmd
|
|
168
|
+
.command('email-templates')
|
|
169
|
+
.option('--no-commit')
|
|
170
|
+
.action((opts) => {
|
|
171
|
+
exportEmailTemplates(opts).catch((err) => {
|
|
172
|
+
throw err;
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
150
176
|
program.command('update').action(() => {
|
|
151
177
|
console.log('Updating prbot...');
|
|
152
178
|
execFile('npm', ['i', '-g', '@waron97/prbot'], (error, stdout, stderr) => {
|