Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/src/commands/ci/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Heroku from '@heroku-cli/schema'
import {ux} from '@oclif/core'

import * as Kolkrabbi from '../../lib/ci/interfaces/kolkrabbi.js'
import * as git from '../../lib/ci/git.js'
import {gitService} from '../../lib/ci/git.js'
import {getPipeline} from '../../lib/ci/pipelines.js'
import {createSourceBlob} from '../../lib/ci/source.js'
import {displayAndExit} from '../../lib/ci/test-run.js'
Expand All @@ -25,7 +25,7 @@ export default class CiRun extends Command {
async run() {
const {flags} = await this.parse(CiRun)
const pipeline = await getPipeline(flags, this.heroku)
const commit = await git.readCommit('HEAD')
const commit = await gitService.readCommit('HEAD')

ux.action.start('Preparing source')
const sourceBlobUrl = await createSourceBlob(commit.ref, this)
Expand Down
39 changes: 28 additions & 11 deletions packages/cli/src/lib/ci/git.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'fs-extra'
import {vars} from '@heroku-cli/command'
import {spawn} from 'node:child_process'

import fs from 'fs-extra'
import gh from 'github-url-to-object'
import {spawn} from 'node:child_process'
import tmp from 'tmp'

const NOT_A_GIT_REPOSITORY = 'not a git repository'
Expand Down Expand Up @@ -79,11 +78,11 @@ async function readCommit(commit: string) {
const ref = await getRef(commit)
const message = await getCommitTitle(ref!)

return Promise.resolve({
return {
branch,
ref,
message,
})
ref,
}
}

function sshGitUrl(app: string) {
Expand Down Expand Up @@ -144,14 +143,32 @@ async function createRemote(remote: string, url: string) {
return null
}

// GitService class for easier testing/stubbing
export class GitService {
async createArchive(ref: string) {
return createArchive(ref)
}

async githubRepository() {
return githubRepository()
}

async readCommit(commit: string) {
return readCommit(commit)
}
}

// Export a shared instance for use across commands
export const gitService = new GitService()

export {
createArchive,
githubRepository,
readCommit,
sshGitUrl,
gitUrl,
createRemote,
gitUrl,
githubRepository,
inGitRepo,
listRemotes,
readCommit,
rmRemote,
inGitRepo,
sshGitUrl,
}
34 changes: 25 additions & 9 deletions packages/cli/src/lib/ci/source.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import {Command} from '@heroku-cli/command'
import {promises as fs} from 'fs'
import {createReadStream} from 'fs'
import * as git from './git.js'
import {got} from 'got'
import debug from 'debug'
import {createReadStream, promises as fs} from 'fs'
import {got} from 'got'

import {gitService} from './git.js'

const ciDebug = debug('ci')

// FileService class for easier testing/stubbing
export class FileService {
createReadStream(filePath: string) {
return createReadStream(filePath)
}

async stat(filePath: string) {
return fs.stat(filePath)
}
}

const fileService = new FileService()

async function uploadArchive(url: string, filePath: string) {
const request = got.stream.put(url, {
headers: {
'content-length': (await fs.stat(filePath)).size.toString(),
'content-length': (await fileService.stat(filePath)).size.toString(),
},
})

createReadStream(filePath).pipe(request)
fileService.createReadStream(filePath).pipe(request)

return new Promise((resolve: any, reject: any) => {
request.on('error', reject)
Expand All @@ -23,15 +36,15 @@ async function uploadArchive(url: string, filePath: string) {
}

async function prepareSource(ref: any, command: Command) {
const filePath = await git.createArchive(ref)
const filePath = await gitService.createArchive(ref)
const {body: source} = await command.heroku.post<any>('/sources')
await uploadArchive(source.source_blob.put_url, filePath)
return Promise.resolve(source)
return source
}

export async function createSourceBlob(ref: any, command: Command) {
try {
const githubRepository = await git.githubRepository()
const githubRepository = await gitService.githubRepository()
const {user, repo} = githubRepository

const {body: archiveLink} = await command.heroku.get<any>(`https://kolkrabbi.heroku.com/github/repos/${user}/${repo}/tarball/${ref}`)
Expand All @@ -46,3 +59,6 @@ export async function createSourceBlob(ref: any, command: Command) {
const sourceBlob = await prepareSource(ref, command)
return sourceBlob.source_blob.get_url
}

// Export service instances for testing
export {fileService, gitService}
Loading
Loading