Skip to content

Commit c44977b

Browse files
author
Sean Harvey
committed
Merge pull request #37 from tractorcow/pulls/more-messaging
API Update messaging
2 parents da171f8 + a29ee3a commit c44977b

9 files changed

Lines changed: 46 additions & 22 deletions

_config/pipeline.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Pipeline:
77
FilteredCommits: "DNFinishedCommits"
88
Messages:
99
Success: 'Deployment for <project>/<environment> has successfully completed'
10-
Failure: 'Deployment for <project>/<environment> has failed'
10+
Failure: 'Deployment for <project>/<environment> has failed. See <pipelinelink>'
1111
Abort: 'Deployment for <project>/<environment> has been aborted'
1212
RollbackStarted: 'Deployment failed, rollback for <project>/<environment> has begun.'
1313
RollbackSuccess: 'Rollback for <project>/<environment> has successfully completed.'
@@ -43,7 +43,7 @@ UserConfirmationStep:
4343
# Messages only sent to requester
4444
Request-Requester: 'You requested approval for deployment of <project>/<environment>. Cancel? <abortlink>'
4545
# Messages only sent to specified recipients
46-
Request-Recipient: 'Deployment for <project>/<environment> requested by <requester>. Approve? <approvelink>'
46+
Request-Recipient: 'Deployment for <project>/<environment> requested by <requester>. Login to approve at <approvelink>'
4747
Subjects:
4848
# Subject line for all users
4949
Cancel: 'Deployment for <project>/<environment>: Cancelled'

