Skip to content

Use jira.js instead of jira-connector #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ This project depends on the following projects, thanks to every developer who ma

- [discord.js](https://github.com/discordjs/discord.js/)
- [ESLint](https://github.com/eslint/eslint)
- [jira-connector](https://github.com/floralvikings/jira-connector)
- [jira.js](https://github.com/MrRefactoring/jira.js)
- [JS-YAML](https://github.com/nodeca/js-yaml)
- [log4js](https://github.com/log4js-node/log4js-node)
- [moment](https://github.com/moment/moment)
Expand Down
528 changes: 171 additions & 357 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"author": "violine1101",
"contributors": [
"NeunEinser",
"SPGoding"
"SPGoding",
"urielsalis"
],
"repository": "https://github.com/mojira/mojira-discord-bot",
"scripts": {
Expand All @@ -19,20 +20,20 @@
"dependencies": {
"config": "^3.3.1",
"discord.js": "^11.6.4",
"jira-connector": "^3.1.0",
"jira.js": "^1.6.0",
"js-yaml": "^3.14.0",
"log4js": "^6.1.2",
"moment": "^2.24.0",
"log4js": "^6.3.0",
"moment": "^2.27.0",
"node-fetch": "^2.6.0",
"typescript": "^3.8.3"
"typescript": "^3.9.7"
},
"devDependencies": {
"@types/config": "0.0.36",
"@types/emoji-regex": "^8.0.0",
"@types/node": "^12.12.35",
"@types/node": "^12.12.54",
"@types/node-fetch": "^2.5.7",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"eslint": "^6.8.0"
}
}
5 changes: 5 additions & 0 deletions src/MojiraBot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Client, TextChannel, ChannelLogsQueryOptions, Message } from 'discord.js';
import * as log4js from 'log4js';
import { Client as JiraClient } from 'jira.js';
import BotConfig from './BotConfig';
import TaskScheduler from './tasks/TaskScheduler';
import EventRegistry from './events/EventRegistry';
Expand All @@ -26,6 +27,10 @@ export default class MojiraBot {
public static client: Client = new Client();
private static running = false;

public static jira = new JiraClient( {
host: 'https://bugs.mojang.com',
} );

public static async start(): Promise<void> {
if ( this.running ) {
this.logger.error( 'MojiraBot is still running. You can only start a bot that is not currently running.' );
Expand Down
9 changes: 5 additions & 4 deletions src/commands/MentionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ export default class MentionCommand extends Command {
let embed: RichEmbed;
try {
embed = await mention.getEmbed();
} catch ( err ) {
} catch ( jiraError ) {
try {
message.channel.send( err );
} catch ( err ) {
Command.logger.log( err );
Command.logger.warn( jiraError.message );
await message.channel.send( '❌ ' + jiraError.message );
} catch ( discordError ) {
Command.logger.error( discordError );
}
return false;
}
Expand Down
38 changes: 12 additions & 26 deletions src/mentions/MultipleMention.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { RichEmbed } from 'discord.js';
import { Mention } from './Mention';
import JiraClient from 'jira-connector';
import MojiraBot from '../MojiraBot';

export class MultipleMention extends Mention {
private jira: JiraClient;

private tickets: string[];

constructor( tickets: string[] ) {
super();

this.tickets = tickets;

this.jira = new JiraClient( {
host: 'bugs.mojang.com',
strictSSL: true,
} );
}

public async getEmbed(): Promise<RichEmbed> {
Expand All @@ -26,36 +19,29 @@ export class MultipleMention extends Mention {
let searchResults: any;

try {
searchResults = await this.jira.search.search( {
searchResults = await MojiraBot.jira.issueSearch.searchForIssuesUsingJqlGet( {
jql: `id IN (${ this.tickets.join( ',' ) }) ORDER BY key ASC`,
maxResults: 10,
fields: [ 'key', 'summary' ],
} );
} catch ( err ) {
const exception = JSON.parse( err );
if ( !exception ) {
Mention.logger.error( err );
return;
}
let ticketList = this.tickets.join( ', ' );
const lastSeparatorPos = ticketList.lastIndexOf( ', ' );
ticketList = `${ ticketList.substring( 0, lastSeparatorPos ) } and ${ ticketList.substring( lastSeparatorPos + 2, ticketList.length ) }`;

Mention.logger.error( 'Error: status code ' + exception.statusCode );
let errorMessage = `An error occurred while retrieving tickets ${ ticketList }: ${ err.message }`;

// TODO clean up
let errorMessage = `An error occurred while retrieving this ticket: ${ exception.body.errorMessages[0] }`;

if ( exception.statusCode === 404 ) {
errorMessage = 'This ticket doesn\'t seem to exist.';
} else if ( exception.statusCode === 401 ) {
errorMessage = 'This ticket is private or has been deleted.';
if ( err.response?.data?.errorMessages ) {
for ( const msg of err.response.data.errorMessages ) {
errorMessage += `\n${ msg }`;
}
}

throw errorMessage;
throw new Error( errorMessage );
}

if ( !searchResults.issues ) {
Mention.logger.error( 'Error: no issues returned by JIRA' );

throw 'An error occurred while retrieving this ticket: No issues were returned by the JIRA API.';
throw new Error( 'No issues were returned by the JIRA API.' );
}

for ( const issue of searchResults.issues ) {
Expand Down
48 changes: 19 additions & 29 deletions src/mentions/SingleMention.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,46 @@
import { RichEmbed } from 'discord.js';
import { Mention } from './Mention';
import JiraClient from 'jira-connector';
import moment from 'moment';
import MojiraBot from '../MojiraBot';

export class SingleMention extends Mention {
private jira: JiraClient;

private ticket: string;

constructor( ticket: string ) {
super();

this.ticket = ticket;

this.jira = new JiraClient( {
host: 'bugs.mojang.com',
strictSSL: true,
} );
}

public async getEmbed(): Promise<RichEmbed> {
let ticketResult: any;

try {
ticketResult = await this.jira.issue.getIssue( {
issueId: this.ticket,
ticketResult = await MojiraBot.jira.issues.getIssue( {
issueIdOrKey: this.ticket,
} );
} catch ( err ) {
const exception = JSON.parse( err );
if ( !exception ) {
Mention.logger.error( err );
return;
let errorMessage = `An error occurred while retrieving ticket ${ this.ticket }: ${ err.message }`;

if ( err.response ) {
const exception = err.response;

if ( exception.status === 404 ) {
errorMessage = `${ this.ticket } doesn't seem to exist.`;
} else if ( exception.status === 401 ) {
errorMessage = `${ this.ticket } is private or has been deleted.`;
} else if ( exception?.data?.errorMessages ) {
for ( const msg of exception.data.errorMessages ) {
errorMessage += `\n${ msg }`;
}
}
}

Mention.logger.error( 'Error: status code ' + exception.statusCode );

// TODO clean up
let errorMessage = `An error occurred while retrieving this ticket: ${ exception.body.errorMessages[0] }`;

if ( exception.statusCode === 404 ) {
errorMessage = 'This ticket doesn\'t seem to exist.';
} else if ( exception.statusCode === 401 ) {
errorMessage = 'This ticket is private or has been deleted.';
}

throw errorMessage;
throw new Error( errorMessage );
}

if ( !ticketResult.fields ) {
Mention.logger.error( 'Error: no fields returned by JIRA' );

throw 'An error occurred while retrieving this ticket: No fields were returned by the JIRA API.';
throw new Error( 'No fields were returned by the JIRA API.' );
}

let status = ticketResult.fields.status.name;
Expand Down
10 changes: 3 additions & 7 deletions src/tasks/FilterFeedTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { FilterFeedConfig } from '../BotConfig';
import { Client, TextChannel, Channel } from 'discord.js';
import * as log4js from 'log4js';
import Task from './Task';
import JiraClient from 'jira-connector';
import { Client as JiraClient } from 'jira.js';
import { NewsUtil } from '../util/NewsUtil';
import MojiraBot from '../MojiraBot';

export default class FilterFeedTask extends Task {
public static logger = log4js.getLogger( 'FilterFeed' );
Expand All @@ -25,18 +26,13 @@ export default class FilterFeedTask extends Task {
this.jql = jql;
this.title = title;
this.titleSingle = titleSingle || title.replace( /\{\{num\}\}/g, '1' );

this.jira = new JiraClient( {
host: 'bugs.mojang.com',
strictSSL: true,
} );
}

public async run(): Promise<void> {
let upcomingTickets: string[];

try {
const searchResults = await this.jira.search.search( {
const searchResults = await MojiraBot.jira.issueSearch.searchForIssuesUsingJqlGet( {
jql: this.jql,
fields: ['key'],
} );
Expand Down
15 changes: 4 additions & 11 deletions src/tasks/VersionFeedTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as log4js from 'log4js';
import Task from './Task';
import { Channel, TextChannel, RichEmbed } from 'discord.js';
import { VersionFeedConfig } from '../BotConfig';
import JiraClient from 'jira-connector';
import { NewsUtil } from '../util/NewsUtil';
import MojiraBot from '../MojiraBot';

interface JiraVersion {
id: string;
Expand All @@ -21,8 +21,6 @@ interface JiraVersionChange {
export default class VersionFeedTask extends Task {
public static logger = log4js.getLogger( 'Version' );

private jira: JiraClient;

private channel: Channel;
private project: string;
private scope: number;
Expand All @@ -38,11 +36,6 @@ export default class VersionFeedTask extends Task {
this.project = project;
this.scope = scope;

this.jira = new JiraClient( {
host: 'bugs.mojang.com',
strictSSL: true,
} );

this.getVersions().then(
versions => {
this.cachedVersions = versions;
Expand Down Expand Up @@ -76,7 +69,7 @@ export default class VersionFeedTask extends Task {
}

private async getVersions(): Promise<JiraVersion[]> {
const results = await this.jira.project.getVersionsPaginated( {
const results = await MojiraBot.jira.projectVersions.getProjectVersionsPaginated( {
projectIdOrKey: this.project,
maxResults: this.scope,
orderBy: '-sequence',
Expand Down Expand Up @@ -147,8 +140,8 @@ export default class VersionFeedTask extends Task {
};

try {
versionIssueCounts = await this.jira.version.getRelatedIssueCounts( {
versionId: version.id,
versionIssueCounts = await MojiraBot.jira.projectVersions.getVersionsRelatedIssuesCount( {
id: version.id,
} );
} catch ( error ) {
VersionFeedTask.logger.error( error );
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "commonjs",
"lib": ["es2016"],
"outDir": "./bin",
"sourceMap": true,
"esModuleInterop": true
},
"exclude": [
Expand Down