-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathdiff.ts
194 lines (160 loc) · 6.94 KB
/
diff.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import color from '@heroku-cli/color'
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 KolkrabbiAPI from '../../lib/pipelines/kolkrabbi-api'
import {OciImage, Slug} from '../../lib/types/fir'
interface AppInfo {
name: string;
repo?: string;
hash?: string;
}
const PROMOTION_ORDER = ['development', 'staging', 'production']
async function diff(targetApp: AppInfo, downstreamApp: AppInfo, githubToken: string, herokuUserAgent: string) {
if (!downstreamApp.repo) {
return ux.log(`\n${color.app(targetApp.name)} was not compared to ${color.app(downstreamApp.name)} as ${color.app(downstreamApp.name)} is not connected to GitHub`)
}
if (downstreamApp.repo !== targetApp.repo) {
return ux.log(`\n${color.app(targetApp.name)} was not compared to ${color.app(downstreamApp.name)} as ${color.app(downstreamApp.name)} is not connected to the same GitHub repo as ${color.app(targetApp.name)}`)
}
if (!downstreamApp.hash) {
return ux.log(`\n${color.app(targetApp.name)} was not compared to ${color.app(downstreamApp.name)} as ${color.app(downstreamApp.name)} does not have any releases`)
}
if (downstreamApp.hash === targetApp.hash) {
return ux.log(`\n${color.app(targetApp.name)} is up to date with ${color.app(downstreamApp.name)}`)
}
// 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}
if (herokuUserAgent) {
headers['user-agent'] = herokuUserAgent
}
const githubDiff: any = await HTTP.get(`https://api.github.com/repos/${path}`, {
headers,
}).then(res => res.body)
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) => {
return {
sha: commit.sha.slice(0, 7),
date: commit.commit.author.date,
author: commit.commit.author.name,
message: commit.commit.message.split('\n')[0],
}
}).reverse()
ux.table(mapped, {
sha: {
header: 'SHA',
},
date: {},
author: {},
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?')
}
}
export default class PipelinesDiff extends Command {
static description = 'compares the latest release of this app to its downstream app(s)'
static examples = [
'$ heroku pipelines:diff -a my-app-staging',
]
static flags = {
app: flags.app({required: true}),
remote: flags.remote(),
}
kolkrabbi: KolkrabbiAPI = new KolkrabbiAPI(this.config.userAgent, () => this.heroku.auth)
getAppInfo = async (appName: string, appId: string, generation: string): Promise<AppInfo> => {
// Find GitHub connection for the app
const githubApp = await this.kolkrabbi.getAppLink(appId)
.catch(() => {
return {name: appName, repo: null, hash: null}
})
// Find the commit hash of the latest release for this app
let slug: Slug
let ociImages: Array<OciImage>
let commit: string | undefined
try {
const {body: releases} = await getReleases(this.heroku, appId)
const release = releases.find(r => r.status === 'succeeded')
if (!release || !(release.slug || release.oci_image)) {
throw new Error(`no release found for ${appName}`)
}
if (generation === 'cedar' && release.slug) {
slug = await this.heroku.get<Slug>(`/apps/${appId}/slugs/${release.slug.id}`, {
headers: {Accept: SDK_HEADER},
}).then(res => res.body)
commit = slug.commit!
} else if (generation === 'fir' && release.oci_image) {
ociImages = await this.heroku.get<Array<OciImage>>(`/apps/${appId}/oci-images/${release.oci_image.id}`, {
headers: {Accept: SDK_HEADER},
}).then(res => res.body)
commit = ociImages[0]?.commit
}
} catch {
return {name: appName, repo: githubApp.repo, hash: undefined}
}
return {name: appName, repo: githubApp.repo, hash: commit}
}
async run() {
const {flags} = await this.parse(PipelinesDiff)
const targetAppName = flags.app
const coupling = await getCoupling(this.heroku, targetAppName)
.then(res => res.body)
.catch(() => {})
if (!coupling) {
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
ux.action.start('Fetching apps from pipeline')
const allApps = await listPipelineApps(this.heroku, coupling.pipeline.id!)
ux.action.stop()
const sourceStage = coupling.stage
if (!sourceStage) {
return ux.error(`Unable to diff ${targetAppName}`)
}
const downstreamStage = PROMOTION_ORDER[PROMOTION_ORDER.indexOf(sourceStage) + 1]
if (!downstreamStage || !PROMOTION_ORDER.includes(sourceStage)) {
return ux.error(`Unable to diff ${targetAppName}`)
}
const downstreamApps = allApps.filter(app => app.pipelineCoupling.stage === downstreamStage)
if (downstreamApps.length === 0) {
return ux.error(`Cannot diff ${targetAppName} as there are no downstream apps configured`)
}
// Fetch GitHub repo/latest release hash for [target, downstream[0], .., downstream[n]] apps
const appInfoPromises = [this.getAppInfo(targetAppName, targetAppId, generation)]
downstreamApps.forEach(app => {
if (app.name && app.id) {
appInfoPromises.push(this.getAppInfo(app.name, app.id, generation))
}
})
ux.action.start('Fetching release info for all apps')
const appInfo = await Promise.all(appInfoPromises)
ux.action.stop()
// Verify the target app
const targetAppInfo = appInfo[0]
if (!targetAppInfo.repo) {
const command = `heroku pipelines:open ${coupling.pipeline!.name}`
return ux.error(`${targetAppName} does not seem to be connected to GitHub!\nRun ${color.cyan(command)} and "Connect to GitHub".`)
}
if (!targetAppInfo.hash) {
return ux.error(`No release was found for ${targetAppName}, unable to diff`)
}
// Fetch GitHub token for the user
const githubAccount = await this.kolkrabbi.getAccount()
// Diff [{target, downstream[0]}, {target, downstream[1]}, .., {target, downstream[n]}]
const downstreamAppsInfo = appInfo.slice(1)
for (const downstreamAppInfo of downstreamAppsInfo) {
await diff(
targetAppInfo, downstreamAppInfo, githubAccount.github.token, this.config.userAgent,
)
}
}
}