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
44import * as zx from "npm:[email protected] " @@ -52,13 +52,6 @@ const CargoMetadataSchema = z.object({
5252
5353type 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-
6255const 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 ( / ^ h t t p s : \/ \/ g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ?) (?: \. g i t ) ? \/ ? $ / )
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 ( )
168169const docsUrlPromise = fetch ( docsUrl , { method : "HEAD" } )
169170const 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
172179const docsUrlHead = await docsUrlPromise
173180const 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
196189const badges : Badge [ ] = [ ]
197190if ( isPublicGitHubRepo ) {
0 commit comments