Skip to content

Commit 2c8e6bc

Browse files
committed
Preview and publish
1 parent 3c4c73b commit 2c8e6bc

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

.github/workflows/import/daFetch.js

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const daFetch = async (url, opts = {}) => {
3030
console.log("Fetched IMS token")
3131
opts.headers.Authorization = `Bearer ${token}`;
3232
const resp = await fetch(url, opts);
33-
if(resp.ok) console.log("DA import successful")
3433
if(!resp.ok) throw new Error("DA import failed")
3534
return resp;
3635
};

.github/workflows/import/index.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function calculateTime(startTime) {
1515
}
1616

1717
async function saveAllToDa(url, blob) {
18+
console.log("Starting DA import")
1819
const { destPath, editPath, route } = url;
1920

2021
url.daHref = `https://da.live${route}#/${toOrg}/${toRepo}${editPath}`;
@@ -32,6 +33,14 @@ async function saveAllToDa(url, blob) {
3233
}
3334
}
3435

36+
async function previewOrPublish({path, action}) {
37+
const previewUrl = `https://admin.hlx.page/${action}/${toOrg}/${toRepo}/main/${path}`;
38+
const opts = { method: 'POST' };
39+
const resp = await fetch(previewUrl, opts);
40+
if (!resp.ok) throw new Error(`Failed to post to preview: ${resp.statusText}`)
41+
console.log(`Posted to ${action} successfully ${action}/${toOrg}/${toRepo}/main/${path}`);
42+
}
43+
3544
async function importUrl(url) {
3645
const [fromRepo, fromOrg] = url.hostname.split('.')[0].split('--').slice(1).slice(-2);
3746
if (!(fromRepo || fromOrg)) {
@@ -65,7 +74,8 @@ async function importUrl(url) {
6574

6675
try {
6776
const resp = await fetch(`${url.origin}${srcPath}`);
68-
console.log("fetched resource from AEM at: ", `${url.origin}${srcPath}`)
77+
78+
console.log("fetched resource from AEM at:", `${url.origin}${srcPath}`)
6979
if (resp.redirected && !(srcPath.endsWith('.mp4') || srcPath.endsWith('.png') || srcPath.endsWith('.jpg'))) {
7080
url.status = 'redir';
7181
throw new Error('redir');
@@ -81,13 +91,13 @@ async function importUrl(url) {
8191
content = new Blob([html], { type: 'text/html' });
8292
}
8393
url.status = await saveAllToDa(url, content);
84-
console.log("imported resource to DA " + toOrg + "/" + toRepo + " | destination: " + url.destPath)
85-
86-
console.log("TODO - preview and publish.")
94+
console.log("imported resource to DA " + url.daHref);
95+
await previewOrPublish({path: pathname, action: 'preview'});
96+
await previewOrPublish({path: pathname, action: 'live'});
97+
console.log(`Resource: https://main--${toRepo}--${toOrg}.aem.live${url.pathname}`);
8798
} catch (e) {
88-
console.log(e)
99+
console.log("Failed to import resource to DA " + toOrg + "/" + toRepo + " | destination: " + url.pathname, + " | error: " + e.message);
89100
if (!url.status) url.status = 'error';
90-
// Do nothing
91101
}
92102
}
93103

0 commit comments

Comments
 (0)