Skip to content

Commit f4bf83e

Browse files
committed
chore: extract patch related code
1 parent f8b3ec1 commit f4bf83e

File tree

2 files changed

+120
-112
lines changed

2 files changed

+120
-112
lines changed

src/patch.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import * as core from "@actions/core"
2+
import * as github from "@actions/github"
3+
import { Context } from "@actions/github/lib/context"
4+
import fs from "fs"
5+
import path from "path"
6+
import { dir } from "tmp"
7+
import { promisify } from "util"
8+
9+
import { alterDiffPatch } from "./utils/diffUtils"
10+
11+
const writeFile = promisify(fs.writeFile)
12+
const createTempDir = promisify(dir)
13+
14+
export function isOnlyNewIssues(): boolean {
15+
return core.getBooleanInput(`only-new-issues`, { required: true })
16+
}
17+
18+
export async function fetchPatch(): Promise<string> {
19+
if (!isOnlyNewIssues()) {
20+
return ``
21+
}
22+
23+
const ctx = github.context
24+
25+
switch (ctx.eventName) {
26+
case `pull_request`:
27+
case `pull_request_target`:
28+
return await fetchPullRequestPatch(ctx)
29+
case `push`:
30+
return await fetchPushPatch(ctx)
31+
case `merge_group`:
32+
return ``
33+
default:
34+
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`)
35+
return ``
36+
}
37+
}
38+
39+
async function fetchPullRequestPatch(ctx: Context): Promise<string> {
40+
const pr = ctx.payload.pull_request
41+
if (!pr) {
42+
core.warning(`No pull request in context`)
43+
return ``
44+
}
45+
46+
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
47+
48+
let patch: string
49+
try {
50+
const patchResp = await octokit.rest.pulls.get({
51+
owner: ctx.repo.owner,
52+
repo: ctx.repo.repo,
53+
[`pull_number`]: pr.number,
54+
mediaType: {
55+
format: `diff`,
56+
},
57+
})
58+
59+
if (patchResp.status !== 200) {
60+
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`)
61+
return `` // don't fail the action, but analyze without patch
62+
}
63+
64+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
65+
patch = patchResp.data as any
66+
} catch (err) {
67+
console.warn(`failed to fetch pull request patch:`, err)
68+
return `` // don't fail the action, but analyze without patch
69+
}
70+
71+
try {
72+
const tempDir = await createTempDir()
73+
const patchPath = path.join(tempDir, "pull.patch")
74+
core.info(`Writing patch to ${patchPath}`)
75+
await writeFile(patchPath, alterDiffPatch(patch))
76+
return patchPath
77+
} catch (err) {
78+
console.warn(`failed to save pull request patch:`, err)
79+
return `` // don't fail the action, but analyze without patch
80+
}
81+
}
82+
83+
async function fetchPushPatch(ctx: Context): Promise<string> {
84+
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
85+
86+
let patch: string
87+
try {
88+
const patchResp = await octokit.rest.repos.compareCommitsWithBasehead({
89+
owner: ctx.repo.owner,
90+
repo: ctx.repo.repo,
91+
basehead: `${ctx.payload.before}...${ctx.payload.after}`,
92+
mediaType: {
93+
format: `diff`,
94+
},
95+
})
96+
97+
if (patchResp.status !== 200) {
98+
core.warning(`failed to fetch push patch: response status is ${patchResp.status}`)
99+
return `` // don't fail the action, but analyze without patch
100+
}
101+
102+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103+
patch = patchResp.data as any
104+
} catch (err) {
105+
console.warn(`failed to fetch push patch:`, err)
106+
return `` // don't fail the action, but analyze without patch
107+
}
108+
109+
try {
110+
const tempDir = await createTempDir()
111+
const patchPath = path.join(tempDir, "push.patch")
112+
core.info(`Writing patch to ${patchPath}`)
113+
await writeFile(patchPath, alterDiffPatch(patch))
114+
return patchPath
115+
} catch (err) {
116+
console.warn(`failed to save pull request patch:`, err)
117+
return `` // don't fail the action, but analyze without patch
118+
}
119+
}

src/run.ts

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import * as core from "@actions/core"
22
import * as github from "@actions/github"
3-
import { Context } from "@actions/github/lib/context"
43
import { exec, ExecOptions } from "child_process"
54
import * as fs from "fs"
65
import * as path from "path"
7-
import { dir } from "tmp"
86
import { promisify } from "util"
97

