11import * as core from "@actions/core"
22import * as github from "@actions/github"
3- import { Context } from "@actions/github/lib/context"
43import { exec , ExecOptions } from "child_process"
54import * as fs from "fs"
65import * as path from "path"
7- import { dir } from "tmp"
86import { promisify } from "util"
97
108import { restoreCache , saveCache } from "./cache"
119import { install } from "./install"
12- import { alterDiffPatch } from "./utils/diffUtils "
10+ import { fetchPatch , isOnlyNewIssues } from "./patch "
1311
1412const 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
2214type 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-
14433type ExecRes = {
14534 stdout : string
14635 stderr : string
0 commit comments