From fa3a78ec76d0536c6bad391be8a1859b2b81120f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Tue, 13 Aug 2024 19:54:54 -0600 Subject: [PATCH] BREAKING CHANGE: Use project API endpoint to get details Closes https://github.com/readthedocs/actions/issues/42 --- preview/README.md | 2 -- preview/action.yaml | 8 -------- preview/scripts/edit-description.js | 25 +++++++++++++++++++------ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/preview/README.md b/preview/README.md index fe7eb6d..b717408 100644 --- a/preview/README.md +++ b/preview/README.md @@ -52,8 +52,6 @@ the description will be edited to include the link to Read the Docs' documentati These are all the parameters this action supports: * `project-slug` (**_required_**): Project's slug on Read the Docs. You can find it on your Read the Docs project's details page in the right menu under "Project Slug". -* `project-language` (_optional_): Project's language code on Read the Docs. Example: `en` for English, `es` for Spanish, etc. (default: `en`) * `message-template` (_optional_): Text message to be injected by the action in the Pull Request description. It supports the following placeholders to be replaced: * `{docs-pr-index-url}`: URL to the root of the documentation for the Pull Request preview. * `platform` (_optional_): Read the Docs Community (`community`) or Read the Docs for Business (`business`). (default: `community`) -* `single-version` (_optional_): Set this to `'true'` if your project is single version, so we can link to the correct URL. (default: `'false'`) diff --git a/preview/action.yaml b/preview/action.yaml index 2615b4e..35f92c6 100644 --- a/preview/action.yaml +++ b/preview/action.yaml @@ -10,10 +10,6 @@ inputs: project-slug: description: "Project's slug on Read the Docs" required: true - project-language: - description: "Project's language on Read the Docs" - default: "en" - required: false message-template: description: "Template message to use for the PR body" default: | @@ -24,10 +20,6 @@ inputs: description: "Read the Docs Community or Read the Docs for Business (community or business)" default: "community" required: false - single-version: - description: "Site is a single version project, so link to the top level." - default: 'false' - required: false runs: using: "composite" diff --git a/preview/scripts/edit-description.js b/preview/scripts/edit-description.js index b4b739e..2dc4eca 100644 --- a/preview/scripts/edit-description.js +++ b/preview/scripts/edit-description.js @@ -1,26 +1,39 @@ -module.exports = async ({inputs, github, context}) => { +module.exports = async ({ inputs, github, context }) => { const PR_NUMBER = context.issue.number; const RTD_PROJECT_SLUG = inputs["project-slug"]; - const RTD_PROJECT_LANGUAGE = inputs["project-language"]; const RTD_PLATFORM = inputs["platform"]; - const RTD_SINGLE_VERSION = inputs["single-version"]; let RTD_DOMAIN = ""; let RTD_URL = ""; + let RTD_PROJECT_ENDPOINT = ""; if (RTD_PLATFORM === "community") { RTD_DOMAIN = "org.readthedocs.build"; + RTD_PROJECT_ENDPOINT = `https://readthedocs.org/api/v3/projects/${RTD_PROJECT_SLUG}/`; } else if (RTD_PLATFORM === "business") { RTD_DOMAIN = "com.readthedocs.build"; + RTD_PROJECT_ENDPOINT = `https://readthedocs.com/api/v3/projects/${RTD_PROJECT_SLUG}/`; } else { // Log warning here? } const RTD_PROJECT_DOMAIN = `https://${RTD_PROJECT_SLUG}--${PR_NUMBER}.${RTD_DOMAIN}/`; - if (RTD_SINGLE_VERSION === "true") { + // Get project details from the RTD API + const project = await fetch(RTD_PROJECT_ENDPOINT, {}).catch((error) => { + throw new Error(`Failed to fetch project details from Read the Docs API: ${error}`); + }).then((response) => { + if (!response.ok) { + throw new Error(`Failed to fetch project details from Read the Docs API: ${project.statusText}`); + } + return response.json(); + }); + + if (project.versioning_scheme === "single_version_without_translations") { RTD_URL = RTD_PROJECT_DOMAIN; - } else { - RTD_URL = RTD_PROJECT_DOMAIN + `${RTD_PROJECT_LANGUAGE}/${PR_NUMBER}/`; + } else if (project.versioning_scheme === "multiple_versions_with_translations") { + RTD_URL = RTD_PROJECT_DOMAIN + `${project.language.code}/${PR_NUMBER}/`; + } else if (project.versioning_scheme === "multiple_versions_without_translations") { + RTD_URL = RTD_PROJECT_DOMAIN + `${PR_NUMBER}/`; } const MESSAGE_SEPARATOR_START = `\r\n\r\n\r\n`;