Skip to content

Commit

Permalink
add new verbose mode
Browse files Browse the repository at this point in the history
+ fix caching issue
  • Loading branch information
ruettenm committed Jan 25, 2019
1 parent 40ab3b6 commit 3827780
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
20 changes: 13 additions & 7 deletions src/commands/listActivityCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const listActivityCommand = async (command: any) => {
const username = command.username || settingsStore.value('settings.username')
const maxResults = command.max || 500
const filter = command.filter
const verbose = command.verbose

if (filter) {
if (!['week', 'month'].includes(filter)) {
Expand All @@ -24,6 +25,10 @@ export const listActivityCommand = async (command: any) => {
}
}

if (verbose) {
console.log('Running in "verbose" mode.')
}

if (!hostname || !username) {
console.info('Please specify your username and hostname either per command options or defaults.')
console.info('jira-activity --help')
Expand All @@ -37,25 +42,26 @@ export const listActivityCommand = async (command: any) => {
name: 'password'
}) as any

const jiraSettings: JiraSettings = {
username,
password,
hostname
}
const jiraSettings: JiraSettings = { username, password, hostname }

spinner.start()
if (!verbose) {
spinner.start()
}
try {
const activities = await getActivities(
jiraSettings,
maxResults,
verbose,
filter ? moment().startOf(filter).unix() * 1000 : undefined
)
print(activities)
} catch (error) {
console.log(`\nOps, something went wrong: "${error.message}"`)
}

spinner.stop(true)
if (!verbose) {
spinner.stop(true)
}
}

process.exit(0)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ program
.option('-h, --hostname <hostname>', 'specifies the hostname which is used')
.option('-u, --username <username>', 'specifies the username which is used')
.option('-f, --filter <type>', 'filters the list to the current week or month [type: week or month]')
.option('-v, --verbose', 'run in verbose mode')
.option('-m, --max <number>', 'specifies the number of max results. The default is: 500', parseInt)
.description('Loads your activity and lists the parent issues you have worked on grouped by day')

Expand Down
49 changes: 39 additions & 10 deletions src/jira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,64 @@ export interface GroupedActivities {
}
}

export const getActivities = async (jiraSettings: JiraSettings, maxResults: number, fromDate?: number): Promise<GroupedActivities> => {
export const getActivities = async (jiraSettings: JiraSettings, maxResults: number, verbose: boolean, fromDate?: number): Promise<GroupedActivities> => {
const fromDateFilter = fromDate ? `&streams=update-date+AFTER+${fromDate}` : ''
const url = `activity?maxResults=${maxResults}&streams=user+IS+${encodeURIComponent(jiraSettings.username)}${fromDateFilter}&os_authType=basic`
const response = await requestWithCredentials(jiraSettings, url).buffer(true)
const response = await requestWithCredentials(jiraSettings, url, verbose).buffer(true)

if (verbose) {
console.log('\nStart converting the XML response to JS')
}

const body = await xml2js(response.text)

if (verbose) {
console.log('\nStart mapping the entries')
}

const activities = body.feed.entry.map((entry: any) => convertToActivityEntry(entry))

const result: GroupedActivities = {}
for (let activity of activities) {
if (activity) {
const parent = await getParentIssue(jiraSettings, activity)
const parent = await getIssue(jiraSettings, activity, verbose)

addToResult(result, parent || activity, activity.date)
addToResult(result, parent, activity.date)
}
}

return result
}

const getParentIssue = async (jiraSettings: JiraSettings, activity: ActivityEntry): Promise<CachedEntry | undefined> => {
const parentIssue = settingsStore.value(`issues.${activity.id}`)
if (parentIssue) {
return parentIssue
const getIssue = async (jiraSettings: JiraSettings, activity: ActivityEntry, verbose = false): Promise<CachedEntry> => {
const issueFromCache = settingsStore.value(`issues.${activity.id}`)
if (issueFromCache) {
if (verbose) {
console.log(`\nCache hit found for issue ${activity.id}`)
}
return issueFromCache
}

if (verbose) {
console.log(`\nSearching the parent issue for ${activity.id} in jira`)
}

const parentIssueFromJira = await getParentIssueFromJira(jiraSettings, activity)
if (parentIssueFromJira) {
if (verbose) {
console.log(`\nParent issue found for ${activity.id} > ${parentIssueFromJira.id}`)
}

settingsStore.setValue(`issues.${activity.id}`, parentIssueFromJira)
return parentIssueFromJira
}

return undefined
if (verbose) {
console.log(`\nSaving the result into the cache for issue ${activity.id}`)
}

settingsStore.setValue(`issues.${activity.id}`, activity)
return activity
}

const getParentIssueFromJira = async (jiraSettings: JiraSettings, activity: ActivityEntry): Promise<CachedEntry | undefined> => {
Expand Down Expand Up @@ -111,7 +136,11 @@ const addToResult = async (result: GroupedActivities, entry: CachedEntry, date:
}
}

const requestWithCredentials = (jiraSettings: JiraSettings, path: string) => {
const requestWithCredentials = (jiraSettings: JiraSettings, path: string, verbose = false) => {
if (verbose) {
console.log(`\nGET https://${jiraSettings.hostname}/${path}`)
}

return request
.get(`https://${jiraSettings.hostname}/${path}`)
.auth(jiraSettings.username, jiraSettings.password)
Expand Down
3 changes: 1 addition & 2 deletions src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { GroupedActivities } from './jira'

export const print = (activities: GroupedActivities) => {
for (let [ date, entries ] of Object.entries(activities).sort()) {
console.log(`${date}:`)
console.log(`\n${date}:`)
for (let [ issueKey, title ] of Object.entries(entries)) {
console.log(`${issueKey} - ${title}`)
}
console.log('\n')
}
}

0 comments on commit 3827780

Please sign in to comment.