phabricator.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const {MessageEmbed} = require('discord.js');
  2. const logging = require('../util/logging.js');
  3. const {escapeFormatting, limitLength} = require('../util/functions.js');
  4. /**
  5. * Sends a Phabricator task.
  6. * @param {import('../util/i18n.js')} lang - The user language.
  7. * @param {import('discord.js').Message} msg - The Discord message.
  8. * @param {import('../util/wiki.js')} wiki - The wiki.
  9. * @param {URL} link - The link.
  10. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  11. * @param {String} [spoiler] - If the response is in a spoiler.
  12. */
  13. function phabricator_task(lang, msg, wiki, link, reaction, spoiler = '') {
  14. var regex = /^(?:https?:)?\/\/phabricator\.(wikimedia|miraheze)\.org\/T(\d+)(?:#|$)/.exec(link.href);
  15. if ( !regex || !process.env['phabricator_' + regex[1]] ) {
  16. logging(wiki, msg.guild?.id, 'interwiki');
  17. msg.sendChannel( spoiler + ' ' + link + ' ' + spoiler );
  18. if ( reaction ) reaction.removeEmoji();
  19. return;
  20. }
  21. var site = 'https://phabricator.' + regex[1] + '.org/';
  22. logging(site, msg.guild?.id, 'phabricator', regex[1]);
  23. got.get( site + 'api/maniphest.search?api.token=' + process.env['phabricator_' + regex[1]] + '&attachments[projects]=1&constraints[ids][0]=' + regex[2] ).then( response => {
  24. var body = response.body;
  25. if ( response.statusCode !== 200 || !body?.result?.data || body.error_code ) {
  26. console.log( '- ' + response.statusCode + ': Error while getting the Phabricator task: ' + body?.error_info );
  27. msg.sendChannelError( spoiler + ' ' + link + ' ' + spoiler );
  28. if ( reaction ) reaction.removeEmoji();
  29. return;
  30. }
  31. if ( !body.result.data.length ) {
  32. msg.sendChannel( spoiler + ' ' + link + ' ' + spoiler );
  33. if ( reaction ) reaction.removeEmoji();
  34. return;
  35. }
  36. var task = body.result.data[0];
  37. var status = '**' + task.fields.status.name + ':** ' + escapeFormatting(task.fields.name) + '\n';
  38. if ( !msg.showEmbed() ) {
  39. msg.sendChannel( spoiler + status + '<' + link + '>' + spoiler );
  40. if ( reaction ) reaction.removeEmoji();
  41. return;
  42. }
  43. var summary = escapeFormatting(task.fields.name);
  44. if ( summary.length > 250 ) summary = summary.substring(0, 250) + '\u2026';
  45. var embed = new MessageEmbed().setAuthor( 'Phabricator' ).setTitle( summary ).setURL( link ).addField( 'Status', escapeFormatting(task.fields.status.name), true ).addField( 'Priority', escapeFormatting(task.fields.priority.name), true );
  46. if ( task.fields.subtype !== 'default' ) embed.addField( 'Subtype', escapeFormatting(task.fields.subtype), true );
  47. var description = parse_text( task.fields.description.raw, site );
  48. if ( description.length > 2000 ) description = limitLength(description, 2000, 40);
  49. embed.setDescription( description );
  50. Promise.all([
  51. ( task.attachments.projects.projectPHIDs.length ? got.get( site + 'api/phid.lookup?api.token=' + process.env['phabricator_' + regex[1]] + '&' + task.attachments.projects.projectPHIDs.map( (project, i) => 'names[' + i + ']=' + project ).join('&') ).then( presponse => {
  52. var pbody = presponse.body;
  53. if ( presponse.statusCode !== 200 || !pbody?.result || pbody.error_code ) {
  54. console.log( '- ' + presponse.statusCode + ': Error while getting the projects: ' + pbody?.error_info );
  55. return;
  56. }
  57. var projects = Object.values(pbody.result);
  58. var tags = projects.map( project => {
  59. return '[' + escapeFormatting(project.fullName) + '](' + project.uri + ')';
  60. } ).join(',\n');
  61. if ( tags.length > 1000 ) tags = projects.map( project => project.fullName ).join(',\n');
  62. if ( tags.length > 1000 ) tags = tags.substring(0, 1000) + '\u2026';
  63. embed.addField( 'Tags', tags );
  64. }, error => {
  65. console.log( '- Error while getting the projects: ' + error );
  66. } ) : undefined ),
  67. ( /^#\d+$/.test( link.hash ) ? got.get( site + 'api/transaction.search?api.token=' + process.env['phabricator_' + regex[1]] + '&objectIdentifier=' + task.phid ).then( tresponse => {
  68. var tbody = tresponse.body;
  69. if ( tresponse.statusCode !== 200 || !tbody?.result?.data || tbody.error_code ) {
  70. console.log( '- ' + tresponse.statusCode + ': Error while getting the task transactions: ' + tbody?.error_info );
  71. return;
  72. }
  73. var comment = tbody.result.data.find( transaction => '#' + transaction.id === link.hash );
  74. if ( comment.type === 'comment' ) {
  75. var content = parse_text( comment.comments[0].content.raw, site );
  76. if ( content.length > 1000 ) content = limitLength(content, 1000, 20);
  77. embed.spliceFields( 0, 0, {name: 'Comment', value: content} );
  78. }
  79. }, error => {
  80. console.log( '- Error while getting the task transactions: ' + error );
  81. } ) : undefined )
  82. ]).finally( () => {
  83. msg.sendChannel( spoiler + status + '<' + link + '>' + spoiler, {embed} );
  84. if ( reaction ) reaction.removeEmoji();
  85. } );
  86. }, error => {
  87. console.log( '- Error while getting the Phabricator task: ' + error );
  88. msg.sendChannelError( spoiler + ' ' + link + ' ' + spoiler );
  89. if ( reaction ) reaction.removeEmoji();
  90. } );
  91. }
  92. /**
  93. * Parse Phabricator text.
  94. * @param {String} text - The text to parse.
  95. * @param {String} site - The site the Phabricator is for.
  96. * @returns {String}
  97. */
  98. function parse_text(text, site) {
  99. text = text.replace( /```lang=/g, '```' );
  100. text = text.replace( /##(.+?)##/g, '`$1`' );
  101. text = text.replace( /!!(.+?)!!/g, '`$1`' );
  102. text = text.replace( /\/\/(.+?)\/\//g, '*$1*' );
  103. text = text.replace( /\[\[ ?(.+?) ?(?:\| ?(.+?) ?)?\]\]/g, (match, target, display) => {
  104. var link = target;
  105. if ( /^(?:(?:https?:)?\/\/|\/|#)/.test(target) ) link = new URL(target, site).href;
  106. else link = site + 'w/' + target;
  107. return '[' + ( display || target ) + '](' + link + ')';
  108. } );
  109. text = text.replace( /(?<!\w)@([\w-]+)\b/g, '[@$1](' + site + 'p/$1)' );
  110. text = text.replace( /(?<!https?:\/\/[^\s]+)\b\{?(r[A-Z]+[a-f\d]+)\}?\b/g, '[$1](' + site + '$1)' );
  111. text = text.replace( /(?<!https?:\/\/[^\s]+)\b\{?([CDFHLMPQTV]\d+(?:#\d+)?)\}?\b/g, '[$1](' + site + '$1)' );
  112. text = text.replace( /(?<!https?:\/\/[^\s]+)#([a-z0-9_-]+)\b/g, '[#$1](' + site + 'tag/$1)' );
  113. return text;
  114. }
  115. module.exports = phabricator_task;