Skip to content

Commit

Permalink
feat: allow env var for github directly in env
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuckson committed Jul 25, 2023
1 parent 091cbd5 commit f64ae91
Showing 1 changed file with 65 additions and 39 deletions.
104 changes: 65 additions & 39 deletions lib/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { Octokit } from "@octokit/rest";
import { createAppAuth } from "@octokit/auth-app";

import fs from "fs";
import { cacheWrite, cacheRead } from '@/lib/redis';
import { cacheWrite, cacheRead } from "@/lib/redis";

let gitHubInstance;


function getGitHubConfiguration() {
const privateKeyPath = process.env.GITHUB_PRIVATE_KEY_FILE;
const privateKey = fs.readFileSync(privateKeyPath, "utf-8");
let privateKey = process.env.GITHUB_PRIVATE_KEY;
if (!privateKey) {
const privateKeyPath = process.env.GITHUB_PRIVATE_KEY_FILE;
privateKey = fs.readFileSync(privateKeyPath, "utf-8");
}
return {
privateKey: privateKey,
appId: process.env.GITHUB_APP_ID,
installationId: process.env.GITHUB_INSTALLATION_ID,
}
};
}

export function createGitHubInstance(config = getGitHubConfiguration()) {
Expand All @@ -25,17 +27,16 @@ export function createGitHubInstance(config = getGitHubConfiguration()) {
appId: config.appId,
privateKey: config.privateKey,
installationId: config.installationId,
},
},
});
return octokit
return octokit;
} catch (e) {
throw new Error(`[GitHub] Could not create a GitHub instance: ${e.message}`);
throw new Error(
`[GitHub] Could not create a GitHub instance: ${e.message}`,
);
}

}



// Function to get a file content
export async function getBranchSha(owner, repo, branch) {
if (!gitHubInstance) {
Expand All @@ -52,7 +53,7 @@ export async function getBranchSha(owner, repo, branch) {
// If the content was found in the cache, return it
return cachedContent;
} else {
console.info('[Github][Cache][MISS]:',cacheKey )
console.info("[Github][Cache][MISS]:", cacheKey);
}
const branchSha = await gitHubInstance.rest.repos.getBranch({
owner,
Expand All @@ -65,17 +66,12 @@ export async function getBranchSha(owner, repo, branch) {
} catch (error) {
console.error(`[GitHub][getBranchSha] Error getting sha: ${error}`);
// throw new Error(`[GitHub][getBranchSha] Could not get sha for branch`);

}

}



// Function to get a file content
export async function getFileContent(owner, repo, branch, path) {

const branchSha = await getBranchSha(owner, repo, branch,)
const branchSha = await getBranchSha(owner, repo, branch);

// Generate a unique cache key for this file
const cacheKey = `github:getContent:${owner}:${repo}:${branchSha}:${path}`;
Expand All @@ -87,7 +83,7 @@ export async function getFileContent(owner, repo, branch, path) {
// If the content was found in the cache, return it
return cachedContent;
} else {
console.info('[Github][Cache][MISS]:',cacheKey )
console.info("[Github][Cache][MISS]:", cacheKey);
}

if (!gitHubInstance) {
Expand All @@ -103,64 +99,94 @@ export async function getFileContent(owner, repo, branch, path) {
});

let content;
if (response.data.encoding === 'base64') {
if (response.data.encoding === "base64") {
// Decode base64 content for image files
content = Buffer.from(response.data.content, 'base64');
content = Buffer.from(response.data.content, "base64");
} else {
// For text files, assume UTF-8 encoding
content = Buffer.from(response.data.content, 'utf-8');
content = Buffer.from(response.data.content, "utf-8");
}
// Store the content in the cache before returning it
await cacheWrite(cacheKey, content, 60*60*24); // cache for 24 hours
await cacheWrite(cacheKey, content, 60 * 60 * 24); // cache for 24 hours
return content;

} catch (error) {
console.error(`[GitHub][getFileContent] Error retrieving file content: ${error}`);
console.error(
`[GitHub][getFileContent] Error retrieving file content: ${error}`,
);
// throw new Error(`[GitHub][getFileContent] Could not get file`);
// console.error('Error retrieving file content:', error, 'path:', path);
// return null;
}
}

// Function to get all files for a given path
export async function getAllFiles(owner, repo, branch, path, recursive = true, filter = null) {
export async function getAllFiles(
owner,
repo,
branch,
path,
recursive = true,
filter = null,
) {
if (!gitHubInstance) {
gitHubInstance = await createGitHubInstance();
}
const branchSha = await getBranchSha(owner, repo, branch,);
const files = await getAllFilesRecursive(gitHubInstance, owner, repo, branchSha, path, recursive, filter);
const branchSha = await getBranchSha(owner, repo, branch);
const files = await getAllFilesRecursive(
gitHubInstance,
owner,
repo,
branchSha,
path,
recursive,
filter,
);
return files;

}


async function getAllFilesRecursive(gitHubInstance, owner, repo, sha, path, recursive, filter) {
async function getAllFilesRecursive(
gitHubInstance,
owner,
repo,
sha,
path,
recursive,
filter,
) {
const response = await gitHubInstance.repos.getContent({
owner,
repo,
path,
ref: sha,
});

const fileObjects = response.data.filter(obj => obj.type === 'file');
let files = fileObjects.map(obj => obj.path);
const fileObjects = response.data.filter((obj) => obj.type === "file");
let files = fileObjects.map((obj) => obj.path);

if (recursive) {
const dirObjects = response.data.filter(obj => obj.type === 'dir');
const dirObjects = response.data.filter((obj) => obj.type === "dir");
for (const dirObject of dirObjects) {
const subPath = path ? `${path}/${dirObject.name}` : dirObject.name;
const subFiles = await getAllFilesRecursive(gitHubInstance, owner, repo, sha, subPath, recursive, filter);
const subFiles = await getAllFilesRecursive(
gitHubInstance,
owner,
repo,
sha,
subPath,
recursive,
filter,
);
files = files.concat(subFiles);
}
}
if (filter) {
const regex = createFilterRegex(filter);
files = files.filter(file => regex.test(file));
files = files.filter((file) => regex.test(file));
}

return files;
}
function createFilterRegex(filter) {
const escapedFilter = filter.replace(/\./g, '\\.').replace(/\*/g, '.*');
return new RegExp(`^.*${escapedFilter}$`, 'i');
}
const escapedFilter = filter.replace(/\./g, "\\.").replace(/\*/g, ".*");
return new RegExp(`^.*${escapedFilter}$`, "i");
}

0 comments on commit f64ae91

Please sign in to comment.