Skip to content

Commit

Permalink
fix: Code improvements and fixes (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonz97 authored Jan 11, 2024
1 parent 22a3ffc commit 8c5fde5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 85 deletions.
98 changes: 52 additions & 46 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22199,18 +22199,20 @@ var require_undici = __commonJS({

// src/index.ts
var import_core3 = __toESM(require_core());
var import_github4 = __toESM(require_github());

// src/config.ts
var import_core = __toESM(require_core());
var import_github = __toESM(require_github());
function loadConfig() {
try {
const githubToken = (0, import_core.getInput)("githubToken", { required: true });
return {
githubToken,
octokit: (0, import_github.getOctokit)(githubToken),
apiToken: (0, import_core.getInput)("apiToken", { required: true }),
accountId: (0, import_core.getInput)("accountId", { required: true }),
projectName: (0, import_core.getInput)("projectName", { required: true }),
directory: (0, import_core.getInput)("directory", { required: true }),
githubToken: (0, import_core.getInput)("githubToken", { required: false }),
branch: (0, import_core.getInput)("branch", { required: false }),
deploymentName: (0, import_core.getInput)("deploymentName", { required: false }),
workingDirectory: (0, import_core.getInput)("workingDirectory", { required: false }),
Expand All @@ -22224,14 +22226,14 @@ function loadConfig() {
var config = loadConfig();

// src/comments.ts
var import_github2 = __toESM(require_github());
var import_github3 = __toESM(require_github());

// src/globals.ts
var import_github = __toESM(require_github());
var import_github2 = __toESM(require_github());
var import_process = require("process");
var githubBranch = import_process.env.GITHUB_HEAD_REF || import_process.env.GITHUB_REF_NAME;
var prBranchOwner = import_github.context.payload.pull_request?.head.repo.owner.login;
var isPR = import_github.context.eventName === "pull_request" || import_github.context.eventName === "pull_request_target";
var prBranchOwner = import_github2.context.payload.pull_request?.head.repo.owner.login;
var isPR = import_github2.context.eventName === "pull_request" || import_github2.context.eventName === "pull_request_target";

// src/comments.ts
async function findExistingComment(opts) {
Expand All @@ -22241,9 +22243,9 @@ async function findExistingComment(opts) {
issue_number: opts.issueNumber,
per_page: 100
};
const listComments = opts.octokit.rest.issues.listComments;
const listComments = config.octokit.rest.issues.listComments;
let found;
for await (const comments of opts.octokit.paginate.iterator(listComments, params)) {
for await (const comments of config.octokit.paginate.iterator(listComments, params)) {
found = comments.data.find(({ body }) => {
return (body?.search(opts.messageId) ?? -1) > -1;
});
Expand All @@ -22257,54 +22259,57 @@ async function findExistingComment(opts) {
}
return;
}
var Status = {
success: "\u2705 Ready",
fail: "\u274C Failed",
building: "\u{1F528} Building"
};
async function createPRComment(opts) {
if (!isPR)
return;
const messageId = `deployment-comment:${config.projectName}`;
const deploymentLogUrl = `${import_github2.context.serverUrl}/${import_github2.context.repo.owner}/${import_github2.context.repo.repo}/actions/runs/${import_github2.context.runId}`;
const deploymentLogUrl = `${import_github3.context.serverUrl}/${import_github3.context.repo.owner}/${import_github3.context.repo.repo}/actions/runs/${import_github3.context.runId}`;
const body = `<!-- ${messageId} -->

### \u26A1 Cloudflare Pages Deployment
| Name | Status | Preview | Last Commit |
| :--- | :----- | :------ | :---------- |
| **${config.projectName}** | ${opts.status} ([View Log](${deploymentLogUrl})) | ${opts.previewUrl} | ${import_github2.context.payload.pull_request?.head.sha || import_github2.context.ref} |
| **${config.projectName}** | ${Status[opts.status]} ([View Log](${deploymentLogUrl})) | ${opts.previewUrl} | ${import_github3.context.payload.pull_request?.head.sha || import_github3.context.ref} |
`;
const existingComment = await findExistingComment({
octokit: opts.octokit,
owner: import_github2.context.repo.owner,
repo: import_github2.context.repo.repo,
issueNumber: import_github2.context.issue.number,
owner: import_github3.context.repo.owner,
repo: import_github3.context.repo.repo,
issueNumber: import_github3.context.issue.number,
messageId
});
if (existingComment !== void 0) {
return await opts.octokit.rest.issues.updateComment({
owner: import_github2.context.repo.owner,
repo: import_github2.context.repo.repo,
issue_number: import_github2.context.issue.number,
return await config.octokit.rest.issues.updateComment({
owner: import_github3.context.repo.owner,
repo: import_github3.context.repo.repo,
issue_number: import_github3.context.issue.number,
comment_id: existingComment.id,
body
});
}
return await opts.octokit.rest.issues.createComment({
owner: import_github2.context.repo.owner,
repo: import_github2.context.repo.repo,
issue_number: import_github2.context.issue.number,
return await config.octokit.rest.issues.createComment({
owner: import_github3.context.repo.owner,
repo: import_github3.context.repo.repo,
issue_number: import_github3.context.issue.number,
body
});
}

// src/deployments.ts
var import_github3 = __toESM(require_github());
var import_github4 = __toESM(require_github());
var import_core2 = __toESM(require_core());
async function createGithubDeployment({
octokit,
productionEnvironment,
environment
}) {
const deployment = await octokit.rest.repos.createDeployment({
owner: import_github3.context.repo.owner,
repo: import_github3.context.repo.repo,
ref: import_github3.context.payload.pull_request?.head.sha || import_github3.context.ref,
const deployment = await config.octokit.rest.repos.createDeployment({
owner: import_github4.context.repo.owner,
repo: import_github4.context.repo.repo,
ref: import_github4.context.payload.pull_request?.head.sha || import_github4.context.ref,
auto_merge: false,
description: "Cloudflare Pages",
required_contexts: [],
Expand All @@ -22316,9 +22321,9 @@ async function createGithubDeployment({
}
}
async function createGithubDeploymentStatus(opts) {
return opts.octokit.rest.repos.createDeploymentStatus({
owner: import_github3.context.repo.owner,
repo: import_github3.context.repo.repo,
return config.octokit.rest.repos.createDeploymentStatus({
owner: import_github4.context.repo.owner,
repo: import_github4.context.repo.repo,
deployment_id: opts.deploymentId,
// @ts-expect-error this should accept a string
environment: opts.environmentName,
Expand Down Expand Up @@ -23411,18 +23416,15 @@ async function getPagesDeployment() {

// src/index.ts
async function main() {
const project = await getPagesProject();
const productionEnvironment = githubBranch === project.production_branch || config.branch === project.production_branch;
const octokit = (0, import_github4.getOctokit)(config.githubToken);
await createPRComment({
octokit,
status: "\u{1F528} Building",
previewUrl: "..."
status: "building",
previewUrl: ""
});
const project = await getPagesProject();
const productionEnvironment = githubBranch === project.production_branch || config.branch === project.production_branch;
let githubDeployment;
if (config.deploymentName.length > 0) {
githubDeployment = await createGithubDeployment({
octokit,
productionEnvironment,
environment: config.deploymentName
});
Expand All @@ -23432,7 +23434,6 @@ async function main() {
await createJobSummary({ deployment: pagesDeployment, aliasUrl: pagesDeployment.url });
if (githubDeployment) {
await createGithubDeploymentStatus({
octokit,
productionEnvironment,
environmentName: githubDeployment.environment,
deploymentId: githubDeployment.id,
Expand All @@ -23446,8 +23447,7 @@ async function main() {
alias = deployment.aliases[0];
}
await createPRComment({
octokit,
status: "\u2705 Ready",
status: "success",
previewUrl: `[Visit Preview](${alias})`
});
(0, import_core3.setOutput)("id", deployment.id);
Expand All @@ -23456,11 +23456,17 @@ async function main() {
(0, import_core3.setOutput)("alias", alias);
await createJobSummary({ deployment, aliasUrl: alias });
}
try {
main();
} catch (error) {
(0, import_core3.setFailed)(error.message);
}
(async () => {
try {
await main();
} catch (error) {
(0, import_core3.setFailed)(error.message);
await createPRComment({
status: "fail",
previewUrl: ""
});
}
})();
/*! Bundled license information:

is-plain-object/dist/is-plain-object.mjs:
Expand Down
23 changes: 12 additions & 11 deletions src/comments.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { context } from '@actions/github';
import { isPR } from './globals.js';
import { config } from './config.js';
import type { Octokit } from './types.js';

type FindExistingCommentOpts = {
octokit: Octokit;
owner: string;
repo: string;
issueNumber: number;
Expand All @@ -19,10 +17,10 @@ export async function findExistingComment(opts: FindExistingCommentOpts) {
per_page: 100,
};

const listComments = opts.octokit.rest.issues.listComments;
const listComments = config.octokit.rest.issues.listComments;
let found: Awaited<ReturnType<typeof listComments>>['data'][number] | undefined;

for await (const comments of opts.octokit.paginate.iterator(listComments, params)) {
for await (const comments of config.octokit.paginate.iterator(listComments, params)) {
found = comments.data.find(({ body }) => {
return (body?.search(opts.messageId) ?? -1) > -1;
});
Expand All @@ -40,12 +38,16 @@ export async function findExistingComment(opts: FindExistingCommentOpts) {
return;
}

const Status = {
success: '✅ Ready',
fail: '❌ Failed',
building: '🔨 Building',
} as const;

type CreatePRCommentOpts = {
octokit: Octokit;
previewUrl: string;
status: string;
status: keyof typeof Status;
};

export async function createPRComment(opts: CreatePRCommentOpts) {
if (!isPR) return;

Expand All @@ -57,21 +59,20 @@ export async function createPRComment(opts: CreatePRCommentOpts) {
### ⚡ Cloudflare Pages Deployment
| Name | Status | Preview | Last Commit |
| :--- | :----- | :------ | :---------- |
| **${config.projectName}** | ${opts.status} ([View Log](${deploymentLogUrl})) | ${
| **${config.projectName}** | ${Status[opts.status]} ([View Log](${deploymentLogUrl})) | ${
opts.previewUrl
} | ${context.payload.pull_request?.head.sha || context.ref} |
`;

const existingComment = await findExistingComment({
octokit: opts.octokit,
owner: context.repo.owner,
repo: context.repo.repo,
issueNumber: context.issue.number,
messageId,
});

if (existingComment !== undefined) {
return await opts.octokit.rest.issues.updateComment({
return await config.octokit.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
Expand All @@ -80,7 +81,7 @@ export async function createPRComment(opts: CreatePRCommentOpts) {
});
}

return await opts.octokit.rest.issues.createComment({
return await config.octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { getInput, setFailed } from '@actions/core';
import { getOctokit } from '@actions/github';

function loadConfig() {
try {
const githubToken = getInput('githubToken', { required: true });
return {
githubToken,
octokit: getOctokit(githubToken),
apiToken: getInput('apiToken', { required: true }),
accountId: getInput('accountId', { required: true }),
projectName: getInput('projectName', { required: true }),
directory: getInput('directory', { required: true }),
githubToken: getInput('githubToken', { required: false }),
branch: getInput('branch', { required: false }),
deploymentName: getInput('deploymentName', { required: false }),
workingDirectory: getInput('workingDirectory', { required: false }),
Expand Down
8 changes: 2 additions & 6 deletions src/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ import { context } from '@actions/github';
import { summary } from '@actions/core';
import { config } from './config.js';
import type { Deployment } from '@cloudflare/types';
import type { Octokit } from './types.js';

type CreateGHDeploymentOpts = {
octokit: Octokit;
productionEnvironment: boolean;
environment: string;
};
export async function createGithubDeployment({
octokit,
productionEnvironment,
environment,
}: CreateGHDeploymentOpts) {
const deployment = await octokit.rest.repos.createDeployment({
const deployment = await config.octokit.rest.repos.createDeployment({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.payload.pull_request?.head.sha || context.ref,
Expand All @@ -31,7 +28,6 @@ export async function createGithubDeployment({
}

type CreateGHDeploymentStatusOpts = {
octokit: Octokit;
deploymentId: number;
environmentUrl: string;
cfDeploymentId: string;
Expand All @@ -40,7 +36,7 @@ type CreateGHDeploymentStatusOpts = {
};

export async function createGithubDeploymentStatus(opts: CreateGHDeploymentStatusOpts) {
return opts.octokit.rest.repos.createDeploymentStatus({
return config.octokit.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: opts.deploymentId,
Expand Down
Loading

0 comments on commit 8c5fde5

Please sign in to comment.