Skip to content

Commit

Permalink
Merge pull request #35 from botpress/cc_add_workspace_uploader
Browse files Browse the repository at this point in the history
bp_uploader: Add workspace support, filter git files
  • Loading branch information
charlescatta authored May 26, 2022
2 parents 18259dc + d312b4c commit 783856a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
11 changes: 9 additions & 2 deletions custom_tools/bp_uploader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ program
"Get from bot.config.json file"
)
)
.option("-i, --ignore", "Ignore file list to read", false)
.addOption(
new Option("-w, --workspace [workspace_id]", "Workspace to upload the bot to").default(
"default"
)
)
.addOption(
new Option("-o, --overwrite", "Overwrite the bot if it exists").default(false)
)
.action(async (url, botFolderPath, options) => {
const { jwt } = auth.getToken(url);
if (!jwt) {
Expand All @@ -44,7 +51,7 @@ program
}
log.info(`Uploading ${botFolderPath} to ${url}`);
try {
await upload(url, botFolderPath, jwt, log, options.botId);
await upload(url, botFolderPath, jwt, log, options.botId, options.workspace, options.overwrite);
} catch (err) {
log.error(`An error occured during bot upload: `, err);
return;
Expand Down
40 changes: 27 additions & 13 deletions custom_tools/bp_uploader/src/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ const getFolderAsTarStream = async (folderPath: string, logger: Logger): Promise
throw new Error(`${folderPath} is not a directory`);
}
const tarStream = tarstream.pack();
const files = await glob(`./**/*`, {
let files = await glob(`./**/*`, {
dot: true,
followSymbolicLinks: false,
cwd: pathString,
onlyFiles: true,
});
files = files.filter(f => !f.includes(".git"))

logger.info(`Found ${files.length} files`);

Expand Down Expand Up @@ -63,21 +64,34 @@ export const upload = async (
folderPath: string,
authToken: string,
logger: Logger,
botId: string
botId: string,
workspace: string = 'default',
overwrite: boolean = false
) => {
const data = await getFolderAsTarStream(folderPath, logger);
const endpoint = getUploadURL(url, botId || global.botId);
return new Promise(async (resolve, reject) => {
const data = await getFolderAsTarStream(folderPath, logger);
const endpoint = getUploadURL(url, botId || global.botId, overwrite);

logger.debug(`Uploading to ${endpoint.href}`);
logger.debug(`Uploading to ${endpoint.href}`);

const req = request.post(endpoint.href, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/tar+gzip",
},
});
return new Promise((resolve) => {
const req = request.post(endpoint.href, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/tar+gzip",
"X-BP-Workspace": workspace
},
}, (err, res, body) => {
logger.info(res.statusCode, body);
if (err) {
logger.error(`An error occured while uploading: `, err);
reject(err);
return;
} else {
logger.info(`Uploaded bot ${botId} to workspace ${workspace}`);
resolve(null);
}
});

data.pipe(req);
data.on("end", resolve);
});
};

0 comments on commit 783856a

Please sign in to comment.