Skip to content

Commit

Permalink
Multiple projects & Read the Docs for Business
Browse files Browse the repository at this point in the history
Allow repositories to use this action more than once in their repositories.
This is useful for monolitic repositories with multiple documentation projects.

Others changes included here:

- use a separate script instead of hard coding it into YAML
- use HTML comment as separator for the injected content
- support Read the Docs for Business
  • Loading branch information
humitos committed Jul 27, 2022
1 parent 1029696 commit c705572
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
45 changes: 10 additions & 35 deletions action.yaml
Expand Up @@ -14,49 +14,24 @@ inputs:
description: "Project's language on Read the Docs"
default: "en"
required: false
message-separator:
description: "Text to separate the original body of the PR from the auto-inserted text"
default: "----------"
required: false
message-template:
description: "Template message to use for the PR body"
default: ":books: Documentation preview :books:: {docs-pr-index-url}"
default: |
----
:books: Documentation preview :books:: {docs-pr-index-url}
required: false
platform:
description: "Read the Docs Community or Read the Docs for Business (community or business)"
default: "community"
required: false

runs:
using: "composite"
steps:
- uses: actions/checkout@v3
- name: "Comment on Pull Request with Read the Docs' preview links"
uses: actions/github-script@v6
with:
script: |
var PR_NUMBER = context.issue.number;
var RTD_PROJECT_SLUG = "${{ inputs.project-slug }}";
var RTD_PROJECT_LANGUAGE = "${{ inputs.project-language }}";
var RTD_URL = `https://${RTD_PROJECT_SLUG}--${PR_NUMBER}.org.readthedocs.build/${RTD_PROJECT_LANGUAGE}/${PR_NUMBER}/`;
var MESSAGE_SEPARATOR = `${{ inputs.message-separator }}`;
var MESSAGE_TEMPLATE = `${{ inputs.message-template }}`;
const { data: pull } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
var body_message = `${MESSAGE_TEMPLATE.replace("{docs-pr-index-url}", RTD_URL)}`;
var body = "";
if (pull.body) {
body = pull.body.split(/\r?\n/);
body = body.slice(0, body.indexOf(MESSAGE_SEPARATOR));
body = body.join("\n").trim()
}
body = body + "\n\n" + MESSAGE_SEPARATOR + "\n\n" + body_message;
github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
body: body,
});
const script = require("./scripts/edit-description.js");
script({inputs, github, context});
39 changes: 39 additions & 0 deletions scripts/edit-description.js
@@ -0,0 +1,39 @@
module.exports = ({inputs, github, context}) => {
var PR_NUMBER = context.issue.number;
var RTD_PROJECT_SLUG = "${{ inputs.project-slug }}";
var RTD_PROJECT_LANGUAGE = "${{ inputs.project-language }}";
var RTD_PLATFORM = "${{ inputs.platform }}";
if (RTD_PLATFORM == "community") {
var RTD_DOMAIN = "org.readthedocs.build";
}
if (RTD_PLATFORM == "business") {
var RTD_DOMAIN = "com.readthedocs.build";
}
var RTD_URL = `https://${RTD_PROJECT_SLUG}--${PR_NUMBER}.${RTD_DOMAIN}/${RTD_PROJECT_LANGUAGE}/${PR_NUMBER}/`;

var MESSAGE_SEPARATOR_START = `<!-- readthedocs-preview ${RTD_PROJECT_SLUG} start -->\n`;
var MESSAGE_SEPARATOR_END = `\n<!-- readthedocs-preview ${RTD_PROJECT_SLUG} end -->`;
var MESSAGE_TEMPLATE = "${{ inputs.message-template }}";

const { data: pull } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});

var body_message = MESSAGE_TEMPLATE.replace("{docs-pr-index-url}", RTD_URL);

var body = "";
if (pull.body) {
body = pull.body.slice(0, pull.body.indexOf(MESSAGE_SEPARATOR_START));
body = body + MESSAGE_SEPARATOR_START + body_message + MESSAGE_SEPARATOR_END;
body = body + pull.body.slice(pull.body.indexOf(MESSAGE_SEPARATOR_END) + MESSAGE_SEPARATOR_END.length);
}

github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
body: body,
});
}

0 comments on commit c705572

Please sign in to comment.