<commit_before>// Packages
import inquirer from 'inquirer';
import color from 'chalk';
import Table from 'cli-table2';
import moment from 'moment';
import opn from 'opn';

// Local
import jira from './jira';

export default class JiraIssues {

	/**
	* Get required meta data to create issues 
	*/
	getMetaData() {
		return jira.apiRequest('/issue/createmeta');	
	}

	/**
	* Crate a new issue 
	* Docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue
	*/
	createIssue( newIssue) {
		// Create new issue
		jira.api.addNewIssue( newIssue )
	  	.then(function( issue ) {

	  		let config = jira.config.defaults;

	  		console.log('');
	    	console.log('New issue: ' + color.bold.red(issue.key));
	    	console.log(config.protocol + '://' + config.host + '/browse/' + issue.key);
	    	console.log('');
	  	})
	  	.catch(function( res ) {
	  		jira.showErrors( res );
	  	});
	}

	/**
	* Crate a new issue object
	* Docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue
	*/
	createIssueObj( options ) {

		const _this = this;
		
		this.getMetaData().then(function( meta ){

			var projects = []; 
			var keys = [];
			var issueTypes = [];
			var selectedProject;

			// Populate projects, keys and their respective issue types
			for ( var index in meta.projects ){
		  		projects.push(meta.projects[index].name);
		  		keys.push(meta.projects[index].key);
		  		issueTypes.push(meta.projects[index].issuetypes);
		  	}

	  		// Create the project question
			var project = [
			  {
			    type: 'list',
			    name: 'project',
			    message: 'Project: ',
			    choices: projects,
			    filter: function( val ){
			    	selectedProject = projects.indexOf( val );
			    	return keys[selectedProject];
			    }
			  }
			];

			inquirer.prompt( project ).then(function( answers1 ){

				var projectIssueTypes = [];
				
				// Get issue types of the selected project
				for ( var i in issueTypes[selectedProject] ){
			  		projectIssueTypes.push({
			  			id: issueTypes[selectedProject][i].id,
			  			name: issueTypes[selectedProject][i].name,
			  			subtask: issueTypes[selectedProject][i].subtask
			  		});
			  	}

				var questions = [
				  {
				    type: 'list',
				    name: 'issueType',
				    message: 'Issue type: ',
				    choices: projectIssueTypes,
				    filter: function( val ){
				    	return projectIssueTypes.find(function( obj ){
				    		return obj.name == val;
				    	});
				    }
				  },
				  {
				  	type: 'input',
				  	name: 'issueName',
				  	message: 'Please provide the issue name :',
				  	default: 'New Issue'
				  }
				];

				// Ask for the issue name and type
				inquirer.prompt( questions ).then(function( answers2 ){

					// Create the issue object
					var newIssue = {
						fields: {
							project: { 
							  key: answers1.project
							},
							summary: answers2.issueName,
							issuetype: {
							  id: answers2.issueType.id
							}
						}
					};
			    
			    	// Assign the issue to the current user if self option is passed
			    	if(  typeof options.self !== 'undefined' ) {
			    		newIssue.fields.assignee = { name: jira.config.defaults.username };
			    	}

			    	if( answers2.issueType.subtask ){
			    		var question = [
			    			{ 
			    				type: 'input',
						  		name: 'issueParentName',
						  		message: 'Please provide the parent issue key:'
			    			}
			    		];

			    		inquirer.prompt( question ).then(function( answers3 ){
			    			newIssue.fields.parent = {
			    				key: answers3.issueParentName
			    			};
			    			_this.createIssue( newIssue );
		    			});
			    	} else {
			    		_this.createIssue( newIssue );
			    	}
				});
			});
		});
	}

