-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathartifacts.ts
172 lines (169 loc) · 5.12 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
import URL from 'url';
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import upath from 'upath';
import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../constants/error-messages';
import {
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
} from '../../constants/platforms';
import * as datasourcePackagist from '../../datasource/packagist';
import { logger } from '../../logger';
import { platform } from '../../platform';
import { ExecOptions, exec } from '../../util/exec';
import {
deleteLocalFile,
ensureDir,
ensureLocalDir,
getSiblingFileName,
readLocalFile,
writeLocalFile,
} from '../../util/fs';
import * as hostRules from '../../util/host-rules';
import { UpdateArtifact, UpdateArtifactsResult } from '../common';
export async function updateArtifacts({
packageFileName,
updatedDeps,
newPackageFileContent,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
logger.debug(`composer.updateArtifacts(${packageFileName})`);
const cacheDir =
process.env.COMPOSER_CACHE_DIR ||
upath.join(config.cacheDir, './others/composer');
await ensureDir(cacheDir);
logger.debug(`Using composer cache ${cacheDir}`);
const lockFileName = packageFileName.replace(/\.json$/, '.lock');
const existingLockFileContent = await readLocalFile(lockFileName);
if (!existingLockFileContent) {
logger.debug('No composer.lock found');
return null;
}
await ensureLocalDir(getSiblingFileName(packageFileName, 'vendor'));
try {
await writeLocalFile(packageFileName, newPackageFileContent);
if (config.isLockFileMaintenance) {
await deleteLocalFile(lockFileName);
}
const authJson = {};
let credentials = hostRules.find({
hostType: PLATFORM_TYPE_GITHUB,
url: 'https://api.github.com/',
});
// istanbul ignore if
if (credentials && credentials.token) {
authJson['github-oauth'] = {
'github.com': credentials.token,
};
}
credentials = hostRules.find({
hostType: PLATFORM_TYPE_GITLAB,
url: 'https://gitlab.com/api/v4/',
});
// istanbul ignore if
if (credentials && credentials.token) {
authJson['gitlab-token'] = {
'gitlab.com': credentials.token,
};
}
try {
// istanbul ignore else
if (is.array(config.registryUrls)) {
for (const regUrl of config.registryUrls) {
if (regUrl) {
const { host } = URL.parse(regUrl);
const hostRule = hostRules.find({
hostType: datasourcePackagist.id,
url: regUrl,
});
// istanbul ignore else
if (hostRule.username && hostRule.password) {
logger.debug('Setting packagist auth for host ' + host);
authJson['http-basic'] = authJson['http-basic'] || {};
authJson['http-basic'][host] = {
username: hostRule.username,
password: hostRule.password,
};
} else {
logger.debug('No packagist auth found for ' + regUrl);
}
}
}
} else if (config.registryUrls) {
logger.warn(
{ registryUrls: config.registryUrls },
'Non-array composer registryUrls'
);
}
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Error setting registryUrls auth for composer');
}
if (authJson) {
await writeLocalFile('auth.json', JSON.stringify(authJson));
}
const execOptions: ExecOptions = {
cwdFile: packageFileName,
extraEnv: {
COMPOSER_CACHE_DIR: cacheDir,
},
docker: {
image: 'renovate/composer',
},
};
const cmd = 'composer';
let args;
if (config.isLockFileMaintenance) {
args = 'install';
} else {
args =
('update ' + updatedDeps.map(quote).join(' ')).trim() +
' --with-dependencies';
}
if (config.composerIgnorePlatformReqs) {
args += ' --ignore-platform-reqs';
}
args += ' --no-ansi --no-interaction';
if (global.trustLevel !== 'high' || config.ignoreScripts) {
args += ' --no-scripts --no-autoloader';
}
logger.debug({ cmd, args }, 'composer command');
await exec(`${cmd} ${args}`, execOptions);
const status = await platform.getRepoStatus();
if (!status.modified.includes(lockFileName)) {
return null;
}
logger.debug('Returning updated composer.lock');
return [
{
file: {
name: lockFileName,
contents: await readLocalFile(lockFileName),
},
},
];
} catch (err) {
if (
err.message &&
err.message.includes(
'Your requirements could not be resolved to an installable set of packages.'
)
) {
logger.info('Composer requirements cannot be resolved');
} else if (
err.message &&
err.message.includes('write error (disk full?)')
) {
throw new Error(SYSTEM_INSUFFICIENT_DISK_SPACE);
} else {
logger.debug({ err }, 'Failed to generate composer.lock');
}
return [
{
artifactError: {
lockFile: lockFileName,
stderr: err.message,
},
},
];
}
}