Skip to content

Add version tagging on publish #539

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

Open
wants to merge 1 commit into
base: main
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
57 changes: 55 additions & 2 deletions packages/root-cms/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export class RootCMSClient {
*/
async publishDocs(
docIds: string[],
options?: {publishedBy: string; batch?: WriteBatch}
options?: {publishedBy: string; batch?: WriteBatch; releaseId?: string}
) {
const projectCollectionsPath = `Projects/${this.projectId}/Collections`;
const publishedBy = options?.publishedBy || 'root-cms-client';
Expand Down Expand Up @@ -384,6 +384,10 @@ export class RootCMSClient {
// // https://firebase.google.com/docs/firestore/manage-data/transactions
let batchCount = 0;
const batch = options?.batch || this.db.batch();
const versionTags = ['published'];
if (options?.releaseId) {
versionTags.push(`release:${options.releaseId}`);
}
const publishedDocs: any[] = [];
for (const doc of docs) {
const {id, collection, slug, sys, fields} = doc;
Expand Down Expand Up @@ -418,6 +422,29 @@ export class RootCMSClient {
});
batchCount += 1;

// Save a version snapshot of the published doc.
const versionRef = this.db.doc(
`${projectCollectionsPath}/${collection}/Drafts/${slug}/Versions/${Date.now()}`
);
const versionData: any = {
id,
collection,
slug,
fields: fields || {},
sys: {
...sys,
firstPublishedAt: firstPublishedAt,
firstPublishedBy: firstPublishedBy,
publishedAt: FieldValue.serverTimestamp(),
publishedBy: publishedBy,
},
};
if (versionTags.length) {
versionData.tags = versionTags;
}
batch.set(versionRef, versionData);
batchCount += 1;

// Remove scheduled doc, if any.
batch.delete(scheduledRef);
batchCount += 1;
Expand Down Expand Up @@ -495,6 +522,7 @@ export class RootCMSClient {
// https://firebase.google.com/docs/firestore/manage-data/transactions
let batchCount = 0;
const batch = this.db.batch();
const versionTags = ['published'];
const publishedDocs: any[] = [];
for (const doc of docs) {
const {id, collection, slug, data} = doc;
Expand Down Expand Up @@ -530,6 +558,27 @@ export class RootCMSClient {
});
batchCount += 1;

// Save a version snapshot of the published doc.
const versionRef = this.db.doc(
`${projectCollectionsPath}/${collection}/Drafts/${slug}/Versions/${Date.now()}`
);
const versionData: any = {
id,
collection,
slug,
fields: data.fields || {},
sys: {
...sys,
firstPublishedAt: firstPublishedAt,
firstPublishedBy: firstPublishedBy,
publishedAt: FieldValue.serverTimestamp(),
publishedBy: scheduledBy || '',
},
tags: versionTags,
};
batch.set(versionRef, versionData);
batchCount += 1;

// Remove published doc.
batch.delete(scheduledRef);
batchCount += 1;
Expand Down Expand Up @@ -583,7 +632,11 @@ export class RootCMSClient {
scheduledAt: FieldValue.delete(),
scheduledBy: FieldValue.delete(),
});
await this.publishDocs(release.docIds || [], {publishedBy, batch});
await this.publishDocs(release.docIds || [], {
publishedBy,
batch,
releaseId: release.id,
});
}
}

Expand Down
40 changes: 35 additions & 5 deletions packages/root-cms/ui/utils/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
updateDoc,
documentId,
where,
Query,
WriteBatch,
DocumentReference,
} from 'firebase/firestore';
Expand Down Expand Up @@ -61,6 +62,7 @@ export interface CMSDoc {

export type Version = CMSDoc & {
_versionId: string;
tags?: string[];
};

export async function cmsDeleteDoc(docId: string) {
Expand Down Expand Up @@ -116,7 +118,7 @@ export async function cmsPublishDoc(docId: string) {
*/
export async function cmsPublishDocs(
docIds: string[],
options?: {batch?: WriteBatch}
options?: {batch?: WriteBatch; releaseId?: string}
) {
if (docIds.length === 0) {
console.log('no docs to publish');
Expand All @@ -133,12 +135,16 @@ export async function cmsPublishDocs(

const draftDocs = await getDraftDocs(docIds);
const batch = options?.batch || writeBatch(db);
const versionTags = ['published'];
if (options?.releaseId) {
versionTags.push(`release:${options.releaseId}`);
}
docIds.forEach((docId) => {
const draftData = draftDocs[docId];
if (!draftData) {
throw new Error(`doc does not exist: ${docId}`);
}
updatePublishedDocDataInBatch(batch, docId, draftData);
updatePublishedDocDataInBatch(batch, docId, draftData, versionTags);
});
await batch.commit();

Expand All @@ -165,7 +171,8 @@ export async function cmsPublishDocs(
function updatePublishedDocDataInBatch(
batch: WriteBatch,
docId: string,
draftData: CMSDoc
draftData: CMSDoc,
versionTags: string[]
) {
if (testPublishingLocked(draftData)) {
throw new Error(`publishing is locked for doc: ${draftData.id}`);
Expand Down Expand Up @@ -221,6 +228,23 @@ function updatePublishedDocDataInBatch(
batch.set(publishedDocRef, {...data, sys});
// Delete any "scheduled" docs if it exists.
batch.delete(scheduledDocRef);
// Save a version snapshot of the published doc.
const versionRef = doc(
db,
'Projects',
projectId,
'Collections',
collectionId,
'Drafts',
slug,
'Versions',
String(Date.now())
);
const versionData: any = {id: docId, collection: collectionId, slug, fields: data.fields || {}, sys};
if (versionTags?.length) {
versionData.tags = versionTags;
}
batch.set(versionRef, versionData);
}

/**
Expand Down Expand Up @@ -673,11 +697,17 @@ export async function getDraftDocs(
return drafts;
}

export async function cmsListVersions(docId: string) {
export async function cmsListVersions(
docId: string,
options?: {tags?: string[]}
) {
const db = window.firebase.db;
const docRef = getDraftDocRef(docId);
const versionsCollection = collection(db, docRef.path, 'Versions');
const q = query(versionsCollection, orderBy('sys.modifiedAt', 'desc'));
let q: Query = query(versionsCollection, orderBy('sys.modifiedAt', 'desc'));
if (options?.tags) {
q = query(q, where('tags', 'array-contains-any', options.tags));
}
const querySnapshot = await getDocs(q);
const versions: Version[] = [];
querySnapshot.forEach((doc) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/root-cms/ui/utils/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export async function publishRelease(id: string) {
scheduledAt: deleteField(),
scheduledBy: deleteField(),
});
await cmsPublishDocs(docIds, {batch});
await cmsPublishDocs(docIds, {batch, releaseId: id});
console.log(`published release: ${id}`);
logAction('release.publish', {metadata: {releaseId: id, docIds: docIds}});
}
Expand Down