Skip to content

Commit

Permalink
MWPW-133692 Promote status action for getting status, files and resul…
Browse files Browse the repository at this point in the history
…ts (#58)

* MWPW-133692 - Promote results

Promote Action
1. Storing the start, end time, batchStatus as Completed, Completed with error and In Progress this along with status done. (in instance_info.json)
2. Added a file milo_pink/instance_results.json with merged results data.

Promote status action with below parameters
1. Promote Status - This lists all the batches in the promote along with overall status, start and end time
2. Promote results - This will list merged batch results (i.e failed promote, preview, publish)
3. Batch File  - This will list the batch-info files in the specific batch (no status per file is being sent)
4. Batch Results - This will list the specific batch results (i.e failed promote, preview, publish)

* Updated for start time for batch progress

* Added a null handling

---------

Co-authored-by: Raghu A <[email protected]>
  • Loading branch information
raga-adbe-gh and Raghu A authored Sep 25, 2023
1 parent 58b171c commit 51492f0
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 14 deletions.
2 changes: 1 addition & 1 deletion actions/FgAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class FgAction {

/**
* Check if the action is in progress.
* @param {*} options with checkActivation flag if the flag is set then activation
* @param {*} options with checkActivation flag if the flag is set then activation
* is check is skipped
* @returns object with ok true
*/
Expand Down
10 changes: 7 additions & 3 deletions actions/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ class Batch {
* @returns Get manifest file content e.g. json for updating status/reporting
*/
async getResultsContent() {
const buffer = await this.filesSdk.read(this.resultsFile);
const data = buffer.toString();
return JSON.parse(data);
const fileProps = await this.filesSdk.list(this.resultsFile);
if (fileProps && fileProps.length) {
const buffer = await this.filesSdk.read(this.resultsFile);
const data = buffer.toString();
return JSON.parse(data);
}
return null;
}
}

Expand Down
28 changes: 27 additions & 1 deletion actions/batchManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class BatchManager {
this.instanceKey = (params.instanceKey || 'default').replaceAll('/', '_');
this.instancePath = `${this.batchFilesPath}/${this.key}/instance${this.instanceKey}`;
this.instanceFile = `${this.instancePath}/instance_info.json`;
this.resultsFile = `${this.instancePath}/instance_results.json`;
return this;
}

Expand Down Expand Up @@ -209,15 +210,40 @@ class BatchManager {
await this.writeToBmTracker(params);
}

async markComplete() {
async markComplete(results) {
const params = {};
params[`${this.instanceKey}`] = {
done: true,
proceed: false,
};
if (results) await this.writeResults(results);
await this.writeToBmTracker(params);
}

/**
* @param {*} data Writes overall resuts to instance-results file
*/
async writeResults(data) {
try {
await this.filesSdk.write(this.resultsFile, JSON.stringify(data));
} catch (err) {
logger.info(`Error while writing to results file ${err.message}`);
}
}

/**
* @returns Get promote results (only failed files are returned)
*/
async getResultsContent() {
const fileProps = await this.filesSdk.list(this.resultsFile);
if (fileProps && fileProps.length) {
const buffer = await this.filesSdk.read(this.resultsFile);
const data = buffer.toString();
return JSON.parse(data);
}
return null;
}

/** Cleanup files for the current action */
async cleanupFiles() {
await this.filesSdk.delete(`${this.instancePath}/`);
Expand Down
21 changes: 13 additions & 8 deletions actions/promote/triggerNTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ async function main(params) {
*/
async function checkBatchesInProg(fgRootFolder, actDtls, ow) {
let fgStatus;
let batchState;
let batchInProg = false;
let allDone = true;
let counter = 0;
Expand All @@ -135,14 +136,18 @@ async function checkBatchesInProg(fgRootFolder, actDtls, ow) {
action: `${PROMOTE_BATCH}_${batchNumber}`,
keySuffix: `Batch_${batchNumber}`
});
batchInProg = await fgStatus?.getStatusFromStateLib().then((result) => {
if (result?.action && FgStatus.isInProgress(result.action.status)) {
return true;
}
return false;
});
batchState = await fgStatus.getStatusFromStateLib().then((result) => result?.action);
batchInProg = false || FgStatus.isInProgress(batchState?.status);
if (batchInProg) batchInProg = await actInProgress(ow, activationId, batchInProg);
actDtls[counter].done = !batchInProg;
if (!batchInProg) {
actDtls[counter].done = true;
actDtls[counter].startTime = batchState?.startTime;
actDtls[counter].endTime = batchState?.endTime;
actDtls[counter].status = batchState?.status;
} else if (batchState) {
actDtls[counter].startTime = batchState.startTime;
actDtls[counter].status = batchState.status;
}
allDone &&= !batchInProg;
} else {
allDone &&= done;
Expand Down Expand Up @@ -229,7 +234,7 @@ async function completePromote(projectExcelPath, actDtls, batchManager, fgStatus
await updateExcelTable(projectExcelPath, 'PROMOTE_STATUS', excelValues);
logger.info('Project excel file updated with promote status.');

await batchManager.markComplete();
await batchManager.markComplete(fgErrors ? { failedPromotes, failedPreviews, failedPublishes } : null);
logger.info('Marked complete in batch manager.');
}

Expand Down
4 changes: 3 additions & 1 deletion actions/promote/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ async function main(params) {
return vStat;
}

fgStatus.clearState();

urlInfo.setUrlInfo(payload.adminPageUri);
respPayload = 'Getting all files to be promoted.';
await fgStatus.updateStatusToStateLib({
status: FgStatus.PROJECT_STATUS.IN_PROGRESS,
status: FgStatus.PROJECT_STATUS.STARTED,
statusMessage: respPayload
});
logger.info(respPayload);
Expand Down
124 changes: 124 additions & 0 deletions actions/promoteStatus/promoteStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* ************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
************************************************************************* */

// eslint-disable-next-line import/no-extraneous-dependencies
const {
errorResponse, getAioLogger, getInstanceKey, PROMOTE_ACTION
} = require('../utils');
const appConfig = require('../appConfig');
const FgUser = require('../fgUser');
const BatchManager = require('../batchManager');

const logger = getAioLogger();
const BAD_REQUEST_SC = 400;
const AUTH_FAILED_SC = 401;
const GEN_ERROR_SC = 500;

/**
* Returns promote status. The details of the status needed are passed a arguments.
* Sample Input
* {
* "promoteStatus": true,
* "batchFiles": 2,
* "batchResults": 2,
* "promoteResults": true,
* "fgShareUrl": "https://adobe.sharepoint.com/:f:/r/sites/adobecom/Shared%20Documents/milo-pink<relativePath>?web=1",
* "spToken": ""
* }
* @param {*} args Action arguments
* @returns results based on parameter below
* "promoteStatus": { batchesInfo: [ {"activationId": "", "batchNumber": 1, "done": true,
* "endTime": "2023-09-19T13:53:32.846Z", "startTime": "2023-09-07T10:00:00.333Z", "status": "COMPLETED"}, ] }
* "batchFiles": [
* "/drafts/fold/lvl1/lvl2/testforfloodgate2.docx",
* "/drafts/fold/lvl11/lvl2/testforfloodgate.docx"
* ]
* "batchResults": {
* "failedPreviews": [
* "/drafts/fold/lvl11/testforfloodgate2",
* "/drafts/fold/lvl1/lvl2/testforfloodgate"
* ],
* "failedPromotes": [],
* "failedPublishes": []
* }
* "promoteResults": {
* "failedPreviews": [
* "/drafts/fold/lvl11/testforfloodgate2",
* "/drafts/fold/lvl1/lvl2/testforfloodgate",
* "/drafts/fold/lvl1/lvl2/testforfloodgate2",
* "/drafts/fold/lvl11/lvl2/testforfloodgate"
* ],
* "failedPromotes": [],
* "failedPublishes": []
* }
*/
async function main(args) {
const payload = {};
try {
appConfig.setAppConfig(args);
const batchNumber = args.batchFiles || args.batchResults;

// Validations
const fgUser = new FgUser({ at: args.spToken });
if (!args.fgShareUrl) {
return errorResponse(BAD_REQUEST_SC, 'Mising required fgShareUrl parameter');
}

if (!await fgUser.isUser()) {
return errorResponse(AUTH_FAILED_SC, 'Authentication failed. Please refresh page and try again.');
}

// Starts
const { siteFgRootPath } = appConfig.getConfig();
const batchManager = new BatchManager({ key: PROMOTE_ACTION, instanceKey: getInstanceKey({ fgRootFolder: siteFgRootPath }) });
await batchManager.init({ batchNumber });
const currentBatch = batchNumber ? await batchManager.getCurrentBatch() : null;

// Read instance_info.json
if (args.promoteStatus !== undefined) {
const instanceContent = await batchManager.getInstanceFileContent();
if (!instanceContent || !instanceContent.dtls) {
throw new Error('Missing instance content!');
}
payload.promoteStatus = { batchesInfo: instanceContent?.dtls?.batchesInfo };
}

if (args.batchFiles !== undefined) {
const batchFilesContent = await currentBatch.getFiles();
payload.batchFiles = batchFilesContent?.map((e) => e.file?.filePath);
}

if (args.batchResults !== undefined) {
const brC = await currentBatch.getResultsContent();
if (brC) payload.batchResults = brC;
}

if (args.promoteResults !== undefined) {
const prC = await batchManager.getResultsContent();
if (prC) payload.promoteResults = prC;
}
} catch (err) {
logger.error(err);
return errorResponse(GEN_ERROR_SC, `Something went wrong: ${err}`);
}

return {
payload,
};
}

exports.main = main;
24 changes: 24 additions & 0 deletions actions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,31 @@ function getInstanceKey(params) {
return params?.fgRootFolder?.replace(/[^a-zA-Z0-9_]/g, '_') || 'default';
}

/**
*
* Returns an error response object and attempts to log.info the status code and error message
*
* @param {number} statusCode the error status code.
* e.g. 400
* @param {string} message the error message.
* e.g. 'missing xyz parameter'
*
* @returns {object} the error object, ready to be returned from the action main's function.
*
*/
function errorResponse(statusCode, message) {
return {
error: {
statusCode,
body: {
error: message,
},
},
};
}

module.exports = {
errorResponse,
getAioLogger,
handleExtension,
getDocPathFromUrl,
Expand Down
6 changes: 6 additions & 0 deletions app.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ application:
LOG_LEVEL: debug
limits:
timeout: 600000
promote-status:
function: actions/promoteStatus/promoteStatus.js
web: 'yes'
runtime: nodejs:16
inputs:
LOG_LEVEL: debug
status:
function: actions/status/status.js
web: 'yes'
Expand Down

0 comments on commit 51492f0

Please sign in to comment.