diff --git a/index.js b/index.js index caa727f..c1a83e8 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ module.exports.getGithubCommit = async (build, octokit) => { try { let githubOwner; let githubRepo; - if (build.source.repoSource) { // mirrored repo triggered build + if (build.source && build.source.repoSource) { // mirrored repo triggered build const cloudSourceRepo = build.source.repoSource.repoName; [, githubOwner, githubRepo] = cloudSourceRepo.split('_'); } else { // github app triggered build diff --git a/test.js b/test.js index f9f0616..bab5886 100644 --- a/test.js +++ b/test.js @@ -5,7 +5,7 @@ const sinon = require('sinon'); const Octokit = require('@octokit/rest'); const lib = require('./index.js'); -const base64Build = 'eyJpZCI6IjE3NDBjZTJhLTYxZDktNGE1OC1iM2M3LWNmYWQ5OWRiOGQwYSIsInByb2plY3RJZCI6Im5vZGUtZXhhbXBsZS1na2UiLCJzdGF0dXMiOiJTVUNDRVNTIiwic291cmNlIjp7InJlcG9Tb3VyY2UiOnsicHJvamVjdElkIjoibm9kZS1leGFtcGxlLWdrZSIsInJlcG9OYW1lIjoibm9kZS1leGFtcGxlLWZyb250ZW5kIiwiYnJhbmNoTmFtZSI6Im1hc3RlciJ9fSwic3RlcHMiOlt7Im5hbWUiOiJnY3IuaW8vY2xvdWQtYnVpbGRlcnMvZG9ja2VyIiwiYXJncyI6WyJidWlsZCIsIi10IiwiZ2NyLmlvL25vZGUtZXhhbXBsZS1na2UvZnJvbnRlbmQ6NDg5OTFiNGE3Yjc0MThhMzVkMDBlZGVkMDI4YWUxZmMwNmE0ZmM3NSIsIi4iXX1dLCJyZXN1bHRzIjp7ImltYWdlcyI6W3sibmFtZSI6Imdjci5pby9ub2RlLWV4YW1wbGUtZ2tlL2Zyb250ZW5kOjQ4OTkxYjRhN2I3NDE4YTM1ZDAwZWRlZDAyOGFlMWZjMDZhNGZjNzUiLCJkaWdlc3QiOiJzaGEyNTY6ZDgyMTMyZDlmYTc4NTllNDA4NWFhZThhZjJlZmY2MmZhM2Q1MDhkYjlhOGZkNDE2OWVlN2I2MTh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Yjc0MThhMzVkMDBlZGVkMDI4YWUxZmMwNmE0ZmM3NSJ9fSwiYnVpbGRUcmlnZ2VySWQiOiJkZWZhdWx0LWdpdGh1Yi1jaGVja3MiLCJsb2dVcmwiOiJodHRwczovL2NvbnNvbGUuZGV2ZWxvcGVycy5nb29nbGUuY29tL2xvZ3Mvdmlld2VyP3Byb2plY3Q9bm9kZS1leGFtcGxlLWdrZVx1MDAyNnJlc291cmNlLmxhYmVscy5idWlsZF9pZD0xNzQwY2UyYS02MWQ5LTRhNTgtYjNjNy1jZmFkOTlkYjhkMGEiLCJzdWJzdGl0dXRpb25zIjp7IkJSQU5DSF9OQU1FIjoibWFzdGVyIiwiQ09NTUlUX1NIQSI6Im40ODk5MWI0YTdiNzQxOGEzNWQwMGVkZWQwMjhhZTFmYzA2YTRmYzc1IiwiUkVQT19OQU1FIjoibm9kZS1leGFtcGxlLWZyb250ZW5kIiwiUkVWSVNJT05fSUQiOiI0ODk5MWI0YTdiNzQxOGEzNWQwMGVkZWQwMjhhZTFmYzA2YTRmYzc1IiwiU0hPUlRfU0hBIjoiNDg5OTFiNGEiLCJUQUdfTkFNRSI6IiJ9LCJ0YWdzIjpbInRyaWdnZXItZGVmYXVsdC1naXRodWItY2hlY2tzIl19Cg=='; const nbCommonFields = 2; const MS_PER_MINUTE = 60000; @@ -26,6 +26,9 @@ describe('createSlackMessage', () => { status: 'SUCCESS', finishTime: '2017-03-19T00:08:12.220502Z', source: {}, + substitutions: { + REPO_NAME: 'horse', + }, }; const message = await lib.createSlackMessage(build); @@ -47,6 +50,9 @@ describe('createSlackMessage', () => { startTime: '2017-03-19T00:08:12.220502Z', finishTime: null, source: {}, + substitutions: { + REPO_NAME: 'horse', + }, }; const message = await lib.createSlackMessage(build); @@ -71,6 +77,9 @@ describe('createSlackMessage', () => { startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE), finishTime: now, source: {}, + substitutions: { + REPO_NAME: 'horse', + }, }; const message = await lib.createSlackMessage(build); @@ -88,6 +97,9 @@ describe('createSlackMessage', () => { startTime: new Date(now - deltaInMinutes * MS_PER_MINUTE), finishTime: null, source: {}, + substitutions: { + REPO_NAME: 'horse', + }, }; const message = await lib.createSlackMessage(build); @@ -103,6 +115,9 @@ describe('createSlackMessage', () => { finishTime: Date.now(), images: ['image-1', 'image-2'], source: {}, + substitutions: { + REPO_NAME: 'horse', + }, }; const message = await lib.createSlackMessage(build); @@ -119,9 +134,28 @@ describe('createSlackMessage', () => { attachment.fields[3].value.should.equal('master'); }); - it('should include the commit author in the message if a github commit is retrieved', async () => { + it('should include the commit author in the message if a github commit is retrieved for mirrored repo triggered build', async () => { const build = lib.eventToBuild(base64Build); - build.source.repoSource.repoName = 'github_artofwar_chapter1'; + const octokit = new Octokit({ + auth: 'token sjhfgsa', + }); + const stub = sinon.stub(octokit.git, 'getCommit') + .returns({ + data: { + author: { + name: 'Sun Tzu', + }, + }, + }); + const githubCommit = await lib.getGithubCommit(build, octokit); + const message = await lib.createSlackMessage(build, githubCommit); + const attachment = message.attachments[0]; + attachment.fields[4].value.should.equal('Sun Tzu'); + stub.reset(); + }); + + it('should include the commit author in the message if a github commit is retrieved for github app triggered build', async () => { + const build = lib.eventToBuild(base64BuildDefaultTrigger); const octokit = new Octokit({ auth: 'token sjhfgsa', }); @@ -149,10 +183,8 @@ describe('createSlackMessage', () => { }); it('should use the right color depending on the status', () => { - const build = { - id: 'build-id', - finishTime: Date.now(), - }; + const build = lib.eventToBuild(base64BuildDefaultTrigger); + const testCases = [ { status: 'QUEUED', @@ -295,7 +327,7 @@ describe('subscribe', () => { doneEach(); }); }, () => { - // clean the status list. + // clean the status list. lib.GC_SLACK_STATUS = null; }); }); @@ -340,7 +372,7 @@ describe('subscribe', () => { doneEach(); }); }, () => { - // clean the status list. + // clean the status list. lib.GC_SLACK_STATUS = null; }); });