From 920cc25d0acc3a0827d7071355cc5359a76cb37f Mon Sep 17 00:00:00 2001 From: Rob van der Leek <5324924+robvanderleek@users.noreply.github.com> Date: Sun, 1 Dec 2024 20:24:19 +0100 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=9A=A7=20Create=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/index.js | 67 +++++++++++++++++++++++---------------------------- src/action.ts | 23 +++--------------- src/github.ts | 41 +++++++++++++++++++------------ 3 files changed, 59 insertions(+), 72 deletions(-) diff --git a/dist/index.js b/dist/index.js index b44e739..320ea36 100644 --- a/dist/index.js +++ b/dist/index.js @@ -46744,10 +46744,10 @@ var require_github2 = __commonJS({ Object.defineProperty(exports2, "__esModule", { value: true }); exports2.branchExists = branchExists; exports2.createBranch = createBranch; - exports2.getBranchHeadSha = getBranchHeadSha; - exports2.getDefaultBranch = getDefaultBranch; + exports2.createInitialCommit = createInitialCommit; exports2.getRepoOwner = getRepoOwner; exports2.getRepoName = getRepoName; + exports2.createFile = createFile; function branchExists(octokit, owner, repo, branchName) { return __awaiter2(this, void 0, void 0, function* () { try { @@ -46778,25 +46778,19 @@ var require_github2 = __commonJS({ } }); } - function getBranchHeadSha(octokit, owner, repo, branch) { + function createInitialCommit(octokit, owner, repo) { return __awaiter2(this, void 0, void 0, function* () { - try { - const res = yield octokit.git.getRef({ - owner, - repo, - ref: `heads/${branch}` - }); - const ref = res.data.object; - return ref.sha; - } catch (e) { - return void 0; - } + const empty_tree_object = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; + const res = yield octokit.git.createCommit({ + owner, + repo, + message: "Initial commit", + tree: empty_tree_object, + parents: [] + }); + return res.data.sha; }); } - function getDefaultBranch(ctx) { - var _a; - return (_a = ctx.payload.repository) === null || _a === void 0 ? void 0 : _a.default_branch; - } function getRepoOwner(ctx) { var _a; return (_a = ctx.payload.repository) === null || _a === void 0 ? void 0 : _a.owner.login; @@ -46805,6 +46799,22 @@ var require_github2 = __commonJS({ var _a; return (_a = ctx.payload.repository) === null || _a === void 0 ? void 0 : _a.name; } + function createFile(octokit, owner, repo, branchName, path, content) { + return __awaiter2(this, void 0, void 0, function* () { + yield octokit.repos.createOrUpdateFileContents({ + owner, + repo, + path, + message: `Update by Code Limit`, + branch: branchName, + content: Buffer.from(content).toString("base64"), + committer: { + name: "Code Limit Action", + email: "robvanderleek@gmail.com" + } + }); + }); + } } }); @@ -46860,29 +46870,12 @@ function main() { process.exit(1); } if (!(yield (0, github_2.branchExists)(octokit, owner, repo, "_codelimit_reports"))) { - const defaultBranch = (0, github_2.getDefaultBranch)(github_1.context); - if (!defaultBranch) { - console.error("Could not determine default branch"); - process.exit(1); - } - const sha = yield (0, github_2.getBranchHeadSha)(octokit, owner, repo, defaultBranch); - if (!sha) { - console.error("Could not determine default branch sha"); - process.exit(1); - } - const empty_tree_object = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; - const res = yield octokit.git.createCommit({ - owner, - repo, - message: "Initial commit", - tree: empty_tree_object, - parents: [] - }); - const initialCommitSha = res.data.sha; + const initialCommitSha = yield (0, github_2.createInitialCommit)(octokit, owner, repo); yield (0, github_2.createBranch)(octokit, owner, repo, "_codelimit_reports", initialCommitSha); } else { console.log("Branch _codelimit_reports already exists"); } + yield (0, github_2.createFile)(octokit, owner, repo, "_codelimit_reports", "main/badge.svg", "Hello from Code Limit"); }); } main(); diff --git a/src/action.ts b/src/action.ts index 0e9143b..63e9f32 100644 --- a/src/action.ts +++ b/src/action.ts @@ -5,7 +5,7 @@ import {getInput} from "@actions/core"; import {promisify} from "util"; import {context} from "@actions/github"; import {Octokit} from "@octokit/action"; -import {branchExists, createBranch, getBranchHeadSha, getDefaultBranch, getRepoName, getRepoOwner} from "./github"; +import {branchExists, createBranch, createFile, createInitialCommit, getRepoName, getRepoOwner} from "./github"; const streamPipeline = promisify(require('stream').pipeline); @@ -103,29 +103,12 @@ async function main() { process.exit(1); } if (!await branchExists(octokit, owner, repo, '_codelimit_reports')) { - const defaultBranch = getDefaultBranch(context); - if (!defaultBranch) { - console.error('Could not determine default branch'); - process.exit(1); - } - const sha = await getBranchHeadSha(octokit, owner, repo, defaultBranch); - if (!sha) { - console.error('Could not determine default branch sha'); - process.exit(1); - } - const empty_tree_object = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; - const res = await octokit.git.createCommit({ - owner: owner, - repo: repo, - message: 'Initial commit', - tree: empty_tree_object, - parents: [] - }); - const initialCommitSha = res.data.sha + const initialCommitSha = await createInitialCommit(octokit, owner, repo); await createBranch(octokit, owner, repo, '_codelimit_reports', initialCommitSha); } else { console.log('Branch _codelimit_reports already exists'); } + await createFile(octokit, owner, repo, '_codelimit_reports', 'main/badge.svg', 'Hello from Code Limit'); // let exitCode = 0; // if (doUpload) { diff --git a/src/github.ts b/src/github.ts index 14536c7..b9d7222 100644 --- a/src/github.ts +++ b/src/github.ts @@ -24,20 +24,16 @@ export async function createBranch(octokit: Octokit, owner: string, repo: string } } -export async function getBranchHeadSha(octokit: Octokit, owner: string, repo: string, branch: string) { - try { - const res = await octokit.git.getRef({ - owner: owner, repo: repo, ref: `heads/${branch}` - }) - const ref = res.data.object - return ref.sha - } catch (e) { - return undefined - } -} - -export function getDefaultBranch(ctx: Context): string | undefined { - return ctx.payload.repository?.default_branch +export async function createInitialCommit(octokit: Octokit, owner: string, repo: string): Promise { + const empty_tree_object = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; + const res = await octokit.git.createCommit({ + owner: owner, + repo: repo, + message: 'Initial commit', + tree: empty_tree_object, + parents: [] + }); + return res.data.sha; } export function getRepoOwner(ctx: Context): string | undefined { @@ -46,4 +42,19 @@ export function getRepoOwner(ctx: Context): string | undefined { export function getRepoName(ctx: Context): string | undefined { return ctx.payload.repository?.name -} \ No newline at end of file +} + +export async function createFile(octokit: Octokit, owner: string, repo: string, branchName: string, path: string, content: string) { + await octokit.repos.createOrUpdateFileContents({ + owner: owner, + repo: repo, + path: path, + message: `Update by Code Limit`, + branch: branchName, + content: Buffer.from(content).toString('base64'), + committer: { + name: 'Code Limit Action', + email: 'robvanderleek@gmail.com' + } + }); +}