All files ghIssuesHandling.js

86.3% Statements 63/73
76.09% Branches 35/46
100% Functions 6/6
86.3% Lines 63/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1783x 3x     100x 100x 100x   100x 106x 106x 106x       100x       55x                               55x                       55x 55x 55x 2x 2x       53x 53x         53x   53x         53x 1x     53x   53x 1x 1x     52x 52x   1x 1x     51x 51x 51x     1x 1x       50x 50x       50x 50x       50x   100x       100x 102x   100x 71x   29x       29x       29x       50x 100x       100x 108x           100x 78x   22x       22x       22x       50x   1x 1x     49x                         3x  
const core   = require( '@actions/core' )
const github = require( '@actions/github' )
 
function sliceGHInput( rawText ) {
    let slicesResult      = []
    let currentParameters = rawText
    let snippet           = null
    
    while( ( snippet = /(?:,\s?)*(?<paramValue>[a-zA-Z1-9-]+)/g.exec( currentParameters ) ) ) {
        Eif( snippet.groups.paramValue ) {
            slicesResult.push( snippet.groups.paramValue )
            currentParameters = currentParameters.replace( snippet.groups.paramValue, '' )
        }
    }
    
    return slicesResult
}
 
async function handleIssues( useSubtaskMode, DEBUG ) {
    const actionPossible   = [ "opened",
                               "edited",
                               "deleted",
                               "transferred",
                               "pinned",
                               "unpinned",
                               "closed",
                               "reopened",
                               "assigned",
                               "unassigned",
                               "labeled",
                               "unlabeled",
                               "locked",
                               "unlocked",
                               "milestoned",
                               "demilestoned" ]
    const actionToConsider = [ "opened",
                               "edited",
                               "deleted",
                               "closed",
                               "reopened",
                               "assigned",
                               "unassigned",
                               "labeled",
                               "unlabeled",
                               "milestoned",
                               "demilestoned" ]
    
    try {
        const jiraProjectKey = core.getInput( 'JIRA_PROJECTKEY' )
        if( !jiraProjectKey ) {
            console.log( '==> action skipped -- no project key(s)' )
            return null
        }
        
        
        const jiraIssueTypeName = core.getInput( 'JIRA_ISSUETYPE_NAME' )
        Iif( !jiraIssueTypeName ) {
            console.log( '==> action skipped -- no issue type name(s)' )
            return null
        }
        
        const changeEvent = github.context.payload
        
        Iif( !changeEvent.issue ) {
            throw Error(
                'This action was not triggered by a Github Issue.\nPlease ensure your GithubAction is triggered only when an Github Issue is changed' )
        }
        
        if( actionPossible.indexOf( changeEvent.action ) === -1 ) {
            core.warning( `The Github Issue event ${ changeEvent.action } is not supported.\nPlease try raising an issue at \nhttps://github.com/b-yond-infinite-network/sync-jira-subtask-to-gh-issues-action/issues` )
        }
    
        DEBUG( changeEvent )
    
        if( actionToConsider.indexOf( changeEvent.action ) === -1 ) {
            console.log( `==> action skipped for unsupported event ${ changeEvent.action }` )
            return null
        }
        
        console.log( '-- retrieving all changes' )
        if( changeEvent.action === 'edited'
            && !changeEvent.changes ) {
            console.log( `==> action skipped for event ${ changeEvent.action } due to empty change set ${ JSON.stringify( changeEvent ) }` )
            return null
        }
        
        console.log( '-- retrieving all labels' )
        const issueDetails = changeEvent.issue
        if( !issueDetails.labels
            || issueDetails.labels.length < 1
            || ( issueDetails.labels.length === 1 && issueDetails.labels[ 0 ].name === '' ) ) {
            console.log( `==> action skipped for event ${ changeEvent.action } - no labels found at all` )
            return null
        }
        
        
        const arrProjectKeys = sliceGHInput( jiraProjectKey )
        Iif( arrProjectKeys.length === 0 ) {
            arrProjectKeys.push( jiraProjectKey )
        }
        
        const arrIssueTypes = sliceGHInput( jiraIssueTypeName )
        Iif( arrIssueTypes.length === 0 ) {
            arrIssueTypes.push( jiraIssueTypeName )
        }
        
        const jiraAttachKeys = useSubtaskMode
                               ? issueDetails.labels.reduce( ( currentKeysAndType, currentLabel ) => {
                Iif( !currentLabel.name ) {
                    console.log( '--- some label have no name' )
                    return currentKeysAndType
                }
                const idxFoundProject = arrProjectKeys.findIndex( currentProjectKey =>
                                                                      currentLabel.name.startsWith( currentProjectKey +
                                                                                                    '-' ) )
                if( idxFoundProject === -1 ) {
                    return currentKeysAndType
                }
                const indexIssueTypeToUse = idxFoundProject >= arrIssueTypes.length
                                            ? arrIssueTypes.length - 1
                                            : idxFoundProject
                
                currentKeysAndType.push( {
                                             jiraIssueKey:  currentLabel.name,
                                             jiraIssueType: arrIssueTypes[ indexIssueTypeToUse ],
                                         } )
                return currentKeysAndType
            }, [] )
                               : []
        
        const jiraReplaceKeys = issueDetails.labels.reduce( ( currentKeysAndType, currentLabel ) => {
            Iif( !currentLabel.name ) {
                console.log( '--- some label have no name' )
                return currentKeysAndType
            }
            const idxFoundProject = arrProjectKeys.findIndex( currentProjectKey =>
                                                                  ( useSubtaskMode
                                                                    ? currentLabel.name.startsWith( 'own' +
                                                                                                    currentProjectKey +
                                                                                                    '-' )
                                                                    : currentLabel.name.startsWith( currentProjectKey +
                                                                                                    '-' ) ) )
            if( idxFoundProject === -1 ) {
                return currentKeysAndType
            }
            const indexIssueTypeToUse = idxFoundProject >= arrIssueTypes.length
                                        ? arrIssueTypes.length - 1
                                        : idxFoundProject
            
            currentKeysAndType.push( {
                                         jiraIssueKey:  currentLabel.name,
                                         jiraIssueType: arrIssueTypes[ indexIssueTypeToUse ],
                                     } )
            return currentKeysAndType
        }, [] )
        
        // console.log( `-- labeled: ${ JSON.stringify( jiraIDS ) }` )
        if( jiraAttachKeys.length === 0 &&
            jiraReplaceKeys.length === 0 ) {
            console.log( `==> action skipped for event ${ changeEvent.action } - no labels found starting with the project key -- ignoring all labels` )
            return null
        }
        
        return {
            event:              changeEvent.action,
            jiraKeysToAttachTo: jiraAttachKeys,
            jiraKeysToReplace:  jiraReplaceKeys,
            changes:            changeEvent.changes,
            details:            issueDetails,
        }
        
    } catch( error ) {
        core.setFailed( error.message )
    }
}
 
module.exports = handleIssues