Skip to content

Commit

Permalink
fix: migrate to parse-git-url
Browse files Browse the repository at this point in the history
closes #60
  • Loading branch information
antongolub committed Oct 30, 2019
1 parent 031a165 commit fde21d4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@
"typings"
],
"dependencies": {
"@types/git-url-parse": "^9.0.0",
"aggregate-error": "^3.0.1",
"dot": "^1.1.2",
"execa": "^3.2.0",
"gh-pages": "^2.1.1",
"git-url-parse": "^11.1.2",
"lodash": "^4.17.15",
"read-pkg": "^5.2.0",
"sync-request": "^6.1.0",
Expand Down
13 changes: 8 additions & 5 deletions src/main/ts/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/** @module semantic-release-gh-pages-plugin */

import gitParse from 'git-url-parse'
import { castArray, get, omit } from 'lodash'
import readPkg from 'read-pkg'
import request from 'sync-request'
import { IGhpagesPluginConfig, TAnyMap, TContext } from './interface'
import { catchToSmth } from './util'
import {
DEFAULT_BRANCH,
DEFAULT_DST,
Expand All @@ -22,29 +24,30 @@ export {
PLUGIN_PATH
}

export const GITIO_REPO_PATTERN = /^https:\/\/git\.io\/[A-Za-z0-9-]+$/
const gitUrlParse = catchToSmth(gitParse, {})

export const REPO_PATTERN = /^(?:[\w+]+:?\/\/)?(?:(\w+)@)?([\w-.]+\.\w+)[/:]([\w.-]+\/[\w.-]+?)(?:\.git)?$/
export const GITIO_REPO_PATTERN = /^https:\/\/git\.io\/[A-Za-z0-9-]+$/

/**
* @private
*/
export const extractRepoName = (repoUrl: string): string => {
return (REPO_PATTERN.exec(repoUrl) || [])[3]
return gitUrlParse(repoUrl).full_name
}

/**
* @private
*/
export const extractRepoDomain = (repoUrl: string): string => {
return (REPO_PATTERN.exec(repoUrl) || [])[2]
return gitUrlParse(repoUrl).resource
}

/**
* @private
*/
export const extractRepoToken = (repoUrl: string): string => {
return (REPO_PATTERN.exec(repoUrl) || [])[1]
const repo = gitUrlParse(repoUrl)
return repo.token || repo.user
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const catchToSmth = (fn: Function, smth?: any) => {
return (...args: any[]) => {
try {
return fn(...args)
} catch {

return smth
}
}
}
17 changes: 8 additions & 9 deletions src/test/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,18 @@ describe('config', () => {
['https://github.com/qiwi/FormattableTextView.git', 'qiwi/FormattableTextView'],
['https://github.com/tesT123/R.e-po.git', 'tesT123/R.e-po'],
['https://github.com/tesT123%%/foo.git', undefined],
['https://github.com/foo/bar/baz.git', undefined],
['https://github.com/foo/bar/baz.git', 'foo/bar/baz'],
['git+https://github.com/qiwi/uniconfig.git', 'qiwi/uniconfig'],
['git@github.com:qiwi/consul-service-discovery.git', 'qiwi/consul-service-discovery'],
['ssh://git@github.com:qiwi/consul-service-discovery.git', 'qiwi/consul-service-discovery'],
['https://github.qiwi.com/qiwi/foo.git', 'qiwi/foo'],
['http://github.qiwi.com/qiwi/foo.git', 'qiwi/foo'],
['http://github.qi&wi.com/qiwi/foo.git', undefined],
['github.qiwi.com/qiwi/foo', 'qiwi/foo'],
['qiwigithub.com/qiwi/foo.git', 'qiwi/foo'],
// ['http://github.qi&wi.com/qiwi/foo.git', undefined],
// ['github.qiwi.com/qiwi/foo', 'qiwi/foo'],
['https://qiwigithub.com/qiwi/foo.git', 'qiwi/foo'],
['https://qiwigithub.ru/qiwi/foo.git', 'qiwi/foo'],
['qiwigithub.com/qiwi/foo', 'qiwi/foo'],
['qiwigithub/qiwi/bar.git', undefined],
['git@github.qiwi.com:m-pismenskiy/semrel.git', 'm-pismenskiy/semrel'],
// ['qiwigithub/qiwi/bar.git', undefined],
['', undefined]
]

Expand All @@ -264,13 +263,13 @@ describe('config', () => {

it('#extractRepoDomain returns proper values', () => {
const cases: Array<[string, string?]> = [
['asd.com/qiwi/foo.git', 'asd.com'],
['git@asd.com:m-pismenskiy/semrel.git', 'asd.com'],
['https://qiwi.com/qiwi/foo.git', 'qiwi.com'],
['http://qiwi.github.com/qiwi/foo.git', 'qiwi.github.com'],
['http://barbar.ru/qiwi/foo.git', 'barbar.ru'],
['git+http://barfoo.ru/qiwi/foo.git', 'barfoo.ru'],
['git+http://bar-foo.ru/qiwi/foo.git', 'bar-foo.ru'],
['http://bar/qiwi/foo.git', undefined]
['http://bar/qiwi/foo.git', 'bar']
]

cases.forEach(([input, result]) => expect(extractRepoDomain(input)).toBe(result))
Expand All @@ -289,7 +288,7 @@ describe('config', () => {
},
cwd,
env: {
REPO_URL: 'qiwigithub.com/qiwi/foo.git',
REPO_URL: 'git@qiwigithub.com:qiwi/foo.git',
GH_TOKEN: 'foo'
}
},
Expand Down
55 changes: 55 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@
resolved "https://registry.yarnpkg.com/@types/gh-pages/-/gh-pages-2.0.1.tgz#d2a25eb84375053c713cddf304c28bed6ca38388"
integrity sha512-Z5CJlEg6xTfLLChYb4/UpYLcN9djv2HzTDz8XJ9I8tasCw8ybkGoA2x+CBU9gyRvi1VeBmdLfcGrEDVYm1L36g==

"@types/git-url-parse@^9.0.0":
version "9.0.0"
resolved "https://registry.yarnpkg.com/@types/git-url-parse/-/git-url-parse-9.0.0.tgz#aac1315a44fa4ed5a52c3820f6c3c2fb79cbd12d"
integrity sha512-kA2RxBT/r/ZuDDKwMl+vFWn1Z0lfm1/Ik6Qb91wnSzyzCDa/fkM8gIOq6ruB7xfr37n6Mj5dyivileUVKsidlg==

"@types/glob@^7.1.1":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.1.tgz#aa59a1c6e3fbc421e07ccd31a944c30eba521575"
Expand Down Expand Up @@ -2589,6 +2594,21 @@ git-log-parser@^1.2.0:
through2 "~2.0.0"
traverse "~0.6.6"

git-up@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/git-up/-/git-up-4.0.1.tgz#cb2ef086653640e721d2042fe3104857d89007c0"
integrity sha512-LFTZZrBlrCrGCG07/dm1aCjjpL1z9L3+5aEeI9SBhAqSc+kiA9Or1bgZhQFNppJX6h/f5McrvJt1mQXTFm6Qrw==
dependencies:
is-ssh "^1.3.0"
parse-url "^5.0.0"

git-url-parse@^11.1.2:
version "11.1.2"
resolved "https://registry.yarnpkg.com/git-url-parse/-/git-url-parse-11.1.2.tgz#aff1a897c36cc93699270587bea3dbcbbb95de67"
integrity sha512-gZeLVGY8QVKMIkckncX+iCq2/L8PlwncvDFKiWkBn9EtCfYDbliRTTp6qzyQ1VMdITUfq7293zDzfpjdiGASSQ==
dependencies:
git-up "^4.0.0"

glob-parent@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.0.0.tgz#1dc99f0f39b006d3e92c2c284068382f0c20e954"
Expand Down Expand Up @@ -3213,6 +3233,13 @@ is-retry-allowed@^1.0.0:
resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz#11a060568b67339444033d0125a61a20d564fb34"
integrity sha1-EaBgVotnM5REAz0BJaYaINVk+zQ=

is-ssh@^1.3.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/is-ssh/-/is-ssh-1.3.1.tgz#f349a8cadd24e65298037a522cf7520f2e81a0f3"
integrity sha512-0eRIASHZt1E68/ixClI8bp2YK2wmBPVWEismTs6M+M099jKgrzl/3E976zIbImSIob48N2/XGe9y7ZiYdImSlg==
dependencies:
protocols "^1.1.0"

is-stream@^1.0.0, is-stream@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
Expand Down Expand Up @@ -4685,6 +4712,11 @@ normalize-url@^1.0.0:
query-string "^4.1.0"
sort-keys "^1.0.0"

normalize-url@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-3.3.0.tgz#b2e1c4dc4f7c6d57743df733a4f5978d18650559"
integrity sha512-U+JJi7duF1o+u2pynbp2zXDW2/PADgC30f0GsHZtRh+HOcXHnw137TrNlyxxRvWW5fjKd3bcLHPxofWuCjaeZg==

normalize-url@^4.0.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-4.3.0.tgz#9c49e10fc1876aeb76dba88bf1b2b5d9fa57b2ee"
Expand Down Expand Up @@ -5258,6 +5290,24 @@ parse-json@^5.0.0:
json-parse-better-errors "^1.0.1"
lines-and-columns "^1.1.6"

parse-path@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/parse-path/-/parse-path-4.0.1.tgz#0ec769704949778cb3b8eda5e994c32073a1adff"
integrity sha512-d7yhga0Oc+PwNXDvQ0Jv1BuWkLVPXcAoQ/WREgd6vNNoKYaW52KI+RdOFjI63wjkmps9yUE8VS4veP+AgpQ/hA==
dependencies:
is-ssh "^1.3.0"
protocols "^1.4.0"

parse-url@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/parse-url/-/parse-url-5.0.1.tgz#99c4084fc11be14141efa41b3d117a96fcb9527f"
integrity sha512-flNUPP27r3vJpROi0/R3/2efgKkyXqnXwyP1KQ2U0SfFRgdizOdWfvrrvJg1LuOoxs7GQhmxJlq23IpQ/BkByg==
dependencies:
is-ssh "^1.3.0"
normalize-url "^3.3.0"
parse-path "^4.0.0"
protocols "^1.4.0"

parse5@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"
Expand Down Expand Up @@ -5459,6 +5509,11 @@ proto-list@~1.2.1:
resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849"
integrity sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=

protocols@^1.1.0, protocols@^1.4.0:
version "1.4.7"
resolved "https://registry.yarnpkg.com/protocols/-/protocols-1.4.7.tgz#95f788a4f0e979b291ffefcf5636ad113d037d32"
integrity sha512-Fx65lf9/YDn3hUX08XUc0J8rSux36rEsyiv21ZGUC1mOyeM3lTRpZLcrm8aAolzS4itwVfm7TAPyxC2E5zd6xg==

protoduck@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/protoduck/-/protoduck-5.0.1.tgz#03c3659ca18007b69a50fd82a7ebcc516261151f"
Expand Down

0 comments on commit fde21d4

Please sign in to comment.