From 098c83187cf68680038ff1a47269270e9741e14e Mon Sep 17 00:00:00 2001 From: Rachael Sewell Date: Thu, 2 Mar 2023 13:33:19 -0800 Subject: [PATCH] fix rest redirects bug (autogenerated redirects) (#35257) --- .../data/client-side-rest-api-redirects.json | 9 +++--- src/rest/lib/rest-api-overrides.json | 18 ++++++++++- src/rest/scripts/update-files.js | 2 +- .../{getRedirects.js => get-redirects.js} | 32 +++++++++++++++++-- 4 files changed, 52 insertions(+), 9 deletions(-) rename src/rest/scripts/utils/{getRedirects.js => get-redirects.js} (78%) diff --git a/src/rest/data/client-side-rest-api-redirects.json b/src/rest/data/client-side-rest-api-redirects.json index b195b0fd64..cf2a31b697 100644 --- a/src/rest/data/client-side-rest-api-redirects.json +++ b/src/rest/data/client-side-rest-api-redirects.json @@ -155,9 +155,13 @@ "/rest/enterprise-admin#update-a-self-hosted-runner-group-for-an-enterprise": "/rest/actions/self-hosted-runner-groups#update-a-self-hosted-runner-group-for-an-enterprise", "/rest/orgs#list-custom-repository-roles-in-an-organization": "/rest/orgs/custom-roles#list-custom-repository-roles-in-an-organization", "/rest/apps/oauth-applications#create-a-scoped-access-token": "/rest/apps/apps#create-a-scoped-access-token", + "/rest/enterprise-admin#get-code-security-analysis-features-for-an-enterprise": "/rest/enterprise-admin/code-security-and-analysis#get-code-security-analysis-features-for-an-enterprise", + "/rest/enterprise-admin#update-code-security-and-analysis-features-for-an-enterprise": "/rest/enterprise-admin/code-security-and-analysis#update-code-security-and-analysis-features-for-an-enterprise", + "/rest/enterprise-admin#enable-or-disable-a-security-feature": "/rest/enterprise-admin/code-security-and-analysis#enable-or-disable-a-security-feature", "/rest/repos#deploy-keys": "/rest/deploy-keys", "/rest/deployments#deploy-keys": "/rest/deploy-keys", "/rest/repos#statuses": "/rest/commits/statuses", + "/rest/enterprise-admin#code-security-and-analysis": "/rest/enterprise-admin/code-security-and-analysis", "/rest/apps#get-the-authenticated-app": "/rest/apps/apps#get-the-authenticated-app", "/rest/apps#create-a-github-app-from-a-manifest": "/rest/apps/apps#create-a-github-app-from-a-manifest", "/rest/apps#get-a-webhook-configuration-for-an-app": "/rest/apps/webhooks#get-a-webhook-configuration-for-an-app", @@ -783,7 +787,6 @@ "/rest/repos#get-a-repository-readme": "/rest/repos/contents#get-a-repository-readme", "/rest/repos#get-a-repository-directory-readme": "/rest/repos/contents#get-a-repository-directory-readme", "/rest/repos#releases": "/rest/repos/releases", - "/rest/releases/releases#create-a-release": "/rest/repos/releases#create-a-release", "/rest/activity#list-stargazers": "/rest/activity/starring#list-stargazers", "/rest/activity#starring": "/rest/activity/starring", "/rest/metrics/statistics#get-the-weekly-commit-activity": "/rest/metrics/statistics#get-the-weekly-commit-activity", @@ -1072,9 +1075,6 @@ "/rest/announcement-banners#enterprises": "/rest/announcement-banners/enterprises", "/rest/announcement-banners/enterprises#set-announcement-banner-for-enterprise": "/rest/announcement-banners/enterprises#set-announcement-banner-for-enterprise", "/rest/announcement-banners/enterprises#remove-announcement-banner-from-enterprise": "/rest/announcement-banners/enterprises#remove-announcement-banner-from-enterprise", - "/rest/enterprise-admin#get-code-security-analysis-features-for-an-enterprise": "/rest/enterprise-admin/code-security-and-analysis#get-code-security-analysis-features-for-an-enterprise", - "/rest/enterprise-admin#code-security-and-analysis": "/rest/enterprise-admin/code-security-and-analysis", - "/rest/enterprise-admin#update-code-security-and-analysis-features-for-an-enterprise": "/rest/enterprise-admin/code-security-and-analysis#update-code-security-and-analysis-features-for-an-enterprise", "/rest/enterprise-admin#list-enterprise-consumed-licenses": "/rest/enterprise-admin/license#list-enterprise-consumed-licenses", "/rest/enterprise-admin#get-a-license-sync-status": "/rest/enterprise-admin/license#get-a-license-sync-status", "/rest/billing#get-github-actions-billing-for-an-enterprise": "/rest/enterprise-admin/billing#get-github-actions-billing-for-an-enterprise", @@ -1082,7 +1082,6 @@ "/rest/billing#export-advanced-security-active-committers-data-for-enterprise": "/rest/enterprise-admin/billing#export-advanced-security-active-committers-data-for-enterprise", "/rest/billing#get-github-packages-billing-for-an-enterprise": "/rest/enterprise-admin/billing#get-github-packages-billing-for-an-enterprise", "/rest/billing#get-shared-storage-billing-for-an-enterprise": "/rest/enterprise-admin/billing#get-shared-storage-billing-for-an-enterprise", - "/rest/enterprise-admin#enable-or-disable-a-security-feature": "/rest/enterprise-admin/code-security-and-analysis#enable-or-disable-a-security-feature", "/rest/orgs#custom_roles": "/rest/orgs/custom_roles", "/rest/actions#list-repository-access-to-a-self-hosted-runner-group-in-an-organization": "/rest/actions/self-hosted-runner-groups#list-repository-access-to-a-self-hosted-runner-group-in-an-organization", "/rest/actions#set-repository-access-to-a-self-hosted-runner-group-in-an-organization": "/rest/actions/self-hosted-runner-groups#set-repository-access-to-a-self-hosted-runner-group-in-an-organization", diff --git a/src/rest/lib/rest-api-overrides.json b/src/rest/lib/rest-api-overrides.json index ebded4e1eb..21f2f844f5 100644 --- a/src/rest/lib/rest-api-overrides.json +++ b/src/rest/lib/rest-api-overrides.json @@ -784,11 +784,27 @@ "category": "apps", "subcategory": "apps", "originalUrl": "/rest/apps/oauth-applications#create-a-scoped-access-token" + }, + "secret-scanning/get-security-analysis-settings-for-enterprise": { + "category": "enterprise-admin", + "subcategory": "code-security-and-analysis", + "originalUrl": "/rest/enterprise-admin#get-code-security-analysis-features-for-an-enterprise" + }, + "secret-scanning/patch-security-analysis-settings-for-enterprise": { + "category": "enterprise-admin", + "subcategory": "code-security-and-analysis", + "originalUrl": "/rest/enterprise-admin#update-code-security-and-analysis-features-for-an-enterprise" + }, + "secret-scanning/post-security-product-enablement-for-enterprise": { + "category": "enterprise-admin", + "subcategory": "code-security-and-analysis", + "originalUrl": "/rest/enterprise-admin#enable-or-disable-a-security-feature" } }, "sectionUrls": { "/rest/repos#deploy-keys": "/rest/deploy-keys", "/rest/deployments#deploy-keys": "/rest/deploy-keys", - "/rest/repos#statuses": "/rest/commits/statuses" + "/rest/repos#statuses": "/rest/commits/statuses", + "/rest/enterprise-admin#code-security-and-analysis": "/rest/enterprise-admin/code-security-and-analysis" } } diff --git a/src/rest/scripts/update-files.js b/src/rest/scripts/update-files.js index f7863f1ff0..9bdf0980d9 100755 --- a/src/rest/scripts/update-files.js +++ b/src/rest/scripts/update-files.js @@ -21,7 +21,7 @@ import { validateVersionsOptions } from './utils/get-openapi-schemas.js' import { allVersions } from '../../../lib/all-versions.js' import { syncWebhookData } from '../../webhooks/scripts/sync.js' import { syncGitHubAppsData } from '../../github-apps/scripts/sync.js' -import { syncRestRedirects } from './utils/getRedirects.js' +import { syncRestRedirects } from './utils/get-redirects.js' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const TEMP_OPENAPI_DIR = path.join(__dirname, '../../../openApiTemp') diff --git a/src/rest/scripts/utils/getRedirects.js b/src/rest/scripts/utils/get-redirects.js similarity index 78% rename from src/rest/scripts/utils/getRedirects.js rename to src/rest/scripts/utils/get-redirects.js index 5ffff4d5d4..718af21aed 100644 --- a/src/rest/scripts/utils/getRedirects.js +++ b/src/rest/scripts/utils/get-redirects.js @@ -60,8 +60,36 @@ async function decorateRedirects(operation, clientSideRedirects) { if (!externalDocs) { return } + const oldUrl = `/rest${externalDocs.url.replace('/rest/reference', '/rest').split('/rest')[1]}` + // Because we generate a list of redirect programmatically which sprung + // from a large move of operations from /rest/reference to /rest on + // docs.github.com, we only want to programmatically generate redirects + // for those cases. I've noticed that sometimes someone will come along and + // update the url in the OpenAPI but they don't update the category + // and subcategory to match. So the url is accurate and category/subcategory + // are out-of-date. + // + // This logic roughly checks to see if the url looks up-to-date by + // seeing if the category and subcategory are in it. If they are not + // it indicates that the url is newer than the category/subcategory and + // we can skip generating a redirect. + if ( + category && + subcategory && + !oldUrl.includes(`${category}/${subcategory}`) && + !externalDocs.url.includes('/rest/reference') + ) { + return + } else if ( + category && + !oldUrl.includes(`${category}`) && + !externalDocs.url.includes('/rest/reference') + ) { + return + } + if (!(oldUrl in clientSideRedirects)) { // There are some operations that aren't nested in the sidebar // For these, don't need to add a client-side redirect, the @@ -69,9 +97,9 @@ async function decorateRedirects(operation, clientSideRedirects) { if (categoriesWithoutSubcategories.includes(category)) { return } - const anchor = oldUrl.split('#')[1] - const fragment = anchor ? `#${oldUrl.split('#')[1]}` : '' + const anchor = oldUrl.split('#')[1] + const fragment = anchor ? `#${anchor}` : '' // If there is no subcategory, a new page with the same name as the // category was created. That page name may change going forward. const redirectTo = subcategory