Skip to content

Commit

Permalink
chore: 🚧 Create file
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Dec 1, 2024
1 parent f48a58b commit 920cc25
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 72 deletions.
67 changes: 30 additions & 37 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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: "[email protected]"
}
});
});
}
}
});

Expand Down Expand Up @@ -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();
Expand Down
23 changes: 3 additions & 20 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 26 additions & 15 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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 {
Expand All @@ -46,4 +42,19 @@ export function getRepoOwner(ctx: Context): string | undefined {

export function getRepoName(ctx: Context): string | undefined {
return ctx.payload.repository?.name
}
}

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: '[email protected]'
}
});
}

0 comments on commit 920cc25

Please sign in to comment.