@waron97/prbot 2.2.0 → 2.3.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waron97/prbot",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,7 @@
1
+ import { exportPb } from './exportPb.js';
2
+
3
+ function exportRip() {
4
+ console.log('Not implemented yet.');
5
+ }
6
+
7
+ export { exportPb, exportRip };
@@ -0,0 +1,156 @@
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
+
9
+ async function getProcessList(token) {
10
+ const url = `${process.env.IMPORTEXPORT_URL}/object/process_builder?addLanguageParam=true`;
11
+ const response = await fetch(url, {
12
+ method: 'POST',
13
+ headers: {
14
+ 'Content-Type': 'application/json',
15
+ Authorization: `Bearer ${token}`,
16
+ },
17
+ body: JSON.stringify({ page: 1, size: 999999, filters: [] }),
18
+ });
19
+ if (!response.ok) throw new Error(await response.text());
20
+ const json = await response.json();
21
+ return json.data;
22
+ }
23
+
24
+ async function initiateExport(guid, token) {
25
+ const url = `${process.env.IMPORTEXPORT_URL}/symphony/export`;
26
+ const response = await fetch(url, {
27
+ method: 'POST',
28
+ headers: {
29
+ 'Content-Type': 'application/json',
30
+ Authorization: `Bearer ${token}`,
31
+ },
32
+ body: JSON.stringify([{ object_guid: guid, object_type: 'process_builder' }]),
33
+ });
34
+ if (!response.ok) throw new Error(await response.text());
35
+ }
36
+
37
+ async function pollExportResult(guid, requestTime, token) {
38
+ const url = `${process.env.IMPORTEXPORT_URL}/export/info/processKey=ExportElement&subProcess=true&status=FAILED,COMPLETED&referenceId=process_builder`;
39
+ // Server createDate is offset -1hr from system time; subtract 1hr+5s buffer
40
+ const cutoff = requestTime - 3_605_000;
41
+
42
+ while (true) {
43
+ await new Promise((r) => setTimeout(r, 3000));
44
+
45
+ const response = await fetch(url, {
46
+ method: 'POST',
47
+ headers: {
48
+ 'Content-Type': 'application/json',
49
+ Authorization: `Bearer ${token}`,
50
+ },
51
+ body: JSON.stringify({ page: 1, size: 7, sorters: [] }),
52
+ });
53
+ if (!response.ok) throw new Error(await response.text());
54
+
55
+ const { data } = await response.json();
56
+ const match = data.find(
57
+ (item) =>
58
+ item.customResponse?.guid === guid &&
59
+ new Date(item.createdDate).getTime() >= cutoff
60
+ );
61
+
62
+ if (!match) continue;
63
+ if (match.status === 'FAILED') throw new Error(`Export failed for guid ${guid}`);
64
+ return match.requestId;
65
+ }
66
+ }
67
+
68
+ async function downloadZip(requestId, token) {
69
+ const url = `${process.env.IMPORTEXPORT_URL}/export/${requestId}`;
70
+ const response = await fetch(url, {
71
+ headers: {
72
+ 'Content-Type': 'application/json',
73
+ Authorization: `Bearer ${token}`,
74
+ },
75
+ });
76
+ if (!response.ok) throw new Error(await response.text());
77
+ return Buffer.from(await response.arrayBuffer());
78
+ }
79
+
80
+ async function findExistingZip(baseDir, filename) {
81
+ async function walk(dir) {
82
+ let entries;
83
+ try {
84
+ entries = await fs.readdir(dir, { withFileTypes: true });
85
+ } catch {
86
+ return null;
87
+ }
88
+ for (const entry of entries) {
89
+ const full = path.join(dir, entry.name);
90
+ if (entry.isDirectory()) {
91
+ const found = await walk(full);
92
+ if (found) return found;
93
+ } else if (entry.name === filename) {
94
+ return full;
95
+ }
96
+ }
97
+ return null;
98
+ }
99
+ return walk(baseDir);
100
+ }
101
+
102
+ async function exportPb(opts) {
103
+ const token = await getToken();
104
+
105
+ const processes = await getProcessList(token);
106
+ const choices = processes.map((p) => ({
107
+ name: `${p.process_name} (${p.document_id})`,
108
+ value: { guid: p.guid, document_id: p.document_id },
109
+ }));
110
+
111
+ const selected = await search({
112
+ message: 'Select PB process to export:',
113
+ source: async (input) => {
114
+ if (!input) return choices;
115
+ const q = input.toLowerCase();
116
+ return choices.filter((c) => c.name.toLowerCase().includes(q));
117
+ },
118
+ });
119
+
120
+ const { guid, document_id } = selected;
121
+ const filename = `${document_id}.zip`;
122
+
123
+ console.log(`Initiating export for ${document_id}...`);
124
+ const requestTime = Date.now();
125
+ await initiateExport(guid, token);
126
+
127
+ console.log('Waiting for export to complete...');
128
+ const requestId = await pollExportResult(guid, requestTime, token);
129
+
130
+ console.log(`Downloading ${filename}...`);
131
+ const zipBuffer = await downloadZip(requestId, token);
132
+
133
+ const ADDONS_PATH = resolveAddonsPath(process.env.ADDONS_PATH);
134
+ const processesDir = path.join(ADDONS_PATH, '.cloudbuild', 'pb', 'B2WA', 'processes');
135
+
136
+ const existing = await findExistingZip(processesDir, filename);
137
+ let savePath;
138
+ if (existing) {
139
+ savePath = existing;
140
+ await fs.writeFile(savePath, zipBuffer);
141
+ console.log(`Updated existing file at ${savePath}`);
142
+ } else {
143
+ savePath = path.join(processesDir, 'all', filename);
144
+ await fs.mkdir(path.dirname(savePath), { recursive: true });
145
+ await fs.writeFile(savePath, zipBuffer);
146
+ console.log(`Created new file at ${savePath}`);
147
+ }
148
+
149
+ if (opts.commit !== false) {
150
+ await execGit(['add', savePath], ADDONS_PATH);
151
+ await execGit(['commit', '-m', '[IMP][.cloudbuild] Update wizard'], ADDONS_PATH);
152
+ console.log('Committed.');
153
+ }
154
+ }
155
+
156
+ export { exportPb };
@@ -116,6 +116,14 @@ async function init(completion) {
116
116
  message: 'AutoPR target branch:',
117
117
  default: existing.AUTOPR_TARGET_BRANCH ?? '15.0-dev',
118
118
  },
