-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
rubyBundler.ts
105 lines (98 loc) · 3.72 KB
/
rubyBundler.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
import { memoizeAsync } from '../util'
import * as sourcegraph from 'sourcegraph'
export interface BundlerExecutionContext {
repository: string
commit: string
path?: string
}
export interface BundlerExecutionResult {
commands: {
combinedOutput: string
ok: boolean
error?: string
}[]
files: { [path: string]: string }
}
const RUBY_BUNDLER_EXEC_SERVICE_URL = new URL(
'/.api/extension-containers/a8n-ruby-bundler-exec',
sourcegraph.internal.sourcegraphURL
)
/**
* Executes a Ruby `bundle` command in a repository tree and returns the resulting contents of
* `Gemfile` and `Gemfile.lock`.
*/
const executeBundlerCommandUncached = async ({
commands,
context,
}: {
commands: string[][]
context: BundlerExecutionContext
}): Promise<BundlerExecutionResult> => {
const url = new URL('', RUBY_BUNDLER_EXEC_SERVICE_URL)
url.searchParams.set(
'params',
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
JSON.stringify({
archiveURL: getPublicRepoArchiveUrl(context.repository, context.commit),
commands,
// TODO!(sqs): support changes to all Gemfile.* and anything else that `bundler remove` might touch
includeFiles: ['Gemfile', 'Gemfile.lock', 'Gemfile.common', 'Gemfile.modules'],
} as {
archiveURL: string
dir?: string
commands: string[][]
includeFiles: string[]
})
)
const resp = await fetch(url.toString(), {
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
})
if (!resp.ok) {
throw new Error(`error executing bundler command in ${context.repository}: HTTP ${resp.status}`)
}
const result: BundlerExecutionResult = await resp.json()
return result
}
export const executeBundlerCommand = memoizeAsync<
Parameters<typeof executeBundlerCommandUncached>[0],
BundlerExecutionResult
>(arg => executeBundlerCommandUncached(arg), arg => JSON.stringify(arg))
/**
* Returns the result of running `bundle remove <gemName>` in a repository tree.
*/
export const bundlerRemove = async (
gemName: string,
context: BundlerExecutionContext
): Promise<BundlerExecutionResult> => {
// TODO!(sqs): run `bundle install --deployment` after to get updates to lockfile?
const result = await executeBundlerCommand({
commands: [['bundler', 'remove', '--', gemName], ['bundler', 'lock', '--conservative', '--local']],
context,
})
for (const commandResult of result.commands) {
if (!commandResult.ok && !commandResult.combinedOutput.includes('is not specified in')) {
throw new Error(`error in bundlerRemove: ${commandResult.error}\n${commandResult.combinedOutput}`)
}
}
return result
}
function getPublicRepoArchiveUrl(repo: string, commit: string): string {
const MAP = {
'AC/activeadmin': 'github.com/activeadmin/activeadmin',
'ACTG/acts-as-taggable-on': 'github.com/mbleigh/acts-as-taggable-on',
'AD/administrate': 'github.com/thoughtbot/administrate',
'CAN/cancancan': 'github.com/CanCanCommunity/cancancan',
'DEV/devise': 'github.com/plataformatec/devise',
'DIS/discourse': 'github.com/discourse/discourse',
'FAK/faker': 'github.com/faker-ruby/faker',
'LIQ/liquid': 'github.com/Shopify/liquid',
'LOG/logstash': 'github.com/elastic/logstash',
'OP/openproject': 'github.com/opf/openproject',
'SID/sidekiq': 'github.com/mperham/sidekiq',
'SOL/solidus': 'github.com/solidusio/solidus',
'SPREE/spree': 'github.com/spree/spree',
}
return `https://sourcegraph.com/${MAP[repo] || repo}@${commit}/-/raw/`
}