Skip to content

Commit

Permalink
Added feature to edit mapping from task to projectId manually.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgebert committed Aug 19, 2023
1 parent 5784d80 commit 129d1ae
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/bcsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class BcsClient implements BcsClientInterface {
this.page = await this.browser.newPage();
await this.page.setViewport(this.viewport);

await this.page.goto(`${baseUrl}/bcs/login`, {timeout: 2000})
await this.page.goto(`${baseUrl}/bcs/login`, {timeout: 3000})
.catch((e) => {
this.close();
throw new PageNotFoundError(`Can not reach ${baseUrl}/bcs/login`);
Expand All @@ -67,7 +67,7 @@ export class BcsClient implements BcsClientInterface {
await this.page.click('#loginbutton');

// TODO fail fast on failed login
await this.page.waitForSelector('input.notificationPermissionLater', {timeout: 2000})
await this.page.waitForSelector('input.notificationPermissionLater', {timeout: 4000})
.then((btn) => btn.click())
.catch((e) => {
this.close();
Expand Down
5 changes: 4 additions & 1 deletion src/commands/commandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {handleResetCommand} from "./resetCommand";
import {exit} from 'process'
import {BcsClient} from "../bcsClient";
import {handleCheckCommand} from "./checkCommand";
import {handleMappingCommand} from "./mappingCommand";


export const handleCommand = async (command: string) => {


switch (command) {
case 'add':
await handleAddCommand();
Expand All @@ -23,6 +23,9 @@ export const handleCommand = async (command: string) => {
case 'check':
await handleCheckCommand();
break;
case 'mapping':
await handleMappingCommand();
break;
case 'quit':
BcsClient.getInstance().then((client) => client.close());
console.log('Goodbye!')
Expand Down
65 changes: 65 additions & 0 deletions src/commands/mappingCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {readFromFile, writeToFile} from "../utils/fileUtils";

const {Input} = require('enquirer');

interface MappingItem {
source: string;
regex: string;
projectId: number;
}


export const handleMappingCommand = async () => {

const mappingFile = readFromFile('mapping.json') || '[]';
const mapping = JSON.parse(mappingFile);

const validateTaskToProjectMapping = (input: string): string | boolean => {
try {
const mapping: MappingItem[] = input
.split('\n')
.filter(m => m)
.map(m => JSON.parse(m));

const isValidMapping = mapping.every(item =>
'source' in item && 'regex' in item && 'projectId' in item
);

if (!isValidMapping) {
return 'Mapping should have the format {"source":"<description|ticket>","regex":"<regex>","projectId":<number>}';
}

const validSources = ['description', 'ticket'];
const hasValidSource = mapping.every(item =>
validSources.includes(item.source)
);

if (!hasValidSource) {
return "Invalid value for 'source' field - valid values are 'description' and 'ticket'";
}

} catch (e) {
if (e instanceof SyntaxError) {
return 'Please provide the mapping in valid JSON format.';
}
return false;
}
return true;
};

const prompt = new Input({
message: 'Please edit the task to projectId mapping:',
initial: mapping.map(m => "\n" + JSON.stringify(m)).join(""),
multiline: true,
validate: validateTaskToProjectMapping
});

await prompt.run()
.then(input => {
const mapping = input.split("\n").filter(m => m).map(m => JSON.parse(m));
// console.log('ANSWER', mapping);
writeToFile('mapping.json', JSON.stringify(mapping));
})
.catch(console.log);

}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {config as dotenvConfig} from 'dotenv';
import {handleCommand} from "./commands/commandHandler";
import {BcsClient, ForbiddenError, PageNotFoundError} from "./bcsClient";
import {setEnvValue} from "./utils/utils";
import {setEnvValue} from "./utils/envUtils";

const colors = require('ansi-colors');
const {prompt} = require('enquirer');
Expand Down Expand Up @@ -113,7 +113,7 @@ const main = async () => {
type: 'select',
name: 'command',
message: 'What do you want to do?',
choices: ['add', 'get', 'check', 'reset', 'quit']
choices: ['add', 'get', 'check', 'reset', 'mapping', 'quit']
}).then(({command}) => {

handleCommand(command).then(() => main())
Expand Down
File renamed without changes.

0 comments on commit 129d1ae

Please sign in to comment.