-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
artifacts.ts
81 lines (80 loc) · 2.44 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
import is from '@sindresorhus/is';
import { TEMPORARY_ERROR } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { exec } from '../../../util/exec';
import type { ExecOptions } from '../../../util/exec/types';
import { readLocalFile } from '../../../util/fs';
import { newlineRegex, regEx } from '../../../util/regex';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types';
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`pip_requirements.updateArtifacts(${packageFileName})`);
if (!is.nonEmptyArray(updatedDeps)) {
logger.debug('No updated pip_requirements deps - returning null');
return null;
}
try {
const cmd: string[] = [];
const rewrittenContent = newPackageFileContent.replace(regEx(/\\\n/g), '');
const lines = rewrittenContent
.split(newlineRegex)
.map((line) => line.trim());
for (const dep of updatedDeps) {
const hashLine = lines.find(
(line) =>
// TODO: types (#7154)
line.startsWith(`${dep.depName!}==`) && line.includes('--hash=')
);
if (hashLine) {
const depConstraint = hashLine.split(' ')[0];
cmd.push(`hashin ${depConstraint} -r ${packageFileName}`);
}
}
if (!cmd.length) {
logger.debug('No hashin commands to run - returning');
return null;
}
const execOptions: ExecOptions = {
cwdFile: '.',
docker: {
image: 'python',
tagScheme: 'pip_requirements',
},
preCommands: ['pip install hashin'],
};
await exec(cmd, execOptions);
const newContent = await readLocalFile(packageFileName, 'utf8');
if (newContent === newPackageFileContent) {
logger.debug(`${packageFileName} is unchanged`);
return null;
}
logger.debug(`Returning updated ${packageFileName}`);
return [
{
file: {
type: 'addition',
path: packageFileName,
contents: newContent,
},
},
];
} catch (err) {
// istanbul ignore if
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug({ err }, `Failed to update ${packageFileName} file`);
return [
{
artifactError: {
lockFile: packageFileName,
stderr: `${String(err.stdout)}\n${String(err.stderr)}`,
},
},
];
}
}