Skip to content

Commit

Permalink
fix: pipeline diff fails on v10
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwilaby committed Dec 21, 2024
1 parent 8954349 commit b99b58e
Show file tree
Hide file tree
Showing 8 changed files with 6,349 additions and 5,742 deletions.
47 changes: 30 additions & 17 deletions packages/cli/src/commands/pipelines/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {Command, flags} from '@heroku-cli/command'
import {ux} from '@oclif/core'
import HTTP from 'http-call'

import {getCoupling, getReleases, listPipelineApps, SDK_HEADER} from '../../lib/api'
import {getCoupling, getPipeline, getReleases, listPipelineApps, SDK_HEADER} from '../../lib/api'
import KolkrabbiAPI from '../../lib/pipelines/kolkrabbi-api'
import {OciImage, Slug} from '../../lib/types/fir'
import type {OciImage, Slug, Pipeline, PipelineCoupling} from '../../lib/types/fir'

interface AppInfo {
name: string;
Expand Down Expand Up @@ -35,19 +35,26 @@ async function diff(targetApp: AppInfo, downstreamApp: AppInfo, githubToken: str
// Do the actual GitHub diff
try {
const path = `${targetApp.repo}/compare/${downstreamApp.hash}...${targetApp.hash}`
const headers: { authorization: string; 'user-agent'?: string} = {authorization: 'token ' + githubToken}
const headers = {
authorization: 'token ' + githubToken,
'Content-Type': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
}

if (herokuUserAgent) {
headers['user-agent'] = herokuUserAgent
Reflect.set(headers, 'user-agent', herokuUserAgent)
}

const githubDiff: any = await HTTP.get(`https://api.github.com/repos/${path}`, {
headers,
}).then(res => res.body)
let githubDiff: GitHubDiff
try {
({body: githubDiff} = await HTTP.get<GitHubDiff>(`https://api.github.com/repos/${path}`, {headers}))
} catch {
throw new Error(`unable to perform diff for ${targetApp.name} and ${downstreamApp.name}. Are you sure you have pushed your latest commits to GitHub?`)
}

ux.log('')
ux.styledHeader(`${color.app(targetApp.name)} is ahead of ${color.app(downstreamApp.name)} by ${githubDiff.ahead_by} commit${githubDiff.ahead_by === 1 ? '' : 's'}`)
const mapped = githubDiff.commits.map((commit: any) => {
const mapped = githubDiff.commits.map((commit: Commit) => {
return {
sha: commit.sha.slice(0, 7),
date: commit.commit.author.date,
Expand All @@ -64,7 +71,6 @@ async function diff(targetApp: AppInfo, downstreamApp: AppInfo, githubToken: str
message: {},
})
ux.log(`\nhttps://github.com/${path}`)
// tslint:disable-next-line: no-unused
} catch {
ux.log(`\n${color.app(targetApp.name)} was not compared to ${color.app(downstreamApp.name)} because we were unable to perform a diff`)
ux.log('are you sure you have pushed your latest commits to GitHub?')
Expand Down Expand Up @@ -126,20 +132,27 @@ export default class PipelinesDiff extends Command {
const {flags} = await this.parse(PipelinesDiff)
const targetAppName = flags.app

const coupling = await getCoupling(this.heroku, targetAppName)
.then(res => res.body)
.catch(() => {})

if (!coupling) {
let coupling: PipelineCoupling | undefined
try {
({body: coupling} = await getCoupling(this.heroku, targetAppName))
} catch {
ux.error(`This app (${targetAppName}) does not seem to be a part of any pipeline`)
return
}

const targetAppId = coupling.app.id!
const generation = coupling.generation
let pipeline: Pipeline
try {
({body: pipeline} = await getPipeline(this.heroku, coupling.pipeline!.id!))
} catch {
ux.error(`Unable to fetch pipeline ${coupling.pipeline!.name}`)
return
}

const targetAppId = coupling!.app!.id!
const generation = pipeline!.generation!.name!

ux.action.start('Fetching apps from pipeline')
const allApps = await listPipelineApps(this.heroku, coupling.pipeline.id!)
const allApps = await listPipelineApps(this.heroku, coupling!.pipeline!.id!)
ux.action.stop()

const sourceStage = coupling.stage
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {APIClient} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {App, PipelineCoupling, Release} from './types/fir'
import {App, Pipeline, PipelineCoupling, Release} from './types/fir'

export const V3_HEADER = 'application/vnd.heroku+json; version=3'
export const SDK_HEADER = 'application/vnd.heroku+json; version=3.sdk'
Expand Down Expand Up @@ -66,7 +66,7 @@ export function getCoupling(heroku: APIClient, app: string) {
}

export function getPipeline(heroku: APIClient, id: string) {
return heroku.request<Heroku.Pipeline>(`/pipelines/${id}`, {
return heroku.request<Pipeline>(`/pipelines/${id}`, {
method: 'GET',
headers: {Accept: PIPELINES_HEADER},
})
Expand Down Expand Up @@ -138,7 +138,7 @@ export function updateCoupling(heroku: APIClient, app: string, stage: string) {
}

export function getReleases(heroku: APIClient, appId: string) {
return heroku.get<Array<Release>>(`/apps/${appId}/releases`, {
return heroku.get<Array<Heroku.Release>>(`/apps/${appId}/releases`, {
headers: {Accept: SDK_HEADER, Range: 'version ..; order=desc'},
partial: true,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/apps/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ async function getApp(appOrName: App | string, herokuApi?: APIClient): Promise<A

export async function isFirApp(appOrName: App | string, herokuApi?: APIClient) {
const app = await getApp(appOrName, herokuApi)
return app.generation === 'fir'
return app.generation.name === 'fir'
}

export async function isCedarApp(appOrName: App | string, herokuApi?: APIClient) {
const app = await getApp(appOrName, herokuApi)
return app.generation === 'cedar'
return app.generation.name === 'cedar'
}
5 changes: 3 additions & 2 deletions packages/cli/src/lib/buildpacks/buildpacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {ux} from '@oclif/core'
import {findIndex as lodashFindIndex} from 'lodash'
import {Result} from 'true-myth'
import push from '../git/push'
import {OciImage, Release} from '../../lib/types/fir'
import {OciImage} from '../../lib/types/fir'
import {isURL} from 'validator'
import * as Heroku from '@heroku-cli/schema'

export type BuildpackResponse = {
buildpack: {
Expand All @@ -29,7 +30,7 @@ export class BuildpackCommand {
async fetch(app: string, isFirApp = false): Promise<any[]> {
let buildpacks: any
if (isFirApp) {
const {body: releases} = await this.heroku.request<Release[]>(`/apps/${app}/releases`, {
const {body: releases} = await this.heroku.request<Heroku.Release[]>(`/apps/${app}/releases`, {
partial: true,
headers: {
Range: 'version ..; max=10, order=desc',
Expand Down
Loading

0 comments on commit b99b58e

Please sign in to comment.