Skip to content

Commit

Permalink
fix(ci): unknown local commit path (#1019)
Browse files Browse the repository at this point in the history
Fix ci:run when it encounters an unknown commit
  • Loading branch information
jabrown85 committed Sep 7, 2018
1 parent 9833c4e commit ce84d86
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
12 changes: 7 additions & 5 deletions packages/ci/src/utils/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const git = require('./git')
import {Command} from '@heroku-cli/command'

import * as fs from 'async-file'
import {ux} from 'cli-ux'

async function uploadArchive(url: string, filePath: string) {
const request = got.stream.put(url, {
Expand All @@ -21,10 +22,8 @@ async function uploadArchive(url: string, filePath: string) {
}

async function prepareSource(ref: any, command: Command) {
const [filePath, source] = await [
git.createArchive(ref),
command.heroku.post('/sources', {body: command})
]
const filePath = await git.createArchive(ref)
const {body: source} = await command.heroku.post<any>('/sources')
await uploadArchive(source.source_blob.put_url, filePath)
return Promise.resolve(source)
}
Expand All @@ -42,7 +41,10 @@ export async function createSourceBlob(ref: any, command: Command) {
if (await urlExists(archiveLink.archive_link)) {
return archiveLink.archive_link
}
} catch (ex) { command.error(ex) }
} catch (ex) {
// the commit isn't in the repo, we will package the local git commit instead
ux.debug(`Commit not found in pipeline repository: ${ex}`)
}

const sourceBlob = await prepareSource(ref, command)
return sourceBlob.source_blob.get_url
Expand Down
94 changes: 93 additions & 1 deletion packages/ci/test/commands/ci/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Nock from '@fancy-test/nock'
import * as Test from '@oclif/test'
import * as fs from 'async-file'
const got = require('got')

import * as git from '../../../src/utils/git'

Expand Down Expand Up @@ -37,7 +39,7 @@ describe('ci:run', () => {
getRef: () => Promise.resolve(ghRepository.ref),
getCommitTitle: () => Promise.resolve(`pushed to ${ghRepository.branch}`),
githubRepository: () => Promise.resolve({user: ghRepository.user, repo: ghRepository.repo}),
createArchive: () => Promise.resolve('https://someurl'),
createArchive: () => Promise.resolve('new-archive.tgz'),
spawn: () => Promise.resolve(),
urlExists: () => Promise.resolve(),
exec: (args: any) => {
Expand All @@ -50,6 +52,21 @@ describe('ci:run', () => {
}
}

const fsFake = {
stat: () => Promise.resolve({size: 500}),
createReadStream: () => ({pipe: () => {}})
}

const gotFake = {
stream: { put: () => {
return {
on: (eventName: string, callback: () => void) => {
if (eventName === 'response') callback()
}
}
}}
}

test
.stdout()
.nock('https://api.heroku.com', api => {
Expand Down Expand Up @@ -120,5 +137,80 @@ describe('ci:run', () => {
.it('it runs the test and displays the test output for the first node', ({stdout}) => {
expect(stdout).to.equal('New Test setup outputNew Test output\n✓ #11 my-test-branch:668a5ce succeeded\n')
})

describe('when the commit is not in the remote repository', function () {
test
.stdout()
.nock('https://api.heroku.com', api => {
api.get(`/pipelines?eq[name]=${pipeline.name}`)
.reply(200, [
{id: pipeline.id}
])

api.post('/test-runs')
.reply(200, newTestRun)

api.get(`/pipelines/${pipeline.id}/test-runs/${newTestRun.number}`)
.reply(200, newTestRun)

api.get(`/test-runs/${newTestRun.id}/test-nodes`)
.times(2)
.reply(200, [
{
commit_branch: newTestRun.commit_branch,
commit_message: newTestRun.commit_message,
commit_sha: newTestRun.commit_sha,
id: newTestRun.id,
number: newTestRun.number,
pipeline: {id: pipeline.id},
exit_code: 0,
status: newTestRun.status,
setup_stream_url: `https://test-setup-output.heroku.com/streams/${newTestRun.id.substring(0, 3)}/test-runs/${newTestRun.id}`,
output_stream_url: `https://test-output.heroku.com/streams/${newTestRun.id.substring(0, 3)}/test-runs/${newTestRun.id}`
}
])

api.post('/sources')
.reply(200, {source_blob: {put_url: 'https://aws-puturl', get_url: 'https://aws-geturl'}})
})
.nock('https://test-setup-output.heroku.com/streams', testOutputAPI => {
testOutputAPI.get(`/${newTestRun.id.substring(0, 3)}/test-runs/${newTestRun.id}`)
.reply(200, 'New Test setup output')
})
.nock('https://test-output.heroku.com/streams', testOutputAPI => {
testOutputAPI.get(`/${newTestRun.id.substring(0, 3)}/test-runs/${newTestRun.id}`)
.reply(200, 'New Test output')
})
.nock('https://kolkrabbi.heroku.com', kolkrabbiAPI => {
kolkrabbiAPI.get(`/github/repos/${ghRepository.user}/${ghRepository.repo}/tarball/${ghRepository.ref}`)
.reply(404)
kolkrabbiAPI.get(`/pipelines/${pipeline.id}/repository`)
.reply(200, {
ci: true,
organization: {id: 'e037ed63-5781-48ee-b2b7-8c55c571b63e'},
owner: {
id: '463147bf-d572-41cf-bbf4-11ebc1c0bc3b',
heroku: {
user_id: '463147bf-d572-41cf-bbf4-11ebc1c0bc3b' },
github: {user_id: 306015}
},
repository: {
id: 138865824,
name: 'raulb/atleti',
type: 'github'
}
})
})
.stub(git, 'readCommit', gitFake.readCommit)
.stub(git, 'githubRepository', gitFake.githubRepository)
.stub(git, 'createArchive', gitFake.createArchive)
.stub(fs, 'stat', fsFake.stat)
.stub(fs, 'createReadStream', fsFake.createReadStream)
.stub(got, 'stream', gotFake.stream)
.command(['ci:run', `--pipeline=${pipeline.name}`])
.it('it runs the test and displays the test output for the first node', ({stdout}) => {
expect(stdout).to.equal('New Test setup outputNew Test output\n✓ #11 my-test-branch:668a5ce succeeded\n')
})
})
})
})

0 comments on commit ce84d86

Please sign in to comment.