119
+ {
120
+ type: 'input',
121
+ name: 'IMPORTEXPORT_URL',
122
+ message: 'ImportExport URL:',
123
+ default:
124
+ existing.IMPORTEXPORT_URL ??
125
+ 'https://sorgenia-test-02.symple.cloud/api/importexport/v1/',
126
+ },
119
127
  ]);
120
128
 
121
129
  if (!answers.KC_PASSWORD && existing.KC_PASSWORD) {
@@ -3,28 +3,7 @@ import fs from 'fs/promises';
3
3
  import path from 'path';
4
4
  import fetch from 'node-fetch';
5
5
  import { resolveAddonsPath } from '../lib/addons.js';
6
-
7
- async function getToken() {
8
- const url = process.env.KC_URL;
9
- const payload = new URLSearchParams();
10
-
11
- payload.append('username', process.env.KC_USER);
12
- payload.append('password', process.env.KC_PASSWORD);
13
- payload.append('client_id', process.env.KC_ID);
14
- payload.append('client_secret', process.env.KC_SECRET);
15
- payload.append('grant_type', 'password');
16
-
17
- const response = await fetch(url, {
18
- method: 'POST',
19
- headers: {
20
- 'Content-Type': 'application/x-www-form-urlencoded',
21
- },
22
- body: payload.toString(),
23
- });
24
-
25
- const json = await response.json();
26
- return json.access_token;
27
- }
6
+ import { getToken } from '../lib/auth.js';
28
7
 
29
8
  async function getFiles(module_name, token) {
30
9
  const url = `${process.env.RIP_URL}/ir.model/xml_prbot`;
package/src/index.js CHANGED
@@ -7,6 +7,7 @@ import omelette from 'omelette';
7
7
  import { autopr } from './commands/autopr.js';
8
8
  import { changelog } from './commands/changelog.js';
9
9
  import { commit } from './commands/commit.js';
10
+ import { exportPb, exportRip } from './commands/export.js';
10
11
  import { init } from './commands/init.js';
11
12
  import { main as prMain } from './commands/pr.js';
12
13
  import { verbot } from './commands/ver.js';
@@ -108,4 +109,32 @@ program.command('commit').action((opts) => {
108
109
  });
109
110
  });
110
111
 
112
+ const exportCmd = program.command('export');
113
+
114
+ exportCmd
115
+ .command('workflow <module>')
116
+ .option('-b, --bump <level>')
117
+ .action((module, opts) => {
118
+ prMain(module)
119
+ .then(() => {
120
+ if (opts.bump) {
121
+ return verbot(module, opts.bump);
122
+ }
123
+ })
124
+ .catch((err) => {
125
+ throw err;
126
+ });
127
+ });
128
+
129
+ exportCmd.command('rip').action(() => exportRip());
130
+
131
+ exportCmd
132
+ .command('pb')
133
+ .option('--no-commit')
134
+ .action((opts) => {
135
+ exportPb(opts).catch((err) => {
136
+ throw err;
137
+ });
138
+ });
139
+
111
140
  program.parse();
@@ -0,0 +1,25 @@
1
+ import fetch from 'node-fetch';
2
+
3
+ async function getToken() {
4
+ const url = process.env.KC_URL;
5
+ const payload = new URLSearchParams();
6
+
7
+ payload.append('username', process.env.KC_USER);
8
+ payload.append('password', process.env.KC_PASSWORD);
9
+ payload.append('client_id', process.env.KC_ID);
10
+ payload.append('client_secret', process.env.KC_SECRET);
11
+ payload.append('grant_type', 'password');
12
+
13
+ const response = await fetch(url, {
14
+ method: 'POST',
15
+ headers: {
16
+ 'Content-Type': 'application/x-www-form-urlencoded',
17
+ },
18
+ body: payload.toString(),
19
+ });
20
+
21
+ const json = await response.json();
22
+ return json.access_token;
23
+ }
24
+
25
+ export { getToken };