Skip to content

Commit fe61bc1

Browse files
committed
refactor: move parse issue and create issue to utils
1 parent 946e797 commit fe61bc1

File tree

5 files changed

+81
-58
lines changed

5 files changed

+81
-58
lines changed

__tests__/main.test.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main.ts

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import {
1111
} from './pullRequest'
1212
import {Condition, GhContext, IssueInfo, RecordBody} from './type'
1313
import {getViewerName} from './user'
14-
import {isPendingMergePr, isStatusCheckPassPr, stringify} from './utils'
14+
import {
15+
createIssueBody,
16+
isPendingMergePr,
17+
isStatusCheckPassPr,
18+
issueBodyPrefix,
19+
parseIssueBody,
20+
stringify
21+
} from './utils'
1522

1623
async function run(): Promise<void> {
1724
try {
@@ -169,36 +176,6 @@ async function getRecordIssue(
169176
}
170177
}
171178

172-
function parseIssueBody(body: string): RecordBody {
173-
try {
174-
const json = body
175-
.split(issueBodyStatusPrefix)
176-
.filter(e => e)
177-
.pop()
178-
?.split(issueBodyStatusSuffix)
179-
.filter(e => e)[0]
180-
return JSON.parse(json ?? '')
181-
} catch (e) {
182-
return {}
183-
}
184-
}
185-
186-
function createIssueBody(body: RecordBody): string {
187-
return `
188-
${issueBodyPrefix}
189-
This issue provides [lcdsmao/update-branch](https://github.com/lcdsmao/update-branch) status.
190-
191-
Status:
192-
193-
${issueBodyStatusPrefix}
194-
${stringify(body)}
195-
${issueBodyStatusSuffix}
196-
`
197-
}
198-
199179
const issueTitle = 'Update Branch Dashboard'
200-
const issueBodyPrefix = '<!-- lcdsmao/update-branch -->'
201-
const issueBodyStatusPrefix = '```json'
202-
const issueBodyStatusSuffix = '```'
203180

204181
run()

src/utils.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {createIssueBody, parseIssueBody} from './utils'
2+
3+
test('parseIssueBody', () => {
4+
const rawBody = `
5+
<!-- lcdsmao/update-branch -->
6+
This issue provides [lcdsmao/update-branch](https://github.com/lcdsmao/update-branch) status.
7+
8+
Status:
9+
10+
\`\`\`json
11+
{
12+
"editing": false,
13+
"pendingMergePullRequestNumber": 1234
14+
}
15+
\`\`\`
16+
17+
`
18+
const body = parseIssueBody(rawBody)
19+
expect(body).toEqual({editing: false, pendingMergePullRequestNumber: 1234})
20+
})
21+
22+
test('createIssueBody', () => {
23+
const body = createIssueBody({
24+
editing: true,
25+
pendingMergePullRequestNumber: 2048
26+
})
27+
expect(body).toEqual(`
28+
<!-- lcdsmao/update-branch -->
29+
This issue provides [lcdsmao/update-branch](https://github.com/lcdsmao/update-branch) status.
30+
31+
Status:
32+
33+
\`\`\`json
34+
{
35+
"editing": true,
36+
"pendingMergePullRequestNumber": 2048
37+
}
38+
\`\`\`
39+
`)
40+
})

src/utils.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import minimatch from 'minimatch'
2-
import {Condition, PullRequestInfo} from './type'
2+
import {Condition, PullRequestInfo, RecordBody} from './type'
33

44
export function isPendingMergePr(
55
pr: PullRequestInfo,
@@ -64,3 +64,34 @@ function isStatusChecksSuccess(
6464
return check.state === 'SUCCESS'
6565
}
6666
}
67+
68+
export function parseIssueBody(body: string): RecordBody {
69+
try {
70+
const json = body
71+
.split(issueBodyStatusPrefix)
72+
.filter(e => e)
73+
.pop()
74+
?.split(issueBodyStatusSuffix)
75+
.filter(e => e)[0]
76+
return JSON.parse(json ?? '')
77+
} catch (e) {
78+
return {}
79+
}
80+
}
81+
82+
export function createIssueBody(body: RecordBody): string {
83+
return `
84+
${issueBodyPrefix}
85+
This issue provides [lcdsmao/update-branch](https://github.com/lcdsmao/update-branch) status.
86+
87+
Status:
88+
89+
${issueBodyStatusPrefix}
90+
${stringify(body)}
91+
${issueBodyStatusSuffix}
92+
`
93+
}
94+
95+
export const issueBodyPrefix = '<!-- lcdsmao/update-branch -->'
96+
export const issueBodyStatusPrefix = '```json'
97+
export const issueBodyStatusSuffix = '```'

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
99
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
1010
},
11-
"exclude": ["node_modules", "**/*.test.ts"]
11+
"exclude": ["node_modules"]
1212
}

0 commit comments

Comments
 (0)