All files ghIssuesHandling.js

90% Statements 36/40
84.62% Branches 22/26
100% Functions 3/3
89.74% Lines 35/39

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 773x 3x     52x 52x   52x 52x 52x 2x 2x   50x   50x     50x 1x   50x   50x 1x 1x     49x 49x   1x 1x     48x 48x 48x     1x 1x     47x 94x         94x         47x     47x 1x 1x     46x                       3x  
const core      = require('@actions/core')
const github    = require('@actions/github')
 
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' )
            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 jiraGHLabelsWithIDAsNames = issueDetails.labels.filter( ( currentLabel ) => {
            Iif( !currentLabel.name ) {
                console.log( '--- some label have no name' )
                return false
            }
            
            return ( useSubtaskMode
                     ? currentLabel.name.startsWith( 'sub' + jiraProjectKey )
                       || currentLabel.name.startsWith( jiraProjectKey )
                     : currentLabel.name.startsWith( jiraProjectKey ) )
        } )
        const jiraKeys = jiraGHLabelsWithIDAsNames.map( currentGHLabel => currentGHLabel.name )
 
        // console.log( `-- labeled: ${ JSON.stringify( jiraIDS ) }` )
        if( jiraKeys.length < 1 ){
            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,
            jiraKeys:   jiraKeys,
            changes:    changeEvent.changes,
            details:    issueDetails
        }
 
    } catch( error ) {
        core.setFailed( error.message )
    }
}
 
module.exports = handleIssues