Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions actions/preview-link-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
- uses: dotnet/docs-tools/actions/preview-link-generator@main
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
max_wait_time_minutes: 20
```
12 changes: 10 additions & 2 deletions actions/preview-link-generator/__tests__/pull-updater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { WorkflowInput, workflowInput } from "../src/types/WorkflowInput";
const {
appendTable,
buildMarkdownPreviewTableFromExtractedLinks,
calculateMaxPollAttempts,
extractPreviewLinksFromBuildReport,
PREVIEW_TABLE_END,
PREVIEW_TABLE_START,
Expand Down Expand Up @@ -80,15 +81,22 @@ ${PREVIEW_TABLE_END}`;
it("options are correctly constructed with expected values from import", () => {
setInput("COLLAPSIBLE_AFTER", "7");
setInput("MAX_ROW_COUNT", "42");
setInput("MAX_WAIT_TIME_MINUTES", "15");
setInput("REPO_TOKEN", "test-token");

const opts: WorkflowInput = workflowInput;

expect(opts).toBeDefined();
expect(opts.collapsibleAfter).toBe(7);
expect(opts.maxRowCount).toBe(42);
expect(opts.maxWaitTimeMinutes).toBe(15);
expect(opts.repoToken).toBe("test-token");
});

it("calculates OPS poll attempts from the maximum wait time", () => {
expect(calculateMaxPollAttempts(15, 30_000)).toBe(31);
});

it("extractPreviewLinksFromBuildReport parses file to preview URL map", () => {
const html = `
<html>
Expand Down Expand Up @@ -145,8 +153,8 @@ ${PREVIEW_TABLE_END}`;
"#### Internal previews\n\n" +
"| File | Preview link |\n" +
"|:--|:--|\n" +
"| [docs/a.md](https://github.com/dotnet/docs/blob/oid/docs/a.md) | [docs/a](https://review.learn.microsoft.com/en-us/dotnet/a?branch=pr-en-us-7) |\n" +
"| [docs/b.yml](https://github.com/dotnet/docs/blob/oid/docs/b.yml) | [docs/b](https://review.learn.microsoft.com/en-us/dotnet/b?branch=pr-en-us-7) |\n"
"| [docs/a.md](https://github.com/dotnet/docs/blob/oid/docs/a.md) | [Preview published page](https://review.learn.microsoft.com/en-us/dotnet/a?branch=pr-en-us-7) |\n" +
"| [docs/b.yml](https://github.com/dotnet/docs/blob/oid/docs/b.yml) | [Preview published page](https://review.learn.microsoft.com/en-us/dotnet/b?branch=pr-en-us-7) |\n"
);
});

Expand Down
3 changes: 3 additions & 0 deletions actions/preview-link-generator/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
max_row_count:
description: 'The maximum number of rows to display in the automated preview table.'
default: '30'
max_wait_time_minutes:
description: 'The maximum number of minutes to wait for the OpenPublishing.Build status check to complete.'
default: '20'
runs:
using: 'node16'
main: 'dist/index.js'
15 changes: 12 additions & 3 deletions actions/preview-link-generator/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion actions/preview-link-generator/dist/index.js.map

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions actions/preview-link-generator/src/pull-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { workflowInput } from "./types/WorkflowInput";
const PREVIEW_TABLE_START = "<!-- PREVIEW-TABLE-START -->";
const PREVIEW_TABLE_END = "<!-- PREVIEW-TABLE-END -->";
const OPS_CHECK_NAME = "OpenPublishing.Build";
const OPS_MAX_POLL_ATTEMPTS = 40;
const OPS_POLL_DELAY_MS = 30_000;

type StatusCheck = {
Expand Down Expand Up @@ -56,7 +55,10 @@ export async function tryUpdatePullRequestBody(token: string) {
token,
commitOid,
OPS_CHECK_NAME,
OPS_MAX_POLL_ATTEMPTS,
calculateMaxPollAttempts(
workflowInput.maxWaitTimeMinutes,
OPS_POLL_DELAY_MS
),
OPS_POLL_DELAY_MS
);

Expand Down Expand Up @@ -188,6 +190,15 @@ function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

function calculateMaxPollAttempts(
maxWaitTimeMinutes: number,
pollDelayMs: number
): number {
return pollDelayMs > 0
? Math.max(1, Math.floor((maxWaitTimeMinutes * 60_000) / pollDelayMs) + 1)
: 1;
}

async function downloadUrl(url: string): Promise<string> {
return await new Promise((resolve) => {
const request = https.get(url, (response) => {
Expand Down Expand Up @@ -315,7 +326,7 @@ function buildMarkdownPreviewTableFromExtractedLinks(
const displayedLinks = sortedLinks.slice(0, workflowInput.maxRowCount);

for (const [file, previewUrl] of displayedLinks) {
const previewTitle = file.replace(".md", "").replace(".yml", "");
const previewTitle = "Preview published page";
markdownTable += `| [${file}](${toGitHubLink(
file,
commitOid
Expand Down Expand Up @@ -392,6 +403,7 @@ function appendTable(body: string, table: string) {
export const exportedForTesting = {
appendTable,
buildMarkdownPreviewTableFromExtractedLinks,
calculateMaxPollAttempts,
extractPreviewLinksFromBuildReport,
PREVIEW_TABLE_END,
PREVIEW_TABLE_START,
Expand Down
5 changes: 5 additions & 0 deletions actions/preview-link-generator/src/types/WorkflowInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export class WorkflowInput {
return parseInt(val || "30");
}

get maxWaitTimeMinutes(): number {
const val = parseInt(getInput("max_wait_time_minutes") || "20");
return val > 0 ? val : 20;
}

constructor() {}
}

Expand Down
Loading