-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen.ts
71 lines (57 loc) · 1.87 KB
/
open.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Args, ux } from '@oclif/core'
import * as open from 'open'
import { BaseCommand } from '../base'
import { prefixStory, trimTrailingSlash } from '../utils'
export default class Open extends BaseCommand<typeof Open> {
static description = 'Open the active story in Jira.'
static examples = [
`$ story open
Opening https://something.atlassian.net/browse/SM-12
`,
`$ story open 42
Opening https://something.atlassian.net/browse/SM-42
`,
`$ story open TS-19
Opening https://something.atlassian.net/browse/TS-19
`
]
static aliases = ['jira']
static args = {
story: Args.string({
required: false,
description: 'Open this story, instead of the current story.'
})
}
private getBaseUrl = async () => {
let url = await (await this.userConfig).get('jiraUrl')
/* eslint-disable no-await-in-loop */
while (!url) {
url = await ux.prompt('What is your Jira site URL?', { required: true })
if (!url || !url.toLowerCase().startsWith('http')) {
this.warn('That doesn\'t appear to be a valid URL.')
url = ''
continue
}
const userConfig = await this.userConfig
userConfig.set('jiraUrl', url)
await userConfig.write()
}
/* eslint-enable no-await-in-loop */
return url
}
async run() {
const { args: { story: storyOverride } } = await this.parse(Open)
const baseUrl = await this.getBaseUrl()
const story = await (async (): Promise<string | undefined> => {
if (storyOverride) {
const defaultProject = await (await this.userConfig).get('defaultProject')
return prefixStory(defaultProject, storyOverride)
}
const story = await this.getStory()
return story?.child ?? story?.parent
})()
const url = story ? `${trimTrailingSlash(baseUrl)}/browse/${story}` : baseUrl
this.log(`Opening ${url}`)
await open(url)
}
}