Skip to content

Commit

Permalink
Migrate changelog-generator, broken-link-checker and utilities to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianrbz committed Jul 1, 2024
1 parent 605c4a9 commit 36555f1
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Accepts a navigation file + converts it to the index page for that product.
This is enough to start spidering URLs using the sidebar for broken links
*/

module.exports = function (filename) {
export default function(filename) {
const lookup = {
deck: "deck",
kgo: "gatway-operator",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');
import { existsSync } from 'fs';

module.exports = function extractNavWithMeta(items, base, srcPrefix) {
export default function extractNavWithMeta(items, base, srcPrefix) {
let urls = [];
for (let u of items) {
if (u.items) {
Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = function extractNavWithMeta(items, base, srcPrefix) {
.replace(/\/$/, ""); // Remove trailing slashes

// check if src.md exists, otherwise check if src/index.md exists
if (fs.existsSync(`${src}.md`)) {
if (existsSync(`${src}.md`)) {
src += '.md';
} else if (`${src}/index.md`) {
src += '/index.md';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
Accepts a file path in app/_src and returns all URLs that the file renders
*/

// This is a hack to enable utilities to access node_modules out of tree
module.paths.unshift(process.mainModule.paths[0]);
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

module.exports = srcToUrls;

const fg = require("fast-glob");
const yaml = require("js-yaml");
const fs = require("fs");

const extractNavWithMeta = require("./nav-to-urls");
import fg from 'fast-glob';
import { load } from "js-yaml";
import { readFileSync } from "fs";
import extractNavWithMeta from "./nav-to-urls.mjs";

async function loadNavEntries(pattern) {
let navEntries = await fg(
`${__dirname}/../../../app/_data/docs_nav_${pattern}.yml`,
);
return navEntries
.map((f) => {
return yaml.load(fs.readFileSync(f, "utf8"));
return load(readFileSync(f, "utf8"));
})
.filter((n) => {
// Skip inherited nav files
Expand All @@ -29,7 +28,7 @@ async function loadNavEntries(pattern) {

async function loadKongVersions() {
let versions = await fg(`${__dirname}/../../../app/_data/kong_versions.yml`);
return yaml.load(fs.readFileSync(versions[0], "utf8"));
return load(readFileSync(versions[0], "utf8"));
}

function getReleaseData(release, versions) {
Expand All @@ -41,14 +40,14 @@ function getReleaseData(release, versions) {
}

// Take a file in src and output the URLs that it renders
async function srcToUrls(pattern, file) {
export default async function(pattern, file) {
let urls = [];

if (!file) {
file = {};
}

src = file.filename;
let src = file.filename;

const navEntries = await loadNavEntries(pattern);
const kongVersions = await loadKongVersions();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const convertNavFileToUrl = require("./lib/nav-to-index-page");
const convertSrcFileToUrls = require("./lib/src-to-urls");
import convertNavFileToUrl from "./lib/nav-to-index-page.mjs";
import convertSrcFileToUrls from "./lib/src-to-urls.mjs";

module.exports = async function (files, options = {}) {
export default async function (files, options = {}) {
files = files.map((f) => {
return {
filename: f.filename,
Expand Down
14 changes: 9 additions & 5 deletions tools/broken-link-checker/full.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const fg = require("fast-glob");
const semver = require("semver");
const argv = require("minimist")(process.argv.slice(2));
import fg from "fast-glob";
import semver from 'semver';
import minimist from 'minimist';
const argv = minimist(process.argv.slice(2));

const { SiteChecker } = require("broken-link-checker");
import pkg from 'broken-link-checker';
const { SiteChecker } = pkg;

import ignoredTargets from "./config/ignored_targets.json?type=json" assert { type: 'json' };

(async function () {
const host = argv.host;
Expand Down Expand Up @@ -90,7 +94,7 @@ const { SiteChecker } = require("broken-link-checker");
});

// Add known list of exclusions
excluded = excluded.concat(require("./config/ignored_targets.json"));
excluded = excluded.concat(ignoredTargets);

// Excluded external sites for full scan only
excluded.push("https://github.com/*");
Expand Down
11 changes: 7 additions & 4 deletions tools/broken-link-checker/lib/check-url-list.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const { HtmlUrlChecker } = require("broken-link-checker");
module.exports = function (changes, opts) {
import pkg from 'broken-link-checker';
const { HtmlUrlChecker } = pkg;

import ignoredTargets from "../config/ignored_targets.json?type=json" assert { type: 'json' };

export function checkUrls(changes, opts) {
return new Promise((resolve) => {
// These are URLs that we might link to, but we don't want to
// check for validity
const ignoredTargets = require("../config/ignored_targets.json");
const brokenLinks = new Set();
const checker = new HtmlUrlChecker(
{
Expand All @@ -24,7 +27,7 @@ module.exports = function (changes, opts) {
}

// Ignore any broken links in the opts.ignore list
for (b of opts.ignore) {
for (const b of opts.ignore) {
if (result.url.resolved.match(b)) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions tools/broken-link-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
43 changes: 19 additions & 24 deletions tools/broken-link-checker/run.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const { Octokit } = require("@octokit/rest");
const github = require("@actions/github");
const argv = require("minimist")(process.argv.slice(2));
const fg = require("fast-glob");
import { Octokit } from "@octokit/rest";
import { context } from '@actions/github';

const convertFilePathsToUrls = require("../_utilities/path-to-url");
const checkUrls = require("./lib/check-url-list");
const srcToUrls = require("../_utilities/lib/src-to-urls");
import fg from "fast-glob";
import minimist from 'minimist';
const argv = minimist(process.argv.slice(2));

import convertFilePathsToUrls from "../_utilities/path-to-url.mjs";
import { checkUrls } from "./lib/check-url-list.js";
import srcToUrls from "../_utilities/lib/src-to-urls.mjs";
import ignoredPaths from './config/ignored_paths.json' assert { type: 'json' };

const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
Expand Down Expand Up @@ -37,11 +40,11 @@ async function main() {
let changes; // This will be set by one of the implementations

if (type == "pr") {
options.pull_number = argv.pr || github.context.issue.number;
options.pull_number = argv.pr || context.issue.number;
options.is_fork =
argv.is_fork !== undefined
? argv.is_fork
: github.context.payload.pull_request.head.repo.fork;
: context.payload.pull_request.head.repo.fork;
console.log(
`Loading changed files for PR ${options.pull_number} (fork: ${options.is_fork})`,
);
Expand All @@ -60,11 +63,10 @@ async function main() {
// Run the check
if (changes.length) {
// Remove any ignored paths
const ignoredPaths = require("./config/ignored_paths.json").map(
(r) => new RegExp(r),
);
const ignoredPathsRegexes = ignoredPaths.map((r) => new RegExp(r));

const ignoredUrls = [];
for (let item of ignoredPaths) {
for (let item of ignoredPathsRegexes) {
changes = changes.filter((u) => {
const shouldIgnore = u.url.match(item);
if (shouldIgnore) {
Expand Down Expand Up @@ -116,9 +118,9 @@ async function main() {
}

// Implementations for each mode
async function buildPrUrls(options) {
if (github.context.repo.owner) {
options = { ...github.context.repo, ...options };
export async function buildPrUrls(options) {
if (context.repo.owner) {
options = { ...context.repo, ...options };
}

// Get pages that have changed in the PR
Expand Down Expand Up @@ -153,11 +155,4 @@ async function buildPluginUrls(options) {
return navEntries;
}

// If module main run directly, otherwise export for testing
if (require.main === module) {
main();
}

module.exports = Object.assign(main, {
buildPrUrls,
});
main();
18 changes: 12 additions & 6 deletions tools/changelog-generator/changelog.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const fs = require("fs").promises;
const { subDays, startOfDay, format } = require("date-fns");
const groupBy = require("lodash.groupby");
import { promises as fs } from 'fs';
import { subDays, startOfDay, format } from 'date-fns';
import groupBy from 'lodash.groupby';
import { Octokit } from "@octokit/rest";

const convertFilePathsToUrls = require("../_utilities/path-to-url");
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

import convertFilePathsToUrls from "../_utilities/path-to-url.mjs";
const now = startOfDay(new Date());
const earliestDate = subDays(now, 7);

(async function () {
// Create an octokit instance
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
Expand All @@ -26,6 +31,7 @@ const earliestDate = subDays(now, 7);
},
(response, done) => {
const validPulls = [];
let prDate;

for (let pr of response.data) {
//console.log(JSON.stringify(pr));
Expand Down Expand Up @@ -65,7 +71,7 @@ const earliestDate = subDays(now, 7);
);

// Convert app, _src, /hub/ etc paths to URLs
affected_files = (
let affected_files = (
await convertFilePathsToUrls(pr_files, { skip_nav_files: true })
).map((v) => {
return {
Expand Down
1 change: 1 addition & 0 deletions tools/changelog-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down

0 comments on commit 36555f1

Please sign in to comment.