33*/
44const Jira = require ( '../Jira' )
55const _ = require ( 'lodash' )
6+ const moment = require ( 'moment' )
67const { createLinkToIssue } = require ( '../common/utils' )
78
89function jiraIssuesBlockMacro ( context ) {
@@ -15,7 +16,9 @@ function jiraIssuesBlockMacro (context) {
1516 const jiraProject = parent . applySubstitutions ( target , [ 'attributes' ] )
1617 const jql = attrs . jql || ( jiraProject === undefined ? 'resolution="Unresolved" ORDER BY priority DESC, key ASC, duedate ASC' : `project = ${ jiraProject } AND resolution="Unresolved" ORDER BY priority DESC, key ASC, duedate ASC` )
1718 const customFields = attrs . customFieldIds || 'priority,created,assignee,issuetype,summary'
19+ doc . getLogger ( ) . info ( `customFields: ${ customFields } ` )
1820 const customFieldIds = customFields . split ( ',' ) . map ( customField => customField . split ( '.' ) [ 0 ] )
21+ doc . getLogger ( ) . info ( `customFieldIds: ${ customFieldIds } ` )
1922 const logger = context . logger
2023 const jiraConfig = require ( '../common/jiraConfig' ) . createConfig ( '' , doc , jiraProject , attrs , logger )
2124 const jiraClient = new Jira ( doc , jiraConfig . auth , jiraConfig . baseUrl )
@@ -30,8 +33,8 @@ function jiraIssuesBlockMacro (context) {
3033 content . push ( '|====' )
3134 content . push ( '|' + headers . join ( '|' ) )
3235
33- for ( let i = 0 ; i < issues . length ; i ++ ) {
34- const issue = issues [ i ]
36+ for ( const element of issues ) {
37+ const issue = element
3538 let idColumn = 'a|'
3639 if ( issue . fields . issuetype && customFieldIds . includes ( 'issuetype' ) ) {
3740 const issueTypeName = issue . fields . issuetype . name
@@ -43,24 +46,31 @@ function jiraIssuesBlockMacro (context) {
4346 idColumn += `${ createLinkToIssue ( jiraConfig . baseUrl , issue . key ) } [${ issue . key } ]`
4447 content . push ( idColumn )
4548
46- for ( let j = 0 ; j < customFieldsArray . length ; j ++ ) {
49+ for ( const element of customFieldsArray ) {
4750 let value
48- if ( ! _ . has ( issue . fields , customFieldsArray [ j ] ) ) {
49- logger . warn ( `Examining issue '${ JSON . stringify ( issue , null , 2 ) } ' for custom field '${ customFieldsArray [ j ] } ', but was not found.` )
51+ if ( ! _ . has ( issue . fields , element ) ) {
52+ logger . warn ( `Examining issue '${ JSON . stringify ( issue , null , 2 ) } ' for custom field '${ element } ', but was not found.` )
5053 value = '-'
5154 } else {
52- value = _ . get ( issue . fields , customFieldsArray [ j ] )
55+ value = _ . get ( issue . fields , element )
5356 if ( value !== null && ( value . constructor === Array ) ) {
5457 value = value . toString ( )
5558 } else if ( ( typeof value === 'object' ) && value != null ) {
56- value = value . name || value . displayName || doc . getAttribute ( `jira-table-${ customFieldsArray [ j ] . replace ( / \. / g, '-' ) } -default` , '-' )
59+ value = value . name || value . displayName || doc . getAttribute ( `jira-table-${ element . replace ( / \. / g, '-' ) } -default` , '-' )
5760 } else {
58- value = value || doc . getAttribute ( `jira-table-${ customFieldsArray [ j ] . replace ( / \. / g, '-' ) } -default` , '-' )
61+ value = value || doc . getAttribute ( `jira-table-${ element . replace ( / \. / g, '-' ) } -default` , '-' )
5962 }
6063 }
6164 if ( typeof value === 'number' ) {
6265 content . push ( '|' + value )
6366 } else {
67+ const inputFormatAttr = `jira-${ element . replace ( / \. / g, '-' ) } -input-format`
68+ const outputFormatAttr = `jira-${ element . replace ( / \. / g, '-' ) } -output-format`
69+ const inputFormat = doc . getAttribute ( inputFormatAttr )
70+ if ( inputFormat ) {
71+ const outputFormat = doc . getAttribute ( outputFormatAttr )
72+ value = formatValue ( doc , value , inputFormat , outputFormat )
73+ }
6474 content . push ( '|' + value . replace ( / \| / g, '\\|' ) )
6575 }
6676 }
@@ -74,6 +84,16 @@ function jiraIssuesBlockMacro (context) {
7484 }
7585}
7686
87+ function formatValue ( doc , value , inputFormat , outputFormat ) {
88+ if ( typeof value === 'string' ) {
89+ const parsedValue = moment ( value , inputFormat , true )
90+ if ( parsedValue . isValid ( ) ) {
91+ return parsedValue . format ( outputFormat )
92+ }
93+ }
94+ return value
95+ }
96+
7797function createHeaders ( doc , customFieldIds ) {
7898 const headers = [ ]
7999 const customFieldsArray = customFieldIds . split ( ',' ) . filter ( item => item !== 'issuetype' )
0 commit comments