Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: Fix an issue with double-encoded params in /changelog (#41648)

Ivan Vasilov0ac60955c1b5
diff --git a/apps/www/pages/changelog.tsx b/apps/www/pages/changelog.tsx
index a5c4c8dbb5c..5a6d4721d16 100644
--- a/apps/www/pages/changelog.tsx
+++ b/apps/www/pages/changelog.tsx
@@ -42,6 +42,87 @@ export type DiscussionsResponse = {
}
}
+// uses the graphql api
+async function fetchDiscussions(
+ owner: string,
+ repo: string,
+ categoryId: string,
+ cursor: string | null = null
+) {
+ const ExtendedOctokit = Octokit.plugin(paginateGraphql)
+ type ExtendedOctokit = InstanceType<typeof ExtendedOctokit>
+
+ const octokit = new ExtendedOctokit({
+ authStrategy: createAppAuth,
+ auth: {
+ appId: process.env.GITHUB_CHANGELOG_APP_ID,
+ installationId: process.env.GITHUB_CHANGELOG_APP_INSTALLATION_ID,
+ privateKey: process.env.GITHUB_CHANGELOG_APP_PRIVATE_KEY,
+ },
+ })
+
+ const query = `
+ query troubleshootDiscussions($cursor: String, $owner: String!, $repo: String!, $categoryId: ID!) {
+ repository(owner: $owner, name: $repo) {
+ discussions(first: 50, after: $cursor, categoryId: $categoryId, orderBy: { field: CREATED_AT, direction: DESC }) {
+ totalCount
+ pageInfo {
+ hasPreviousPage
+ hasNextPage
+ startCursor
+ endCursor
+ }
+ nodes {
+ id
+ publishedAt
+ createdAt
+ url
+ title
+ body
+ }
+ }
+ }
+ }
+ `
+ const queryVars = {
+ owner,
+ repo,
+ categoryId,
+ cursor,
+ }
+
+ // fetch discussions
+ const {
+ repository: {
+ discussions: { nodes: discussions, pageInfo },
+ },
+ } = await octokit.graphql<DiscussionsResponse>(query, queryVars)
+
+ return { discussions, pageInfo }
+}
+
+function isEncoded(uri: string | null | undefined) {
+ uri = uri ?? ''
+ return uri !== decodeURIComponent(uri)
+}
+
+// Decodes a URI if it is encoded
+const recursiveDecodeURI = (uri: string | null) => {
+ if (!uri) {
+ return uri
+ }
+ let tries = 0
+ while (isEncoded(uri)) {
+ uri = decodeURIComponent(uri)
+ tries++
+ if (tries > 10) {
+ break
+ }
+ }
+
+ return uri
+}
+
/**
* [Terry]
* this page powers supabase.com/changelog
@@ -55,7 +136,9 @@ export type DiscussionsResponse = {
export const getServerSideProps: GetServerSideProps = async ({ res, query }) => {
// refresh every 15 minutes
res.setHeader('Cache-Control', 'public, max-age=900, stale-while-revalidate=900')
- const next = query.next ?? (null as string | null)
+ const encodedNext = (query.next ?? null) as string | null
+ // in some cases the next cursor is encoded twice or more times due to the user pasting the url, so we need to decode it multiple times.
+ const next = recursiveDecodeURI(encodedNext)
const restPage = query.restPage ? Number(query.restPage) : 1
const octokitRest = new OctokitRest({
@@ -93,65 +176,11 @@ export const getServerSideProps: GetServerSideProps = async ({ res, query }) =>
(release) => release.id && oldReleases.includes(release.id)
)
- // uses the graphql api
- async function fetchDiscussions(owner: string, repo: string, categoryId: string, cursor: string) {
- const ExtendedOctokit = Octokit.plugin(paginateGraphql)
- type ExtendedOctokit = InstanceType<typeof ExtendedOctokit>
-
- const octokit = new ExtendedOctokit({
- authStrategy: createAppAuth,
- auth: {
- appId: process.env.GITHUB_CHANGELOG_APP_ID,
- installationId: process.env.GITHUB_CHANGELOG_APP_INSTALLATION_ID,
- privateKey: process.env.GITHUB_CHANGELOG_APP_PRIVATE_KEY,
- },
- })
-
- const query = `
- query troubleshootDiscussions($cursor: String, $owner: String!, $repo: String!, $categoryId: ID!) {
- repository(owner: $owner, name: $repo) {
- discussions(first: 50, after: $cursor, categoryId: $categoryId, orderBy: { field: CREATED_AT, direction: DESC }) {
- totalCount
- pageInfo {
- hasPreviousPage
- hasNextPage
- startCursor
- endCursor
- }
- nodes {
- id
- publishedAt
- createdAt
- url
- title
- body
- }
- }
- }
- }
- `
- const queryVars = {
- owner,
- repo,
- categoryId,
- cursor: next,
- }
-
- // fetch discussions
- const {
- repository: {
- discussions: { nodes: discussions, pageInfo },
- },
- } = await octokit.graphql<DiscussionsResponse>(query, queryVars)
-
- return { discussions, pageInfo }
- }
-
const { discussions, pageInfo } = await fetchDiscussions(
'supabase',
'supabase',
'DIC_kwDODMpXOc4CAFUr', // 'Changelog' category
- next as string
+ next
)
if (!discussions) {