@jpmorganchase/mosaic-workflows 0.1.0-beta.84 → 0.1.0-beta.86

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.
@@ -0,0 +1,19 @@
1
+ import { GitRepoSourceOptions } from '@jpmorganchase/mosaic-source-git-repo';
2
+ import type { SendSourceWorkflowMessage, SourceWorkflow } from '@jpmorganchase/mosaic-types';
3
+ interface GitHubPullRequestWorkflowData {
4
+ user: {
5
+ id: string;
6
+ name: string;
7
+ email: string;
8
+ };
9
+ markdown: string;
10
+ }
11
+ interface GitHubPullRequestWorkflowOptions {
12
+ apiEndpoint: string;
13
+ commitMessage: (filePath: string) => string;
14
+ titlePrefix: string;
15
+ proxyEndpoint?: string;
16
+ }
17
+ export declare function createPullRequest(sendWorkflowProgressMessage: SendSourceWorkflowMessage, sourceOptions: GitRepoSourceOptions, { apiEndpoint, commitMessage, titlePrefix, proxyEndpoint }: GitHubPullRequestWorkflowOptions, filePath: string, { user, markdown }: GitHubPullRequestWorkflowData): Promise<any>;
18
+ declare const workflow: SourceWorkflow;
19
+ export default workflow;
@@ -0,0 +1,116 @@
1
+ import { Octokit } from '@octokit/core';
2
+ import path from 'node:path';
3
+ import fs from 'node:fs';
4
+ import { v4 as uuidv4 } from 'uuid';
5
+ import { Repo } from '@jpmorganchase/mosaic-source-git-repo';
6
+ import { mdx } from '@jpmorganchase/mosaic-serialisers';
7
+ import { ProxyAgent, fetch as undiciFetch } from 'undici';
8
+ function getErrorMessage(error) {
9
+ if (error instanceof Error)
10
+ return error.message;
11
+ return String(error);
12
+ }
13
+ export async function createPullRequest(sendWorkflowProgressMessage, sourceOptions, { apiEndpoint, commitMessage, titlePrefix, proxyEndpoint }, filePath, { user, markdown }) {
14
+ const { credentials, remote, branch: sourceBranch, repo: repoUrl, subfolder, prefixDir } = sourceOptions;
15
+ if (!repoUrl || !markdown) {
16
+ return false;
17
+ }
18
+ let repoInstance = new Repo(credentials, remote, sourceBranch, repoUrl);
19
+ await repoInstance.init();
20
+ const userId = user.id.toLowerCase();
21
+ sendWorkflowProgressMessage('GitHub clone complete', 'IN_PROGRESS');
22
+ const branchName = `${userId}-${uuidv4()}`;
23
+ await repoInstance.createWorktree(userId, branchName);
24
+ sendWorkflowProgressMessage('Created git worktree', 'IN_PROGRESS');
25
+ /**
26
+ * strip out the namespace from the file path.
27
+ * We are interested in the file on disk not in the VFS
28
+ */
29
+ const pathOnDisk = path.posix.join(repoInstance.dir, subfolder, filePath.replace(new RegExp(`${prefixDir}/`), ''));
30
+ const rawPage = await fs.promises.readFile(pathOnDisk);
31
+ const { content, ...metadata } = await mdx.deserialise(pathOnDisk, rawPage);
32
+ const updatedPage = { ...metadata, content: markdown };
33
+ sendWorkflowProgressMessage('Updated page content', 'IN_PROGRESS');
34
+ await fs.promises.writeFile(pathOnDisk, await mdx.serialise(pathOnDisk, updatedPage));
35
+ sendWorkflowProgressMessage('Saved page', 'IN_PROGRESS');
36
+ /** create a new fetcher with proxy agent configured if required. */
37
+ const fetcher = proxyEndpoint
38
+ ? (url, opts) => {
39
+ return undiciFetch(url, {
40
+ ...opts,
41
+ dispatcher: new ProxyAgent({
42
+ uri: proxyEndpoint,
43
+ keepAliveTimeout: 10,
44
+ keepAliveMaxTimeout: 10
45
+ })
46
+ });
47
+ }
48
+ : undiciFetch;
49
+ const token = credentials.split(':')[1];
50
+ // get a new client
51
+ const githubClient = new Octokit({
52
+ auth: token,
53
+ request: {
54
+ fetch: fetcher,
55
+ timeout: 5000
56
+ }
57
+ });
58
+ sendWorkflowProgressMessage('Creating Pull Request', 'IN_PROGRESS');
59
+ let prResult = null;
60
+ try {
61
+ await repoInstance.configureGitUser(user.name, user.email);
62
+ await repoInstance.addChanges();
63
+ await repoInstance.commitChanges(user.name, user.email, commitMessage(filePath));
64
+ await repoInstance.pushBranch(branchName);
65
+ const result = await githubClient.request(`POST ${apiEndpoint}`, {
66
+ owner: repoInstance.projectName,
67
+ repo: repoInstance.repoName,
68
+ title: `${titlePrefix} - Content update - ${filePath}`,
69
+ body: commitMessage(filePath),
70
+ head: branchName,
71
+ base: sourceBranch,
72
+ headers: {
73
+ 'X-GitHub-Api-Version': '2022-11-28'
74
+ }
75
+ });
76
+ /**
77
+ * https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#create-a-pull-request
78
+ * Status 201 = created
79
+ */
80
+ if (result.status === 201) {
81
+ prResult = result.data.url;
82
+ }
83
+ else {
84
+ throw new Error(`${result.data.status} - ${result.data.message}`);
85
+ }
86
+ }
87
+ catch (e) {
88
+ console.group('[Mosaic] Pull Request Error');
89
+ console.log('fullPath', filePath);
90
+ console.log('Head', branchName);
91
+ console.log('Base', sourceBranch);
92
+ console.error(e);
93
+ console.groupEnd();
94
+ prResult = {
95
+ error: `Error creating Pull Request: ${getErrorMessage(e)} `,
96
+ source: `${repoInstance.name}`
97
+ };
98
+ }
99
+ finally {
100
+ await repoInstance.removeWorktree(userId);
101
+ repoInstance = null;
102
+ }
103
+ sendWorkflowProgressMessage(prResult, 'COMPLETE');
104
+ return prResult;
105
+ }
106
+ const workflow = {
107
+ name: 'save',
108
+ options: {
109
+ titlePrefix: 'Mosaic Docs',
110
+ apiEndpoint: 'https://api.github.com/repos/{owner}/{repo}/pulls',
111
+ commitMessage: (filePath) => `docs: updated content ${filePath} (UIE-7026)`
112
+ },
113
+ action: createPullRequest
114
+ };
115
+ export default workflow;
116
+ //# sourceMappingURL=GitHubPullRequestWorkflow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GitHubPullRequestWorkflow.js","sourceRoot":"","sources":["../src/GitHubPullRequestWorkflow.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,IAAI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QAChC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QAClC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QACjB,OAAO,CAAC,QAAQ,EAAE,CAAC;QAEnB,QAAQ,GAAG;YACT,KAAK,EAAE,gCAAgC,eAAe,CAAC,CAAC,CAAC,GAAG;YAC5D,MAAM,EAAE,GAAG,YAAY,CAAC,IAAI,EAAE;SAC/B,CAAC;KACH;YAAS;QACR,MAAM,YAAY,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QAC1C,YAAY,GAAG,IAAI,CAAC;KACrB;IAED,2BAA2B,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IAClD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,MAAM,QAAQ,GAAmB;IAC/B,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE;QACP,WAAW,EAAE,aAAa;QAC1B,WAAW,EAAE,mDAAmD;QAChE,aAAa,EAAE,CAAC,QAAgB,EAAE,EAAE,CAAC,yBAAyB,QAAQ,aAAa;KACpF;IACD,MAAM,EAAE,iBAAiB;CAC1B,CAAC;AAEF,eAAe,QAAQ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,17 @@
1
+ import type { SourceWorkflow } from '@jpmorganchase/mosaic-types';
1
2
  import BitbucketPullRequestWorkflow from './BitbucketPullRequestWorkflow.js';
