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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | 3x 3x 46x 46x 46x 46x 46x 46x 46x 46x 46x 44x 44x 43x 43x 42x 42x 42x 42x 42x 42x 42x 4x 46x 2x 44x 1x 43x 43x 45x 43x 1x 1x 42x 48x 48x 1x 42x 42x 20x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 42x 22x 20x 20x 20x 20x 1x 1x 19x 19x 19x 19x 19x 13x 6x 6x 6x 6x 6x 20x 19x 19x 6x 13x 13x 13x 19x 19x 6x 6x 13x 13x 13x 13x 13x 13x 13x 6x 6x 6x 6x 6x 4x 1x 1x 3x 3x 1x 1x 2x 2x 2x 2x 3x | const core = require( '@actions/core' ) const JiraClient = require( 'jira-connector' ) async function handleSubtask( issueChanges, useSubtaskMode, DEBUG ) { //ISSUE CHANGE will be: // { // event: payload.action, // jiraKeys: jiraKeys, // changes: changedValues, // details: payload.issue // } try { Iif( !issueChanges.jiraKeys ) { //we have no parent task in JIRA, this should never happen console.log( `WARNING: no JIRA parent story is labelled.` ) } const jiraProjectKey = core.getInput( 'JIRA_PROJECTKEY' ) console.log( `- we are pushed to project with key ${ jiraProjectKey }` ) const jiraIssueTypeName = core.getInput( 'JIRA_ISSUETYPE_NAME' ) console.log( `-- using issue type ${ jiraIssueTypeName }` ) //Let's login to JIRA first const jiraSession = new JiraClient( { host: core.getInput( 'JIRA_BASEURL' ), basic_auth: { email: core.getInput( 'JIRA_USEREMAIL' ), api_token: core.getInput( 'JIRA_APITOKEN' ), }, } ) console.log( '-- login in JIRA' ) await loginToJiraAndCheckMyself( jiraSession ) console.log( '--- checking project exist' ) await checkProjectExist( jiraSession, jiraProjectKey ) console.log( '--- checking issue type exist' ) await findIssueTypeRequested( jiraSession, jiraProjectKey, jiraIssueTypeName ) // const isSubtaskType = jiraIssuetypeFound.subtask const labeledJIRAIssuesKeyToAttachTo = issueChanges.jiraKeys.filter( currentJiraIssueKey => currentJiraIssueKey.startsWith( jiraProjectKey ) ) const labeledJIRAIssueKeySubtasks = issueChanges.jiraKeys.filter( currentJiraIssueKey => currentJiraIssueKey.startsWith( 'sub' + jiraProjectKey ) ) Iif( labeledJIRAIssuesKeyToAttachTo.length === 0 && labeledJIRAIssueKeySubtasks.length === 0 ) { console.log( `ERROR: no label match the project key ${ jiraProjectKey } -- this should have be trapped before, \n` + 'please log an issue at https://github.com/b-yond-infinite-network/sync-jira-subtask-to-gh-issues-action/issues' ) return } // // Prioritizing sub-XXX label as a force sync const { jiraIssueToUpdateDirectly, leftOverLabelToUpdateFromParent, } = await listToUpdateDirectly( jiraSession, labeledJIRAIssueKeySubtasks, labeledJIRAIssuesKeyToAttachTo ) const summaryToLookFor = ( issueChanges.changes && issueChanges.changes.title ? issueChanges.changes.title.from : issueChanges.details.title ? issueChanges.details.title : '#' + issueChanges.details.number ) const jiraIssueToUpdateFromParent = await listToUpdateByAttachingToParent( jiraSession, jiraProjectKey, jiraIssueTypeName, useSubtaskMode, leftOverLabelToUpdateFromParent, summaryToLookFor ) return !jiraIssueToUpdateDirectly || jiraIssueToUpdateDirectly.length === 0 ? !jiraIssueToUpdateFromParent || jiraIssueToUpdateFromParent.length === 0 ? null : jiraIssueToUpdateFromParent : !jiraIssueToUpdateFromParent || jiraIssueToUpdateFromParent.length === 0 ? jiraIssueToUpdateDirectly : [...jiraIssueToUpdateDirectly, ...jiraIssueToUpdateFromParent] } catch( error ) { core.setFailed( error.message ? error.message : error.body && error.body.message ? error.body.message : JSON.stringify( error ) ) } async function loginToJiraAndCheckMyself( jiraSession ) { try { await jiraSession.myself.getMyself() } catch( myselfError ) { manageJIRAAPIError( myselfError, jiraSession ) } } async function checkProjectExist( jiraSession, jiraProjectKey ) { try { return await jiraSession.project.getProject( { projectIdOrKey: jiraProjectKey } ) } catch( projectError ) { manageJIRAAPIError( projectError, jiraSession, `### project ${ jiraProjectKey } is invalid in JIRA`, 'Project doesn\'t exist in JIRA' ) } } async function findIssueTypeRequested( jiraSession, jiraProjectKey, issuetypeToFind ) { const foundData = await jiraSession.issue.getCreateMetadata( { projectKeys: jiraProjectKey, issuetypeNames: issuetypeToFind, } ) Iif( !foundData || !foundData.projects || foundData.projects.length === 0 || !foundData.projects[ 0 ].issuetypes || !foundData.projects[ 0 ].issuetypes.length === 0 ) return null const issueTypefound = foundData.projects[ 0 ].issuetypes.find( currentIssueType => currentIssueType.name === issuetypeToFind ) if( !issueTypefound ) { console.log( `### Issue Type ${ issuetypeToFind } is invalid in JIRA` ) throw `The JIRA issue type specified does not exist or is ambiguous` } return issueTypefound } async function findIssue( jiraSession, jiraIssueKey ) { try { return await jiraSession.issue.getIssue( { issueKey: jiraIssueKey } ) } catch( issueError ) { manageJIRAAPIError( issueError, jiraSession, `### issue ${ jiraIssueKey } is inaccessible in JIRA\n==> action skipped for label ${ jiraIssueKey }` ) } } async function listToUpdateDirectly( jiraSession, listOfIssueKeyToReplaceDirectly, fullLabelList ){ let listOfIssueKeyLabelToRemoveFromFullList = [] if( listOfIssueKeyToReplaceDirectly.length === 0 ) return { jiraIssueToUpdateDirectly: null, leftOverLabelToUpdateFromParent: fullLabelList } const jiraIssueToUpdateDirectly = await Promise.all( listOfIssueKeyToReplaceDirectly.map( async currentJIRASubtaskKey => { const issueKeyWithoutSub = currentJIRASubtaskKey.replace('sub', '') console.log( `Adding sub-ed JIRA Issue ${ issueKeyWithoutSub } to the list of JIRA Issues to upgrade` ) const foundJIRAIssue = await findIssue( jiraSession, issueKeyWithoutSub ) Iif( !foundJIRAIssue ) { console.log( `--! Issue ${ issueKeyWithoutSub } is not in JIRA -- skipping it` ) return null } const foundParentKey = foundJIRAIssue && foundJIRAIssue.fields && foundJIRAIssue.fields.parent && foundJIRAIssue.fields.parent.key ? foundJIRAIssue.fields.parent.key : null Iif( foundParentKey && fullLabelList && fullLabelList.includes( foundParentKey ) ) { console.log( `--- ignoring parent labelling ${ foundParentKey } and upgrading ${ issueKeyWithoutSub } directly` ) listOfIssueKeyLabelToRemoveFromFullList.push( foundParentKey ) } return foundJIRAIssue } ) ) return { jiraIssueToUpdateDirectly: jiraIssueToUpdateDirectly.filter( currentSubtaskOrIssue => currentSubtaskOrIssue !== null ), issueKeyLabelList: fullLabelList.filter( currentJIRAIssueKey => !listOfIssueKeyLabelToRemoveFromFullList.includes( currentJIRAIssueKey ) ) } } async function listToUpdateByAttachingToParent( jiraSession, jiraProjectKey, jiraIssueTypeName, useSubtaskMode, issueKeyLabelToAttachTo, summaryToLookForInSubtasks ){ if( !issueKeyLabelToAttachTo || issueKeyLabelToAttachTo.length === 0 ) return null const jiraIssueToAttachTo = await Promise.all( issueKeyLabelToAttachTo.map( async ( currentJIRAIssueKey ) => { console.log( `-- currently attaching to JIRA Issue: ${ currentJIRAIssueKey }` ) const foundJIRAIssue = await findIssue( jiraSession, currentJIRAIssueKey ) if( !foundJIRAIssue ) { console.log( `--! trying to attach to Issue that is not in JIRA ${ currentJIRAIssueKey } -- skipping it` ) return null } Iif( !useSubtaskMode ) { return foundJIRAIssue } Eif( summaryToLookForInSubtasks ) { console.log( `-- Subtask mode using ${ summaryToLookForInSubtasks }` ) const foundSubtask = findSubtaskByTitle( foundJIRAIssue, summaryToLookForInSubtasks ) if( foundSubtask ) { return foundSubtask } } console.log( `-- Creation in JIRA with title: ${ summaryToLookForInSubtasks }` ) const createdIssue = await createJIRAIssue( jiraSession, jiraProjectKey, jiraIssueTypeName, currentJIRAIssueKey, summaryToLookForInSubtasks ) DEBUG( `Created issue with return info ${ createdIssue }` ) Iif( !createdIssue ) { throw `Unable to create the the Jira Issue` } return findIssue( jiraSession, createdIssue.key ) } ) ) return jiraIssueToAttachTo.filter( currentSubtaskOrIssue => currentSubtaskOrIssue !== null ) } function findSubtaskByTitle( jiraParentIssue, titleToLookFor ) { // - the story with the same title const jiraSubtasks = findAllSubtasks( jiraParentIssue ) if( !jiraSubtasks || jiraSubtasks.length === 0 ) { return null } const foundWithTitle = findSubtaskWithTitle( jiraSubtasks, titleToLookFor ) Eif( foundWithTitle ) { return foundWithTitle } console.log( `----! no subtasks found with the title '${ titleToLookFor }' found, creating a new one` ) return null } function findAllSubtasks( jiraParentIssue ) { console.log( `-- looking for subtask of JIRA Issue ${ jiraParentIssue.key }` ) if( !jiraParentIssue.fields[ "subtasks" ] || jiraParentIssue.fields[ "subtasks" ].length === 0 ) { // no subtask found means we will create one console.log( '----! no subtasks found, we will be creating a new one' ) return null } return jiraParentIssue.fields[ "subtasks" ] } // function findSubtaskWithKey( jiraSubtasksIssuesArray, githubLabelForSubtaskKeyArray ) { // console.log( `--- looking for one with key inside "${ JSON.stringify( githubLabelForSubtaskKeyArray ) }"` ) // //filtering subtask to find a key that is in the list of subtask Labels in GITHUB // return jiraSubtasksIssuesArray.find( currentSubtask => githubLabelForSubtaskKeyArray.includes( currentSubtask.key ) ) // } function findSubtaskWithTitle( jiraSubtasksIssuesArray, summaryToFind ) { console.log( `--- looking for one with summary "${ summaryToFind }"` ) //filtering subtask to find our title const arrFoundSubtasksIssueWithTitle = jiraSubtasksIssuesArray.filter( currentJIRASubtaskObject => { return currentJIRASubtaskObject.fields.summary && currentJIRASubtaskObject.fields.summary === summaryToFind } ) Iif( arrFoundSubtasksIssueWithTitle.length === 0 ) { return null } Iif( arrFoundSubtasksIssueWithTitle.length > 1 ) console.log( '----! found more than one subtask with our title, returning the first' ) return arrFoundSubtasksIssueWithTitle[ 0 ] } async function createJIRAIssue( jiraSession, jiraProjectKey, jiraIssueTypeNameToUse, jiraParentIssueKey, title ) { const issueData = { "fields": { "project": { "key": jiraProjectKey, }, "issuetype": { "name": jiraIssueTypeNameToUse, }, "parent": { "key": jiraParentIssueKey, }, "summary": title, // "description": { // "type": "doc", // "version": 1, // "content": [ // { // "type": "paragraph", // "content": [ // { // "text": , // "type": "text" // } // ] // } // ] // }, // "labels": [ // "GITHUB" + ghIssue.id // ] } } console.log( `Creating JIRA Issue of type : ${ JSON.stringify( jiraIssueTypeNameToUse ) } with title: ${ title }` ) DEBUG( `Creating JIRA ${ JSON.stringify( jiraIssueTypeNameToUse ) } with datas ${ JSON.stringify( issueData ) }` ) try { return await jiraSession.issue.createIssue( issueData ) } catch( createError ) { manageJIRAAPIError( createError, jiraSession, '##### Unable to create Issue in JIRA', 'Create Issue failed' ) } } function manageJIRAAPIError( triggeredError, jiraSession, messageToLog, exceptionToRaise ) { if( triggeredError.message ) { console.log( `### JIRA API URL is invalid or inaccessible for ${ jiraSession.host }` ) throw 'Invalid API URL' } else { const parsedError = JSON.parse( triggeredError ) if( parsedError.statusCode === 401 ) { console.log( `### credentials unauthorized \n${ parsedError.body }` ) throw 'Unauthorized' } Iif( !messageToLog ) return Eif( parsedError.statusCode === 404 ) { console.log( messageToLog ) if( exceptionToRaise ) throw exceptionToRaise } } } } module.exports = handleSubtask |