Skip to content

Commit 3de9823

Browse files
szechyjsldez
authored andcommitted
Use default branch for only-new-issues when push event
1 parent fdf641b commit 3de9823

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
@@ -67782,17 +67782,42 @@ function fetchPatch() {
6778267782
return ``;
6778367783
}
6778467784
const ctx = github.context;
67785-
if (ctx.eventName !== `pull_request`) {
67786-
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`);
67785+
let patch;
67786+
if (ctx.eventName === `pull_request`) {
67787+
patch = yield patchFromPR(ctx);
67788+
}
67789+
else if (ctx.eventName === `push`) {
67790+
patch = yield patchFromPush(ctx);
67791+
}
67792+
else {
67793+
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}`);
67794+
return ``;
67795+
}
67796+
if (!patch) {
67797+
core.info(`Not using patch for showing only new issues because it's empty`);
6778767798
return ``;
6778867799
}
67800+
try {
67801+
const tempDir = yield createTempDir();
67802+
const patchPath = path.join(tempDir, "pull.patch");
67803+
core.info(`Writing patch to ${patchPath}`);
67804+
yield writeFile(patchPath, patch);
67805+
return patchPath;
67806+
}
67807+
catch (err) {
67808+
console.warn(`failed to save pull request patch:`, err);
67809+
return ``; // don't fail the action, but analyze without patch
67810+
}
67811+
});
67812+
}
67813+
function patchFromPR(ctx) {
67814+
return __awaiter(this, void 0, void 0, function* () {
6778967815
const pull = ctx.payload.pull_request;
6779067816
if (!pull) {
6779167817
core.warning(`No pull request in context`);
6779267818
return ``;
6779367819
}
6779467820
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
67795-
let patch;
6779667821
try {
6779767822
const patchResp = yield octokit.rest.pulls.get({
6779867823
owner: ctx.repo.owner,
@@ -67807,21 +67832,45 @@ function fetchPatch() {
6780767832
return ``; // don't fail the action, but analyze without patch
6780867833
}
6780967834
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67810-
patch = patchResp.data;
67835+
return patchResp.data;
6781167836
}
6781267837
catch (err) {
6781367838
console.warn(`failed to fetch pull request patch:`, err);
6781467839
return ``; // don't fail the action, but analyze without patch
6781567840
}
67841+
});
67842+
}
67843+
function patchFromPush(ctx) {
67844+
return __awaiter(this, void 0, void 0, function* () {
67845+
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
6781667846
try {
67817-
const tempDir = yield createTempDir();
67818-
const patchPath = path.join(tempDir, "pull.patch");
67819-
core.info(`Writing patch to ${patchPath}`);
67820-
yield writeFile(patchPath, patch);
67821-
return patchPath;
67847+
const repoResp = yield octokit.rest.repos.get({
67848+
owner: ctx.repo.owner,
67849+
repo: ctx.repo.repo,
67850+
});
67851+
if (repoResp.status !== 200) {
67852+
core.warning(`failed to fetch repo: response status is ${repoResp.status}`);
67853+
return ``;
67854+
}
67855+
const defaultBranch = repoResp.data.default_branch;
67856+
const patchResp = yield octokit.rest.repos.compareCommits({
67857+
owner: ctx.repo.owner,
67858+
repo: ctx.repo.repo,
67859+
base: defaultBranch,
67860+
head: ctx.sha,
67861+
mediaType: {
67862+
format: `diff`,
67863+
}
67864+
});
67865+
if (patchResp.status !== 200) {
67866+
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`);
67867+
return ``; // don't fail the action, but analyze without patch
67868+
}
67869+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67870+
return patchResp.data;
6782267871
}
6782367872
catch (err) {
67824-
console.warn(`failed to save pull request patch:`, err);
67873+
console.warn(`failed to fetch push patch:`, err);
6782567874
return ``; // don't fail the action, but analyze without patch
6782667875
}
6782767876
});

dist/run/index.js

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67782,17 +67782,42 @@ function fetchPatch() {
6778267782
return ``;
6778367783
}
6778467784
const ctx = github.context;
67785-
if (ctx.eventName !== `pull_request`) {
67786-
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`);
67785+
let patch;
67786+
if (ctx.eventName === `pull_request`) {
67787+
patch = yield patchFromPR(ctx);
67788+
}
67789+
else if (ctx.eventName === `push`) {
67790+
patch = yield patchFromPush(ctx);
67791+
}
67792+
else {
67793+
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}`);
67794+
return ``;
67795+
}
67796+
if (!patch) {
67797+
core.info(`Not using patch for showing only new issues because it's empty`);
6778767798
return ``;
6778867799
}
67800+
try {
67801+
const tempDir = yield createTempDir();
67802+
const patchPath = path.join(tempDir, "pull.patch");
67803+
core.info(`Writing patch to ${patchPath}`);
67804+
yield writeFile(patchPath, patch);
67805+
return patchPath;
67806+
}
67807+
catch (err) {
67808+
console.warn(`failed to save pull request patch:`, err);
67809+
return ``; // don't fail the action, but analyze without patch
67810+
}
67811+
});
67812+
}
67813+
function patchFromPR(ctx) {
67814+
return __awaiter(this, void 0, void 0, function* () {
6778967815
const pull = ctx.payload.pull_request;
6779067816
if (!pull) {
6779167817
core.warning(`No pull request in context`);
6779267818
return ``;
6779367819
}
6779467820
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
67795-
let patch;
6779667821
try {
6779767822
const patchResp = yield octokit.rest.pulls.get({
6779867823
owner: ctx.repo.owner,
@@ -67807,21 +67832,45 @@ function fetchPatch() {
6780767832
return ``; // don't fail the action, but analyze without patch
6780867833
}
6780967834
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67810-
patch = patchResp.data;
67835+
return patchResp.data;
6781167836
}
6781267837
catch (err) {
6781367838
console.warn(`failed to fetch pull request patch:`, err);
6781467839
return ``; // don't fail the action, but analyze without patch
6781567840
}
67841+
});
67842+
}
67843+
function patchFromPush(ctx) {
67844+
return __awaiter(this, void 0, void 0, function* () {
67845+
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
6781667846
try {
67817-
const tempDir = yield createTempDir();
67818-
const patchPath = path.join(tempDir, "pull.patch");
67819-
core.info(`Writing patch to ${patchPath}`);
67820-
yield writeFile(patchPath, patch);
67821-
return patchPath;
67847+
const repoResp = yield octokit.rest.repos.get({
67848+
owner: ctx.repo.owner,
67849+
repo: ctx.repo.repo,
67850+
});
67851+
if (repoResp.status !== 200) {
67852+
core.warning(`failed to fetch repo: response status is ${repoResp.status}`);
67853+
return ``;
67854+
}
67855+
const defaultBranch = repoResp.data.default_branch;
67856+
const patchResp = yield octokit.rest.repos.compareCommits({
67857+
owner: ctx.repo.owner,
67858+
repo: ctx.repo.repo,
67859+
base: defaultBranch,
67860+
head: ctx.sha,
67861+
mediaType: {
67862+
format: `diff`,
67863+
}
67864+
});
67865+
if (patchResp.status !== 200) {
67866+
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`);
67867+
return ``; // don't fail the action, but analyze without patch
67868+
}
67869+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67870+
return patchResp.data;
6782267871
}
6782367872
catch (err) {
67824-
console.warn(`failed to save pull request patch:`, err);
67873+
console.warn(`failed to fetch push patch:`, err);
6782567874
return ``; // don't fail the action, but analyze without patch
6782667875
}
6782767876
});

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)