Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverting script to automate reverting the editor from a published developer portal revision #2495

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
78 changes: 78 additions & 0 deletions scripts.v3/revert-revision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* This script automates reverting the editor from a published developer portal revision.
* In order to run it, you need to:
*
* 1) Clone the api-management-developer-portal repository:
* git clone https://github.com/Azure/api-management-developer-portal.git
*
* 2) Install NPM packages:
* npm install
*
* 3) Run this script with a valid combination of arguments:
* node ./revert-revision ^
* --subscriptionId "< your subscription ID >" ^
* --resourceGroupName "< your resource group name >" ^
* --serviceName "< your service name >" ^
* --snapshotPath "< path to snapshot json file >"
*/
const path = require("path");
const { ImporterExporter } = require("./utils");

const yargs = require('yargs')
.example(`node ./revert-revision ^ \r
--subscriptionId "< your subscription ID >" ^ \r
--resourceGroupName "< your resource group name >" ^ \r
--serviceName "< your service name >" ^ \r
--snapshotPath "< your revision number, like C:\/Temp\/20240521140951.json >" \n`)
.option('subscriptionId', {
type: 'string',
description: 'Azure subscription ID.',
demandOption: true
})
.option('resourceGroupName', {
type: 'string',
description: 'Azure resource group name.',
demandOption: true
})
.option('serviceName', {
type: 'string',
description: 'API Management service name.',
demandOption: true
})
.option('snapshotPath', {
type: 'string',
description: 'Path to the published revision snapshot JSON file.',
demandOption: true
})

.help()
.argv;

async function revertRevision() {
console.log(`Reverting editor to revision ${yargs.snapshotPath}`);
const importerExporter = new ImporterExporter(
yargs.subscriptionId,
yargs.resourceGroupName,
yargs.serviceName,
null,
null,
null,
null,
);
await importerExporter.revertRevisionSnapshot(yargs.snapshotPath);
}

revertRevision()
.then(() => {
console.log("DONE");
process.exit(0);
})
.catch(error => {
console.error(error.message);
process.exit(1);
});


module.exports = {
revertRevision
}
6 changes: 6 additions & 0 deletions scripts.v3/revert-revision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This script automates reverting the editor from a published developer portal revision.
node ./revert-revision \
--subscriptionId "< your subscription ID >" \
--resourceGroupName "< your resource group name >" \
--serviceName "< your service name >"
--snapshotPath "< path to snapshot json file >"
183 changes: 183 additions & 0 deletions scripts.v3/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,189 @@ class ImporterExporter {
}
}


async revertRevisionSnapshot(revisionSnapshotFilePath) {

console.log(`Loading snapshot file ${revisionSnapshotFilePath}`);
const snapshot = JSON.parse(fs.readFileSync(revisionSnapshotFilePath, 'utf8'));
console.log(`Converting snapshot file to arm contract...`);

for (let [snaphostItemsName, paperbitsItems] of Object.entries(snapshot)) {

console.log(`Processing ${snaphostItemsName}:`);

if (snaphostItemsName == "styles" || snaphostItemsName == "navigationItems") {
let armPath = snaphostItemsName == "styles" ?
"/contentTypes/document/contentItems/stylesheet" :
"/contentTypes/document/contentItems/navigation";
let armContract = this.convertPaperbitsContractToArmContract(paperbitsItems)
let body = {
properties: {
nodes: snaphostItemsName == "styles"? [armContract] : armContract
}
};
console.log(`- Updating content item ${armPath}`);
await this.httpClient.sendRequest("PUT", armPath, body);
continue;
}

for (let paperbitsContract of Object.values(paperbitsItems)) {
let armContract = this.convertPaperbitsContractToArmContract(paperbitsContract);
let armPath = armContract["id"];
delete armContract["id"];

if (snaphostItemsName.includes("settings")) {
armPath = "contentTypes/document/contentItems/configuration";
armContract = { nodes: [armContract] };
}

if (armPath.includes("files")) {
delete armContract["type"];
}

let body = null
if (armContract.locales) {
body = { properties: { en_us: armContract.locales["en-us"] } }
} else {
body = { properties: armContract }
}
console.log(`- Updating content item ${armPath}`);
await this.httpClient.sendRequest("PUT", armPath, body);
}
}
}

convertPaperbitsContractToArmContract(contract) {
const reservedPaperbitsIds = ["containerKey", "webContainerKey"];
let converted;

if (contract === null || contract === undefined) { // here we expect "false" as a value too
return null;
}

if (Array.isArray(contract)) {
converted = contract.map(x => this.convertPaperbitsContractToArmContract(x));
}
else if (typeof contract === "object") {
converted = {};
const propertyNames = Object.keys(contract);
for(const propertyName of propertyNames) {
const propertyValue = contract[propertyName];
let convertedKey = propertyName;
let convertedValue = propertyValue;

if (!reservedPaperbitsIds.includes(convertedKey)) {
convertedKey = propertyName
.replace(/contentKey/gm, "documentId")
.replace(/Key\b/gm, "Id")
.replace(/\bkey\b/gm, "id");
}

if (typeof propertyValue === "string") {
if (propertyName !== convertedKey) {
convertedValue = this.paperbitsKeyToArmResource(propertyValue);
}
}
else {
convertedValue = this.convertPaperbitsContractToArmContract(propertyValue);
}

converted[convertedKey] = convertedValue;
};
}
else {
converted = contract;
}

return converted;
}

paperbitsKeyToArmResource(key) {
if (key.startsWith("/")) {
key = key.substring(1);
}

if (key.startsWith("contentTypes")) {
return key;
}

const segments = key.split("/");
const contentType = segments[0];
const contentItem = segments[1];

let mapiContentType;
let mapiContentItem;

switch (contentType) {
case "pages":
mapiContentType = "page";
mapiContentItem = contentItem;
break;

case "layouts":
mapiContentType = "layout";
mapiContentItem = contentItem;
break;

case "uploads":
mapiContentType = "blob";
mapiContentItem = contentItem;
break;

case "blocks":
mapiContentType = "block";
mapiContentItem = contentItem;
break;

case "urls":
mapiContentType = "url";
mapiContentItem = contentItem;
break;

case "navigationItems":
mapiContentType = "document";
mapiContentItem = "navigation";
break;

case "settings":
mapiContentType = "document";
mapiContentItem = "configuration";
break;

case "styles":
mapiContentType = "document";
mapiContentItem = "stylesheet";
break;

case "files":
mapiContentType = "document";
mapiContentItem = contentItem;
break;

case "locales":
mapiContentType = "locales";
mapiContentItem = "en-us";
break;

case "popups":
mapiContentType = "popup";
mapiContentItem = contentItem;
break;

default:
// throw new AppError(`Unknown content type: "${contentType}"`);
return key;
}

let resource = `contentTypes/${mapiContentType}/contentItems`;

if (mapiContentItem) {
resource += `/${mapiContentItem}`;
}

return resource;
}

/**
* Downloads media files from storage of specified API Management service.
*/
Expand Down
Loading