All files ghHandling.js

87.18% Statements 68/78
84.62% Branches 44/52
100% Functions 7/7
86.67% Lines 65/75

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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 1943x 3x 3x     61x                               61x                       61x   61x 3x   61x 58x   61x 61x   61x 61x   61x 61x 2x 2x     59x 59x         59x   59x         59x 1x     59x   59x 1x 1x     58x 58x   1x 1x     57x 57x 57x     1x 1x       56x 56x       56x 56x           56x           56x       56x   2x 2x     54x                               53x 106x       106x 108x     106x   31x       31x       31x           56x 112x       112x 120x               112x 87x   25x       25x       25x       3x  
const core           = require( '@actions/core' )
const github         = require( '@actions/github' )
const { sliceInput } = require( './utils' )
 
async function handleGHIssues( useSubtaskMode, useEpicMode, 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 {
    
        if( useSubtaskMode ) { console.log( '-- SUBTASK MODE is ON - we\'re attaching ourselves to labelled issue(s)' ) }
        else { console.log( '-- SUBTASK MODE is OFF - we\'re replacing the labelled issue(s)' ) }
    
        if( useEpicMode ) { console.log( '-- EPIC MODE is ON - we maintain Epic Link from the labelled issue(s)' ) }
        else { console.log( '--- EPIC MODE is OFF - we ignore Epic Link from the labelled issue(s)' ) }
    
        const labelForceCreate = core.getInput( 'FORCE_CREATION_LABEL' )
        console.log( `--- Force creation label is ${ labelForceCreate }` )
    
        const labelOwn = core.getInput( 'OWN_LABEL' )
        console.log( `--- Own label is ${ labelOwn }` )
    
        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 = sliceInput( jiraProjectKey )
        Iif( arrProjectKeys.length === 0 ) {
            arrProjectKeys.push( jiraProjectKey )
        }
    
        const arrIssueTypes = sliceInput( jiraIssueTypeName )
        Iif( arrIssueTypes.length === 0 ) {
            arrIssueTypes.push( jiraIssueTypeName )
        }
    
        //we look into each of the labels the issue has
        // and match them up with our allowed project keys
        const jiraAttachKeys = useSubtaskMode
                               ? findLabelledKeysMeantToBeAttachedTo( issueDetails.labels,
                                                                      arrProjectKeys,
                                                                      arrIssueTypes )
                               : []
    
        const jiraReplaceKeys = findLabelledKeysMeantToBeReplaced( issueDetails.labels, arrProjectKeys, arrIssueTypes,
                                                                   useSubtaskMode, labelOwn, labelForceCreate )
    
        // 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,
            repository:         changeEvent.repository,
        }
    
    }
    catch( error ) {
        core.setFailed( error.message )
    }
}
 
function findLabelledKeysMeantToBeAttachedTo( arrLabels, arrAllowedProjectKeys, arrAllowedIssueTypes ) {
    return arrLabels.reduce( ( currentKeysAndType, currentLabel ) => {
        Iif( !currentLabel.name ) {
            console.log( '--- some label have no name' )
            return currentKeysAndType
        }
        const idxFoundProject = arrAllowedProjectKeys.findIndex( currentProjectKey => {
            return currentLabel.name.startsWith( currentProjectKey + '-' )
        } )
        
        if( idxFoundProject === -1 ) { return currentKeysAndType }
        
        const indexIssueTypeToUse = idxFoundProject >= arrAllowedIssueTypes.length
                                    ? arrAllowedIssueTypes.length - 1
                                    : idxFoundProject
        
        currentKeysAndType.push( {
                                     jiraIssueKey:  currentLabel.name,
                                     jiraIssueType: arrAllowedIssueTypes[ indexIssueTypeToUse ],
                                 } )
        return currentKeysAndType
    }, [] )
}
 
function findLabelledKeysMeantToBeReplaced( arrLabels, arrProjectKeys, arrIssueTypes, useSubtaskMode, labelOwn,
                                            labelForceCreate ) {
    return arrLabels.reduce( ( currentKeysAndType, currentLabel ) => {
        Iif( !currentLabel.name ) {
            console.log( '--- some label have no name' )
            return currentKeysAndType
        }
        const idxFoundProject = arrProjectKeys.findIndex( currentProjectKey =>
                                                              ( useSubtaskMode
                                                                ? currentLabel.name.startsWith( labelOwn +
                                                                                                currentProjectKey +
                                                                                                '-' )
                                                                  || currentLabel.name === labelForceCreate
                                                                : currentLabel.name.startsWith( currentProjectKey +
                                                                                                '-' )
                                                                  || currentLabel.name === labelForceCreate ) )
        if( idxFoundProject === -1 ) {
            return currentKeysAndType
        }
        const indexIssueTypeToUse = idxFoundProject >= arrIssueTypes.length
                                    ? arrIssueTypes.length - 1
                                    : idxFoundProject
        
        currentKeysAndType.push( {
                                     jiraIssueKey:  currentLabel.name,
                                     jiraIssueType: arrIssueTypes[ indexIssueTypeToUse ],
                                 } )
        return currentKeysAndType
    }, [] )
}
 
module.exports.handleGHIssues = handleGHIssues