taubot.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import argparse
  2. import signal
  3. import irc
  4. import json
  5. import sys
  6. # parse arguments
  7. parser = argparse.ArgumentParser(description='IRC bot to send a pipe to an IRC channel')
  8. parser.add_argument('--server',default='127.0.0.1', help='IRC server')
  9. parser.add_argument('--port', default=22024, help='port of the IRC server')
  10. parser.add_argument('--nickname', default='tau-notifier', help='bot nickname in IRC')
  11. parser.add_argument('--channel', default="#dev", action='append', help='channel to join')
  12. parser.add_argument('--pipe', default="/tmp/tau_pipe" , help='pipe to read from')
  13. parser.add_argument('--skip', default="prv", help='Project or Tags to skip notifications for')
  14. parser.add_argument('--skip-workspace', default="prv-ws", help='Workspace to skip notifications for')
  15. parser.add_argument('--alt-chan', default="#test", required='--skip' in sys.argv or '--skip-workspace' in sys.argv, help='Alternative channel to send notifications to when there are skipped tasks')
  16. args = parser.parse_args()
  17. channels = [args.channel, args.alt_chan] if args.alt_chan is not None else args.channel
  18. ircc = irc.IRC()
  19. ircc.connect(args.server, int(args.port), channels, args.nickname)
  20. def signal_handler(sig, frame):
  21. print("Caught termination signal, cleaning up and exiting...")
  22. ircc.disconnect(args.server, args.port)
  23. print("Shut down successfully")
  24. exit(0)
  25. signal.signal(signal.SIGINT, signal_handler)
  26. while True:
  27. with open(args.pipe) as handle:
  28. while True:
  29. log_line = handle.readline()
  30. if not log_line:
  31. break
  32. print(log_line)
  33. print("======================================")
  34. task = json.loads(log_line)
  35. channel = args.channel
  36. for event in task['events']:
  37. cmd = event['action']
  38. if cmd == "add_task":
  39. user = task['owner']
  40. refid = task['ref_id'][:6]
  41. title = task['title']
  42. assigned = ", ".join(task['assign'])
  43. project = task['project'] if task['project'] is not None else []
  44. if args.skip in project or '+' + args.skip in task['tags'] or args.skip_workspace in task['workspace']:
  45. channel = args.alt_chan
  46. if len(assigned) > 0:
  47. notification = f"{user} added task ({refid}): {title}. assigned to {assigned}"
  48. else:
  49. notification = f"{user} added task ({refid}): {title}"
  50. # print(notification)
  51. ircc.send(channel, notification)
  52. elif cmd == "state":
  53. user = event['author']
  54. state = event['content']
  55. refid = task['ref_id'][:6]
  56. title = task['title']
  57. project = task['project'] if task['project'] is not None else []
  58. if args.skip in project or '+' + args.skip in task['tags'] or args.skip_workspace in task['workspace']:
  59. channel = args.alt_chan
  60. if state == "start":
  61. notification = f"{user} started task ({refid}): {title}"
  62. elif state == "pause":
  63. notification = f"{user} paused task ({refid}): {title}"
  64. elif state == "stop":
  65. notification = f"{user} stopped task ({refid}): {title}"
  66. elif state == "cancel":
  67. notification = f"{user} canceled task ({refid}): {title}"
  68. # print(notification)
  69. ircc.send(channel, notification)
  70. elif cmd == "comment":
  71. user = event['author']
  72. refid = task['ref_id'][:6]
  73. title = task['title']
  74. project = task['project'] if task['project'] is not None else []
  75. if args.skip in project or '+' + args.skip in task['tags'] or args.skip_workspace in task['workspace']:
  76. channel = args.alt_chan
  77. notification = f"{user} commented on task ({refid}): {title}"
  78. # print(notification)
  79. ircc.send(channel, notification)
  80. elif cmd == "assign":
  81. user = event['author']
  82. assignees = event['content']
  83. refid = task['ref_id'][:6]
  84. title = task['title']
  85. project = task['project'] if task['project'] is not None else []
  86. if args.skip in project or '+' + args.skip in task['tags'] or args.skip_workspace in task['workspace']:
  87. channel = args.alt_chan
  88. removed = []
  89. added = []
  90. for assignee in assignees.split(', '):
  91. if assignee.startswith("-"):
  92. removed.append(assignee[1:])
  93. else:
  94. added.append(assignee)
  95. if removed and added:
  96. notification = f"{user} added {', '.join(added)} and removed {', '.join(removed)} from assign in task ({refid}): {title}"
  97. if (not removed) and added:
  98. notification = f"{user} added {', '.join(added)} to assign in task ({refid}): {title}"
  99. if (not added) and removed:
  100. notification = f"{user} removed {', '.join(removed)} from assign in task ({refid}): {title}"
  101. # print(notification)
  102. ircc.send(channel, notification)