Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hocgin committed Mar 19, 2024
1 parent 6452ef9 commit f6039c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import {debug, getInput, info, setFailed, warning} from "@actions/core";
import {context, getOctokit} from '@actions/github';
import {info, warning} from "@actions/core";
import * as github from '@actions/github';
import {Inputs, Outputs} from "./main";


// @ts-ignore
const crepo = github.context;

// @ts-ignore
export const octokit = getOctokit(process.env.GITHUB_TOKEN!);

type GetContentResponseType = any;

// @ts-ignore
const tag = (prefix: string) => `${prefix.padEnd(9)} |`

async function getFileContents(branch: string, owner?: string, repo?: string, filepath?: string): Promise<GetContentResponseType | undefined> {
async function getFileContents(branch: string, owner: string, repo: string, filepath: string, token: string): Promise<GetContentResponseType | undefined> {
try {
const octokit = github.getOctokit(token);
const body = {owner, repo, ref: branch, path: filepath}
info(`👉 ${JSON.stringify(body, null, 2)}`);
const {data} = await octokit.rest.repos.getContent(body);
Expand All @@ -31,24 +26,30 @@ function nodeBase64ToUtf8(data: string) {
return Buffer.from(data, "base64").toString("utf-8");
}

async function getBranch(branch?: string): Promise<string> {
async function getBranch(branch: string, repo: string, token: string): Promise<string> {
const octokit = github.getOctokit(token);
if (branch) {
return Promise.resolve(branch);
}
const {data} = await octokit.rest.repos.get(context.repo);
const {data} = await octokit.rest.repos.get(repo);
return data.default_branch;
}

export async function run(input: Inputs): Promise<Outputs> {
// @ts-ignore
const crepo = github.context.repo;
let env = {};
const branch = await getBranch(input?.branch);
let file = input?.file ?? "env";
let owner = input?.owner ?? crepo?.owner;
let repo = input?.repo ?? crepo?.repo;
let owner: string = input?.owner ?? crepo.owner;
let repo = input?.repo ?? crepo.repo;
// @ts-ignore
let token = input?.token ?? process.env.GITHUB_TOKEN;

const branch = await getBranch((input.branch ?? github.context.ref), repo, token);

info(`${tag("🟡 QUEUE")} read file content`);

let currentFile = await getFileContents(branch, owner, repo, file);
let currentFile = await getFileContents(branch, owner, repo, file, token);
if (currentFile && 'content' in currentFile) {
const fileContent = nodeBase64ToUtf8(currentFile.content || '');
env = JSON.parse(fileContent);
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Inputs {
owner?: string;
repo?: string;
branch?: string;
token?: string;
}

export interface Outputs {
Expand Down

0 comments on commit f6039c4

Please sign in to comment.