Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {

rules: {
curly: ["error", "multi-line"],
"no-console": 2,
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/ban-types": 1,
Expand Down
534 changes: 311 additions & 223 deletions dist/build.js

Large diffs are not rendered by default.

404 changes: 244 additions & 160 deletions dist/checkoutPRRef.js

Large diffs are not rendered by default.

428 changes: 256 additions & 172 deletions dist/index.js

Large diffs are not rendered by default.

626 changes: 360 additions & 266 deletions dist/merge.js

Large diffs are not rendered by default.

644 changes: 369 additions & 275 deletions dist/preview.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import YAML from "yaml";
import { makeCommitMessageConventional } from "./commitMessage";
import { getBooleanInput, getInput, setOutput } from "./compat";
import { readConfig } from "./config";
import { logger } from "./logger";
import { runBuilds, RunResult } from "./runBuilds";

async function main() {
Expand Down Expand Up @@ -35,7 +36,7 @@ async function main() {
const stainless = new Stainless({
project: projectName,
apiKey,
logLevel: "warn",
logger,
});

let lastValue: RunResult;
Expand Down Expand Up @@ -79,17 +80,17 @@ async function main() {

fs.writeFileSync(documentedSpecOutputPath, documentedSpecOutput);
} else if (documentedSpecOutputPath) {
console.error("No documented spec found.");
logger.error("No documented spec found.");
}
} catch (error) {
if (
error instanceof Stainless.BadRequestError &&
error.message.includes("No changes to commit")
) {
console.log("No changes to commit, skipping build.");
logger.info("No changes to commit, skipping build.");
process.exit(0);
} else {
console.error("Error interacting with API:", error);
logger.error("Error interacting with API:", error);
process.exit(1);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/checkoutPRRef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getInput } from "./compat";
import * as exec from "@actions/exec";
import { getMergeBase, saveConfig } from "./config";
import { logger } from "./logger";

function assertRef(ref: string): asserts ref is "base" | "head" {
if (ref !== "base" && ref !== "head") {
Expand Down Expand Up @@ -34,15 +35,15 @@ async function main() {
throw new Error(`Expected OpenAPI spec at ${oasPath}.`);
}
if (savedSha !== null && savedSha !== mergeBaseSha) {
console.warn(
logger.warn(
`Expected HEAD to be ${mergeBaseSha}, but was ${savedSha}. This might cause issues with getting the base revision.`,
);
}

// Checkout the head SHA.
await exec.exec("git", ["checkout", headSha], { silent: true });
} catch (error) {
console.error("Error in checkout-pr-ref action:", error);
logger.error("Error in checkout-pr-ref action:", { error });
process.exit(1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/comment.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import type { Outcomes } from "./runBuilds";
import { parseCommitMessage, printComment } from "./comment";
import * as MD from "./markdown";
import type { Outcomes } from "./runBuilds";

vi.mock("@actions/github", () => {
return {
Expand Down
11 changes: 7 additions & 4 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Stainless } from "@stainless-api/sdk";
import { Outcomes } from "./runBuilds";
import { logger } from "./logger";
import * as MD from "./markdown";
import { Outcomes } from "./runBuilds";
import {
createCommentClient,
getCITerm,
Expand Down Expand Up @@ -621,7 +622,9 @@ export async function upsertComment({
}) {
const client = createCommentClient(token, prNumber);

console.log(`Upserting comment on ${getPRTerm()}:`, prNumber);
logger.info(`Upserting comment on ${getPRTerm()}`, {
pr: github.context.issue.number,
});

const comments = await client.listComments();

Expand All @@ -631,10 +634,10 @@ export async function upsertComment({
);

if (existingComment) {
console.log("Updating existing comment:", existingComment.id);
logger.info("Updating existing comment", { id: existingComment.id });
await client.updateComment(existingComment.id, body);
} else if (!skipCreate) {
console.log("Creating new comment");
logger.info("Creating new comment");
await client.createComment(body);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/commitMessage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { logger } from "./logger";

// https://www.conventionalcommits.org/en/v1.0.0/
const CONVENTIONAL_COMMIT_REGEX = new RegExp(
/^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?(!?): .*$/,
Expand All @@ -9,7 +11,7 @@ export function makeCommitMessageConventional(
): string | undefined;
export function makeCommitMessageConventional(message?: string) {
if (message && !CONVENTIONAL_COMMIT_REGEX.test(message)) {
console.warn(
logger.warn(
`Commit message: "${message}" is not in Conventional Commits format: https://www.conventionalcommits.org/en/v1.0.0/. Prepending "feat" and using anyway.`,
);
return `feat: ${message}`;
Expand Down
21 changes: 15 additions & 6 deletions src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createClient as createGitHubClient,
type PartialGitHub,
} from "@stainless-api/github-internal/tree-shakable";
import { logger } from "./logger";

interface Comment {
id: string | number;
Expand Down Expand Up @@ -94,22 +95,30 @@ export function isPullRequestOpenedEvent(): boolean {
}
}

export function startGroup(id: string, name: string) {
export function startGroupStr(id: string, name: string) {
if (isGitLabCI()) {
console.log(`\x1b[0Ksection_start:${Date.now()}:${id}\r\x1b[0K${name}`);
return `\x1b[0Ksection_start:${Date.now()}:${id}\r\x1b[0K${name}`;
} else {
core.startGroup(name);
return `::group::${name}`;
}
}

export function endGroup(id: string) {
export function endGroupStr(id: string) {
if (isGitLabCI()) {
console.log(`\x1b[0Ksection_end:${Date.now()}:${id}\r\x1b[0K`);
return `\x1b[0Ksection_end:${Date.now()}:${id}\r\x1b[0K`;
} else {
core.endGroup();
return "::endgroup::";
}
}

export function startGroup(id: string, name: string) {
logger.info(startGroupStr(id, name));
}

export function endGroup(id: string) {
logger.info(endGroupStr(id));
}

export function createCommentClient(
token: string,
prNumber: number,
Expand Down
31 changes: 19 additions & 12 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as exec from "@actions/exec";
import * as fs from "node:fs";
import { tmpdir } from "node:os";
import * as path from "node:path";
import { logger } from "./logger";

export type Config = {
oas?: string;
Expand Down Expand Up @@ -39,7 +40,7 @@ export async function saveConfig({
if (!savedSha) {
throw new Error("Unable to determine current SHA; is there a git repo?");
}
console.log("Saving generated config for", savedSha);
logger.info("Saving generated config for", savedSha);

if (oasPath && fs.existsSync(oasPath)) {
hasOAS = true;
Expand Down Expand Up @@ -85,7 +86,7 @@ export async function readConfig({
if (!sha) {
throw new Error("Unable to determine current SHA; is there a git repo?");
}
console.log("Reading config at", sha);
logger.info("Reading config at", sha);

const results: Config = {};

Expand All @@ -98,14 +99,14 @@ export async function readConfig({
return;
}
if (!filePath || !fs.existsSync(filePath)) {
console.log("Skipping missing", file, "at", filePath);
logger.info(`Skip missing file`, { file, filePath });
return;
}
results[file] = fs.readFileSync(filePath, "utf-8");
results[`${file}Hash`] = (
await exec.getExecOutput("md5sum", [filePath], { silent: true })
).stdout.split(" ")[0];
console.log(`Using ${file} via`, via, "hash", results[`${file}Hash`]);
logger.info(`Using ${file} via ${via}`, { hash: results[`${file}Hash`] });
};

try {
Expand All @@ -114,11 +115,11 @@ export async function readConfig({
.catch(() => null);
await exec.exec("git", ["checkout", sha], { silent: true });
} catch {
console.log("Could not checkout", sha);
logger.info("Could not checkout", sha);
}

await addToResults("oas", oasPath, `git ${sha}`);
await addToResults("config", configPath, `git ${sha}`);
await addToResults("oas", oasPath, "git");
await addToResults("config", configPath, "git");

try {
await addToResults(
Expand All @@ -132,7 +133,7 @@ export async function readConfig({
`saved ${sha}`,
);
} catch {
console.log("Could not get config from saved file path");
logger.info("Could not get config from saved file path");
}

return results;
Expand Down Expand Up @@ -182,7 +183,7 @@ export async function getMergeBase({
throw new Error("Could not determine merge base SHA");
}

console.log(`Merge base: ${mergeBaseSha}`);
logger.info(`Merge base: ${mergeBaseSha}`);

return { mergeBaseSha };
}
Expand All @@ -198,7 +199,7 @@ export async function getNonMainBaseRef({

if (baseRef !== defaultBranch) {
nonMainBaseRef = `preview/${baseRef}`;
console.log(`Non-main base ref: ${nonMainBaseRef}`);
logger.info(`Non-main base ref: ${nonMainBaseRef}`);
}

return { nonMainBaseRef };
Expand All @@ -214,12 +215,18 @@ export async function isConfigChanged({
let changed = false;

if (before.oasHash !== after.oasHash) {
console.log("OAS file changed");
logger.info("OAS file changed", {
before: before.oasHash,
after: after.oasHash,
});
changed = true;
}

if (before.configHash !== after.configHash) {
console.log("Config file changed");
logger.info("Config file changed", {
before: before.configHash,
after: after.configHash,
});
changed = true;
}

Expand Down
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { error, info, warn } from "node:console";
import { readFileSync, writeFileSync } from "node:fs";
import YAML from "yaml";
import { getBooleanInput, getInput, isGitLabCI } from "./compat";
import { logger } from "./logger";

// https://www.conventionalcommits.org/en/v1.0.0/
const CONVENTIONAL_COMMIT_REGEX = new RegExp(
Expand Down Expand Up @@ -41,7 +42,7 @@ export async function main() {
}

if (!projectName) {
const stainless = new Stainless({ apiKey: stainless_api_key });
const stainless = new Stainless({ apiKey: stainless_api_key, logger });
const projects = await stainless.projects.list({ limit: 2 });
if (projects.data.length === 0) {
const errorMsg = "No projects found. Please create a project first.";
Expand Down Expand Up @@ -117,7 +118,7 @@ async function uploadSpecAndConfig(
const stainless = new Stainless({ apiKey: token, project: projectName });
const specContent = readFileSync(specPath, "utf8");

let configContent;
let configContent: string | undefined;

if (guessConfig) {
configContent = Object.values(
Expand Down Expand Up @@ -209,8 +210,8 @@ async function uploadSpecAndConfig(
}

if (require.main === module) {
main().catch((err) => {
console.error(err);
main().catch((error) => {
logger.error("got error", { error });
process.exit(1);
});
}
Loading