-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrello_card_comment.php
232 lines (201 loc) · 6.8 KB
/
trello_card_comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Trello Card Comment
* Version: 2.0
* Author: John Money <[email protected]>
*
* Posts a new comment to Trello card for each a commit with a [cardID] in the
* commit message. Requires Terminus Secrets Manager Plugin to set trello_key,
* trello_token and trello_boardid with scope=user, web.
*/
//Trello supports markdown syntax in comments
define("COMMENT_FORMAT", "**%author** %message on %server");
//config options
$commentsOnMultidevByCardId = false;
$commentsOnMultidevByEnvName = true;
if (checkKeys())
{
$commits = getCommits($_SERVER['HOME'] . "/files/{$_ENV['PANTHEON_ENVIRONMENT']}_trello_last_commit.txt");
$processedCommits = array('trello' => array());
if ($_ENV['PANTHEON_ENVIRONMENT'] == "dev" || $commentsOnMultidevByCardId)
{
$processedCommits = getCommitsByCardId($commits);
}
else if ($_ENV['PANTHEON_ENVIRONMENT'] != "dev" && $commentsOnMultidevByEnvName)
{
if ($shortLink = getShortlink($_SERVER['HOME'] . "/files/{$_ENV['PANTHEON_ENVIRONMENT']}_trello_shortlink.txt", $_ENV['PANTHEON_ENVIRONMENT']))
{
$processedCommits = getCommitsByEnvName($commits, $shortLink);
}
}
// Check each commit message for Trello card IDs
foreach ($processedCommits['trello'] as $card_id => $commit_ids) {
foreach ($commit_ids as $commit_id) {
postComment($card_id,
array(
'%message' => $processedCommits['message'][$commit_id],
'%author' => $processedCommits['author'][$commit_id],
'%server' => "[{$_ENV['PANTHEON_ENVIRONMENT']}](https://{$_ENV['DRUSH_OPTIONS_URI']})"
)
);
}
}
}
/// <summary>
/// Check if requried keys are set
/// </summary>
/// <returns>
/// Bool true if keys are not null or empty
/// </returns>
function checkKeys()
{
$key = pantheon_get_secret('trello_key');
if ($key === null || trim($key) === '')
return false;
$key = pantheon_get_secret('trello_token');
if ($key === null || trim($key) === '')
return false;
$key = pantheon_get_secret('trello_boardid');
if ($key === null || trim($key) === '')
return false;
return true;
}
/// <summary>
/// Do git operations to find all commits between the specified commit hashes
/// </summary>
/// <returns>
/// Associative array containing all applicable commits that contain references to Trello cards.
/// </returns>
function getCommits($filepath)
{
// Get latest commit
$current_commithash = shell_exec('git rev-parse HEAD');
$last_commithash = false;
// Retrieve the last commit processed by this script
if (file_exists($filepath)) {
$last_processed_commithash = trim(file_get_contents($filepath));
// We should (almost) always find our last commit still in the repository;
// if the user has force-pushed a branch, though, then our last commit
// may be overwritten. If this happens, only process the most recent commit.
exec("git rev-parse $last_processed_commithash 2> /dev/null", $output, $status);
if (!$status) {
$last_commithash = $last_processed_commithash;
}
}
// Update the last commit file with the latest commit
file_put_contents($filepath, $current_commithash, LOCK_EX);
// Get commits in range
$cmd = 'git log --pretty="format:%h|%s|%cn"'; // add -p to include diff
if (!$last_commithash)
$cmd .= ' -n 1';
else
$cmd .= ' ' . $last_commithash . '...' . $current_commithash;
$cmdResult = shell_exec($cmd);
return explode("\n", $cmdResult);
}
function getCommitsByCardId($commitsRaw)
{
$commits = array(
// Formatted array of commits being sent to Trello
'message' => array(),
'author' => array(),
// An array keyed by Trello card id, each holding an
// array of commit ids.
'trello' => array()
);
foreach ($commitsRaw as $line)
{
list($commitId, $message, $author) = explode("|", $line, 3);
// Look for matches on a Trello card ID format
// = [8 characters]
preg_match('/\[[a-zA-Z0-9]{8}\]/', $message, $matches);
if (count($matches) > 0)
{
// Build the $commits['trello'] array so there is
// only 1 item per ticket id
foreach ($matches as $card_id_enc)
{
$card_id = substr($card_id_enc, 1, -1);
if (!isset($commits['trello'][$card_id]))
{
$commits['trello'][$card_id] = array();
}
// ... and only 1 item per commit id
$commits['trello'][$card_id][$commitId] = $commitId;
}
// Add the commit to the history array since there was a match.
$commits['message'][$commitId] = $message;
$commits['author'][$commitId] = $author;
}
}
return $commits;
}
function getCommitsByEnvName($commitsRaw, $shortLink)
{
$commits = array(
// Formatted array of commits being sent to Trello
'message' => array(),
'author' => array(),
// An array keyed by Trello card id, each holding an
// array of commit ids.
'trello' => array()
);
//find a valid shortLink from env name
foreach ($commitsRaw as $line)
{
list($commitId, $message, $author) = explode("|", $line, 3);
$commits['trello'][$shortLink][$commitId] = $commitId;
$commits['message'][$commitId] = $message;
$commits['author'][$commitId] = $author;
}
return $commits;
}
function getShortlink($filepath, $env)
{
$shortLink = false;
// Retrieve cached shortlink by this script
if (file_exists($filepath)) {
$shortLink = trim(file_get_contents($filepath));
echo " * cached shortLink: $shortLink\n";
}
else
{
$uri = 'https://api.trello.com/1/boards/' . pantheon_get_secret('trello_boardid') . '/cards?&key=' . pantheon_get_secret('trello_key') . '&token=' . pantheon_get_secret('trello_token');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
curl_close($ch);
foreach (json_decode($result, true) as $item)
{
if (strcasecmp($env, $item['shortLink']) == 0)
{
$shortLink = $item['shortLink'];
echo " * api returned shortLink:" . $item['shortLink'] . "\n";
break;
}
}
file_put_contents($filepath, $shortLink, LOCK_EX);
}
return $shortLink;
}
/// <summary>
/// Post comment on Trello card
/// </summary>
function postComment($cardId, $data) {
print(" * commenting on $cardId\n");
$payload = array(
'text' => str_replace(array_keys($data), array_values($data), COMMENT_FORMAT),
'key' => pantheon_get_secret('trello_key'),
'token' => pantheon_get_secret('trello_token')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.trello.com/1/cards/' . $cardId . '/actions/comments');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
curl_close($ch);
}