Skip to content

Commit 1551b9f

Browse files
Merge remote-tracking branch 'repoconf-rust-public-lib-template/main'
2 parents 4fc515e + 6528f18 commit 1551b9f

File tree

2 files changed

+1223
-25
lines changed

2 files changed

+1223
-25
lines changed

README.ts

Lines changed: 18 additions & 25 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:[email protected]"
@@ -52,13 +52,6 @@ const CargoMetadataSchema = z.object({
5252

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

55-
const GitHubRepoSchema = z.object({
56-
url: z.string().url(),
57-
visibility: z.enum(["PUBLIC", "PRIVATE"]),
58-
})
59-
60-
type GitHubRepo = z.infer<typeof GitHubRepoSchema>
61-
6255
const BadgeSchema = z.object({
6356
name: z.string().min(1),
6457
image: z.string().url(),
@@ -93,6 +86,7 @@ const stub = <T>(message = "Implement me"): T => {
9386
* Examples:
9487
*
9588
* `normalizeGitRemoteUrl("[email protected]:DenisGorbachev/rust-private-template.git") == "https://github.com/DenisGorbachev/rust-private-template"`
89+
* `normalizeGitRemoteUrl("https://github.com/DenisGorbachev/rust-private-template.git") == "https://github.com/DenisGorbachev/rust-private-template"`
9690
*
9791
* @param url
9892
*/
@@ -104,7 +98,14 @@ const normalizeGitRemoteUrl = (url: string) => {
10498
return `https://github.com/${username}/${repo}`
10599
}
106100

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

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

172179
const docsUrlHead = await docsUrlPromise
173180
const docsUrlIs200 = docsUrlHead.status === 200
@@ -177,21 +184,7 @@ const insertCrateDocsIntoReadme = async (readmePath: string) => {
177184
await $`cargo insert-docs crate-into-readme --allow-dirty --link-to-latest --shrink-headings 0 --readme-path ${readmePath}`
178185
}
179186

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

196189
const badges: Badge[] = []
197190
if (isPublicGitHubRepo) {

0 commit comments

Comments
 (0)