@@ -80,6 +80,30 @@ export function getCommitsSinceTag(range, cwd, repository) {
8080 return entries
8181}
8282
83+ // Normalizes the `repository` field of package.json (which may be a plain
84+ // "owner/repo" shorthand string, a full git/https URL string, or an object
85+ // with a `url` property) into a plain "owner/repo" string suitable for
86+ // building GitHub URLs.
87+ export function normalizeRepository ( repository ) {
88+ const raw = typeof repository === 'string' ? repository : repository ?. url
89+ if ( ! raw ) {
90+ throw new Error ( 'Unable to determine the GitHub "owner/repo" from package.json\'s "repository" field.' )
91+ }
92+
93+ // Plain "owner/repo" shorthand, e.g. "github/remote-input-element".
94+ if ( / ^ [ ^ / \s : ] + \/ [ ^ / \s ] + $ / . test ( raw ) ) {
95+ return raw
96+ }
97+
98+ // A GitHub URL (git/https/ssh), e.g.
99+ // "git+https://github.com/owner/repo.git" or "git@github.com:owner/repo.git".
100+ const match = raw . match ( / (?: ^ | \/ \/ | @ ) g i t h u b \. c o m [: / ] ( [ ^ / \s ] + \/ [ ^ / \s ] + ?) (?: \. g i t ) ? \/ ? $ / )
101+ if ( ! match ) {
102+ throw new Error ( `Unable to parse a GitHub "owner/repo" from repository field: ${ JSON . stringify ( raw ) } ` )
103+ }
104+ return match [ 1 ]
105+ }
106+
83107function parseCommit ( { sha, subject, body} , repository ) {
84108 const shortSha = sha . slice ( 0 , 7 )
85109 const mergeMatch = subject . match ( / ^ M e r g e p u l l r e q u e s t # ( \d + ) f r o m / )
@@ -151,15 +175,16 @@ export function prepareRelease({cwd = process.cwd()} = {}) {
151175 }
152176
153177 const range = `${ tag } ..HEAD`
154- const entries = getCommitsSinceTag ( range , cwd , pkg . repository )
178+ const repository = normalizeRepository ( pkg . repository )
179+ const entries = getCommitsSinceTag ( range , cwd , repository )
155180 if ( entries . length === 0 ) {
156181 if ( existsSync ( autoChangesetPath ) ) {
157182 rmSync ( autoChangesetPath )
158183 }
159184 return { action : 'skipped-no-changes' , tag}
160185 }
161186
162- const content = buildChangesetContent ( pkg . name , entries , pkg . repository )
187+ const content = buildChangesetContent ( pkg . name , entries , repository )
163188 writeFileSync ( autoChangesetPath , content )
164189 return { action : 'created' , tag, path : autoChangesetPath , entries}
165190}
0 commit comments