Flyspray - The bug killer!

  • Status Unconfirmed
  • Percent Complete
    0%
  • Task Type Feature Request
  • Category Notifications
  • Assigned To No-one
  • Operating System All
  • Severity Low
  • Priority Very Low
  • Reported Version 1.0-rc7
  • Due in Version Undecided
  • Due Date Undecided
  • Votes
  • Private
Attached to Project: Flyspray - The bug killer!
Opened by Gunther Vottela - 24.10.2018
Last edited by peterdd - 25.10.2018

FS#2520 - Send a notification for a new task in slack integration

Hi, i made a way to integrate slack in flyspray using webhooks.. i not added to git.. if some can add for me it’s ok.

The process is simple.. maybe in the front the team can add a field for add the webhook for each action could be better.. in this moment the webhook is triggered only for newtask using the log method.. but you can simply add some “if” for each “type” of log.

Process:

1. Create a new field in the table flyspray_users named “slack”, and add the user slack for each user.
2. Edit the file /includes/class.flyspray.php , go to the function “logEvent” and change it by this:

public static function logEvent($task_id, $type, $newvalue = '', $oldvalue = '', $field = '', $time = null)
			{
				global $db, $user;
 
				// This function creates entries in the history table.  These are the event types:
				//  0: Fields changed in a task
				//  1: New task created
				//  2: Task closed
				//  3: Task edited (for backwards compatibility with events prior to the history system)
				//  4: Comment added
				//  5: Comment edited
				//  6: Comment deleted
				//  7: Attachment added
				//  8: Attachment deleted
				//  9: User added to notification list
				// 10: User removed from notification list
				// 11: Related task added to this task
				// 12: Related task removed from this task
				// 13: Task re-opened
				// 14: Task assigned to user / re-assigned to different user / Unassigned
				// 15: This task was added to another task's related list
				// 16: This task was removed from another task's related list
				// 17: Reminder added
				// 18: Reminder deleted
				// 19: User took ownership
				// 20: Closure request made
				// 21: Re-opening request made
				// 22: Adding a new dependency
				// 23: This task added as a dependency of another task
				// 24: Removing a dependency
				// 25: This task removed from another task's dependency list
				// 26: Task was made private
				// 27: Task was made public
				// 28: PM request denied
				// 29: User added to the list of assignees
				// 30: New user registration
				// 31: User deletion
				// 32: Add new subtask
				// 33: Remove Subtask
				// 34: Add new parent
				// 35: Remove parent
 
				$query_params = array(intval($task_id), intval($user->id),
				((!is_numeric($time)) ? time() : $time),
				$type, $field, $oldvalue, $newvalue);
 
                               // CREATE A NOTIFICATION IN SLACK WHEN A NEW TASK IS CREATED ONLY TO THE USER SELECTED
				if ($type == 14) {
 
					$usuarios = $db->query("SELECT DISTINCT a.slack, c.item_summary, d.project_title
						FROM {users} a
						JOIN {assigned} b on b.user_id = a.user_id
						JOIN {tasks} c on c.task_id = b.task_id
						JOIN {projects} d on d.project_id = c.project_id
						WHERE b.task_id = ?
						and a.slack <> '-'", array(intval($task_id)));
						$slackUsers = $db->fetchAllArray($usuarios);
 
						foreach ($slackUsers as $users) {
							    // GET THE USERS FOR IN THE SLACK TO SEND THE NOTIFICATION.
							$data = array(
								'text' => "New task: Project: " . $users['project_title'] . " | summary: " . $users['item_summary'] . " | taskId:" . $task_id ,
								'username' => "bott inslack",
								'link_names' => "1",
								'channel' =>  "@" . $users['slack'],
								'icon_emoji' => ":panda_face:"
							);
 
							$options = array(
								'http' => array(
									'header'  => 'Content-type: application/x-www-form-urlencoded\r\n',
									'method'  => 'POST',
									'content' => json_encode($data)
								)
							);
 
                                                      // DEFINE SLACK WEBHOOK
							$context  = stream_context_create($options);
							$result = file_get_contents("https://hooks.slack.com/services/xxxxxxxxx", false, $context);
 
						}
 
					}												
 
					if($db->query('INSERT INTO {history} (task_id, user_id, event_date, event_type, field_changed,
						old_value, new_value) VALUES (?, ?, ?, ?, ?, ?, ?)', $query_params)) {
 
							return true;
						}
 
						return false;
					}
Project Manager
peterdd commented on 25.10.2018 02:31

Sending notifications or making requests to foreign controlled services in the same user http request should be avoided. We cannot be sure if the other service is reachable or how it reacts.

Added the Slack and also Telegram tag.

Gunther Vottela commented on 25.10.2018 15:56

Hi Peter,

I added this to my flyspray instance in my company and works very well.. is only a curl request, obviusly i found a simple way because many people ask for that.. maybe the "correct way" should be have a library or module that recevie some parameters like:

integration: slack / telegram / etc
message:
users: array of them
webhook or endpoint:

And the controller call to this function for trigger the action.. i not develop in symphony so is complex for me.. take me some time understand the code.. i can do it faster in CI for example :). but maybe is a begin.

Loading...

Available keyboard shortcuts

Tasklist

Task Details

Task Editing