108
import { restoreCache, saveCache } from "./cache"
119
import { install } from "./install"
12-
import { alterDiffPatch } from "./utils/diffUtils"
10+
import { fetchPatch, isOnlyNewIssues } from "./patch"
1311

1412
const execShellCommand = promisify(exec)
15-
const writeFile = promisify(fs.writeFile)
16-
const createTempDir = promisify(dir)
17-
18-
function isOnlyNewIssues(): boolean {
19-
return core.getBooleanInput(`only-new-issues`, { required: true })
20-
}
2113

2214
type Env = {
2315
binPath: string
@@ -38,109 +30,6 @@ async function prepareEnv(): Promise<Env> {
3830
return { binPath, patchPath }
3931
}
4032

41-
async function fetchPatch(): Promise<string> {
42-
if (!isOnlyNewIssues()) {
43-
return ``
44-
}
45-
46-
const ctx = github.context
47-
48-
switch (ctx.eventName) {
49-
case `pull_request`:
50-
case `pull_request_target`:
51-
return await fetchPullRequestPatch(ctx)
52-
case `push`:
53-
return await fetchPushPatch(ctx)
54-
case `merge_group`:
55-
return ``
56-
default:
57-
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`)
58-
return ``
59-
}
60-
}
61-
62-
async function fetchPullRequestPatch(ctx: Context): Promise<string> {
63-
const pr = ctx.payload.pull_request
64-
if (!pr) {
65-
core.warning(`No pull request in context`)
66-
return ``
67-
}
68-
69-
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
70-
71-
let patch: string
72-
try {
73-
const patchResp = await octokit.rest.pulls.get({
74-
owner: ctx.repo.owner,
75-
repo: ctx.repo.repo,
76-
[`pull_number`]: pr.number,
77-
mediaType: {
78-
format: `diff`,
79-
},
80-
})
81-
82-
if (patchResp.status !== 200) {
83-
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`)
84-
return `` // don't fail the action, but analyze without patch
85-
}
86-
87-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88-
patch = patchResp.data as any
89-
} catch (err) {
90-
console.warn(`failed to fetch pull request patch:`, err)
91-
return `` // don't fail the action, but analyze without patch
92-
}
93-
94-
try {
95-
const tempDir = await createTempDir()
96-
const patchPath = path.join(tempDir, "pull.patch")
97-
core.info(`Writing patch to ${patchPath}`)
98-
await writeFile(patchPath, alterDiffPatch(patch))
99-
return patchPath
100-
} catch (err) {
101-
console.warn(`failed to save pull request patch:`, err)
102-
return `` // don't fail the action, but analyze without patch
103-
}
104-
}
105-
106-
async function fetchPushPatch(ctx: Context): Promise<string> {
107-
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
108-
109-
let patch: string
110-
try {
111-
const patchResp = await octokit.rest.repos.compareCommitsWithBasehead({
112-
owner: ctx.repo.owner,
113-
repo: ctx.repo.repo,
114-
basehead: `${ctx.payload.before}...${ctx.payload.after}`,
115-
mediaType: {
116-
format: `diff`,
117-
},
118-
})
119-
120-
if (patchResp.status !== 200) {
121-
core.warning(`failed to fetch push patch: response status is ${patchResp.status}`)
122-
return `` // don't fail the action, but analyze without patch
123-
}
124-
125-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
126-
patch = patchResp.data as any
127-
} catch (err) {
128-
console.warn(`failed to fetch push patch:`, err)
129-
return `` // don't fail the action, but analyze without patch
130-
}
131-
132-
try {
133-
const tempDir = await createTempDir()
134-
const patchPath = path.join(tempDir, "push.patch")
135-
core.info(`Writing patch to ${patchPath}`)
136-
await writeFile(patchPath, alterDiffPatch(patch))
137-
return patchPath
138-
} catch (err) {
139-
console.warn(`failed to save pull request patch:`, err)
140-
return `` // don't fail the action, but analyze without patch
141-
}
142-
}
143-
14433
type ExecRes = {
14534
stdout: string
14635
stderr: string

0 commit comments

Comments
 (0)