Skip to content

Commit 7c950bd

Browse files
committed
Use default branch for only-new-issues when push event
1 parent 88a6133 commit 7c950bd

File tree

4 files changed

+180
-30
lines changed

4 files changed

+180
-30
lines changed

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodejs lts

dist/post_run/index.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66938,17 +66938,42 @@ function fetchPatch() {
6693866938
return ``;
6693966939
}
6694066940
const ctx = github.context;
66941-
if (ctx.eventName !== `pull_request`) {
66942-
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`);
66941+
let patch;
66942+
if (ctx.eventName === `pull_request`) {
66943+
patch = yield patchFromPR(ctx);
66944+
}
66945+
else if (ctx.eventName === `push`) {
66946+
patch = yield patchFromPush(ctx);
66947+
}
66948+
else {
66949+
core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`);
66950+
return ``;
66951+
}
66952+
if (!patch) {
66953+
core.info(`Not using patch for showing only new issues because it's empty`);
6694366954
return ``;
6694466955
}
66956+
try {
66957+
const tempDir = yield createTempDir();
66958+
const patchPath = path.join(tempDir, "pull.patch");
66959+
core.info(`Writing patch to ${patchPath}`);
66960+
yield writeFile(patchPath, patch);
66961+
return patchPath;
66962+
}
66963+
catch (err) {
66964+
console.warn(`failed to save pull request patch:`, err);
66965+
return ``; // don't fail the action, but analyze without patch
66966+
}
66967+
});
66968+
}
66969+
function patchFromPR(ctx) {
66970+
return __awaiter(this, void 0, void 0, function* () {
6694566971
const pull = ctx.payload.pull_request;
6694666972
if (!pull) {
6694766973
core.warning(`No pull request in context`);
6694866974
return ``;
6694966975
}
6695066976
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
66951-
let patch;
6695266977
try {
6695366978
const patchResp = yield octokit.rest.pulls.get({
6695466979
owner: ctx.repo.owner,
@@ -66963,21 +66988,45 @@ function fetchPatch() {
6696366988
return ``; // don't fail the action, but analyze without patch
6696466989
}
6696566990
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66966-
patch = patchResp.data;
66991+
return patchResp.data;
6696766992
}
6696866993
catch (err) {
6696966994
console.warn(`failed to fetch pull request patch:`, err);
6697066995
return ``; // don't fail the action, but analyze without patch
6697166996
}
66997+
});
66998+
}
66999+
function patchFromPush(ctx) {
67000+
return __awaiter(this, void 0, void 0, function* () {
67001+
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
6697267002
try {
66973-
const tempDir = yield createTempDir();
66974-
const patchPath = path.join(tempDir, "pull.patch");
66975-
core.info(`Writing patch to ${patchPath}`);
66976-
yield writeFile(patchPath, patch);
66977-
return patchPath;
67003+
const repoResp = yield octokit.rest.repos.get({
67004+
owner: ctx.repo.owner,
67005+
repo: ctx.repo.repo,
67006+
});
67007+
if (repoResp.status !== 200) {
67008+
core.warning(`failed to fetch repo: response status is ${repoResp.status}`);
67009+
return ``;
67010+
}
67011+
const defaultBranch = repoResp.data.default_branch;
67012+
const patchResp = yield octokit.rest.repos.compareCommits({
67013+
owner: ctx.repo.owner,
67014+
repo: ctx.repo.repo,
67015+
base: defaultBranch,
67016+
head: ctx.sha,
67017+
mediaType: {
67018+
format: `diff`,
67019+
}
67020+
});
67021+
if (patchResp.status !== 200) {
67022+
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`);
67023+
return ``; // don't fail the action, but analyze without patch
67024+
}
67025+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67026+
return patchResp.data;
6697867027
}
6697967028
catch (err) {
66980-
console.warn(`failed to save pull request patch:`, err);
67029+
console.warn(`failed to fetch push patch:`, err);
6698167030
return ``; // don't fail the action, but analyze without patch
6698267031
}
6698367032
});

dist/run/index.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66938,17 +66938,42 @@ function fetchPatch() {
6693866938
return ``;
6693966939
}
6694066940
const ctx = github.context;
66941-
if (ctx.eventName !== `pull_request`) {
66942-
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`);
66941+
let patch;
66942+
if (ctx.eventName === `pull_request`) {
66943+
patch = yield patchFromPR(ctx);
66944+
}
66945+
else if (ctx.eventName === `push`) {
66946+
patch = yield patchFromPush(ctx);
66947+
}
66948+
else {
66949+
core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`);
66950+
return ``;
66951+
}
66952+
if (!patch) {
66953+
core.info(`Not using patch for showing only new issues because it's empty`);
6694366954
return ``;
6694466955
}
66956+
try {
66957+
const tempDir = yield createTempDir();
66958+
const patchPath = path.join(tempDir, "pull.patch");
66959+
core.info(`Writing patch to ${patchPath}`);
66960+
yield writeFile(patchPath, patch);
66961+
return patchPath;
66962+
}
66963+
catch (err) {
66964+
console.warn(`failed to save pull request patch:`, err);
66965+
return ``; // don't fail the action, but analyze without patch
66966+
}
66967+
});
66968+
}
66969+
function patchFromPR(ctx) {
66970+
return __awaiter(this, void 0, void 0, function* () {
6694566971
const pull = ctx.payload.pull_request;
6694666972
if (!pull) {
6694766973
core.warning(`No pull request in context`);
6694866974
return ``;
6694966975
}
6695066976
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
66951-
let patch;
6695266977
try {
6695366978
const patchResp = yield octokit.rest.pulls.get({
6695466979
owner: ctx.repo.owner,
@@ -66963,21 +66988,45 @@ function fetchPatch() {
6696366988
return ``; // don't fail the action, but analyze without patch
6696466989
}
6696566990
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66966-
patch = patchResp.data;
66991+
return patchResp.data;
6696766992
}
6696866993
catch (err) {
6696966994
console.warn(`failed to fetch pull request patch:`, err);
6697066995
return ``; // don't fail the action, but analyze without patch
6697166996
}
66997+
});
66998+
}
66999+
function patchFromPush(ctx) {
67000+
return __awaiter(this, void 0, void 0, function* () {
67001+
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
6697267002
try {
66973-
const tempDir = yield createTempDir();
66974-
const patchPath = path.join(tempDir, "pull.patch");
66975-
core.info(`Writing patch to ${patchPath}`);
66976-
yield writeFile(patchPath, patch);
66977-
return patchPath;
67003+
const repoResp = yield octokit.rest.repos.get({
67004+
owner: ctx.repo.owner,
67005+
repo: ctx.repo.repo,
67006+
});
67007+
if (repoResp.status !== 200) {
67008+
core.warning(`failed to fetch repo: response status is ${repoResp.status}`);
67009+
return ``;
67010+
}
67011+
const defaultBranch = repoResp.data.default_branch;
67012+
const patchResp = yield octokit.rest.repos.compareCommits({
67013+
owner: ctx.repo.owner,
67014+
repo: ctx.repo.repo,
67015+
base: defaultBranch,
67016+
head: ctx.sha,
67017+
mediaType: {
67018+
format: `diff`,
67019+
}
67020+
});
67021+
if (patchResp.status !== 200) {
67022+
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`);
67023+
return ``; // don't fail the action, but analyze without patch
67024+
}
67025+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67026+
return patchResp.data;
6697867027
}
6697967028
catch (err) {
66980-
console.warn(`failed to save pull request patch:`, err);
67029+
console.warn(`failed to fetch push patch:`, err);
6698167030
return ``; // don't fail the action, but analyze without patch
6698267031
}
6698367032
});

src/run.ts

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as core from "@actions/core"
22
import * as github from "@actions/github"
3+
import { Context } from '@actions/github/lib/context'
34
import { exec, ExecOptions } from "child_process"
45
import * as fs from "fs"
56
import * as path from "path"
@@ -29,17 +30,40 @@ async function fetchPatch(): Promise<string> {
2930
}
3031

3132
const ctx = github.context
32-
if (ctx.eventName !== `pull_request`) {
33-
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`)
33+
let patch: string
34+
if (ctx.eventName === `pull_request`) {
35+
patch = await patchFromPR(ctx)
36+
} else if (ctx.eventName === `push`) {
37+
patch = await patchFromPush(ctx)
38+
} else {
39+
core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`)
40+
return ``
41+
}
42+
43+
if (!patch) {
44+
core.info(`Not using patch for showing only new issues because it's empty`)
3445
return ``
3546
}
47+
48+
try {
49+
const tempDir = await createTempDir()
50+
const patchPath = path.join(tempDir, "pull.patch")
51+
core.info(`Writing patch to ${patchPath}`)
52+
await writeFile(patchPath, patch)
53+
return patchPath
54+
} catch (err) {
55+
console.warn(`failed to save pull request patch:`, err)
56+
return `` // don't fail the action, but analyze without patch
57+
}
58+
}
59+
60+
async function patchFromPR(ctx: Context): Promise<string> {
3661
const pull = ctx.payload.pull_request
3762
if (!pull) {
3863
core.warning(`No pull request in context`)
3964
return ``
4065
}
4166
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
42-
let patch: string
4367
try {
4468
const patchResp = await octokit.rest.pulls.get({
4569
owner: ctx.repo.owner,
@@ -56,20 +80,47 @@ async function fetchPatch(): Promise<string> {
5680
}
5781

5882
// eslint-disable-next-line @typescript-eslint/no-explicit-any
59-
patch = patchResp.data as any
83+
return patchResp.data as any
6084
} catch (err) {
6185
console.warn(`failed to fetch pull request patch:`, err)
6286
return `` // don't fail the action, but analyze without patch
6387
}
88+
}
6489

90+
async function patchFromPush(ctx: Context): Promise<string> {
91+
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
6592
try {
66-
const tempDir = await createTempDir()
67-
const patchPath = path.join(tempDir, "pull.patch")
68-
core.info(`Writing patch to ${patchPath}`)
69-
await writeFile(patchPath, patch)
70-
return patchPath
93+
const repoResp = await octokit.rest.repos.get({
94+
owner: ctx.repo.owner,
95+
repo: ctx.repo.repo,
96+
})
97+
98+
if (repoResp.status !== 200) {
99+
core.warning(`failed to fetch repo: response status is ${repoResp.status}`)
100+
return ``
101+
}
102+
103+
const defaultBranch = repoResp.data.default_branch
104+
105+
const patchResp = await octokit.rest.repos.compareCommits({
106+
owner: ctx.repo.owner,
107+
repo: ctx.repo.repo,
108+
base: defaultBranch,
109+
head: ctx.sha,
110+
mediaType: {
111+
format: `diff`,
112+
}
113+
})
114+
115+
if (patchResp.status !== 200) {
116+
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`)
117+
return `` // don't fail the action, but analyze without patch
118+
}
119+
120+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
121+
return patchResp.data as any
71122
} catch (err) {
72-
console.warn(`failed to save pull request patch:`, err)
123+
console.warn(`failed to fetch push patch:`, err)
73124
return `` // don't fail the action, but analyze without patch
74125
}
75126
}

0 commit comments

Comments
 (0)