From 9f78597965b18a69a70ea3fc691c7d664cd6368c Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 22:16:49 -0500 Subject: [PATCH 01/24] Add AST preprocessor --- .gitattributes | 1 - .gitignore | 2 ++ .vscode/settings.json | 3 ++ action.yml | 10 +++++- cli.ts | 75 +++++++++++++++++++++++++++++++++++++++++++ cliw | 32 ++++++++++++++++++ src/clone.sh | 40 ----------------------- src/init.sh | 40 ----------------------- 8 files changed, 121 insertions(+), 82 deletions(-) delete mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 cli.ts create mode 100755 cliw delete mode 100755 src/clone.sh delete mode 100755 src/init.sh diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 50ca329..0000000 --- a/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -*.sh eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4bf8613 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# https://github.com/jcbhmr/deno_wrapper +.deno diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cbac569 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "deno.enable": true +} diff --git a/action.yml b/action.yml index f1b1de7..d7353a6 100644 --- a/action.yml +++ b/action.yml @@ -64,6 +64,13 @@ inputs: push to the remote wiki. The default is false. This is useful for testing. required: true default: false + preprocess-links: + description: >- + Whether or not to preprocess links before pushing to the wiki. This will + not mutate the source tree. It replaces all links to .md files with links + to the corresponding wiki page. The default is true. + required: true + default: true outputs: wiki_url: description: >- @@ -75,7 +82,7 @@ runs: using: composite steps: - id: main - run: '"${GITHUB_ACTION_PATH%/}/src/$INPUT_STRATEGY.sh"' + run: '"${GITHUB_ACTION_PATH%/}/cliw"' shell: bash env: INPUT_STRATEGY: ${{ inputs.strategy }} @@ -86,3 +93,4 @@ runs: INPUT_COMMIT_MESSAGE: ${{ inputs.commit-message }} INPUT_IGNORE: ${{ inputs.ignore }} INPUT_DRY_RUN: ${{ inputs.dry-run }} + INPUT_PREPROCESS_LINKS: ${{ inputs.preprocess-links }} diff --git a/cli.ts b/cli.ts new file mode 100644 index 0000000..10f2f98 --- /dev/null +++ b/cli.ts @@ -0,0 +1,75 @@ +#!/usr/bin/env -S deno run -Aq +// Copyright 2023 Jacob Hummer +// SPDX-License-Identifier: Apache-2.0 +import process from "node:process"; +import { readFile, writeFile, cp, appendFile, readdir } from "node:fs/promises"; +import * as core from "npm:@actions/core@^1.10.0"; +import { temporaryDirectory } from "npm:tempy@^3.1.0"; +import { $ } from "npm:zx@^7.2.2"; +import { remark } from "npm:remark@^14.0.3"; +import { visit } from "npm:unist-util-visit@^5.0.0"; +import { resolve } from "node:path"; + +$.verbose = false; +if (core.isDebug()) { + $.verbose = true; +} + +const serverURL = core.getInput("github_server_url"); +const repo = core.getInput("repository"); +const wikiGitURL = `${serverURL}/${repo}.wiki.git`; +$.cwd = temporaryDirectory(); + +process.env.GH_TOKEN = core.getInput("token"); +process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; +await $`gh auth setup-git`; + +// https://github.com/stefanzweifel/git-auto-commit-action/blob/master/action.yml#L35-L42 +await $`git config user.name github-actions[bot]`; +await $`git config user.email 41898282+github-actions[bot]@users.noreply.github.com`; + +if (core.getInput("strategy") === "clone") { + await $`git config --global --add safe.directory ${$.cwd}`; + await $`git clone ${wikiGitURL} .`; +} else if (core.getInput("strategy") === "init") { + await $`git init -b master`; + await $`git remote add origin ${wikiGitURL}`; + await $`git fetch`; +} else { + throw new DOMException("Unknown strategy", "NotSupportedError"); +} + +appendFile(resolve($.cwd!, ".git/info/exclude"), core.getInput("ignore")); +cp(core.getInput("path"), $.cwd!, { recursive: true }); + +function plugin() { + function visitor(node: any) { + if (/\.md$/.test(node.url)) { + node.url = node.url.replace(/\.md$/, ""); + } + } + return (tree: any) => visit(tree, ["link", "linkReference"], visitor); +} + +for (const file of await readdir($.cwd!)) { + if (!/\.(?:md|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/.test(file)) { + continue; + } + + let md = await readFile(resolve($.cwd!, file), "utf-8"); + md = (await remark().use(plugin).process(md)).toString(); + await writeFile(resolve($.cwd!, file), md); +} + +await $`git add -Av`; +await $`git commit --allow-empty -m ${core.getInput("message")}`; + +if (["true", "1"].includes(core.getInput("dry_run"))) { + await $`git show`; + await $`git push -f origin master --dry-run`; +} else { + await $`git push -f origin master`; +} + +const wikiURL = `${serverURL}/${repo}/wiki`; +appendFile(`wiki_url=${wikiURL}`, process.env.GITHUB_OUTPUT!); diff --git a/cliw b/cliw new file mode 100755 index 0000000..d842b28 --- /dev/null +++ b/cliw @@ -0,0 +1,32 @@ +#!/bin/sh +# Copyright 2023 Jacob Hummer +# SPDX-License-Identifier: Apache-2.0 +# Based on https://github.com/jcbhmr/deno_wrapper +set -e +# https://stackoverflow.com/a/29835459 +script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P) +deno_dir="$script_dir/.deno" + +# https://manpages.ubuntu.com/manpages/kinetic/en/man1/chronic.1.html +chronic() ( + set +e + output=$($@ 2>&1) + exit_code=$? + set -e + if [ "$exit_code" -ne 0 ]; then + echo "$output" >&2 + fi + return "$exit_code" +) + +if [ ! -d "$deno_dir" ]; then + # https://github.com/denoland/deno_install#readme + export DENO_INSTALL=$deno_dir + curl -fsSL https://deno.land/x/install/install.sh | chronic sh -s "v1.35.1" +fi + +# https://github.com/denoland/deno_install/blob/master/install.sh#L53 +export DENO_INSTALL=$deno_dir +export PATH="$DENO_INSTALL/bin:$PATH" + +exec "$script_dir/cli.ts" "$@" diff --git a/src/clone.sh b/src/clone.sh deleted file mode 100755 index e3dfd60..0000000 --- a/src/clone.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# Copyright 2023 Jacob Hummer -# SPDX-License-Identifier: Apache-2.0 -set -e -if [[ -n $RUNNER_DEBUG ]]; then - set -x -fi - -export GITHUB_TOKEN="$INPUT_TOKEN" -export GITHUB_SERVER_URL="$INPUT_GITHUB_SERVER_URL" -export GITHUB_REPOSITORY="$INPUT_REPOSITORY" -export GH_HOST="${GITHUB_SERVER_URL#*//}" - -export GIT_DIR && GIT_DIR=$(mktemp -d) -export GIT_WORK_TREE="$INPUT_PATH" -trap 'rm -rf "$GIT_DIR"' SIGINT SIGTERM ERR EXIT - -gh auth setup-git -git config --global --add safe.directory "$GIT_DIR" - -git clone "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.wiki.git" "$GIT_DIR" --bare -git config --unset core.bare - -echo "$INPUT_IGNORE" >>"$GIT_DIR/info/exclude" -git add -Av - -# https://github.com/stefanzweifel/git-auto-commit-action/blob/master/action.yml#L35-L42 -git config user.name github-actions[bot] -git config user.email 41898282+github-actions[bot]@users.noreply.github.com -git commit --allow-empty -m "$INPUT_COMMIT_MESSAGE" - -if [[ $INPUT_DRY_RUN == true ]]; then - echo 'Dry run' - git remote show origin - git show - exit 0 -fi - -git push origin master -echo "wiki_url=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/wiki" >>"$GITHUB_OUTPUT" diff --git a/src/init.sh b/src/init.sh deleted file mode 100755 index e5f55d5..0000000 --- a/src/init.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# Copyright 2023 Jacob Hummer -# SPDX-License-Identifier: Apache-2.0 -set -e -if [[ -n $RUNNER_DEBUG ]]; then - set -x -fi - -export GITHUB_TOKEN="$INPUT_TOKEN" -export GITHUB_SERVER_URL="$INPUT_GITHUB_SERVER_URL" -export GITHUB_REPOSITORY="$INPUT_REPOSITORY" -export GH_HOST="${GITHUB_SERVER_URL#*//}" - -export GIT_DIR && GIT_DIR=$(mktemp -d) -export GIT_WORK_TREE="$INPUT_PATH" -trap 'rm -rf "$GIT_DIR"' SIGINT SIGTERM ERR EXIT - -gh auth setup-git -git config --global --add safe.directory "$GIT_DIR" - -git init -b master -git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.wiki.git" - -echo "$INPUT_IGNORE" >>"$GIT_DIR/info/exclude" -git add -Av - -git config user.name github-actions[bot] -git config user.email 41898282+github-actions[bot]@users.noreply.github.com - -git commit --allow-empty -m "$INPUT_COMMIT_MESSAGE" - -if [[ $INPUT_DRY_RUN == true ]]; then - echo 'Dry run' - git remote show origin - git show - exit 0 -fi - -git push -f origin master -echo "wiki_url=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/wiki" >>"$GITHUB_OUTPUT" From 8ee5411268dee723f2188567761589902d36dcdf Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 22:46:29 -0500 Subject: [PATCH 02/24] fix test --- .../workflows/{test.yml => test-action.yml} | 25 +++++++++++-------- .github/workflows/update-tags.yml | 1 - cli.ts | 0 3 files changed, 14 insertions(+), 12 deletions(-) rename .github/workflows/{test.yml => test-action.yml} (73%) mode change 100644 => 100755 cli.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test-action.yml similarity index 73% rename from .github/workflows/test.yml rename to .github/workflows/test-action.yml index c6023c4..bd78552 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test-action.yml @@ -1,7 +1,6 @@ # Copyright 2023 Jacob Hummer # SPDX-License-Identifier: Apache-2.0 - -name: Test +name: Test action on: push: branches: [master] @@ -20,29 +19,33 @@ concurrency: group: ${{ github.workflow }}-${{ github.ref_name }} cancel-in-progress: true jobs: - test-shellcheck: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: ludeeus/action-shellcheck@master - test: + test-action-clone: strategy: + fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 - - name: ./:clone + - name: strategy=clone uses: ./ with: strategy: clone dry-run: true - - name: ./:init + test-action-init: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: strategy=init uses: ./ with: strategy: init dry-run: true - test-master: + test-action-master: if: github.ref_name == 'master' needs: test permissions: diff --git a/.github/workflows/update-tags.yml b/.github/workflows/update-tags.yml index 785d5de..90dc4a1 100644 --- a/.github/workflows/update-tags.yml +++ b/.github/workflows/update-tags.yml @@ -1,6 +1,5 @@ # Copyright 2023 Jacob Hummer # SPDX-License-Identifier: Apache-2.0 - name: Update tags on: release: diff --git a/cli.ts b/cli.ts old mode 100644 new mode 100755 From e62faaeb3afc705bc8088ec6c9e215b20d159bb1 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 22:46:51 -0500 Subject: [PATCH 03/24] use newer convention --- .github/workflows/update-tags.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/update-tags.yml b/.github/workflows/update-tags.yml index 90dc4a1..12a4ea5 100644 --- a/.github/workflows/update-tags.yml +++ b/.github/workflows/update-tags.yml @@ -5,7 +5,7 @@ on: release: types: [released] concurrency: - group: update-tags + group: ${{ github.workflow }} cancel-in-progress: true permissions: contents: write From 42f2e233dd9b26bcfbee13c66b085144f74b0f11 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 22:47:23 -0500 Subject: [PATCH 04/24] add .gitignore to ignored paths --- .github/workflows/test-action.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml index bd78552..f196886 100644 --- a/.github/workflows/test-action.yml +++ b/.github/workflows/test-action.yml @@ -5,12 +5,14 @@ on: push: branches: [master] paths-ignore: + - .gitignore - LICENSE - README.md - .github/** - "!.github/workflows/test.yml" pull_request: paths-ignore: + - .gitignore - LICENSE - README.md - .github/** From ba675053b1b1eab94fcc33a873841639931106e6 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 22:54:33 -0500 Subject: [PATCH 05/24] add test trigger --- .github/workflows/test-action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml index 266d74a..5563829 100644 --- a/.github/workflows/test-action.yml +++ b/.github/workflows/test-action.yml @@ -17,6 +17,7 @@ on: - README.md - .github/** - "!.github/workflows/test-action.yml" + workflow_dispatch: concurrency: group: ${{ github.workflow }}-${{ github.ref_name }} cancel-in-progress: true From d41d74e46e7c916ac71b727af35b45fe9e22e48e Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 22:55:20 -0500 Subject: [PATCH 06/24] remove test trigger --- .github/workflows/test-action.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml index 5563829..266d74a 100644 --- a/.github/workflows/test-action.yml +++ b/.github/workflows/test-action.yml @@ -17,7 +17,6 @@ on: - README.md - .github/** - "!.github/workflows/test-action.yml" - workflow_dispatch: concurrency: group: ${{ github.workflow }}-${{ github.ref_name }} cancel-in-progress: true From d5bda23bc6828e6567ed5352d553a9ddcd3646a6 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 23:11:45 -0500 Subject: [PATCH 07/24] add debug info + use fs-extra --- cli.ts | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/cli.ts b/cli.ts index 10f2f98..de80887 100755 --- a/cli.ts +++ b/cli.ts @@ -2,7 +2,8 @@ // Copyright 2023 Jacob Hummer // SPDX-License-Identifier: Apache-2.0 import process from "node:process"; -import { readFile, writeFile, cp, appendFile, readdir } from "node:fs/promises"; +import { readFile, writeFile, appendFile, readdir } from "node:fs/promises"; +import { copy } from "npm:fs-extra@^11.1.1" import * as core from "npm:@actions/core@^1.10.0"; import { temporaryDirectory } from "npm:tempy@^3.1.0"; import { $ } from "npm:zx@^7.2.2"; @@ -20,6 +21,10 @@ const repo = core.getInput("repository"); const wikiGitURL = `${serverURL}/${repo}.wiki.git`; $.cwd = temporaryDirectory(); +if (core.isDebug()) { + console.table({ serverURL, repo, wikiGitURL, "$.cwd": $.cwd }); +} + process.env.GH_TOKEN = core.getInput("token"); process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; await $`gh auth setup-git`; @@ -39,26 +44,32 @@ if (core.getInput("strategy") === "clone") { throw new DOMException("Unknown strategy", "NotSupportedError"); } -appendFile(resolve($.cwd!, ".git/info/exclude"), core.getInput("ignore")); -cp(core.getInput("path"), $.cwd!, { recursive: true }); +await appendFile(resolve($.cwd!, ".git/info/exclude"), core.getInput("ignore")); +await copy(core.getInput("path"), $.cwd!, { recursive: true }); function plugin() { function visitor(node: any) { if (/\.md$/.test(node.url)) { node.url = node.url.replace(/\.md$/, ""); + + if (core.isDebug()) { + console.log(`Rewrote to ${node.url}`); + } } } return (tree: any) => visit(tree, ["link", "linkReference"], visitor); } -for (const file of await readdir($.cwd!)) { - if (!/\.(?:md|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/.test(file)) { - continue; - } +if (["true", "1"].includes(core.getInput("preprocess_links"))) { + for (const file of await readdir($.cwd!)) { + if (!/\.(?:md|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/.test(file)) { + continue; + } - let md = await readFile(resolve($.cwd!, file), "utf-8"); - md = (await remark().use(plugin).process(md)).toString(); - await writeFile(resolve($.cwd!, file), md); + let md = await readFile(resolve($.cwd!, file), "utf-8"); + md = (await remark().use(plugin).process(md)).toString(); + await writeFile(resolve($.cwd!, file), md); + } } await $`git add -Av`; @@ -72,4 +83,4 @@ if (["true", "1"].includes(core.getInput("dry_run"))) { } const wikiURL = `${serverURL}/${repo}/wiki`; -appendFile(`wiki_url=${wikiURL}`, process.env.GITHUB_OUTPUT!); +await appendFile(`wiki_url=${wikiURL}`, process.env.GITHUB_OUTPUT!); From ca3cecdfad11b7f0ef0fa5685071d2474ba15db3 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 23:14:49 -0500 Subject: [PATCH 08/24] move user setup to AFTER init --- cli.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli.ts b/cli.ts index de80887..d1846f8 100755 --- a/cli.ts +++ b/cli.ts @@ -29,10 +29,6 @@ process.env.GH_TOKEN = core.getInput("token"); process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; await $`gh auth setup-git`; -// https://github.com/stefanzweifel/git-auto-commit-action/blob/master/action.yml#L35-L42 -await $`git config user.name github-actions[bot]`; -await $`git config user.email 41898282+github-actions[bot]@users.noreply.github.com`; - if (core.getInput("strategy") === "clone") { await $`git config --global --add safe.directory ${$.cwd}`; await $`git clone ${wikiGitURL} .`; @@ -44,6 +40,10 @@ if (core.getInput("strategy") === "clone") { throw new DOMException("Unknown strategy", "NotSupportedError"); } +// https://github.com/stefanzweifel/git-auto-commit-action/blob/master/action.yml#L35-L42 +await $`git config user.name github-actions[bot]`; +await $`git config user.email 41898282+github-actions[bot]@users.noreply.github.com`; + await appendFile(resolve($.cwd!, ".git/info/exclude"), core.getInput("ignore")); await copy(core.getInput("path"), $.cwd!, { recursive: true }); From 4a624ed490181d6e865fecc6e825413ed34fc9a4 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 23:16:03 -0500 Subject: [PATCH 09/24] fix commit message input --- cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.ts b/cli.ts index d1846f8..304cf5b 100755 --- a/cli.ts +++ b/cli.ts @@ -73,7 +73,7 @@ if (["true", "1"].includes(core.getInput("preprocess_links"))) { } await $`git add -Av`; -await $`git commit --allow-empty -m ${core.getInput("message")}`; +await $`git commit --allow-empty -m ${core.getInput("commit_message")}`; if (["true", "1"].includes(core.getInput("dry_run"))) { await $`git show`; From 17480185460cfffa853232fa7a287d873cb6142a Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 23:28:43 -0500 Subject: [PATCH 10/24] ungate debug --- cli.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/cli.ts b/cli.ts index 304cf5b..777e7bb 100755 --- a/cli.ts +++ b/cli.ts @@ -11,19 +11,14 @@ import { remark } from "npm:remark@^14.0.3"; import { visit } from "npm:unist-util-visit@^5.0.0"; import { resolve } from "node:path"; -$.verbose = false; -if (core.isDebug()) { - $.verbose = true; -} +console.table(process.env) const serverURL = core.getInput("github_server_url"); const repo = core.getInput("repository"); const wikiGitURL = `${serverURL}/${repo}.wiki.git`; $.cwd = temporaryDirectory(); -if (core.isDebug()) { - console.table({ serverURL, repo, wikiGitURL, "$.cwd": $.cwd }); -} +console.table({ serverURL, repo, wikiGitURL, "$.cwd": $.cwd }); process.env.GH_TOKEN = core.getInput("token"); process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; @@ -51,10 +46,7 @@ function plugin() { function visitor(node: any) { if (/\.md$/.test(node.url)) { node.url = node.url.replace(/\.md$/, ""); - - if (core.isDebug()) { - console.log(`Rewrote to ${node.url}`); - } + console.log(`Rewrote to ${node.url}`); } } return (tree: any) => visit(tree, ["link", "linkReference"], visitor); From e31e31ef590c764e739284fefab6f1ff808febd1 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 23:31:28 -0500 Subject: [PATCH 11/24] =?UTF-8?q?duh=20switch=20args=20=F0=9F=A4=A6?= =?UTF-8?q?=E2=80=8D=E2=99=80=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.ts b/cli.ts index 777e7bb..7555e6c 100755 --- a/cli.ts +++ b/cli.ts @@ -75,4 +75,4 @@ if (["true", "1"].includes(core.getInput("dry_run"))) { } const wikiURL = `${serverURL}/${repo}/wiki`; -await appendFile(`wiki_url=${wikiURL}`, process.env.GITHUB_OUTPUT!); +await appendFile(process.env.GITHUB_OUTPUT!, `wiki_url=${wikiURL}`); From a184dfa4f35fe66dc9a3729fcbe0872bfbf9ba07 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sat, 15 Jul 2023 23:33:56 -0500 Subject: [PATCH 12/24] remove third arg --- cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.ts b/cli.ts index 7555e6c..2e5f725 100755 --- a/cli.ts +++ b/cli.ts @@ -40,7 +40,7 @@ await $`git config user.name github-actions[bot]`; await $`git config user.email 41898282+github-actions[bot]@users.noreply.github.com`; await appendFile(resolve($.cwd!, ".git/info/exclude"), core.getInput("ignore")); -await copy(core.getInput("path"), $.cwd!, { recursive: true }); +await copy(core.getInput("path"), $.cwd!); function plugin() { function visitor(node: any) { From 13382972a7df5bc8af2227171cf8c9be35fa541d Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 00:07:41 -0500 Subject: [PATCH 13/24] Make it rewrite README.md to Home.md --- action.yml | 9 ++++---- cli.ts | 66 +++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/action.yml b/action.yml index d7353a6..d832bcb 100644 --- a/action.yml +++ b/action.yml @@ -64,11 +64,12 @@ inputs: push to the remote wiki. The default is false. This is useful for testing. required: true default: false - preprocess-links: + preprocess: description: >- - Whether or not to preprocess links before pushing to the wiki. This will - not mutate the source tree. It replaces all links to .md files with links - to the corresponding wiki page. The default is true. + If this option is true, we will preprocess the wiki to move the README.md + to Home.md as well as rewriting all .md links to be bare links. This helps + ensure that the Markdown works in source control as well as the wiki. The + default is true. required: true default: true outputs: diff --git a/cli.ts b/cli.ts index 2e5f725..5a3e8a2 100755 --- a/cli.ts +++ b/cli.ts @@ -2,8 +2,15 @@ // Copyright 2023 Jacob Hummer // SPDX-License-Identifier: Apache-2.0 import process from "node:process"; -import { readFile, writeFile, appendFile, readdir } from "node:fs/promises"; -import { copy } from "npm:fs-extra@^11.1.1" +import { + readFile, + writeFile, + appendFile, + readdir, + rename, +} from "node:fs/promises"; +import { existsSync } from "node:fs"; +import { copy } from "npm:fs-extra@^11.1.1"; import * as core from "npm:@actions/core@^1.10.0"; import { temporaryDirectory } from "npm:tempy@^3.1.0"; import { $ } from "npm:zx@^7.2.2"; @@ -11,14 +18,20 @@ import { remark } from "npm:remark@^14.0.3"; import { visit } from "npm:unist-util-visit@^5.0.0"; import { resolve } from "node:path"; -console.table(process.env) - const serverURL = core.getInput("github_server_url"); const repo = core.getInput("repository"); const wikiGitURL = `${serverURL}/${repo}.wiki.git`; -$.cwd = temporaryDirectory(); +const path = resolve(core.getInput("path")); +process.chdir(temporaryDirectory()); +await copy(path, process.cwd()); -console.table({ serverURL, repo, wikiGitURL, "$.cwd": $.cwd }); +console.table({ + serverURL, + repo, + wikiGitURL, + path, + "process.cwd()": process.cwd(), +}); process.env.GH_TOKEN = core.getInput("token"); process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; @@ -39,28 +52,41 @@ if (core.getInput("strategy") === "clone") { await $`git config user.name github-actions[bot]`; await $`git config user.email 41898282+github-actions[bot]@users.noreply.github.com`; -await appendFile(resolve($.cwd!, ".git/info/exclude"), core.getInput("ignore")); -await copy(core.getInput("path"), $.cwd!); +await appendFile(".git/info/exclude", core.getInput("ignore")); -function plugin() { - function visitor(node: any) { - if (/\.md$/.test(node.url)) { - node.url = node.url.replace(/\.md$/, ""); - console.log(`Rewrote to ${node.url}`); - } +if (["true", "1"].includes(core.getInput("preprocess"))) { + // https://github.com/nodejs/node/issues/39960 + if (existsSync("README.md")) { + await rename("README.md", "Home.md"); + console.log("Moved README.md to Home.md"); } - return (tree: any) => visit(tree, ["link", "linkReference"], visitor); -} -if (["true", "1"].includes(core.getInput("preprocess_links"))) { + const mdRe = /\.(?:md|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/; + const plugin = () => (tree: any) => + visit(tree, ["link", "linkReference"], (node: any) => { + if (!mdRe.test(node.url)) { + return; + } + if (!new URL(node.url, "file:///-/").href.startsWith("file:///-/")) { + return; + } + + const x = node.url; + node.url = node.url.replace(mdRe, ""); + if (new URL(node.url, "file:///-/").href === "file:///-/README") { + node.url = "Home"; + } + + console.log(`Rewrote ${x} to ${node.url}`); + }); for (const file of await readdir($.cwd!)) { - if (!/\.(?:md|markdown|mdown|mkdn|mkd|mdwn|mkdown|ron)$/.test(file)) { + if (!mdRe.test(file)) { continue; } - let md = await readFile(resolve($.cwd!, file), "utf-8"); + let md = await readFile(file, "utf-8"); md = (await remark().use(plugin).process(md)).toString(); - await writeFile(resolve($.cwd!, file), md); + await writeFile(file, md); } } From 0b374a6914efc46c9e855b39bd5bf93fb17180a6 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 00:15:38 -0500 Subject: [PATCH 14/24] fix git cloning into full folder --- cli.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/cli.ts b/cli.ts index 5a3e8a2..92176ef 100755 --- a/cli.ts +++ b/cli.ts @@ -18,20 +18,15 @@ import { remark } from "npm:remark@^14.0.3"; import { visit } from "npm:unist-util-visit@^5.0.0"; import { resolve } from "node:path"; +core.startGroup("process.env"); +console.table(process.env); +core.endGroup(); + const serverURL = core.getInput("github_server_url"); const repo = core.getInput("repository"); const wikiGitURL = `${serverURL}/${repo}.wiki.git`; -const path = resolve(core.getInput("path")); +const workspacePath = process.cwd(); process.chdir(temporaryDirectory()); -await copy(path, process.cwd()); - -console.table({ - serverURL, - repo, - wikiGitURL, - path, - "process.cwd()": process.cwd(), -}); process.env.GH_TOKEN = core.getInput("token"); process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; @@ -53,6 +48,7 @@ await $`git config user.name github-actions[bot]`; await $`git config user.email 41898282+github-actions[bot]@users.noreply.github.com`; await appendFile(".git/info/exclude", core.getInput("ignore")); +await copy(resolve(workspacePath, core.getInput("path")), process.cwd()); if (["true", "1"].includes(core.getInput("preprocess"))) { // https://github.com/nodejs/node/issues/39960 From 436759f15601c9f7a045c3d5e3491882bf49df44 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 01:34:22 -0500 Subject: [PATCH 15/24] try to fix cwd issue --- cli.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cli.ts b/cli.ts index 92176ef..5dd8f0d 100755 --- a/cli.ts +++ b/cli.ts @@ -26,14 +26,17 @@ const serverURL = core.getInput("github_server_url"); const repo = core.getInput("repository"); const wikiGitURL = `${serverURL}/${repo}.wiki.git`; const workspacePath = process.cwd(); -process.chdir(temporaryDirectory()); +const d = temporaryDirectory(); +process.chdir(d); + +console.log($.cwd, process.cwd(), resolve()); process.env.GH_TOKEN = core.getInput("token"); process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; await $`gh auth setup-git`; if (core.getInput("strategy") === "clone") { - await $`git config --global --add safe.directory ${$.cwd}`; + await $`git config --global --add safe.directory ${process.cwd()}`; await $`git clone ${wikiGitURL} .`; } else if (core.getInput("strategy") === "init") { await $`git init -b master`; From 48c7eb3288cf461de1eea6e07dfc1fe23441e758 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 01:36:40 -0500 Subject: [PATCH 16/24] try again with explicit $.cwd --- cli.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/cli.ts b/cli.ts index 5dd8f0d..dbd8f09 100755 --- a/cli.ts +++ b/cli.ts @@ -28,6 +28,7 @@ const wikiGitURL = `${serverURL}/${repo}.wiki.git`; const workspacePath = process.cwd(); const d = temporaryDirectory(); process.chdir(d); +$.cwd = d; console.log($.cwd, process.cwd(), resolve()); From 0563ede057c167e45532efb313030feba755110a Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 01:38:26 -0500 Subject: [PATCH 17/24] remove log --- cli.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/cli.ts b/cli.ts index dbd8f09..5cadded 100755 --- a/cli.ts +++ b/cli.ts @@ -30,8 +30,6 @@ const d = temporaryDirectory(); process.chdir(d); $.cwd = d; -console.log($.cwd, process.cwd(), resolve()); - process.env.GH_TOKEN = core.getInput("token"); process.env.GH_HOST = new URL(core.getInput("github_server_url")).host; await $`gh auth setup-git`; From d1ca4576a041a46dadced4f0a571c5bfa3c69462 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 01:42:21 -0500 Subject: [PATCH 18/24] fix preprocess input --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index d832bcb..96bef34 100644 --- a/action.yml +++ b/action.yml @@ -94,4 +94,4 @@ runs: INPUT_COMMIT_MESSAGE: ${{ inputs.commit-message }} INPUT_IGNORE: ${{ inputs.ignore }} INPUT_DRY_RUN: ${{ inputs.dry-run }} - INPUT_PREPROCESS_LINKS: ${{ inputs.preprocess-links }} + INPUT_PREPROCESS: ${{ inputs.preprocess }} From 0e953c50a68887deeec4aa5a9be0eaaa3dba360a Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 01:46:52 -0500 Subject: [PATCH 19/24] use more appropriate core methods --- cli.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cli.ts b/cli.ts index 5cadded..d7aeb50 100755 --- a/cli.ts +++ b/cli.ts @@ -52,7 +52,7 @@ await $`git config user.email 41898282+github-actions[bot]@users.noreply.github. await appendFile(".git/info/exclude", core.getInput("ignore")); await copy(resolve(workspacePath, core.getInput("path")), process.cwd()); -if (["true", "1"].includes(core.getInput("preprocess"))) { +if (core.getBooleanInput("preprocess")) { // https://github.com/nodejs/node/issues/39960 if (existsSync("README.md")) { await rename("README.md", "Home.md"); @@ -91,12 +91,11 @@ if (["true", "1"].includes(core.getInput("preprocess"))) { await $`git add -Av`; await $`git commit --allow-empty -m ${core.getInput("commit_message")}`; -if (["true", "1"].includes(core.getInput("dry_run"))) { +if (core.getBooleanInput("dry_run")) { await $`git show`; await $`git push -f origin master --dry-run`; } else { await $`git push -f origin master`; } -const wikiURL = `${serverURL}/${repo}/wiki`; -await appendFile(process.env.GITHUB_OUTPUT!, `wiki_url=${wikiURL}`); +core.setOutput("wiki_url", `${serverURL}/${repo}/wiki`); From a0f0d679f55651d17b62c9f7f56a2f92b76e1bd3 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 01:52:52 -0500 Subject: [PATCH 20/24] add dev section --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index ea83fff..4e6e01c 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,22 @@ jobs: path: . ``` +## Development + +![Deno](https://img.shields.io/static/v1?style=for-the-badge&message=Deno&color=000000&logo=Deno&logoColor=FFFFFF&label=) +![GitHub Actions](https://img.shields.io/static/v1?style=for-the-badge&message=GitHub+Actions&color=2088FF&logo=GitHub+Actions&logoColor=FFFFFF&label=) + +This GitHub Action uses a self-downloaded version of Deno. See `cliw` for the +`cli.ts` wrapper script that downloads the Deno binary and runs the TypeScript +code. The main script itself is ~100 lines of code, so it's not too bad. + +ℹ Because the version of Deno is _pinned_, it's recommended to every-so-often +bump it to the latest version. + +To test the action, open a PR! The `test-action.yml` workflow will run the code +with `dry-run: true` as well as a real run! Yes, this does get tedious swapping +between your IDE and the PR, but it's the easiest way to test the action. + [Decathlon/wiki-page-creator-action#11]: https://github.com/Decathlon/wiki-page-creator-action/issues/11 [supported markup languages]: https://github.com/github/markup#markups From 4b81a9bceebc5496585ab67d2984650e54766907 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 01:52:57 -0500 Subject: [PATCH 21/24] add preprocess note --- README.md | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 4e6e01c..ad9a32a 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,11 @@ is specific to GitHub wikis. not push to the remote wiki. The default is `false`. This is useful for testing. +- **`preprocess`:** If this option is true, we will preprocess the wiki to move + the `README.md` to `Home.md` as well as rewriting all `.md` links to be bare + links. This helps ensure that the Markdown works in source control as well as + the wiki. The default is true. + #### `strategy:` input There are some specific usecases where using `strategy: init` might be better @@ -133,28 +138,6 @@ than the default `strategy: clone`. tab. This is essentially the concatenation of `${{ github.server_url }}`, `${{ github.repository }}`, and the `/wiki` page. -### Preprocessing - -You may wish to strip the `[link](page.md)` `.md` suffix from your links to make -them viewable in GitHub source view (with the `.md`) _as well as_ in GitHub wiki -(without the `.md`; pretty URLs!). You can use a preprocessing action like -[Strip MarkDown extensions from links action] to remove those `.md` suffixes -before using this action. Here's an example: - -```yml -publish-wiki: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: impresscms-dev/strip-markdown-extensions-from-links-action@v1.0.0 - with: - path: wiki - - uses: Andrew-Chen-Wang/github-wiki-action@v4 -``` - -❤️ If you have an awesome preprocessor action that you want to add here, let us -know! We'd love to add an example. - ### Cross-repo wikis You _can_ use this action to deploy your octocat/mega-docs repository to the @@ -208,6 +191,5 @@ between your IDE and the PR, but it's the easiest way to test the action. [Decathlon/wiki-page-creator-action#11]: https://github.com/Decathlon/wiki-page-creator-action/issues/11 [supported markup languages]: https://github.com/github/markup#markups -[Strip MarkDown extensions from links action]: https://github.com/marketplace/actions/strip-markdown-extensions-from-links-action [PAT]: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token From df95057733c1ef705500029d7d8acd7d0adb6a2d Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 03:18:18 -0500 Subject: [PATCH 22/24] Create .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bc9befa --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +cliw eol=lf From 10944fae48d64b803c456055fc04becf02caeea7 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 03:18:41 -0500 Subject: [PATCH 23/24] Update test-action.yml --- .github/workflows/test-action.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test-action.yml b/.github/workflows/test-action.yml index 48376ee..346eb7c 100644 --- a/.github/workflows/test-action.yml +++ b/.github/workflows/test-action.yml @@ -6,6 +6,7 @@ on: branches: "master" paths-ignore: - .gitignore + - .gitattributes - LICENSE - README.md - .github/** @@ -13,6 +14,7 @@ on: pull_request: paths-ignore: - .gitignore + - .gitattributes - LICENSE - README.md - .github/** From e17592a6d69b4e1893d638e5f49a98627a2a8f72 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Sun, 16 Jul 2023 03:20:08 -0500 Subject: [PATCH 24/24] Update .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index bc9befa..6457f67 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ cliw eol=lf +cli.ts eol=lf