-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathartifacts.ts
233 lines (220 loc) · 6.74 KB
/
artifacts.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { quote } from 'shlex';
import { BUNDLER_INVALID_CREDENTIALS } from '../../constants/error-messages';
import { logger } from '../../logger';
import { platform } from '../../platform';
import { HostRule } from '../../types';
import { get, set } from '../../util/cache/run';
import { ExecOptions, exec } from '../../util/exec';
import {
deleteLocalFile,
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../util/fs';
import { isValid } from '../../versioning/ruby';
import { UpdateArtifact, UpdateArtifactsResult } from '../common';
import {
findAllAuthenticatable,
getAuthenticationHeaderValue,
getDomain,
} from './host-rules';
import { getGemHome } from './utils';
const hostConfigVariablePrefix = 'BUNDLE_';
async function getRubyConstraint(
updateArtifact: UpdateArtifact
): Promise<string> {
const { packageFileName, config } = updateArtifact;
const { compatibility = {} } = config;
const { ruby } = compatibility;
let rubyConstraint: string;
if (ruby) {
logger.debug('Using rubyConstraint from config');
rubyConstraint = ruby;
} else {
const rubyVersionFile = getSiblingFileName(
packageFileName,
'.ruby-version'
);
const rubyVersionFileContent = await readLocalFile(rubyVersionFile, 'utf8');
if (rubyVersionFileContent) {
logger.debug('Using ruby version specified in .ruby-version');
rubyConstraint = rubyVersionFileContent
.replace(/^ruby-/, '')
.replace(/\n/g, '')
.trim();
}
}
return rubyConstraint;
}
function buildBundleHostVariable(hostRule: HostRule): Record<string, string> {
const varName =
hostConfigVariablePrefix +
getDomain(hostRule)
.split('.')
.map((term) => term.toUpperCase())
.join('__');
return {
[varName]: `${getAuthenticationHeaderValue(hostRule)}`,
};
}
export async function updateArtifacts(
updateArtifact: UpdateArtifact
): Promise<UpdateArtifactsResult[] | null> {
const {
packageFileName,
updatedDeps,
newPackageFileContent,
config,
} = updateArtifact;
const { compatibility = {} } = config;
logger.debug(`bundler.updateArtifacts(${packageFileName})`);
const existingError = get<string>('bundlerArtifactsError');
// istanbul ignore if
if (existingError) {
logger.debug('Aborting Bundler artifacts due to previous failed attempt');
throw new Error(existingError);
}
const lockFileName = `${packageFileName}.lock`;
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.debug('No Gemfile.lock found');
return null;
}
if (config.isLockFileMaintenance) {
await deleteLocalFile(lockFileName);
}
try {
await writeLocalFile(packageFileName, newPackageFileContent);
let cmd;
if (config.isLockFileMaintenance) {
cmd = 'bundle lock';
} else {
cmd = `bundle lock --update ${updatedDeps.map(quote).join(' ')}`;
}
let bundlerVersion = '';
const { bundler } = compatibility;
if (bundler) {
if (isValid(bundler)) {
logger.debug({ bundlerVersion: bundler }, 'Found bundler version');
bundlerVersion = ` -v ${quote(bundler)}`;
} else {
logger.warn({ bundlerVersion: bundler }, 'Invalid bundler version');
}
} else {
logger.debug('No bundler version constraint found - will use latest');
}
const preCommands = [
'ruby --version',
`gem install bundler${bundlerVersion}`,
];
const bundlerHostRulesVariables = findAllAuthenticatable({
hostType: 'bundler',
}).reduce((variables, hostRule) => {
return { ...variables, ...buildBundleHostVariable(hostRule) };
}, {} as Record<string, string>);
const execOptions: ExecOptions = {
cwdFile: packageFileName,
extraEnv: {
...bundlerHostRulesVariables,
GEM_HOME: await getGemHome(config),
},
docker: {
image: 'renovate/ruby',
tagScheme: 'ruby',
tagConstraint: await getRubyConstraint(updateArtifact),
preCommands,
},
};
await exec(cmd, execOptions);
const status = await platform.getRepoStatus();
if (!status.modified.includes(lockFileName)) {
return null;
}
logger.debug('Returning updated Gemfile.lock');
const lockFileContent = await readLocalFile(lockFileName);
return [
{
file: {
name: lockFileName,
contents: lockFileContent,
},
},
];
} catch (err) /* istanbul ignore next */ {
const output = err.stdout + err.stderr;
if (
err.message.includes('fatal: Could not parse object') ||
output.includes('but that version could not be found')
) {
return [
{
artifactError: {
lockFile: lockFileName,
stderr: output,
},
},
];
}
if (
(err.stdout &&
err.stdout.includes('Please supply credentials for this source')) ||
(err.stderr && err.stderr.includes('Authentication is required')) ||
(err.stderr &&
err.stderr.includes(
'Please make sure you have the correct access rights'
))
) {
logger.debug(
{ err },
'Gemfile.lock update failed due to missing credentials - skipping branch'
);
// Do not generate these PRs because we don't yet support Bundler authentication
set('bundlerArtifactsError', BUNDLER_INVALID_CREDENTIALS);
throw new Error(BUNDLER_INVALID_CREDENTIALS);
}
const resolveMatchRe = new RegExp('\\s+(.*) was resolved to', 'g');
if (output.match(resolveMatchRe) && !config.isLockFileMaintenance) {
logger.debug({ err }, 'Bundler has a resolve error');
const resolveMatches = [];
let resolveMatch;
do {
resolveMatch = resolveMatchRe.exec(output);
if (resolveMatch) {
resolveMatches.push(resolveMatch[1].split(' ').shift());
}
} while (resolveMatch);
if (resolveMatches.some((match) => !updatedDeps.includes(match))) {
logger.debug(
{ resolveMatches, updatedDeps },
'Found new resolve matches - reattempting recursively'
);
const newUpdatedDeps = [
...new Set([...updatedDeps, ...resolveMatches]),
];
return updateArtifacts({
packageFileName,
updatedDeps: newUpdatedDeps,
newPackageFileContent,
config,
});
}
logger.debug(
{ err },
'Gemfile.lock update failed due to incompatible packages'
);
} else {
logger.info(
{ err },
'Gemfile.lock update failed due to an unknown reason'
);
}
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.stdout + '\n' + err.stderr,
},
},
];
}
}