Skip to content

Commit

Permalink
Merge pull request #62 from mislav/version-extract
Browse files Browse the repository at this point in the history
Fix extracting version tags from GitHub download URLs
  • Loading branch information
mislav authored Sep 20, 2023
2 parents 6df4077 + be15a94 commit 1a58dc7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
50 changes: 37 additions & 13 deletions src/version-test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import test from 'ava'
import { fromUrl } from './version'
import { compare, fromUrl } from './version'

test('fromUrl()', (t) => {
t.is(
fromUrl('https://github.com/me/myproject/archive/refs/tags/v1.2.3.tar.gz'),
'v1.2.3'
)
t.is(
fromUrl(
'https://github.com/me/myproject/releases/download/v1.2.3/file.tgz'
),
'v1.2.3'
)
t.is(fromUrl('http://myproject.net/download/v1.2.3.tgz'), 'v1.2.3')
t.is(fromUrl('https://example.com/v1.2.3.zip'), 'v1.2.3')
const cases = new Map<string, string>([
[
'https://github.com/me/myproject/archive/refs/tags/v1.2.3.tar.gz',
'v1.2.3',
],
[
'https://github.com/me/myproject/releases/download/v1.2.3/file.tgz',
'v1.2.3',
],
['http://myproject.net/download/v1.2.3.tgz', 'v1.2.3'],
['https://example.com/v1.2.3.zip', 'v1.2.3'],
[
'https://github.com/SmartThingsCommunity/smartthings-cli/releases/download/%40smartthings%2Fcli%401.7.0/smartthings-macos-arm64.tar.gz',
'@smartthings/[email protected]',
],
[
'https://github.com/SmartThingsCommunity/smartthings-cli/releases/download/@smartthings/[email protected]/smartthings-macos-x64.tar.gz',
'@smartthings/[email protected]',
],
[
'https://github.com/orf/gping/archive/refs/tags/gping-v1.14.0.tar.gz',
'gping-v1.14.0',
],
])
for (const item of cases) {
t.is(fromUrl(item[0]), item[1], item[0])
}
})

test('compare()', (t) => {
t.is(compare('v1.2.0', 'v1.2.1'), -1)
t.is(compare('v1.2.0', 'v1.1.9.0'), 1)
t.is(compare('gping-v1.13', 'gping-v1.14.0'), -1)
t.is(compare('@smartthings/[email protected]', '@smartthings/[email protected]'), 1)
t.is(compare('@smartthings/[email protected]', '@smartthings/[email protected]'), 0)
t.is(compare('@smartthings/[email protected]', '@smartthings/[email protected]'), -1)
})
5 changes: 2 additions & 3 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ export function compare(v1: string, v2: string): -1 | 0 | 1 {
}

const ghDownloadRE =
/^https:\/\/github.com\/[^/]+\/[^/]+\/releases\/download\/([^/]+)/
/^https:\/\/github.com\/[^/]+\/[^/]+\/releases\/download\/(.+)\/[^/]+$/

// TODO: https://github.com/Homebrew/brew/blob/675e38b5e4fe0290fa05f65af23c9a82d3e7cc76/Library/Homebrew/version.rb#L225-L363
export function fromUrl(url: string): string {
const downloadMatch = url.match(ghDownloadRE)
if (downloadMatch) {
return downloadMatch[1]
return decodeURIComponent(downloadMatch[1])
}
return basename(url).replace(/\.(tar\.gz|tgz|zip)$/, '')
}

0 comments on commit 1a58dc7

Please sign in to comment.