Skip to content

Commit

Permalink
Releasing Stage to Main (#28)
Browse files Browse the repository at this point in the history
* MWPW-150372: Refactor (#23)

* MWPW-150372: Refactor

* MWPW-150372: Removing temp logs

* MWPW-150372: Unit tests failure after refactor

* MWPW-154226: Remove host and referrer (#27)

* .env template update
  • Loading branch information
sukamat authored Jul 29, 2024
1 parent e1dc24d commit f3da185
Show file tree
Hide file tree
Showing 14 changed files with 2,465 additions and 1,762 deletions.
127 changes: 86 additions & 41 deletions actions/appConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ const crypto = require('crypto');
const { strToArray, getAioLogger } = require('./utils');
const UrlInfo = require('./urlInfo');

// Max activation is 1hrs, set to 2hrs
const MAX_ACTIVATION_TIME = 2 * 60 * 60 * 1000;
const ENV_VAR_ACTIVATION_ID = '__OW_ACTIVATION_ID';
const GRAPH_API = 'https://graph.microsoft.com/v1.0';

/**
* This store the Graybox configs.
* This stores the Graybox configs.
*/
class AppConfig {
// set payload per activation
configMap = { payload: {} };
constructor(params) {
this.configMap = { payload: {} };
if (params) {
this.setAppConfig(params);
}
}

setAppConfig(params) {
const payload = this.initPayload();
// Called during action start to cleanup old entries
this.removeOldPayload();
const payload = this.getPayload();

// These are payload parameters
payload.spToken = params.spToken;
// eslint-disable-next-line no-underscore-dangle
const headers = params.__ow_headers;
payload.spToken = headers?.['user-token'] || params.spToken;
payload.adminPageUri = params.adminPageUri;
payload.projectExcelPath = params.projectExcelPath;
payload.rootFolder = params.rootFolder;
Expand Down Expand Up @@ -70,37 +72,8 @@ class AppConfig {
};
}

// Activation Payload Related
initPayload() {
this.configMap.payload[this.getPayloadKey()] = {
payloadAccessedOn: new Date().getTime()
};
return this.configMap.payload[this.getPayloadKey()];
}

getPayloadKey() {
return process.env[ENV_VAR_ACTIVATION_ID];
}

getPayload() {
this.configMap.payload[this.getPayloadKey()].payloadAccessedOn = new Date().getTime();
return this.configMap.payload[this.getPayloadKey()];
}

removePayload() {
delete this.configMap.payload[this.getPayloadKey()];
}

/**
* Similar to LRU
*/
removeOldPayload() {
const { payload } = this.configMap;
const payloadKeys = Object.keys(payload);
const leastTime = new Date().getTime();
payloadKeys
.filter((key) => payload[key]?.payloadAccessedOn < leastTime - MAX_ACTIVATION_TIME)
.forEach((key) => delete payload[key]);
return this.configMap.payload;
}

// Configs related methods
Expand Down Expand Up @@ -173,6 +146,78 @@ class AppConfig {
ignoreUserCheck() {
return true && this.configMap.ignoreUserCheck;
}

getUserToken() {
return this.getPayload().spToken;
}

getSpConfig() {
if (!this.getUrlInfo().isValid()) {
return undefined;
}

const config = this.getConfig();

// get drive id if available
const { driveId, rootFolder, gbRootFolder } = this.getPayload();
const drive = driveId ? `/drives/${driveId}` : '/drive';

const baseURI = `${config.spSite}${drive}/root:${rootFolder}`;
const gbBaseURI = `${config.spSite}${drive}/root:${gbRootFolder}`;
const baseItemsURI = `${config.spSite}${drive}/items`;
return {
api: {
url: GRAPH_API,
file: {
get: { baseURI, gbBaseURI },
download: { baseURI: `${config.spSite}${drive}/items` },
upload: {
baseURI,
gbBaseURI,
method: 'PUT',
},
delete: {
baseURI,
gbBaseURI,
method: 'DELETE',
},
update: {
baseURI,
gbBaseURI,
method: 'PATCH',
},
createUploadSession: {
baseURI,
gbBaseURI,
method: 'POST',
payload: { '@microsoft.graph.conflictBehavior': 'replace' },
},
copy: {
baseURI,
gbBaseURI,
method: 'POST',
payload: { '@microsoft.graph.conflictBehavior': 'replace' },
},
},
directory: {
create: {
baseURI,
gbBaseURI,
method: 'PATCH',
payload: { folder: {} },
},
},
excel: {
get: { baseItemsURI },
update: {
baseItemsURI,
method: 'POST',
},
},
batch: { uri: `${GRAPH_API}/$batch` },
},
};
}
}

module.exports = new AppConfig();
module.exports = AppConfig;
116 changes: 0 additions & 116 deletions actions/config.js

This file was deleted.

Loading

0 comments on commit f3da185

Please sign in to comment.