All files ghUpdate.js

92.68% Statements 38/41
80% Branches 8/10
100% Functions 7/7
91.89% Lines 34/37

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 893x 3x     49x 49x   49x   49x 98x 97x   98x   49x           51x   49x 2x 2x     49x 47x 49x 49x             47x   47x 47x   47x           47x 47x       49x 49x         15x     34x 34x   34x             34x   34x                 204x     3x  
const core   = require( '@actions/core' )
const github = require( '@actions/github' )
 
async function handleGHUpdate( issueToUpdate, jiraIssues, DEBUG ) {
	const repoToken          = core.getInput( 'GITHUB_TOKEN', { required: true } )
	const ghForceCreateLabel = core.getInput( 'FORCE_CREATION_LABEL' )
	
	const ghClient = new github.GitHub( repoToken )
	
	const arrLabelsWithoutForceCreate = issueToUpdate.details.labels.reduce( ( accumulatedLabels, currentLabel ) => {
		if( currentLabel.name !== ghForceCreateLabel ) {
			accumulatedLabels.push( currentLabel.name )
		}
		return accumulatedLabels
	}, [] )
	Iif( arrLabelsWithoutForceCreate.length <= 0 ) {
		console.log( '-- nothing to update in GITHUB' )
		return
	}
	
	//making sure we don't have any label already
	const arrUniqueLabelsFromJiraIssues = jiraIssues.filter( currentIssue => !arrLabelsWithoutForceCreate.includes(
		currentIssue.key ) )
	if( arrUniqueLabelsFromJiraIssues.length <= 0 ) {
		console.log( '-- all stories are already in the labels for this issue, nothing to update in GITHUB' )
		return
	}
	
	const arrLabelsFromJiraKeyToAdd = arrUniqueLabelsFromJiraIssues.map( currentJiraIssue => ( currentJiraIssue.key ) )
	for( const currentJiraIssue of arrUniqueLabelsFromJiraIssues ) {
		console.log( `-- checking label ${ currentJiraIssue.key } exist in repo` )
		await createLabelIfNotExist( ghClient,
									 issueToUpdate.repository.owner.login,
									 issueToUpdate.repository.name,
									 currentJiraIssue.key,
									 currentJiraIssue.parentTitle )
	}
	
	const arrFinalLabels = [ ...arrLabelsWithoutForceCreate, ...arrLabelsFromJiraKeyToAdd ]
	
	DEBUG( arrFinalLabels )
	console.log( `-- update GITHUB issue ${ issueToUpdate.details.number }` )
	
	const updateStatus = await ghClient.issues.update( {
														   owner:        issueToUpdate.repository.owner.login,
														   repo:         issueToUpdate.repository.name,
														   issue_number: issueToUpdate.details.number,
														   labels:       arrFinalLabels,
													   } )
	DEBUG( updateStatus )
	console.log( '- finishing sync with GITHUB' )
}
 
async function createLabelIfNotExist( ghClient, loginRepoOwner, nameRepo, nameLabel, parentTitle ) {
	try {
		await ghClient.issues.getLabel( {
											owner: loginRepoOwner,
											repo:  nameRepo,
											name:  nameLabel,
										} )
		console.log( `--- label ${ nameLabel } exists already, we just add it to the issue` )
	}
	catch( error ) {
		Eif( error.status === 404 ) {
			console.log( `--- creating label ${ nameLabel }` )
			
			const labelToCreate = {
				owner: loginRepoOwner,
				repo:  nameRepo,
				name:  nameLabel,
				color: randomHexaColor(),
			}
			
			if( parentTitle ) { labelToCreate.description = parentTitle }
			
			await ghClient.issues.createLabel( labelToCreate )
		}
		else {
			throw error
		}
	}
}
 
function randomHexaColor() {
	return "000000".replace( /0/g, () => { return ( ~~( Math.random() * 16 ) ).toString( 16 ) } )
}
 
module.exports.handleGHUpdate = handleGHUpdate