1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* I needed something that sends emails on dev/build. Later it became this release notificator. |
5
|
|
|
* |
6
|
|
|
* @author [email protected] |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class ReleaseNotification extends DataObject |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private static $db = array( |
|
|
|
|
15
|
|
|
'Changelog' => 'Text', |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* We are abusing requireDefaultRecords to run this on dev/build. |
20
|
|
|
*/ |
21
|
|
|
public function requireDefaultRecords() |
22
|
|
|
{ |
23
|
|
|
parent::requireDefaultRecords(); |
24
|
|
|
|
25
|
|
|
// verbose messages |
26
|
|
|
DB::alteration_message('Searching for environment configuration for ' . Director::absoluteURL('/'), 'created'); |
27
|
|
|
|
28
|
|
|
// get the config based on the URL (URL is key) |
29
|
|
|
$config = $this->prepConfig($this->config()->get('environments'), Director::absoluteURL('/')); |
|
|
|
|
30
|
|
|
|
31
|
|
|
// only run if everything is fine |
32
|
|
|
if (!is_array($config) || empty($config)) { |
33
|
|
|
DB::alteration_message('No configuration found.', 'created'); |
34
|
|
|
} elseif (!file_exists('../' . $config['filename'])) { |
35
|
|
|
DB::alteration_message('No CHANGELOG.md-file found.', 'created'); |
36
|
|
|
} else { |
37
|
|
|
DB::alteration_message($config['environment_name'] . ' identified.', 'created'); |
38
|
|
|
|
39
|
|
|
// get the information in the database for the last release notification |
40
|
|
|
$record = (self::get()->count() == 0) ? new self() : self::get()->first(); |
41
|
|
|
|
42
|
|
|
// load the changelog and remove the first 4 lines |
43
|
|
|
$changelog = trim(preg_replace('/^(.*\n){4}/', '', file_get_contents('../' . $config['filename']))); |
44
|
|
|
|
45
|
|
|
// check if the CHANGELOG.md file has been changed since the last run |
46
|
|
|
if ($changelog == '') { |
47
|
|
|
DB::alteration_message('No CHANGELOG found.', 'created'); |
48
|
|
|
} elseif (md5($changelog) != md5($record->Changelog)) { |
49
|
|
|
// remove the former releases from the changelog |
50
|
|
|
$release = trim(str_replace($record->Changelog, '', $changelog)); |
51
|
|
|
|
52
|
|
|
// email the changelog out |
53
|
|
|
foreach ($config['recipients'] as $recipient) { |
54
|
|
|
Email::create( |
55
|
|
|
$config['from'], |
56
|
|
|
$recipient, |
57
|
|
|
$config['subject'], |
58
|
|
|
|
59
|
|
|
// remove the old part and add more useful information to the content |
60
|
|
|
sprintf("%s (%s)\n\n%s", $config['environment_name'], $config['url'], $release) |
61
|
|
|
)->sendPlain(); |
62
|
|
|
|
63
|
|
|
DB::alteration_message($recipient . ' notified', 'created'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// save the new changelog to ensure we aren't re-running this in the next step |
67
|
|
|
$record->Changelog = $changelog; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// say welcome :) |
71
|
|
|
if (!$record->ID) { |
72
|
|
|
DB::alteration_message('Install of FriendsOfSilverStripe/release-notifications'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$record->write(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* sorts the config a bit different and returns it. |
81
|
|
|
* |
82
|
|
|
* @param array $environments |
83
|
|
|
* @param string $url |
84
|
|
|
* |
85
|
|
|
* @return array (either configuration or empty) |
86
|
|
|
*/ |
87
|
|
|
public function prepConfig($environments, $url) |
88
|
|
|
{ |
89
|
|
|
$environmentsByURL = array(); |
90
|
|
|
if (!is_array($environments)) { |
91
|
|
|
$environments = array(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($environments as $environment) { |
95
|
|
|
if (is_array($environment) && array_key_exists('url', $environment)) { |
96
|
|
|
$environmentsByURL[$environment['url']] = $environment; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return array_key_exists($url, $environmentsByURL) ? $environmentsByURL[$url] : []; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|