From 66613688043a70a8b176e9da22c19897fb61458d Mon Sep 17 00:00:00 2001 From: Nikita Wootten Date: Sun, 10 Sep 2023 20:29:47 -0400 Subject: [PATCH] Reduce linkchecker spam (#1916) * Test linkchecker behavior * Add permissions to linkchecker * Removed test link failure * Tidy up extraneous comment --- .github/workflows/periodic.yml | 69 ++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/.github/workflows/periodic.yml b/.github/workflows/periodic.yml index 44ffab9c17..245d51b3dc 100644 --- a/.github/workflows/periodic.yml +++ b/.github/workflows/periodic.yml @@ -8,6 +8,9 @@ jobs: linkcheck: name: Validate Markdown Links runs-on: ubuntu-22.04 + permissions: + # Needed to post comments and issues + issues: write steps: - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 with: @@ -21,12 +24,64 @@ jobs: run: | make linkcheck working-directory: build - - name: Create an issue if bad links are detected + - name: Create an issue or comment if bad links are detected if: failure() - uses: peter-evans/create-issue-from-file@433e51abf769039ee20ba1293a088ca19d573b7f # v3.0.0 + uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 with: - title: Scheduled Check of Markdown Documents Found Bad Hyperlinks - content-filepath: build/generated/mlc_report.log - labels: | - bug - Scope: Documentation + script: | + // Read the markdown linkcheck report + const fs = require('fs'); + const body = fs.readFileSync('build/generated/mlc_report.log', 'utf8'); + + const title = "Scheduled Check of Markdown Documents Found Bad Hyperlinks"; + const labels = ["bug", "Scope: Documentation"]; + + // Find open linkchecker issues + const query = `query($owner:String!, $name:String!, $labels:[String!], $user:String!) { + repository(owner:$owner, name:$name) { + issues(first: 100, orderBy: { + direction: DESC, + field: CREATED_AT + }, filterBy: { + createdBy: $user, + labels: $labels, + states: OPEN + }) { + nodes { + title + number + } + } + } + }`; + const result = await github.graphql(query, { + owner: context.repo.owner, + name: context.repo.repo, + labels: labels, + user: "github-actions[bot]" + }); + + // find the first issue with a matching title, and create a comment + for (const issue of result.repository.issues.nodes) { + if (issue.title !== title) { + continue; + } + + github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + body + }); + + return; + } + + // or, if none were found, create an issue + github.rest.issues.create({ + owner: context.repo.owner, + repo: context.repo.repo, + title, + body, + labels + });