Skip to content

Commit 80d903a

Browse files
committed
add content change logic to skip build on changelog
1 parent a0a86fa commit 80d903a

File tree

3 files changed

+68
-39
lines changed

3 files changed

+68
-39
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {getChangelogsUncached} from './src/server/utils';
2+
3+
async function checkChanges() {
4+
const {hash, changelogs} = await getChangelogsUncached();
5+
console.log({changelogs});
6+
// extract the hash from the body tag of the live site
7+
// example: <body data-content-hash="055114ed2b57063a941b3433ea44074b53e795891fe603cc87cf24b96b32f3bd">
8+
const prodHash = await fetch('https://sentry.io/changelog/')
9+
.then(r => r.text())
10+
.then(html => {
11+
const match = html.match(/data-content-hash="([^"]+)"/);
12+
return match ? match[1] : null;
13+
});
14+
15+
if (prodHash === null) {
16+
console.error('could not find changelogs hash on on live changelog page');
17+
// build anyway since we are not sure
18+
process.exit(1);
19+
}
20+
21+
if (prodHash !== hash) {
22+
console.info('⚠️ changelogs have changed since last deployment');
23+
// should build
24+
process.exit(1);
25+
}
26+
console.info('changelogs are up to date');
27+
// skip build
28+
process.exit(0);
29+
}
30+
checkChanges();

apps/changelog/src/server/utils.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
1-
import {prismaClient} from '@/server/prisma-client';
1+
import {prismaClient} from './prisma-client';
22
import {Changelog} from '@prisma/client';
33
import {unstable_cache} from 'next/cache';
44

5-
export const getChangelogs = unstable_cache(
6-
async () => {
7-
const changelogs = await prismaClient.changelog.findMany({
8-
include: {
9-
categories: true,
10-
},
11-
where: {
12-
published: true,
13-
},
14-
orderBy: {
15-
publishedAt: 'desc',
16-
},
17-
});
18-
const hashHex = (buffer: ArrayBuffer) => {
19-
const hexCodes = Array.from(new Uint8Array(buffer)).map(value =>
20-
value.toString(16).padStart(2, '0')
21-
);
22-
return hexCodes.join('');
23-
};
24-
const hashChangelogs = (changelogs: Changelog[]) =>
25-
crypto.subtle
26-
.digest(
27-
'SHA-256',
28-
new TextEncoder().encode(
29-
changelogs
30-
.map(changelogs => changelogs.id + ':' + changelogs.updatedAt)
31-
.join(',')
32-
)
33-
)
34-
.then(hashHex);
35-
return {
36-
changelogs,
37-
hash: await hashChangelogs(changelogs),
38-
};
39-
},
40-
['changelogs'],
41-
{tags: ['changelogs']}
42-
);
5+
const hashHex = (buffer: ArrayBuffer) => {
6+
const hexCodes = Array.from(new Uint8Array(buffer)).map(value =>
7+
value.toString(16).padStart(2, '0')
8+
);
9+
return hexCodes.join('');
10+
};
11+
12+
const hashChangelogs = async (changelogs: Changelog[]) => {
13+
return crypto.subtle
14+
.digest('SHA-256', new TextEncoder().encode(JSON.stringify(changelogs)))
15+
.then(hashHex);
16+
};
17+
export const getChangelogsUncached = async () => {
18+
const changelogs = await prismaClient.changelog.findMany({
19+
include: {
20+
categories: true,
21+
},
22+
where: {
23+
published: true,
24+
},
25+
orderBy: {
26+
publishedAt: 'desc',
27+
},
28+
});
29+
return {
30+
changelogs,
31+
hash: await hashChangelogs(changelogs),
32+
};
33+
};
34+
35+
export const getChangelogs = unstable_cache(getChangelogsUncached, ['changelogs'], {
36+
tags: ['changelogs'],
37+
});

skip-build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ non_content_diff_status=$(git diff HEAD^ HEAD --name-only | grep -vE '^(docs/|pl
1818
changelog_diff_status=$(git diff HEAD^ HEAD --name-only | grep -vE '^(apps/changelog/|yarn.lock)' | wc -l)
1919

2020
if [[ "$NEXT_PUBLIC_CHANGELOG" === "1" ]] ; then
21+
if yarn tsx ./apps/changelog/has-content-changes.ts ; then
22+
exit 1
23+
fi
24+
2125
if [[ $changelog_diff_status -gt 0 ]] ; then
2226
exit 1
2327
else

0 commit comments

Comments
 (0)