@@ -4,26 +4,52 @@ import { createTransport } from 'nodemailer'
4
4
import * as Mail from 'nodemailer/lib/mailer'
5
5
import { DocumentInfo } from './DocumentInfo'
6
6
import * as Handlebars from 'handlebars'
7
+ import { Logger } from 'loglevel'
8
+ import { Moment } from 'moment'
9
+ import log = require( 'loglevel' )
10
+ import moment = require( 'moment' )
7
11
8
12
export class Notification {
9
13
private _configuration : Configuration
10
14
private _confluence : Confluence
11
15
private _transport : Mail
16
+ private _log : Logger
17
+ private readonly _dryRun : boolean
12
18
13
- constructor ( configuration : Configuration , confluence : Confluence , transport : Mail = null ) {
19
+ constructor ( configuration : Configuration , confluence : Confluence , transport : Mail = null , dryRun = false ) {
14
20
this . _configuration = configuration
15
21
this . _confluence = confluence
16
22
this . _transport = transport || createTransport ( this . _configuration . transportOptions )
23
+
24
+ this . _log = log . getLogger ( 'Notification' )
25
+ this . _dryRun = dryRun
17
26
}
18
27
19
28
public async notify ( documentInfo : DocumentInfo ) : Promise < void > {
29
+ Handlebars . registerHelper ( 'moment' , ( text , format ) => {
30
+ return moment ( text ) . format ( format )
31
+ } )
20
32
const subjectTemplate = Handlebars . compile ( this . _configuration . notificationSubjectTemplate )
21
33
const bodyTemplate = Handlebars . compile ( this . _configuration . notificationBodyTemplate )
22
- await this . _transport . sendMail ( {
34
+
35
+ documentInfo . lastVersionDate = ( documentInfo . lastVersionDate as Moment ) . toISOString ( ) as string
36
+
37
+ for ( const maintainer of this . _configuration . maintainer ) {
38
+ if ( maintainer . pagePattern . test ( documentInfo . title ) ) {
39
+ documentInfo . author = maintainer . maintainer
40
+ }
41
+ }
42
+
43
+ const mailOptions = {
23
44
from : this . _configuration . notificationFrom ,
24
45
to : `${ documentInfo . author } @${ this . _configuration . domain } ` ,
25
46
subject : subjectTemplate ( documentInfo ) ,
26
47
html : bodyTemplate ( documentInfo ) ,
27
- } )
48
+ }
49
+ this . _log . info ( `Notifying ${ mailOptions . to } about ${ documentInfo . title } ` )
50
+ this . _log . trace ( mailOptions )
51
+ if ( ! this . _dryRun ) {
52
+ await this . _transport . sendMail ( mailOptions )
53
+ }
28
54
}
29
55
}
0 commit comments