1
0

phabricator.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const {MessageEmbed} = require('discord.js');
  2. const logging = require('../util/logging.js');
  3. /**
  4. * Sends a Phabricator task.
  5. * @param {import('../util/i18n.js')} lang - The user language.
  6. * @param {import('discord.js').Message} msg - The Discord message.
  7. * @param {import('../util/wiki.js')} wiki - The wiki.
  8. * @param {URL} link - The link.
  9. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  10. * @param {String} [spoiler] - If the response is in a spoiler.
  11. */
  12. function phabricator_task(lang, msg, wiki, link, reaction, spoiler = '') {
  13. var regex = /^(?:https?:)?\/\/phabricator\.(wikimedia|miraheze)\.org\/T(\d+)(?:#|$)/.exec(link.href);
  14. if ( !regex || !process.env['phabricator-' + regex[1]] ) {
  15. logging(wiki, msg.guild?.id, 'interwiki');
  16. msg.sendChannel( spoiler + ' ' + link + ' ' + spoiler );
  17. if ( reaction ) reaction.removeEmoji();
  18. return;
  19. }
  20. logging(link.origin, msg.guild?.id, 'phabricator', regex[1]);
  21. got.get( 'https://phabricator.' + regex[1] + '.org/api/maniphest.search?api.token=' + process.env['phabricator-' + regex[1]] + '&constraints[ids][0]=' + regex[2] ).then( response => {
  22. var body = response.body;
  23. if ( response.statusCode !== 200 || !body?.result?.data || body.error_code ) {
  24. console.log( '- ' + response.statusCode + ': Error while getting the Phabricator task: ' + body?.error_info );
  25. msg.sendChannelError( spoiler + ' ' + link + ' ' + spoiler );
  26. if ( reaction ) reaction.removeEmoji();
  27. return;
  28. }
  29. if ( !body.result.data.length ) {
  30. msg.sendChannel( spoiler + ' ' + link + ' ' + spoiler );
  31. if ( reaction ) reaction.removeEmoji();
  32. return;
  33. }
  34. var task = body.result.data[0];
  35. if ( !msg.showEmbed() ) {
  36. var status = '**' + task.fields.status.name + ':** ' + task.fields.name.escapeFormatting();
  37. msg.sendChannel( spoiler + status + '\n<' + link + '>' + spoiler );
  38. if ( reaction ) reaction.removeEmoji();
  39. return;
  40. }
  41. var embed = new MessageEmbed().setAuthor( 'Phabricator' ).setTitle( task.fields.name.escapeFormatting() ).setURL( link ).addField( 'Status', task.fields.status.name, true ).addField( 'Priority', task.fields.priority.name, true );
  42. if ( task.fields.subtype !== 'default' ) embed.addField( 'Subtype', task.fields.subtype, true );;
  43. var description = task.fields.description.raw.replace( /```lang=/g, '```' );
  44. if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
  45. embed.setDescription( parse_links( description ) );
  46. if ( /^#\d+$/.test( link.hash ) ) return got.get( 'https://phabricator.' + regex[1] + '.org/api/transaction.search?api.token=' + process.env['phabricator-' + regex[1]] + '&objectIdentifier=' + task.phid ).then( response => {
  47. var body = response.body;
  48. if ( response.statusCode !== 200 || !body?.result?.data || body.error_code ) {
  49. console.log( '- ' + response.statusCode + ': Error while getting the task transactions: ' + body?.error_info );
  50. return;
  51. }
  52. var comment = body.result.data.find( transaction => '#' + transaction.id === link.hash );
  53. if ( comment.type === 'comment' ) {
  54. var content = comment.comments[0].content.raw;
  55. if ( content.length > 1000 ) content = content.substring(0, 1000) + '\u2026';
  56. embed.spliceFields( 0, 0, {name: 'Comment', value: parse_links( content )} );
  57. }
  58. }, error => {
  59. console.log( '- Error while getting the task transactions: ' + error );
  60. } ).finally( () => {
  61. msg.sendChannel( spoiler + '<' + link + '>' + spoiler, {embed} );
  62. if ( reaction ) reaction.removeEmoji();
  63. } );
  64. msg.sendChannel( spoiler + '<' + link + '>' + spoiler, {embed} );
  65. if ( reaction ) reaction.removeEmoji();
  66. }, error => {
  67. console.log( '- Error while getting the Phabricator task: ' + error );
  68. msg.sendChannelError( spoiler + ' ' + link + ' ' + spoiler );
  69. if ( reaction ) reaction.removeEmoji();
  70. } );
  71. }
  72. /**
  73. * Parse Phabricator links.
  74. * @param {String} text - The text to parse.
  75. * @returns {String}
  76. */
  77. function parse_links(text) {
  78. text = text.replace( /\[\[ *(.+?) *\| *(.+?) *\]\]/g, '[$2]($1)' );
  79. return text;
  80. }
  81. module.exports = phabricator_task;