Skip to content

Commit cf43070

Browse files
committed
scripts(feat[docs]) Add delete-preview to docs.lua
why: docs-preview-cleanup.yml checked the pull request number with a bash regex and then ran aws s3 rm in one block. SCRIPTS.md keeps only workflow run lines that call one command. what: - Add docs.preview and docs.lua delete-preview --bucket NAME --pr NUMBER. It refuses a number that is not all digits and an unset bucket before it starts anything, then runs `aws s3 rm s3://BUCKET/en/lua/pr-NUMBER/ --recursive` through support.proc. - Test the command, the refusals and a failing aws with an injected runner, and document the command. Refs #113
1 parent 6cba76d commit cf43070

4 files changed

Lines changed: 125 additions & 1 deletion

File tree

‎scripts/docs/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ the READMEs quote what the gates run.
77
| --- | --- |
88
| `export` | Runs the pinned LuaLS over a copy of `lua/`, adds the public declarations, the guides, the examples and the validated complete API examples, and writes `api.json` and the bundles. `--check` validates without writing. |
99
| `readmes` | Checks each README's logo block, that every Lua block is an example or excerpt of a file the gates run, console output against the examples manifest, prose length and links. |
10+
| `delete-preview --bucket NAME --pr NUMBER` | Removes one closed pull request's preview, `en/lua/pr-NUMBER/`, from the site bucket with `aws s3 rm`. It refuses a number that is not all digits before it runs anything. |
1011

1112
## Who runs it
1213

1314
- The `types` gate runs `export --check`; the `docs` gate runs `readmes`.
1415
- The `docs` workflow runs `export` for the documentation build.
16+
- The `docs-preview-cleanup` workflow runs `delete-preview` when a pull request closes.
1517

