Skip to content

Commit

Permalink
BREAKING CHANGE: Use project API endpoint to get details
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Aug 14, 2024
1 parent 43ac444 commit fa3a78e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 0 additions & 2 deletions preview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`)
8 changes: 0 additions & 8 deletions preview/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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"
Expand Down
25 changes: 19 additions & 6 deletions preview/scripts/edit-description.js
Original file line number Diff line number Diff line change
@@ -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<!-- readthedocs-preview ${RTD_PROJECT_SLUG} start -->\r\n`;
Expand Down

0 comments on commit fa3a78e

Please sign in to comment.