Skip to content
This repository was archived by the owner on Jan 14, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ecmaVersion": 11,
"sourceType": "module"
},
"plugins": ["react"],
"plugins": ["react", "@emotion"],
"rules": {
"class-methods-use-this": "off",
"comma-dangle": "off",
Expand Down
5 changes: 0 additions & 5 deletions common/config.js

This file was deleted.

9 changes: 0 additions & 9 deletions common/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const axios = require('axios');

const storage = require('./storage');

let instance;

const TWENTY_SECONDS = 20000;
Expand Down Expand Up @@ -43,13 +41,6 @@ const getInstance = () => instance;

const reset = () => { instance = undefined; };

if (storage.user.isDefined()) {
const { api_key, redmineEndpoint } = storage.user.get();
if (api_key && redmineEndpoint) {
initialize(redmineEndpoint, api_key);
}
}

const handleReject = (error) => {
// if this request was not cancelled
if (!axios.isCancel(error)) {
Expand Down
26 changes: 0 additions & 26 deletions common/storage.js

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion main/exceptionCatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const isDev = require('electron-is-dev');
const { app, dialog, clipboard } = require('electron');
const logger = require('electron-log');

const { report } = require('../common/reporter');
const { report } = require('./handlers/errorHandler');

const config = {
showDialog: !isDev,
Expand Down
File renamed without changes.
61 changes: 38 additions & 23 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const electronUtils = require('electron-util');
const isDev = require('electron-is-dev');
const logger = require('electron-log');

const storage = require('../common/storage');
const storage = require('./storage');
const { redmineClient } = require('./redmine');

const Tray = require('./tray');
Expand Down Expand Up @@ -344,32 +344,53 @@ const initialize = () => {
notification.show();
});

ipcMain.on('menu', (event, { settings }) => {
ipcMain.on('menu', (event, message) => {
const { settings } = JSON.stringify(message);
generateMenu({ settings });
});

ipcMain.on('storage', (event, message) => {
const { action, data } = JSON.parse(message);
if (action === 'read') {
event.reply('storage', storage.settings.get());
} else if (action === 'save') {
storage.settings.set(data);
} else {
throw new Error('Unable to process the requested action', action);
}
});
storage.initializeSessionEvents(ipcMain);

ipcMain.on('request', async (event, message) => {
const { payload, config, id } = JSON.parse(message);
console.log('Received a request for query', { payload, config });
const { payload, id } = JSON.parse(message);
console.log('Received a request for query', { payload });

if (!redmineClient.isInitialized()) {
redmineClient.initialize(config);
const error = new Error('Unauthorized');
error.status = 401;
event.reply(`response:${id}`, { success: false, error });
return;
}

const response = await redmineClient.send(payload);

event.reply(`response:${id}`, response);
});

ipcMain.on('system-request', async (event, message) => {
const { action, payload, id } = JSON.parse(message);
console.log(`Received system request for ${action}`, { payload });

switch (action.toLowerCase()) {
case 'login': {
const response = await redmineClient.initialize({
endpoint: payload.endpoint,
token: payload.token,
headers: payload.headers
});
event.reply(`system-response:${id}`, response);
break;
}
case 'logout': {
redmineClient.reset();
await storage.resetActiveSession();
event.reply(`system-response:${id}`, { success: true });
break;
}
default:
break;
}
});
};

app.on('certificate-error', (event, webContents, _url, error, certificate, callback) => {
Expand All @@ -391,15 +412,8 @@ app.once('ready', () => {
}

// eslint-disable-next-line global-require
const config = require('../common/config');
const config = require('./env');
PORT = config.PORT;
// eslint-disable-next-line global-require
require('../common/request'); // to initialize from storage

if (!storage.settings.isDefined()) {
// sets defaul value that storage.settings.get returns as a fallback
storage.settings.set(storage.settings.get());
}

initialize();
generateMenu();
Expand All @@ -410,6 +424,7 @@ app.once('ready', () => {

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
storage.disposeSessionEvents(ipcMain);
app.quit();
}
});
Expand Down
85 changes: 65 additions & 20 deletions main/redmine.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,76 @@
const got = require('got');
const { transform } = require('./transformers/index');

const createRequestClient = (initConfig = {}) => {
let isInitialized = false;
const createRequestClient = () => {
let instance;

const initialize = (config) => {
isInitialized = Boolean(config.endpoint && config.token);
instance = got.extend({
prefixUrl: config.endpoint,
headers: { 'X-Redmine-API-Key': config.token },
responseType: 'json'
});
};
let isInitialized;

const handleUnsuccessfulRequest = (error) => ({
success: false,
error
});

const initialize = async (data) => {
if (isInitialized) {
reset();
}

const route = 'users/current.json';

const configuration = data.token
? {
prefixUrl: data.endpoint,
headers: { 'X-Redmine-API-Key': data.token },
responseType: 'json'
}
: {
prefixUrl: data.endpoint,
headers: {
Authorization: data.headers.Authorization
},
responseType: 'json'
};

try {
const response = await got(route, {
method: 'GET',
...configuration
});

console.log(response.statusCode, response.body);

const loginSuccess = response.statusCode === 200;
isInitialized = loginSuccess;

if (loginSuccess) {
const payload = transform(route, response.body);

instance = got.extend({
...configuration,
headers: {
'X-Redmine-API-Key': payload.token
}
});

return {
success: true,
payload
};
}

return handleUnsuccessfulRequest(response.body);
} catch (error) {
return handleUnsuccessfulRequest(error);
}
};

const reset = () => {
isInitialized = false;
instance = undefined;
};

const send = async (data) => {
if (!instance && !data.headers?.Authorization) {
if (!isInitialized) {
throw new Error('Http client is not initialized.');
}

Expand All @@ -34,12 +84,8 @@ const createRequestClient = (initConfig = {}) => {
console.log(response.statusCode, response.body);

if (response.statusCode === 200) {
if (!isInitialized && data.route === 'users/current.json' && response.body.user?.api_key) {
initialize({ endpoint: initConfig.endpoint, token: response.body.user?.api_key });
}

return {
data: transform(data.route, response.body),
payload: transform(data.route, response.body),
success: true,
};
}
Expand All @@ -50,12 +96,11 @@ const createRequestClient = (initConfig = {}) => {
}
};

initialize(initConfig);

return {
initialize,
isInitialized: () => isInitialized,
send
send,
reset
};
};

Expand Down
Loading