1618
The Python `export-docs` this replaced is `git show 8dc4642~1:scripts/export-docs`. The comparison and its result (the exports differ only in exporter.name) are in
1719
[libtmux-lua#106](https://github.com/libtmux/libtmux-lua/issues/106#issuecomment-6907114327).
@@ -36,5 +38,6 @@ Run the tests:
3638
$ luajit scripts/support/run_tests.lua \
3739
scripts/docs/tests/test_api_examples.lua \
3840
scripts/docs/tests/test_export.lua \
41+
scripts/docs/tests/test_preview.lua \
3942
scripts/docs/tests/test_readme_examples.lua
4043
```

‎scripts/docs/docs.lua‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
--
44
-- luajit scripts/docs/docs.lua export [--source DIR] [--luals PATH] [--output DIR] [--check]
55
-- luajit scripts/docs/docs.lua readmes [--source DIR]
6+
-- luajit scripts/docs/docs.lua delete-preview --bucket NAME --pr NUMBER
67
--
78
-- `export` writes api.json and the guide and example bundles; with --check it only validates. It
89
-- needs the pinned LuaLS that scripts/bootstrap/bootstrap.lua luals installs. `readmes` exits 1 and
9-
-- names the first README that differs from its sources. See README.md.
10+
-- names the first README that differs from its sources. `delete-preview` removes a closed pull
11+
-- request's preview from the site bucket and is run only by the docs-preview-cleanup workflow. See
12+
-- README.md.
1013
local here = debug.getinfo(1, "S").source:match("^@(.*)/[^/]*$") or "."
1114
package.path = here .. "/../?.lua;" .. package.path
1215
local repo = require("support.repo")
@@ -25,6 +28,10 @@ export:option("--output", "where to write the export"):default(repo.root .. "/do
2528
export:flag("--check", "validate without writing")
2629
local readmes = cli:command("readmes", "Check the READMEs against their sources.")
2730
readmes:option("--source", "the checkout to check"):default(repo.root)
31+
local delete =
32+
cli:command("delete-preview", "Delete a closed pull request's documentation preview.")
33+
delete:option("--bucket", "the site bucket"):count(1)
34+
delete:option("--pr", "the pull request number"):count(1)
2835
local parsed, arguments = cli:pparse({ ... })
2936
if not parsed then
3037
io.stderr:write(cli:get_usage(), "\ndocs.lua: error: ", arguments, "\n")
@@ -39,6 +46,9 @@ local ok, failure = pcall(function()
3946
output = arguments.output,
4047
check = arguments.check,
4148
}))
49+
elseif arguments.target == "delete-preview" then
50+
require("docs.preview").delete(arguments.bucket, arguments.pr)
51+
print(("PASS deleted the preview of pull request %s"):format(arguments.pr))
4252
else
4353
require("docs.readme_examples").check_readmes(arguments.source)
4454
print("README check PASS")

‎scripts/docs/preview.lua‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-- Delete the documentation preview of a closed pull request from the site bucket.
2+
local M = {}
3+
4+
--- Where the Lua previews live under the bucket, one directory per pull request.
5+
M.PREFIX = "en/lua/pr-"
6+
7+
---@param message string
8+
local function refuse(message)
9+
error(message, 0)
10+
end
11+
12+
--- The `aws s3 rm` command that removes one pull request's preview.
13+
---@param bucket? string
14+
---@param number? string
15+
---@return string[]
16+
function M.delete_command(bucket, number)
17+
if type(number) ~= "string" or not number:match("^%d+$") then
18+
refuse("pull request number must be numeric")
19+
end
20+
if bucket == nil or bucket == "" then
21+
refuse("the documentation bucket is not set")
22+
end
23+
return { "aws", "s3", "rm", ("s3://%s/%s%s/"):format(bucket, M.PREFIX, number), "--recursive" }
24+
end
25+
26+
---@param argv string[]
27+
---@return support.RunResult
28+
local function run_attached(argv)
29+
return require("support.proc").run(argv, { inherit = true })
30+
end
31+
32+
--- Deletes the preview, refusing a bad number before anything runs.
33+
---@param bucket? string
34+
---@param number? string
35+
---@param runner? fun(argv: string[]): support.RunResult
36+
function M.delete(bucket, number, runner)
37+
local command = M.delete_command(bucket, number)
38+
local result = (runner or run_attached)(command)
39+
if result.code ~= 0 then
40+
refuse("aws s3 rm failed with status " .. result.code)
41+
end
42+
end
43+
44+
return M
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
-- The documentation preview cleanup refuses a bad pull request number and runs the one command.
2+
local test = require("luaunit")
3+
local preview = require("docs.preview")
4+
5+
local M = {}
6+
7+
function M.test_the_command_removes_one_pull_requests_preview()
8+
test.assertEquals(
9+
preview.delete_command("site-bucket", "42"),
10+
{ "aws", "s3", "rm", "s3://site-bucket/en/lua/pr-42/", "--recursive" }
11+
)
12+
end
13+
14+
function M.test_a_number_that_is_not_digits_is_refused()
15+
for _, bad in ipairs({ "", "4a", "-1", "1/..", "1 2", "1\n" }) do
16+
test.assertErrorMsgContains(
17+
"pull request number must be numeric",
18+
preview.delete_command,
19+
"site-bucket",
20+
bad
21+
)
22+
end
23+
test.assertErrorMsgContains(
24+
"pull request number must be numeric",
25+
preview.delete_command,
26+
"site-bucket",
27+
nil
28+
)
29+
end
30+
31+
function M.test_an_unset_bucket_is_refused()
32+
test.assertErrorMsgContains(
33+
"the documentation bucket is not set",
34+
preview.delete_command,
35+
"",
36+
"42"
37+
)
38+
test.assertErrorMsgContains(
39+
"the documentation bucket is not set",
40+
preview.delete_command,
41+
nil,
42+
"42"
43+
)
44+
end
45+
46+
function M.test_delete_runs_the_command_and_reports_a_failure()
47+
local ran
48+
preview.delete("site-bucket", "7", function(argv)
49+
ran = argv
50+
return { code = 0 }
51+
end)
52+
test.assertEquals(ran[4], "s3://site-bucket/en/lua/pr-7/")
53+
test.assertErrorMsgContains("status 3", preview.delete, "site-bucket", "7", function()
54+
return { code = 3 }
55+
end)
56+
end
57+
58+
function M.test_a_refused_number_runs_nothing()
59+
local ran = false
60+
pcall(preview.delete, "site-bucket", "x", function()
61+
ran = true
62+
return { code = 0 }
63+
end)
64+
test.assertFalse(ran)
65+
end
66+
67+
return M

0 commit comments

Comments
 (0)