Skip to content

Commit

Permalink
feat(1668): add more information to email notifications (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshwata authored and tkyi committed Nov 15, 2019
1 parent ec15dc3 commit 9da3ce8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,40 @@ class EmailNotifier extends NotificationBase {
return;
}

const changedFiles = Hoek.reach(buildData, 'build.meta.commit.changedFiles').split(',');

let changedFilesStr = '';

if (changedFiles.length > 0 && changedFiles[0] !== '') {
changedFiles.forEach((file) => {
const li = '<li>{{contents}}</li>';

changedFilesStr += tinytim.tim(li, { contents: file });
});
} else {
changedFilesStr = 'There are no changed files.';
}

const ul = '<ul>{{list}}</ul>';

changedFilesStr = tinytim.tim(ul, { list: changedFilesStr });

const subject = `${buildData.status} - Screwdriver ` +
`${Hoek.reach(buildData, 'pipeline.scmRepo.name')} ` +
`${buildData.jobName} #${Hoek.reach(buildData, 'build.id')}`;
const message = `Build status: ${buildData.status}` +
`\nBuild link:${buildData.buildLink}`;
const commitSha = Hoek.reach(buildData, 'build.meta.build.sha').slice(0, 7);
const commitMessage = Hoek.reach(buildData, 'build.meta.commit.message');
const commitLink = Hoek.reach(buildData, 'build.meta.commit.url');
const html = tinytim.renderFile(path.resolve(__dirname, './template/email.html'), {
buildStatus: buildData.status,
buildLink: buildData.buildLink,
buildId: buildData.build.id,
changedFiles: changedFilesStr,
commitSha,
commitMessage,
commitLink,
statusColor: COLOR_MAP[buildData.status]
});

Expand Down
2 changes: 2 additions & 0 deletions template/email.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ <h3><b>Screwdriver Build Notification</b></h3>
</tbody>
</table>
<p><b>Build URL:</b> <a href="{{buildLink}}" target="_blank">{{buildLink}}</a></p>
<p><b>Changed files:</b>{{changedFiles}}</p>
<p><b>Commit:</b>{{commitMessage}}(<a href="{{commitLink}}" target="_blank">{{commitSha}}</a>)</p>
</td>
</tr>
</table>
Expand Down
94 changes: 91 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,20 @@ describe('index', () => {
}
},
jobName: 'publish',
build: { id: '1234' },
build: {
id: '1234',
meta: {
build: {
sha: '123abc'
},
commit: {
changedFiles: 'foo.txt,bar,txt',
message: 'update something',
url: `https://ghe.corp.dummy/screwdriver-cd/
notifications/commit/85b159c5457441c9bc9ff1bc9944f4f6bbd1ff89`
}
}
},
event: {
id: '12345',
causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications',
Expand Down Expand Up @@ -166,6 +179,55 @@ describe('index', () => {
});
});

it(`sets addresses and statuses for simple
email string config settings with no changed files`, (done) => {
const buildDataMockSimple = {
settings: {
email: '[email protected]'
},
status: 'FAILURE',
pipeline: {
id: '123',
scmRepo: { name: 'screwdriver-cd/notifications' }
},
jobName: 'publish',
build: {
id: '1234',
meta: {
build: {
sha: '123abc'
},
commit: {
changedFiles: '',
message: 'update something',
url: `https://ghe.corp.dummy/screwdriver-cd/
notifications/commit/85b159c5457441c9bc9ff1bc9944f4f6bbd1ff89`
}
}
},
event: {
id: '12345',
causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications',
creator: { username: 'foo' },
commit: {
author: { name: 'foo' },
message: 'fixing a bug'
}
},
buildLink: 'http://thisisaSDtest.com/builds/1234'
};

serverMock.event(eventMock);
serverMock.events.on(eventMock, data => notifier.notify(data));
serverMock.events.emit(eventMock, buildDataMockSimple);

process.nextTick(() => {
assert.calledWith(nodemailerMock.createTransport,
{ host: configMock.host, port: configMock.port });
done();
});
});

it('sets addresses and statuses for simple email string config settings', (done) => {
const buildDataMockSimple = {
settings: {
Expand All @@ -177,7 +239,20 @@ describe('index', () => {
scmRepo: { name: 'screwdriver-cd/notifications' }
},
jobName: 'publish',
build: { id: '1234' },
build: {
id: '1234',
meta: {
build: {
sha: '123abc'
},
commit: {
changedFiles: 'foo.txt,bar,txt',
message: 'update something',
url: `https://ghe.corp.dummy/screwdriver-cd/
notifications/commit/85b159c5457441c9bc9ff1bc9944f4f6bbd1ff89`
}
}
},
event: {
id: '12345',
causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications',
Expand Down Expand Up @@ -212,7 +287,20 @@ describe('index', () => {
scmRepo: { name: 'screwdriver-cd/notifications' }
},
jobName: 'publish',
build: { id: '1234' },
build: {
id: '1234',
meta: {
build: {
sha: '123abc'
},
commit: {
changedFiles: 'foo.txt,bar,txt',
message: 'update something',
url: `https://ghe.corp.dummy/screwdriver-cd/
notifications/commit/85b159c5457441c9bc9ff1bc9944f4f6bbd1ff89`
}
}
},
event: {
id: '12345',
causeMessage: 'Merge pull request #26 from screwdriver-cd/notifications',
Expand Down

0 comments on commit 9da3ce8

Please sign in to comment.