Skip to content

Commit

Permalink
Merge branch 'main' into br-top-level-exports-2
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Sep 27, 2023
2 parents de6ac0e + 15e7026 commit 58625e7
Show file tree
Hide file tree
Showing 8 changed files with 6,249 additions and 45 deletions.
33 changes: 25 additions & 8 deletions api/hubdb.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import http from '../http';
import { QueryParams } from '../types/Http';
import { CreateRowsResponse, Row, Schema, Table } from '../types/Hubdb';
import {
CreateRowsResponse,
FetchRowsResponse,
Row,
Schema,
Table,
} from '../types/Hubdb';

const HUBDB_API_PATH = 'cms/v3/hubdb';

export async function fetchTable(
accountId: number,
tableId: number
tableId: string
): Promise<Table> {
return http.get(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}`,
Expand All @@ -23,9 +29,20 @@ export async function createTable(
});
}

export async function updateTable(
accountId: number,
tableId: string,
schema: Schema
) {
return http.patch(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}/draft`,
body: schema,
});
}

export async function publishTable(
accountId: number,
tableId: number
tableId: string
): Promise<Table> {
return http.post(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}/draft/publish`,
Expand All @@ -34,7 +51,7 @@ export async function publishTable(

export async function deleteTable(
accountId: number,
tableId: number
tableId: string
): Promise<void> {
return http.delete(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}`,
Expand All @@ -43,7 +60,7 @@ export async function deleteTable(

export async function createRows(
accountId: number,
tableId: number,
tableId: string,
rows: Array<Row>
): Promise<CreateRowsResponse> {
return http.post(accountId, {
Expand All @@ -54,9 +71,9 @@ export async function createRows(

export async function fetchRows(
accountId: number,
tableId: number,
tableId: string,
query: QueryParams = {}
): Promise<Array<Row>> {
): Promise<FetchRowsResponse> {
return http.get(accountId, {
url: `${HUBDB_API_PATH}/tables/${tableId}/rows/draft`,
query,
Expand All @@ -65,7 +82,7 @@ export async function fetchRows(

export async function deleteRows(
accountId: number,
tableId: number,
tableId: string,
rowIds: Array<string>
): Promise<void> {
return http.post(accountId, {
Expand Down
3 changes: 3 additions & 0 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ en:
logging:
creatingFile: "Creating file at {{ path }}"
errors:
hubdb:
invalidJsonPath: "The HubDB table file must be a '.json' file"
invalidJsonFile: "The '{{{ src }}' path is not a path to a file"
archive:
extractZip:
write: "An error occured writing temp project source."
Expand Down
35 changes: 19 additions & 16 deletions lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ declare global {
var githubToken: string;
}

type RepoPath = `${string}/${string}`;

const GITHUB_AUTH_HEADERS = {
authorization:
global && global.githubToken ? `Bearer ${global.githubToken}` : null,
};

export async function fetchJsonFromRepository(
repoName: string,
repoPath: RepoPath,
filePath: string,
ref: string
): Promise<JSON> {
try {
const URL = `https://raw.githubusercontent.com/HubSpot/${repoName}/${ref}/${filePath}`;
const URL = `https://raw.githubusercontent.com/${repoPath}/${ref}/${filePath}`;
debug('github.fetchJsonFromRepository', { url: URL });

const { data } = await axios.get<JSON>(URL, {
Expand All @@ -46,16 +48,16 @@ export async function fetchJsonFromRepository(
}

export async function fetchReleaseData(
repoName: string,
repoPath: RepoPath,
tag = ''
): Promise<GithubReleaseData> {
tag = tag.trim().toLowerCase();
if (tag.length && tag[0] !== 'v') {
tag = `v${tag}`;
}
const URI = tag
? `https://api.github.com/repos/${repoName}/releases/tags/${tag}`
: `https://api.github.com/repos/${repoName}/releases/latest`;
? `https://api.github.com/repos/${repoPath}/releases/tags/${tag}`
: `https://api.github.com/repos/${repoPath}/releases/latest`;
try {
const { data } = await axios.get<GithubReleaseData>(URI, {
headers: { ...DEFAULT_USER_AGENT_HEADERS, ...GITHUB_AUTH_HEADERS },
Expand All @@ -72,7 +74,7 @@ export async function fetchReleaseData(
}

async function downloadGithubRepoZip(
repoName: string,
repoPath: RepoPath,
tag = '',
releaseType: ValueOf<
typeof GITHUB_RELEASE_TYPES
Expand All @@ -82,12 +84,12 @@ async function downloadGithubRepoZip(
try {
let zipUrl: string;
if (releaseType === GITHUB_RELEASE_TYPES.REPOSITORY) {
debug('github.downloadGithubRepoZip.fetching', { releaseType, repoName });
zipUrl = `https://api.github.com/repos/${repoName}/zipball${
debug('github.downloadGithubRepoZip.fetching', { releaseType, repoPath });
zipUrl = `https://api.github.com/repos/${repoPath}/zipball${
ref ? `/${ref}` : ''
}`;
} else {
const releaseData = await fetchReleaseData(repoName, tag);
const releaseData = await fetchReleaseData(repoPath, tag);
zipUrl = releaseData.zipball_url;
const { name } = releaseData;
debug('github.downloadGithubRepoZip.fetchingName', { name });
Expand All @@ -114,7 +116,7 @@ const cloneGithubRepoCallbackKeys = ['success'];
export async function cloneGithubRepo(
dest: string,
type: string,
repoName: string,
repoPath: RepoPath,
sourceDir: string,
options: CloneGithubRepoOptions = {},
logCallbacks?: LogCallbacksArg<typeof cloneGithubRepoCallbackKeys>
Expand All @@ -125,7 +127,8 @@ export async function cloneGithubRepo(
);
const { themeVersion, projectVersion, releaseType, ref } = options;
const tag = projectVersion || themeVersion;
const zip = await downloadGithubRepoZip(repoName, tag, releaseType, ref);
const zip = await downloadGithubRepoZip(repoPath, tag, releaseType, ref);
const repoName = repoPath.split('/')[1];
const success = await extractZipArchive(zip, repoName, dest, { sourceDir });

if (success) {
Expand All @@ -135,12 +138,12 @@ export async function cloneGithubRepo(
}

async function getGitHubRepoContentsAtPath(
repoName: string,
repoPath: RepoPath,
path: string,
ref?: string
): Promise<Array<GithubRepoFile>> {
const refQuery = ref ? `?ref=${ref}` : '';
const contentsRequestUrl = `https://api.github.com/repos/${repoName}/contents/${path}${refQuery}`;
const contentsRequestUrl = `https://api.github.com/repos/${repoPath}/contents/${path}${refQuery}`;

const response = await axios.get<Array<GithubRepoFile>>(contentsRequestUrl, {
headers: { ...DEFAULT_USER_AGENT_HEADERS, ...GITHUB_AUTH_HEADERS },
Expand All @@ -160,9 +163,9 @@ async function fetchGitHubRepoContentFromDownloadUrl(
fs.writeFileSync(dest, resp.data, 'utf8');
}

// Writes files from a HubSpot public repository to the destination folder
// Writes files from a public repository to the destination folder
export async function downloadGithubRepoContents(
repoName: string,
repoPath: RepoPath,
contentPath: string,
dest: string,
ref?: string,
Expand All @@ -172,7 +175,7 @@ export async function downloadGithubRepoContents(

try {
const contentsResp = await getGitHubRepoContentsAtPath(
repoName,
repoPath,
contentPath,
ref
);
Expand Down
Loading

0 comments on commit 58625e7

Please sign in to comment.