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:zx@8.3.2"
@@ -51,13 +51,6 @@ const CargoMetadataSchema = z.object({
5151
5252type 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-
6154const 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 ( / ^ h t t p s : \/ \/ g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ?) (?: \. g i t ) ? \/ ? $ / )
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 ( )
165166const docsUrlPromise = fetch ( docsUrl , { method : "HEAD" } )
166167const 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
169176const docsUrlHead = await docsUrlPromise
170177const 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
193186const badges : Badge [ ] = [ ]
194187if ( 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}
197190if ( docsUrlIs200 ) {
198191 badges . push ( badge ( "Documentation" , `https://docs.rs/${ name } /badge.svg` , docsUrl ) )
0 commit comments