Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default branch for only-new-issues when push event #520

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
@@ -0,0 +1 @@
nodejs lts
65 changes: 55 additions & 10 deletions dist/post_run/index.js
Expand Up @@ -67782,17 +67782,38 @@ function fetchPatch() {
return ``;
}
const ctx = github.context;
if (ctx.eventName !== `pull_request`) {
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`);
let patch;
if (ctx.eventName === `pull_request`) {
patch = yield patchFromPR(ctx);
}
else if (ctx.eventName === `push`) {
patch = yield patchFromPush(ctx);
}
else {
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}`);
return ``;
}
try {
const tempDir = yield createTempDir();
const patchPath = path.join(tempDir, "pull.patch");
core.info(`Writing patch to ${patchPath}`);
yield writeFile(patchPath, patch);
return patchPath;
}
catch (err) {
console.warn(`failed to save pull request patch:`, err);
return ``; // don't fail the action, but analyze without patch
}
});
}
function patchFromPR(ctx) {
return __awaiter(this, void 0, void 0, function* () {
const pull = ctx.payload.pull_request;
if (!pull) {
core.warning(`No pull request in context`);
return ``;
}
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
let patch;
try {
const patchResp = yield octokit.rest.pulls.get({
owner: ctx.repo.owner,
Expand All @@ -67807,21 +67828,45 @@ function fetchPatch() {
return ``; // don't fail the action, but analyze without patch
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
patch = patchResp.data;
return patchResp.data;
}
catch (err) {
console.warn(`failed to fetch pull request patch:`, err);
return ``; // don't fail the action, but analyze without patch
}
});
}
function patchFromPush(ctx) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
try {
const tempDir = yield createTempDir();
const patchPath = path.join(tempDir, "pull.patch");
core.info(`Writing patch to ${patchPath}`);
yield writeFile(patchPath, patch);
return patchPath;
const repoResp = yield octokit.rest.repos.get({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
});
if (repoResp.status !== 200) {
core.warning(`failed to fetch repo: response status is ${repoResp.status}`);
return ``;
}
const defaultBranch = repoResp.data.default_branch;
const patchResp = yield octokit.rest.repos.compareCommits({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
base: defaultBranch,
head: ctx.sha,
mediaType: {
format: `diff`,
},
});
if (patchResp.status !== 200) {
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`);
return ``; // don't fail the action, but analyze without patch
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return patchResp.data;
}
catch (err) {
console.warn(`failed to save pull request patch:`, err);
console.warn(`failed to fetch push patch:`, err);
return ``; // don't fail the action, but analyze without patch
}
});
Expand Down
65 changes: 55 additions & 10 deletions dist/run/index.js
Expand Up @@ -67782,17 +67782,38 @@ function fetchPatch() {
return ``;
}
const ctx = github.context;
if (ctx.eventName !== `pull_request`) {
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`);
let patch;
if (ctx.eventName === `pull_request`) {
patch = yield patchFromPR(ctx);
}
else if (ctx.eventName === `push`) {
patch = yield patchFromPush(ctx);
}
else {
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}`);
return ``;
}
try {
const tempDir = yield createTempDir();
const patchPath = path.join(tempDir, "pull.patch");
core.info(`Writing patch to ${patchPath}`);
yield writeFile(patchPath, patch);
return patchPath;
}
catch (err) {
console.warn(`failed to save pull request patch:`, err);
return ``; // don't fail the action, but analyze without patch
}
});
}
function patchFromPR(ctx) {
return __awaiter(this, void 0, void 0, function* () {
const pull = ctx.payload.pull_request;
if (!pull) {
core.warning(`No pull request in context`);
return ``;
}
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
let patch;
try {
const patchResp = yield octokit.rest.pulls.get({
owner: ctx.repo.owner,
Expand All @@ -67807,21 +67828,45 @@ function fetchPatch() {
return ``; // don't fail the action, but analyze without patch
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
patch = patchResp.data;
return patchResp.data;
}
catch (err) {
console.warn(`failed to fetch pull request patch:`, err);
return ``; // don't fail the action, but analyze without patch
}
});
}
function patchFromPush(ctx) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }));
try {
const tempDir = yield createTempDir();
const patchPath = path.join(tempDir, "pull.patch");
core.info(`Writing patch to ${patchPath}`);
yield writeFile(patchPath, patch);
return patchPath;
const repoResp = yield octokit.rest.repos.get({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
});
if (repoResp.status !== 200) {
core.warning(`failed to fetch repo: response status is ${repoResp.status}`);
return ``;
}
const defaultBranch = repoResp.data.default_branch;
const patchResp = yield octokit.rest.repos.compareCommits({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
base: defaultBranch,
head: ctx.sha,
mediaType: {
format: `diff`,
},
});
if (patchResp.status !== 200) {
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`);
return ``; // don't fail the action, but analyze without patch
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return patchResp.data;
}
catch (err) {
console.warn(`failed to save pull request patch:`, err);
console.warn(`failed to fetch push patch:`, err);
return ``; // don't fail the action, but analyze without patch
}
});
Expand Down
68 changes: 58 additions & 10 deletions src/run.ts
@@ -1,5 +1,6 @@
import * as core from "@actions/core"
import * as github from "@actions/github"
import { Context } from "@actions/github/lib/context"
import { exec, ExecOptions } from "child_process"
import * as fs from "fs"
import * as path from "path"
Expand Down Expand Up @@ -29,17 +30,37 @@ async function fetchPatch(): Promise<string> {
}

const ctx = github.context
if (ctx.eventName !== `pull_request`) {
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`)
let patch: string
if (ctx.eventName === `pull_request`) {
patch = await patchFromPR(ctx)
} else if (ctx.eventName === `push`) {
patch = await patchFromPush(ctx)
} else {
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}`
)
return ``
}

try {
const tempDir = await createTempDir()
const patchPath = path.join(tempDir, "pull.patch")
core.info(`Writing patch to ${patchPath}`)
await writeFile(patchPath, patch)
return patchPath
} catch (err) {
console.warn(`failed to save pull request patch:`, err)
return `` // don't fail the action, but analyze without patch
}
}

async function patchFromPR(ctx: Context): Promise<string> {
const pull = ctx.payload.pull_request
if (!pull) {
core.warning(`No pull request in context`)
return ``
}
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
let patch: string
try {
const patchResp = await octokit.rest.pulls.get({
owner: ctx.repo.owner,
Expand All @@ -56,20 +77,47 @@ async function fetchPatch(): Promise<string> {
}

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

async function patchFromPush(ctx: Context): Promise<string> {
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
try {
const tempDir = await createTempDir()
const patchPath = path.join(tempDir, "pull.patch")
core.info(`Writing patch to ${patchPath}`)
await writeFile(patchPath, patch)
return patchPath
const repoResp = await octokit.rest.repos.get({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
})

if (repoResp.status !== 200) {
core.warning(`failed to fetch repo: response status is ${repoResp.status}`)
return ``
}

const defaultBranch = repoResp.data.default_branch

const patchResp = await octokit.rest.repos.compareCommits({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
base: defaultBranch,
head: ctx.sha,
mediaType: {
format: `diff`,
},
})

if (patchResp.status !== 200) {
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`)
return `` // don't fail the action, but analyze without patch
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return patchResp.data as any
} catch (err) {
console.warn(`failed to save pull request patch:`, err)
console.warn(`failed to fetch push patch:`, err)
return `` // don't fail the action, but analyze without patch
}
}
Expand Down