code/model/Pipeline.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class Pipeline extends DataObject {
112112
private static $db = array(
113113
'Status' => 'Enum("Running,Complete,Failed,Aborted,Rollback,Queued","Queued")',
114114
'Config' => 'Text', // serialized array of configuration for this pipeline
115-
'SHA' => 'Varchar(255)'
115+
'SHA' => 'Varchar(255)',
116+
'LastMessageSent' => 'Varchar(255)' // ID of last message sent
116117
);
117118

118119
/**
@@ -205,10 +206,12 @@ public function getReplacements() {
205206
$environment = $this->Environment();
206207
return array(
207208
'<abortlink>' => Director::absoluteURL($this->Environment()->Link()),
209+
'<pipelinelink>' => Director::absoluteURL($this->Link()),
208210
'<requester>' => $author->Title,
209211
'<requester-email>' => $author->Email,
210212
'<environment>' => $environment->Name,
211-
'<project>' => $environment->Project()->Name
213+
'<project>' => $environment->Project()->Name,
214+
'<commitsha>' => $this->SHA
212215
);
213216
}
214217

@@ -539,7 +542,10 @@ public function markComplete() {
539542
$this->Status = "Complete";
540543
$this->log("Pipeline completed successfully.");
541544
$this->write();
542-
$this->sendMessage(self::ALERT_SUCCESS);
545+
// Some steps may pre-emptively send a success message before the pipeline itself has completed
546+
if($this->LastMessageSent !== self::ALERT_SUCCESS) {
547+
$this->sendMessage(self::ALERT_SUCCESS);
548+
}
543549
}
544550

545551
/**
@@ -652,8 +658,10 @@ protected function canStartRollback() {
652658
* Notify Pipeline that a step has failed and failure processing should kick in. If rollback steps are present
653659
* the pipeline will be put into 'Rollback' state. After rollback is complete, regardless of the rollback result,
654660
* the pipeline will be failed.
661+
*
662+
* @param bool $notify Set to false to disable notifications for this failure
655663
*/
656-
public function markFailed() {
664+
public function markFailed($notify = true) {
657665
// Abort all running or queued steps.
658666
$steps = $this->Steps();
659667
foreach($steps as $step) {
@@ -669,7 +677,7 @@ public function markFailed() {
669677
$this->Status = 'Failed';
670678
$this->log("Pipeline failed, not running rollback (not configured or not applicable yet).");
671679
$this->write();
672-
$this->sendMessage(self::ALERT_FAILURE);
680+
if($notify) $this->sendMessage(self::ALERT_FAILURE);
673681
}
674682
}
675683

@@ -754,16 +762,22 @@ public function injectMessageReplacements($message, $subject, $substitutions) {
754762
/**
755763
* Sends a specific message to all marked recipients, including the author of this pipeline
756764
*
757-
* @param string $messageID Message ID. One of 'Abort', 'Success', or 'Failure'
765+
* @param string $messageID Message ID. One of 'Abort', 'Success', or 'Failure', or some custom message
758766
* @return boolean True if successful
759767
*/
760-
protected function sendMessage($messageID) {
768+
public function sendMessage($messageID) {
761769
// Check message, subject, and additional arguments to include
762770
list($subject, $message) = $this->generateMessageTemplate($messageID);
763771
if(empty($subject) || empty($message)) {
764772
$this->log("Skipping sending message. None configured for $messageID");
765773
return true;
766774
}
775+
776+
// Save last sent message
777+
$this->LastMessageSent = $messageID;
778+
$this->write();
779+
780+
// Setup messaging arguments
767781
$arguments = array_merge(
768782
$this->getConfigSetting('PipelineConfig', 'ServiceArguments') ?: array(),
769783
array('subject' => $subject)

code/model/steps/EmergencyRollbackStep.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public function beginRollbackWindow() {
131131
$this->log(_t('EmergencyRollbackStep.BEGINROLLBACKWINDOW',
132132
"{$this->Title} is beginning a rollback window..."));
133133
$this->write();
134+
// Message author that the deployment is complete
135+
$this->Pipeline()->sendMessage(Pipeline::ALERT_SUCCESS);
134136
return true;
135137
}
136138

code/model/steps/PipelineStep.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class PipelineStep extends DataObject {
4848
'Status',
4949
'LastEdited'
5050
);
51-
51+
5252
/**
5353
* Cached of config merged with defaults
5454
*
@@ -136,11 +136,16 @@ public function finish() {
136136
$this->write();
137137
}
138138

139-
public function markFailed() {
139+
/**
140+
* Fail this pipeline step
141+
*
142+
* @param bool $notify Set to false to disable notifications for this failure
143+
*/
144+
public function markFailed($notify = true) {
140145
$this->Status = 'Failed';
141146
$this->log('Marking pipeline step as failed. See earlier log messages to determine cause.');
142147
$this->write();
143-
$this->Pipeline()->markFailed();
148+
$this->Pipeline()->markFailed($notify);
144149
}
145150

146151
/**

code/model/steps/RollbackStep.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ protected function startRevertDeploy() {
8282
$deployment->LeaveMaintenacePage = $this->doRestoreDB();
8383
$deployment->EnvironmentID = $pipeline->EnvironmentID;
8484
$deployment->SHA = $previous->SHA;
85+
$deployment->DeployerID = $pipeline->AuthorID;
8586
$deployment->write();
8687
$deployment->start();
8788
$this->RollbackDeploymentID = $deployment->ID;

code/model/steps/SmokeTestPipelineStep.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ public function start() {
169169
}
170170
}
171171

172-
public function markFailed() {
172+
public function markFailed($notify = true) {
173173
// Put up maintenance page after this fails
174174
$this->log("Smoke testing failed: Putting up emergency maintenance page");
175175
$this->Pipeline()->Environment()->enableMaintenace($this->Pipeline()->getLogger());
176176

177177
// Mark pipeline and step failed
178-
parent::markFailed();
178+
parent::markFailed($notify);
179179
}
180180

181181
}

code/model/steps/UserConfirmationStep.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ public function getMessagingService() {
122122
return $this->messagingService;
123123
}
124124

125-
public function __isset($property) {
126-
// Workaround fixed in https://github.com/silverstripe/silverstripe-framework/pull/3201
127-
// Remove this once we update to a version of framework which supports this
128-
if($property === 'MessagingService') return !empty($this->messagingService);
129-
return parent::__isset($property);
130-
}
131-
132125
/**
133126
* Determine if the confirmation has been responded to (ether with acceptance, rejection, or cancelled)
134127
*
@@ -218,7 +211,7 @@ public function reject() {
218211
$this->Approval = 'Rejected';
219212
$this->log("{$this->Title} has been rejected");
220213
$this->ResponderID = Member::currentUserID();
221-
$this->markFailed();
214+
$this->markFailed(false);
222215
$this->sendMessage(self::ALERT_REJECT);
223216
return true;
224217
}

tests/EmergencyRollbackStepTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ public function testBeginRollbackWindow() {
114114

115115
$this->logInWithPermission('APPLY_ROLES');
116116
$this->assertTrue($step->beginRollbackWindow());
117+
118+
// Check that the message is sent
119+
$this->assertSentMessage('Deployment for testproject/uat has successfully completed', 'test@example.com');
117120
}
118121

119122
public function testRunningConfiguration() {
@@ -131,5 +134,8 @@ public function testStartFail() {
131134
$step->Status = 'Failed';
132135

133136
$this->assertFalse($step->start());
137+
138+
// Check that the message is sent
139+
$this->assertNotSentMessage('Deployment for testproject/uat has successfully completed', 'test@example.com');
134140
}
135141
}

tests/UserConfirmationStepTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ public function testRejection() {
161161
$this->assertEquals('Failed', $step->Status);
162162
$this->assertEquals('Rejected', $step->Approval);
163163
$this->assertHasLog('TestConfirmStep has been rejected');
164+
$this->assertNotSentMessage('Deployment for testproject/uat has failed', 'test@example.com');
165+
$this->assertNotSentMessage('Deployment for testproject/uat has failed', 'abort@example.com');
166+
$this->assertNotSentMessage('Deployment for testproject/uat has failed', 'errors@example.com');
164167
$this->assertSentMessage('Deployment for testproject/uat has been rejected', 'test@example.com');
165168
$this->assertSentMessage('Deployment for testproject/uat has been rejected', 'admin@example.com');
166169
}

0 commit comments

Comments
 (0)