2
- export { BitbucketPullRequestWorkflow };
3
+ import GitHubPullRequestWorkflow from './GitHubPullRequestWorkflow.js';
4
+ export { BitbucketPullRequestWorkflow, GitHubPullRequestWorkflow };
5
+ /**
6
+ * Helper function to apply additional or overwrite existing workflow options
7
+ * @param workflow the workflow to modify
8
+ * @param options the new/updated options
9
+ * @returns Workflow with options updated
10
+ *
11
+ * @example
12
+ * import { GitHubPullRequestWorkflow, applyWorkflowOptions } from "@jpmorganchase/mosaic-workflows";
13
+ *
14
+ * applyWorkflowOptions( GitHubPullRequestWorkflow, {proxyEndpoint: 'http://your.proxy.com'})
15
+ *
16
+ */
17
+ export declare const applyWorkflowOptions: (workflow: SourceWorkflow, options: Record<string, unknown>) => SourceWorkflow;
package/dist/index.js CHANGED
@@ -1,3 +1,23 @@
1
1
  import BitbucketPullRequestWorkflow from './BitbucketPullRequestWorkflow.js';
2
- export { BitbucketPullRequestWorkflow };
2
+ import GitHubPullRequestWorkflow from './GitHubPullRequestWorkflow.js';
3
+ export { BitbucketPullRequestWorkflow, GitHubPullRequestWorkflow };
4
+ /**
5
+ * Helper function to apply additional or overwrite existing workflow options
6
+ * @param workflow the workflow to modify
7
+ * @param options the new/updated options
8
+ * @returns Workflow with options updated
9
+ *
10
+ * @example
11
+ * import { GitHubPullRequestWorkflow, applyWorkflowOptions } from "@jpmorganchase/mosaic-workflows";
12
+ *
13
+ * applyWorkflowOptions( GitHubPullRequestWorkflow, {proxyEndpoint: 'http://your.proxy.com'})
14
+ *
15
+ */
16
+ export const applyWorkflowOptions = (workflow, options) => {
17
+ const { options: workflowBaseOptions, ...restWorkflow } = workflow;
18
+ return {
19
+ ...restWorkflow,
20
+ options: { ...workflowBaseOptions, ...options }
21
+ };
22
+ };
3
23
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,mCAAmC,CAAC;AAE7E,OAAO,EAAE,4BAA4B,EAAE,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,4BAA4B,MAAM,mCAAmC,CAAC;AAC7E,OAAO,yBAAyB,MAAM,gCAAgC,CAAC;AAEvE,OAAO,EAAE,4BAA4B,EAAE,yBAAyB,EAAE,CAAC;AAEnE;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAClC,QAAwB,EACxB,OAAgC,EAChB,EAAE;IAClB,MAAM,EAAE,OAAO,EAAE,mBAAmB,EAAE,GAAG,YAAY,EAAE,GAAG,QAAQ,CAAC;IAEnE,OAAO;QACL,GAAG,YAAY;QACf,OAAO,EAAE,EAAE,GAAG,mBAAmB,EAAE,GAAG,OAAO,EAAE;KAChD,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jpmorganchase/mosaic-workflows",
3
- "version": "0.1.0-beta.84",
3
+ "version": "0.1.0-beta.86",
4
4
  "description": "Mosaic workflows for core file system",
5
5
  "license": "Apache-2.0",
6
6
  "author": "",
@@ -15,7 +15,7 @@
15
15
  }
16
16
  },
17
17
  "engines": {
18
- "node": ">=16.10.0 || >=18.0.0"
18
+ "node": ">=18.0.0 || >=20.0.0"
19
19
  },
20
20
  "files": [
21
21
  "dist"
@@ -34,9 +34,11 @@
34
34
  "@types/uuid": "^8.3.4"
35
35
  },
36
36
  "dependencies": {
37
- "@jpmorganchase/mosaic-serialisers": "^0.1.0-beta.84",
38
- "@jpmorganchase/mosaic-source-git-repo": "^0.1.0-beta.84",
39
- "@jpmorganchase/mosaic-types": "^0.1.0-beta.84",
37
+ "@jpmorganchase/mosaic-serialisers": "^0.1.0-beta.86",
38
+ "@jpmorganchase/mosaic-source-git-repo": "^0.1.0-beta.86",
39
+ "@jpmorganchase/mosaic-types": "^0.1.0-beta.86",
40
+ "@octokit/core": "^6.1.2",
41
+ "undici": "^6.19.5",
40
42
  "uuid": "^7.0.3"
41
43
  }
42
44
  }