Skip to content

Commit 8c757b0

Browse files
authored
feat: preview mode show the pr number (#1729)
1 parent 3ac5eca commit 8c757b0

5 files changed

Lines changed: 29 additions & 5 deletions

File tree

app/components/AppHeader.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const mobileLinks = computed<NavigationConfigWithGroups>(() => [
127127
128128
const showFullSearch = shallowRef(false)
129129
const showMobileMenu = shallowRef(false)
130-
const { env } = useAppConfig().buildInfo
130+
const { env, prNumber } = useAppConfig().buildInfo
131131
132132
// On mobile, clicking logo+search button expands search
133133
const route = useRoute()
@@ -227,6 +227,15 @@ onKeyStroke(
227227
{{ env === 'release' ? 'alpha' : env }}
228228
</span>
229229
</NuxtLink>
230+
<NuxtLink
231+
v-if="prNumber"
232+
:to="`https://github.com/npmx-dev/npmx.dev/pull/${prNumber}`"
233+
:aria-label="`Open GitHub pull request ${prNumber}`"
234+
>
235+
<span class="text-xs px-1.5 py-0.5 rounded badge-green font-sans font-medium ms-2">
236+
PR #{{ prNumber }}
237+
</span>
238+
</NuxtLink>
230239
</div>
231240
<!-- Spacer when logo is hidden on desktop -->
232241
<span v-else class="hidden sm:block w-1" />

config/env.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ export { version } from '../package.json'
1616
*/
1717
export const isPR = process.env.PULL_REQUEST === 'true' || !!process.env.VERCEL_GIT_PULL_REQUEST_ID
1818

19+
/**
20+
* Environment variable `REVIEW_ID` provided by Netlify.
21+
* @see {@link https://docs.netlify.com/configure-builds/environment-variables/#git-metadata}
22+
*
23+
* Environment variable `VERCEL_GIT_PULL_REQUEST_ID` provided by Vercel.
24+
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_GIT_PULL_REQUEST_ID}
25+
*
26+
* Pull request number (if in a PR environment)
27+
*/
28+
export const prNumber = process.env.REVIEW_ID || process.env.VERCEL_GIT_PULL_REQUEST_ID || null
29+
1930
/**
2031
* Environment variable `BRANCH` provided by Netlify.
2132
* @see {@link https://docs.netlify.com/build/configure-builds/environment-variables/#git-metadata}
@@ -147,5 +158,6 @@ export async function getEnv(isDevelopment: boolean) {
147158
env,
148159
previewUrl,
149160
productionUrl,
161+
prNumber,
150162
} as const
151163
}

modules/build-env.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ export default defineNuxtModule({
2323
branch: 'xxx',
2424
time: time.getTime(),
2525
privacyPolicyDate: time.toISOString(),
26+
prNumber: null,
2627
} satisfies BuildInfo
2728
} else {
28-
const [{ env: useEnv, commit, shortCommit, branch }, privacyPolicyDate] = await Promise.all([
29-
getEnv(nuxt.options.dev),
30-
getFileLastUpdated('app/pages/privacy.vue'),
31-
])
29+
const [{ env: useEnv, commit, shortCommit, branch, prNumber }, privacyPolicyDate] =
30+
await Promise.all([getEnv(nuxt.options.dev), getFileLastUpdated('app/pages/privacy.vue')])
3231
env = useEnv
3332
nuxt.options.appConfig.env = useEnv
3433
nuxt.options.appConfig.buildInfo = {
@@ -39,6 +38,7 @@ export default defineNuxtModule({
3938
branch,
4039
env,
4140
privacyPolicyDate,
41+
prNumber,
4242
} satisfies BuildInfo
4343
}
4444

shared/types/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export interface BuildInfo {
88
branch: string
99
env: EnvType
1010
privacyPolicyDate: string
11+
prNumber: string | null
1112
}

test/nuxt/components/BuildEnvironment.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('BuildEnvironment', () => {
1313
shortCommit: 'abc',
1414
branch: 'main',
1515
privacyPolicyDate: new Date().toISOString(),
16+
prNumber: null,
1617
}
1718
const component = await mountSuspended(BuildEnvironment, {
1819
props: {
@@ -39,6 +40,7 @@ describe('BuildEnvironment', () => {
3940
shortCommit: 'abc',
4041
branch: 'release',
4142
privacyPolicyDate: new Date().toISOString(),
43+
prNumber: null,
4244
}
4345

4446
const component = await mountSuspended(BuildEnvironment, {

0 commit comments

Comments
 (0)