Switch to GitHub Action for linting markdown (#19899) (#20309)

This commit is contained in:
Travis Plunk
2023-09-20 09:28:16 -07:00
committed by GitHub
parent 1fcbc76de7
commit 22a813e8e5
7 changed files with 4 additions and 2732 deletions
+4 -1
View File
@@ -6,7 +6,10 @@ on:
paths:
- '**.md'
name: Check links for modified files
name: Check modified markdown files
permissions:
contents: read
jobs:
markdown-link-check:
runs-on: ubuntu-latest
-1
View File
@@ -1 +0,0 @@
node_modules/
-60
View File
@@ -1,60 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
function runTest() {
"use strict";
var gulp = require("gulp");
var concat = require("gulp-concat");
var through2 = require("through2");
var markdownlint = require("markdownlint");
gulp.task("test-mdsyntax", function task() {
var paths = [];
var rootpath;
// assign --repoRoot <rootpath> into rootpath
var j = process.argv.indexOf("--rootpath");
if (j > -1) {
rootpath = process.argv[j + 1];
}
if (rootpath === null) {
throw "--rootpath <repoRoot> must be specified before all other parameters";
}
// parse --filter into paths. --rootpath must be specified first.
j = process.argv.indexOf("--filter");
if (j > -1) {
var filters = process.argv[j + 1].split(",");
filters.forEach(function(filter) {
paths.push(rootpath + "/" + filter);
}, this);
}
if (paths.length === 0) {
throw "--filter <filter relative to repoRoot> must be specified";
}
var rootJsonFile = rootpath + "/.markdownlint.json";
var fs = require("fs");
fs.appendFileSync("markdownissues.txt", "--EMPTY--\r\n");
return gulp.src(paths, { "read": false })
.pipe(through2.obj(function obj(file, enc, next) {
markdownlint({
"files": [file.path],
"config": require(rootJsonFile)
},
function callback(err, result) {
var resultString = (result || "").toString();
if (resultString) {
file.contents = Buffer.from(resultString);
}
next(err, file);
});
}))
.pipe(concat("markdownissues.txt", { newLine: "\r\n" }))
.pipe(gulp.dest("."));
});
}
runTest();
@@ -1,145 +0,0 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Verify Markdown Links" {
BeforeAll {
if(!(Get-Command -Name 'markdown-link-check' -ErrorAction SilentlyContinue))
{
Write-Verbose "installing markdown-link-check ..." -Verbose
start-nativeExecution {
sudo yarn global add markdown-link-check@3.8.5
}
}
if(!(Get-Module -Name 'ThreadJob' -ListAvailable -ErrorAction SilentlyContinue))
{
Install-Module -Name ThreadJob -Scope CurrentUser
}
# Cleanup jobs for reliability
Get-Job | Remove-Job -Force
}
AfterAll {
# Cleanup jobs to leave the process the same
Get-Job | Remove-Job -Force
}
$groups = Get-ChildItem -Path "$PSScriptRoot\..\..\..\*.md" -Recurse | Where-Object {$_.DirectoryName -notlike '*node_modules*'} | Group-Object -Property directory
$jobs = @{}
# start all link verification in parallel
Foreach($group in $groups)
{
Write-Verbose -Verbose "starting jobs for $($group.Name) ..."
$job = Start-ThreadJob {
param([object] $group)
foreach($file in $group.Group)
{
$results = markdown-link-check -r $file 2>&1
Write-Output ([PSCustomObject]@{
file = $file
results = $results
})
}
} -ArgumentList @($group)
$jobs.add($group.name,$job)
}
Write-Verbose -Verbose "Getting results ..."
# Get the results and verify
foreach($key in $jobs.keys)
{
$job = $jobs.$key
$results = Receive-Job -Job $job -Wait
Remove-Job -Job $Job
foreach($jobResult in $results)
{
$file = $jobResult.file
$result = $jobResult.results
Context "Verify links in $file" {
# failures look like `[✖] https://someurl` (perhaps without the https://)
# passes look like `[✓] https://someurl` (perhaps without the https://)
$failures = $result -like '*[✖]*' | ForEach-Object { $_.Substring(4).Trim() }
$passes = $result -like '*[✓]*' | ForEach-Object {
@{url=$_.Substring(4).Trim() }
}
$trueFailures = @()
$verifyFailures = @()
foreach ($failure in $failures) {
if($failure -like 'https://www.amazon.com*')
{
# In testing amazon links often failed when they are valid
# Verify manually
$verifyFailures += @{url = $failure}
}
else
{
$trueFailures += @{url = $failure}
}
}
# must have some code in the test for it to pass
function noop {
}
if($passes)
{
It "<url> should work" -TestCases $passes {
noop
}
}
if($trueFailures)
{
It "<url> should work" -TestCases $trueFailures {
param($url)
# there could be multiple reasons why a failure is ok
# check against the allowed failures
$allowedFailures = [System.Net.HttpStatusCode[]](
503, # Service Unavailable
504, # Gateway Timeout
403 # Forbidden, some sites block with from AzDO with this code
)
$prefix = $url.Substring(0,7)
# Logging for diagnosability. Azure DevOps sometimes redacts the full url.
Write-Verbose "prefix: '$prefix'"
if($url -match '^http(s)?:')
{
# If invoke-WebRequest can handle the URL, re-verify, with 6 retries
try
{
$null = Invoke-WebRequest -Uri $url -RetryIntervalSec 10 -MaximumRetryCount 6
}
catch [Microsoft.PowerShell.Commands.HttpResponseException]
{
if ( $allowedFailures -notcontains $_.Exception.Response.StatusCode ) {
throw "Failed to complete request to `"$url`". $($_.Exception.Response.StatusCode) $($_.Exception.Message)"
}
}
}
else {
throw "Tool reported URL as unreachable."
}
}
}
if($verifyFailures)
{
It "<url> should work" -TestCases $verifyFailures -Pending {
}
}
if(!$passes -and !$trueFailures -and !$verifyFailures)
{
It "has no links" {
noop
}
}
}
}
}
}
-100
View File
@@ -1,100 +0,0 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Import-Module HelpersCommon
$moduleRootFilePath = Split-Path -Path $PSScriptRoot -Parent
# Identify the repository root path of the resource module
$repoRootPath = (Resolve-Path -LiteralPath (Join-Path $moduleRootFilePath "../..")).ProviderPath
$repoRootPathFound = $false
Describe 'Common Tests - Validate Markdown Files' -Tag 'CI' {
BeforeAll {
Push-Location $psscriptroot
$skip = $false
$NpmInstalled = "not installed"
if (Get-Command -Name 'yarn' -ErrorAction SilentlyContinue)
{
$NpmInstalled = "Installed"
Write-Verbose -Message "Checking if Gulp is installed. This may take a few moments." -Verbose
start-nativeExecution { yarn }
if(!(Get-Command -Name 'gulp' -ErrorAction SilentlyContinue))
{
start-nativeExecution {
sudo yarn global add 'gulp@4.0.2'
}
}
if(!(Get-Command -Name 'node' -ErrorAction SilentlyContinue))
{
throw "node not found"
}
}
if(!(Get-Command -Name 'node' -ErrorAction SilentlyContinue))
{
<#
On Windows, pre-requisites are missing
For now we will skip, and write a warning. Work to resolve this is tracked in:
https://github.com/PowerShell/PowerShell/issues/3429
#>
Write-Warning "Node and yarn are required to run this test"
$skip = $true
}
$mdIssuesPath = Join-Path -Path $PSScriptRoot -ChildPath "markdownissues.txt"
Remove-Item -Path $mdIssuesPath -Force -ErrorAction SilentlyContinue
}
AfterAll {
Pop-Location
}
It "Should not have errors in any markdown files" -Skip:$skip {
$NpmInstalled | Should -BeExactly "Installed"
$mdErrors = 0
Push-Location -Path $PSScriptRoot
try
{
$docsToTest = @(
'./.github/*.md'
'./README.md'
'./demos/python/*.md'
'./docker/*.md'
'./docs/building/*.md'
'./docs/community/*.md'
'./docs/host-powershell/*.md'
'./docs/cmdlet-example/*.md'
'./docs/maintainers/*.md'
'./test/powershell/README.md'
'./tools/*.md'
'./.github/ISSUE_TEMPLATE/*.md'
)
$filter = ($docsToTest -join ',')
# Gulp 4 beta is returning non-zero exit code even when there is not an error
Start-NativeExecution {
&"gulp" test-mdsyntax --silent `
--rootpath $repoRootPath `
--filter $filter
} -VerboseOutputOnError -IgnoreExitcode
}
finally
{
Pop-Location
}
$mdIssuesPath | Should -Exist
[string[]] $markdownErrors = Get-Content -Path $mdIssuesPath
Remove-Item -Path $mdIssuesPath -Force -ErrorAction SilentlyContinue
if ($markdownErrors -ne "--EMPTY--")
{
$markdownErrors += ' (See https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md for an explanation of the error codes)'
}
$markdownErrors | Write-Host
$markdownErrors -join "`n" | Should -BeExactly "--EMPTY--"
}
}
-35
View File
@@ -1,35 +0,0 @@
{
"name": "powershell.common.markdown.tests",
"private": true,
"version": "1.0.0",
"description": "The PowerShell Common Markdown Tests.",
"main": "gulpfile.js",
"dependencies": {
"gulp": "^4.0.2",
"markdownlint": "^0.22.0",
"through2": "^4.0.2"
},
"resolutions": {
"yargs-parser": "^13.1.2" ,
"y18n": "^5.0.5",
"ini": "^1.3.6",
"copy-props":"^2.0.5",
"glob-parent":"^5.1.2",
"hosted-git-info":"^3.0.8",
"set-value":"^4.0.1"
},
"devDependencies": {
"gulp-concat": "^2.6.1",
"gulp-debug": "^4.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/PowerShell/PowerShell.git"
},
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/PowerShell/PowerShell/issues"
},
"homepage": "https://github.com/PowerShell/PowerShell#readme"
}
File diff suppressed because it is too large Load Diff