1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DanielPieper\MergeReminder\Service; |
4
|
|
|
|
5
|
|
|
use DanielPieper\MergeReminder\ValueObject\MergeRequestApproval; |
6
|
|
|
use Razorpay\Slack\Attachment; |
7
|
|
|
use Razorpay\Slack\Client; |
8
|
|
|
|
9
|
|
|
class SlackService |
10
|
|
|
{ |
11
|
|
|
/** @var Client */ |
12
|
|
|
private $slackClient; |
13
|
|
|
|
14
|
|
|
public function __construct(Client $slackClient) |
15
|
|
|
{ |
16
|
|
|
$this->slackClient = $slackClient; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param MergeRequestApproval[] $mergeRequestApprovals |
21
|
|
|
* @param string $messageText |
22
|
|
|
*/ |
23
|
|
|
public function postMessage(array $mergeRequestApprovals, $messageText = 'Your pending merge requests'): void |
24
|
|
|
{ |
25
|
|
|
$message = $this->slackClient->createMessage(); |
26
|
|
|
$message->setText($messageText); |
27
|
|
|
|
28
|
|
|
foreach ($mergeRequestApprovals as $mergeRequestApproval) { |
29
|
|
|
$attachment = $this->getAttachment($mergeRequestApproval); |
30
|
|
|
$message->attach($attachment); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->slackClient->sendMessage($message); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param MergeRequestApproval $mergeRequestApproval |
38
|
|
|
* @return Attachment |
39
|
|
|
*/ |
40
|
|
|
protected function getAttachment(MergeRequestApproval $mergeRequestApproval): Attachment |
41
|
|
|
{ |
42
|
|
|
$mergeRequest = $mergeRequestApproval->getMergeRequest(); |
43
|
|
|
$project = $mergeRequest->getProject(); |
44
|
|
|
$author = $mergeRequest->getAuthor(); |
45
|
|
|
|
46
|
|
|
$attachment = new Attachment([ |
47
|
|
|
'fallback' => $mergeRequest->getWebUrl(), |
48
|
|
|
'title' => $mergeRequest->getTitle(), |
49
|
|
|
'title_link' => $mergeRequest->getWebUrl(), |
50
|
|
|
'text' => $mergeRequest->getDescription(), |
51
|
|
|
'author_name' => $author->getUsername(), |
52
|
|
|
/* 'author_icon' => $author->getAvatarUrl(), */ |
53
|
|
|
'author_link' => $author->getWebUrl(), |
54
|
|
|
'color' => $this->getColor($mergeRequestApproval), |
55
|
|
|
'fields' => $this->getFields($mergeRequestApproval), |
56
|
|
|
'footer' => $project->getName(), |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
return $attachment; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getFields(MergeRequestApproval $mergeRequestApproval): array |
63
|
|
|
{ |
64
|
|
|
$fields = [ |
65
|
|
|
[ |
66
|
|
|
'title' => 'Created', |
67
|
|
|
'value' => $mergeRequestApproval->getCreatedAt()->shortRelativeToNowDiffForHumans(), |
68
|
|
|
'short' => true, |
69
|
|
|
], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if ($mergeRequestApproval->getUpdatedAt()->diffInDays($mergeRequestApproval->getCreatedAt()) > 0) { |
73
|
|
|
$fields[] = [ |
74
|
|
|
'title' => 'Updated', |
75
|
|
|
'value' => $mergeRequestApproval->getUpdatedAt()->shortRelativeToNowDiffForHumans(), |
76
|
|
|
'short' => true, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$approverNames = $mergeRequestApproval->getApproverNames(); |
81
|
|
|
if (count($approverNames) > 0) { |
82
|
|
|
$fields[] = [ |
83
|
|
|
'title' => 'Approvers', |
84
|
|
|
'value' => implode(', ', $approverNames), |
85
|
|
|
'short' => true, |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$approverGroupNames = $mergeRequestApproval->getApproverGroupNames(); |
90
|
|
|
if (count($approverGroupNames) > 0) { |
91
|
|
|
$fields[] = [ |
92
|
|
|
'title' => 'Approver Groups', |
93
|
|
|
'value' => implode(', ', $approverGroupNames), |
94
|
|
|
'short' => true, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $fields; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param MergeRequestApproval $mergeRequestApproval |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getColor(MergeRequestApproval $mergeRequestApproval): string |
106
|
|
|
{ |
107
|
|
|
$ageInDays = $mergeRequestApproval->getCreatedAt()->diffInDays(); |
108
|
|
|
|
109
|
|
|
if ($ageInDays > 2) { |
110
|
|
|
return 'danger'; |
111
|
|
|
} |
112
|
|
|
if ($ageInDays > 1) { |
113
|
|
|
return 'warning'; |
114
|
|
|
} |
115
|
|
|
return 'good'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|