Skip to content

Commit 2497e68

Browse files
Merge remote-tracking branch 'repoconf-rust-public-lib-template/main'
2 parents 6cf9ae6 + 4ce845d commit 2497e68

File tree

3 files changed

+1225
-26
lines changed

3 files changed

+1225
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# stub!() - a better todo!()
66

7+
[![Build](https://github.com/DenisGorbachev/stub-macro/actions/workflows/ci.yml/badge.svg)](https://github.com/DenisGorbachev/stub-macro)
78
[![Documentation](https://docs.rs/stub-macro/badge.svg)](https://docs.rs/stub-macro)
89

910
<!-- crate documentation start -->

README.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env -S deno run --allow-write --allow-read --allow-run=bash,git,cargo --allow-net=docs.rs:443 --allow-env --allow-sys --no-lock
1+
#!/usr/bin/env -S deno run --allow-write --allow-read --allow-run=bash,git,cargo --allow-net=docs.rs:443,github.com:443 --allow-env --allow-sys --no-lock
22

33
// NOTE: Pin the versions of the packages because the script runs without a lock file
44
import * as zx from "npm:zx@8.3.2"
@@ -51,13 +51,6 @@ const CargoMetadataSchema = z.object({
5151

5252
type CargoMetadata = z.infer<typeof CargoMetadataSchema>
5353

54-
const GitHubRepoSchema = z.object({
55-
url: z.string().url(),
56-
visibility: z.enum(["PUBLIC", "PRIVATE"]),
57-
})
58-
59-
type GitHubRepo = z.infer<typeof GitHubRepoSchema>
60-
6154
const BadgeSchema = z.object({
6255
name: z.string().min(1),
6356
image: z.string().url(),
@@ -92,6 +85,7 @@ const stub = <T>(message = "Implement me"): T => {
9285
* Examples:
9386
*
9487
* `normalizeGitRemoteUrl("git@github.com:DenisGorbachev/rust-private-template.git") == "https://github.com/DenisGorbachev/rust-private-template"`
88+
* `normalizeGitRemoteUrl("https://github.com/DenisGorbachev/rust-private-template.git") == "https://github.com/DenisGorbachev/rust-private-template"`
9589
*
9690
* @param url
9791
*/
@@ -103,7 +97,14 @@ const normalizeGitRemoteUrl = (url: string) => {
10397
return `https://github.com/${username}/${repo}`
10498
}
10599

106-
// Return original if not a GitHub SSH URL
100+
// Handle GitHub HTTPS format: https://github.com/username/repo(.git)
101+
const httpsMatch = url.match(/^https:\/\/github\.com\/([^/]+)\/([^/]+?)(?:\.git)?\/?$/)
102+
if (httpsMatch) {
103+
const [, username, repo] = httpsMatch
104+
return `https://github.com/${username}/${repo}`
105+
}
106+
107+
// Return original if not a GitHub URL we recognize
107108
return url
108109
}
109110

@@ -164,7 +165,13 @@ const crateDocsPlaceholder = `
164165
`.trim()
165166
const docsUrlPromise = fetch(docsUrl, {method: "HEAD"})
166167
const helpPromise = primaryBinTarget ? $`cargo run --quiet --bin ${primaryBinTarget.name} -- --help` : undefined
167-
const ghRepoViewPromise = $`gh repo view --json url,visibility ${theOriginUrl}`.nothrow().quiet()
168+
const isPublicGitHubRepoPromise = (async () => {
169+
if (!theOriginUrl.startsWith("https://github.com")) return false
170+
const response = await fetch(theOriginUrl, {method: "GET"})
171+
if (response.status === 200) return true
172+
if (response.status === 404) return false
173+
throw new Error(`Unexpected response status while checking GitHub repo visibility: ${response.status} ${response.statusText}`)
174+
})()
168175

169176
const docsUrlHead = await docsUrlPromise
170177
const docsUrlIs200 = docsUrlHead.status === 200
@@ -174,25 +181,11 @@ const insertCrateDocsIntoReadme = async (readmePath: string) => {
174181
await $`cargo insert-docs crate-into-readme --allow-dirty --link-to-latest --shrink-headings 0 --readme-path ${readmePath}`
175182
}
176183

177-
const theGitHubRepo = await (async () => {
178-
const output = await ghRepoViewPromise
179-
if (output.exitCode === 0) {
180-
return parse(GitHubRepoSchema, output)
181-
} else {
182-
const text = output.text()
183-
if (text.includes('argument error: expected the "[HOST/]OWNER/REPO" format')) {
184-
return null
185-
} else {
186-
throw new Error("Failure in ghRepoViewPromise: \n" + text)
187-
}
188-
}
189-
})()
190-
const isGitHubRepo = theGitHubRepo !== null
191-
const isPublicGitHubRepo = isGitHubRepo && theGitHubRepo.visibility === "PUBLIC"
184+
const isPublicGitHubRepo = await isPublicGitHubRepoPromise
192185

193186
const badges: Badge[] = []
194187
if (isPublicGitHubRepo) {
195-
badge("Build", `${theCargoToml.package.repository}/actions/workflows/ci.yml/badge.svg`, theCargoToml.package.repository)
188+
badges.push(badge("Build", `${theCargoToml.package.repository}/actions/workflows/ci.yml/badge.svg`, theCargoToml.package.repository))
196189
}
197190
if (docsUrlIs200) {
198191
badges.push(badge("Documentation", `https://docs.rs/${name}/badge.svg`, docsUrl))

0 commit comments

Comments
 (0)