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
|
|
|
} else if (!file_exists('../' . $config['filename'])) { |
35
|
|
|
DB::alteration_message('No CHANGELOG.md-file found.', 'created'); |
36
|
|
|
} else { |
37
|
|
|
// get the information in the database for the last release notification |
38
|
|
|
$record = (self::get()->count() == 0) ? new self() : self::get()->first(); |
39
|
|
|
|
40
|
|
|
// load the changelog |
41
|
|
|
$changelog = file_get_contents('../' . $config['filename']); |
42
|
|
|
|
43
|
|
|
// check if the CHANGELOG.md file has been changed since the last run |
44
|
|
|
if ($changelog != '' && md5($changelog) != md5($record->Changelog)) { |
45
|
|
|
// email the changelog out |
46
|
|
|
foreach ($config['recipients'] as $recipient) { |
47
|
|
|
Email::create( |
48
|
|
|
$config['from'], |
49
|
|
|
$recipient, |
50
|
|
|
$config['subject'], |
51
|
|
|
|
52
|
|
|
// remove the old part and add more useful information to the content |
53
|
|
|
sprintf( |
54
|
|
|
"%s (%s)\n\n%s", |
55
|
|
|
$config['environment_name'], |
56
|
|
|
$config['url'], |
57
|
|
|
$changelog |
58
|
|
|
) |
59
|
|
|
)->sendPlain(); |
60
|
|
|
|
61
|
|
|
DB::alteration_message($recipient . ' notified', 'created'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// save the new changelog to ensure we aren't re-running this in the next step |
65
|
|
|
$record->Changelog = $changelog; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// say welcome :) |
69
|
|
|
if (!$record->ID) { |
70
|
|
|
DB::alteration_message('Install of FriendsOfSilverStripe/release-notifications'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$record->write(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* sorts the config a bit different and returns it. |
79
|
|
|
* |
80
|
|
|
* @param array $environments |
81
|
|
|
* @param string $url |
82
|
|
|
* |
83
|
|
|
* @return array (either configuration or empty) |
84
|
|
|
*/ |
85
|
|
|
public function prepConfig($environments, $url) |
86
|
|
|
{ |
87
|
|
|
$environmentsByURL = array(); |
88
|
|
|
if (!is_array($environments)) { |
89
|
|
|
$environments = array(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ($environments as $environment) { |
93
|
|
|
if (is_array($environment) && array_key_exists('url', $environment)) { |
94
|
|
|
$environmentsByURL[$environment['url']] = $environment; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return array_key_exists($url, $environmentsByURL) ? $environmentsByURL[$url] : []; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.