@cdwr/core
Version:
A set of core utilities for the Codeware ecosystem.
138 lines (131 loc) • 4.02 kB
JavaScript
// packages/core/src/lib/actions/add-pull-request-comment.ts
import * as github from "@actions/github";
// packages/core/src/lib/actions/with-github.ts
import { RequestError } from "@octokit/request-error";
import { StatusCodes, getReasonPhrase } from "http-status-codes";
async function withGitHub(operation, option) {
try {
return await operation();
} catch (error) {
if (error instanceof RequestError) {
switch (error.status) {
case StatusCodes.UNAUTHORIZED:
throw new Error(
`Authentication failed. Please check your GitHub token.
${error.message}`
);
case StatusCodes.FORBIDDEN:
throw new Error(
`Permission to the operation was denied. Please check your GitHub token.
${error.message}`
);
case 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} - ${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
import * as github2 from "@actions/github";
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
import { z } from "zod";
var EnvironmentSchema = z.enum(["preview", "production"]);
var getDeployEnv = (context5, mainBranch) => {
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`
};
};
// packages/core/src/lib/actions/get-pull-request.ts
import * as core from "@actions/core";
import * as github3 from "@actions/github";
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
import * as core2 from "@actions/core";
import * as github4 from "@actions/github";
var printGitHubContext = () => {
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}`);
}
}
};
export {
EnvironmentSchema,
addPullRequestComment,
getDeployEnv,
getPullRequest,
getRepositoryDefaultBranch,
printGitHubContext,
withGitHub
};