@cdwr/core
Version:
A set of core utilities for the Codeware ecosystem.
195 lines (186 loc) • 6.57 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/core/src/actions.ts
var actions_exports = {};
__export(actions_exports, {
EnvironmentSchema: () => EnvironmentSchema,
addPullRequestComment: () => addPullRequestComment,
getDeployEnv: () => getDeployEnv,
getPullRequest: () => getPullRequest,
getRepositoryDefaultBranch: () => getRepositoryDefaultBranch,
printGitHubContext: () => printGitHubContext,
withGitHub: () => withGitHub
});
module.exports = __toCommonJS(actions_exports);
// packages/core/src/lib/actions/add-pull-request-comment.ts
var github = __toESM(require("@actions/github"), 1);
// packages/core/src/lib/actions/with-github.ts
var import_request_error = require("@octokit/request-error");
var import_http_status_codes = require("http-status-codes");
async function withGitHub(operation, option) {
try {
return await operation();
} catch (error) {
if (error instanceof import_request_error.RequestError) {
switch (error.status) {
case import_http_status_codes.StatusCodes.UNAUTHORIZED:
throw new Error(
`Authentication failed. Please check your GitHub token.
${error.message}`
);
case import_http_status_codes.StatusCodes.FORBIDDEN:
throw new Error(
`Permission to the operation was denied. Please check your GitHub token.
${error.message}`
);
case import_http_status_codes.StatusCodes.NOT_FOUND:
if (option === "not-found-returns-null") {
return null;
}
throw new Error(
`Requested information was not found.
${error.message}`
);
default:
throw new Error(
`Request failed with ${error.status} - ${(0, import_http_status_codes.getReasonPhrase)(error.status)}.
${error.message}`
);
}
}
throw error;
}
}
// packages/core/src/lib/actions/add-pull-request-comment.ts
var addPullRequestComment = async (token, pullRequest, comment) => {
const octokit = github.getOctokit(token);
const {
data: { id }
} = await withGitHub(
() => octokit.rest.issues.createComment({
...github.context.repo,
issue_number: pullRequest,
body: comment
})
);
return id;
};
// packages/core/src/lib/actions/get-repository-default-branch.ts
var github2 = __toESM(require("@actions/github"), 1);
var getRepositoryDefaultBranch = async (token) => {
const octokit = github2.getOctokit(token);
const {
data: { default_branch }
} = await withGitHub(
async () => octokit.rest.repos.get({
...github2.context.repo
})
);
return default_branch;
};
// packages/core/src/lib/actions/get-deploy-env.ts
var import_zod = require("zod");
var EnvironmentSchema = import_zod.z.enum(["preview", "production"]);
var getDeployEnv = (context5, mainBranch) => {
try {
const { eventName, ref } = context5;
const currentBranch = ref.split("/").at(-1);
if (eventName === "pull_request") {
return { environment: "preview" };
}
if (eventName === "push") {
if (currentBranch === mainBranch) {
return { environment: "production" };
}
return {
environment: null,
reason: `'${currentBranch}' is not supported for production deployment, only '${mainBranch}' is supported`
};
}
return {
environment: null,
reason: `'${eventName}' is not a supported event for deployment, only 'pull_request' and 'push' are supported`
};
} catch (error) {
return {
environment: null,
reason: `Failed to determine deploy environment: ${error instanceof Error ? error.message : String(error)}`
};
}
};
// packages/core/src/lib/actions/get-pull-request.ts
var core = __toESM(require("@actions/core"), 1);
var github3 = __toESM(require("@actions/github"), 1);
var getPullRequest = async (token, pullRequest) => {
const octokit = github3.getOctokit(token);
core.debug(`Get pull request from number #${pullRequest}`);
const pr = await withGitHub(
() => octokit.rest.pulls.get({
...github3.context.repo,
pull_number: pullRequest
}),
"not-found-returns-null"
);
if (!pr) {
core.debug(`Pull request #${pullRequest} could not be found`);
return void 0;
}
return pr.data;
};
// packages/core/src/lib/actions/print-github-context.ts
var core2 = __toESM(require("@actions/core"), 1);
var github4 = __toESM(require("@actions/github"), 1);
var printGitHubContext = () => {
try {
core2.info("== Repo ==");
core2.info(`- owner: ${github4.context.repo.owner}`);
core2.info(`- repo: ${github4.context.repo.repo}`);
core2.info("== Misc ==");
for (const [key, value] of Object.entries(github4.context)) {
if (typeof value === "string") {
core2.info(`- ${key}: ${value}`);
}
}
} catch (error) {
core2.info(
`GitHub context details unavailable:
${error instanceof Error ? error.message : String(error)}`
);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
EnvironmentSchema,
addPullRequestComment,
getDeployEnv,
getPullRequest,
getRepositoryDefaultBranch,
printGitHubContext,
withGitHub
});