	/**
	* Search issues 
	*/
	search ( args ) {
		const _this = this;
		jira.api.searchJira( "summary ~ '" + args + "'" ).then(function( r ){
			if( r.total ){
				_this.showIssues( r.issues );<commit_msg>fix(issues): allows not passing 'self' flag<commit_after>// Packages
import inquirer from 'inquirer';
import color from 'chalk';
import Table from 'cli-table2';
import moment from 'moment';
import opn from 'opn';

// Local
import jira from './jira';

export default class JiraIssues {

	/**
	* Get required meta data to create issues 
	*/
	getMetaData() {
		return jira.apiRequest('/issue/createmeta');	
	}

	/**
	* Crate a new issue 
	* Docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue
	*/
	createIssue( newIssue) {
		// Create new issue
		jira.api.addNewIssue( newIssue )
	  	.then(function( issue ) {

	  		let config = jira.config.defaults;

	  		console.log('');
	    	console.log('New issue: ' + color.bold.red(issue.key));
	    	console.log(config.protocol + '://' + config.host + '/browse/' + issue.key);
	    	console.log('');
	  	})
	  	.catch(function( res ) {
	  		jira.showErrors( res );
	  	});
	}

	/**
	* Crate a new issue object
	* Docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-createIssue
	*/
	createIssueObj( options = {} ) {

		const _this = this;
		
		this.getMetaData().then(function( meta ){

			var projects = []; 
			var keys = [];
			var issueTypes = [];
			var selectedProject;

			// Populate projects, keys and their respective issue types
			for ( var index in meta.projects ){
		  		projects.push(meta.projects[index].name);
		  		keys.push(meta.projects[index].key);
		  		issueTypes.push(meta.projects[index].issuetypes);
		  	}

	  		// Create the project question
			var project = [
			  {
			    type: 'list',
			    name: 'project',
			    message: 'Project: ',
			    choices: projects,
			    filter: function( val ){
			    	selectedProject = projects.indexOf( val );
			    	return keys[selectedProject];
			    }
			  }
			];

			inquirer.prompt( project ).then(function( answers1 ){

				var projectIssueTypes = [];
				
				// Get issue types of the selected project
				for ( var i in issueTypes[selectedProject] ){
			  		projectIssueTypes.push({
			  			id: issueTypes[selectedProject][i].id,
			  			name: issueTypes[selectedProject][i].name,
			  			subtask: issueTypes[selectedProject][i].subtask
			  		});
			  	}

				var questions = [
				  {
				    type: 'list',
				    name: 'issueType',
				    message: 'Issue type: ',
				    choices: projectIssueTypes,
				    filter: function( val ){
				    	return projectIssueTypes.find(function( obj ){
				    		return obj.name == val;
				    	});
				    }
				  },
				  {
				  	type: 'input',
				  	name: 'issueName',
				  	message: 'Please provide the issue name :',
				  	default: 'New Issue'
				  }
				];

				// Ask for the issue name and type
				inquirer.prompt( questions ).then(function( answers2 ){

					// Create the issue object
					var newIssue = {
						fields: {
							project: { 
							  key: answers1.project
							},
							summary: answers2.issueName,
							issuetype: {
							  id: answers2.issueType.id
							}
						}
					};
			    
			    	// Assign the issue to the current user if self option is passed
			    	if( typeof options.self !== 'undefined' ) {
			    		newIssue.fields.assignee = { name: jira.config.defaults.username };
			    	}

			    	if( answers2.issueType.subtask ){
			    		var question = [
			    			{ 
			    				type: 'input',
						  		name: 'issueParentName',
						  		message: 'Please provide the parent issue key:'
			    			}
			    		];

			    		inquirer.prompt( question ).then(function( answers3 ){
			    			newIssue.fields.parent = {
			    				key: answers3.issueParentName
			    			};
			    			_this.createIssue( newIssue );
		    			});
			    	} else {
			    		_this.createIssue( newIssue );
			    	}
				}).catch((err) => { 
					console.log(err); 
					process.exit();
				});
			});
		});
	}

	/**
	* Search issues 
	*/
	search ( args ) {
		const _this = this;
		jira.api.searchJira( "summary ~ '" + args + "'" ).then(function( r ){
			if( r.total ){
				_this.showIssues( r.issues );
{'fraction_non_alphanumeric': 0.13458627648839555, 'fraction_numerical': 0.0027749747729566097, 'mean_word_length': 2.0696864111498257, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6291698', 'n_tokens_mistral': 3332, 'n_tokens_neox': 2534, 'n_words': 564}