feat(gomod): private module support (#3495)

Adds support for fetching private modules off github.com

Closes #3202
This commit is contained in:
Rhys Arkins 2019-04-07 08:53:16 +03:00 committed by GitHub
parent 9cf43e2fde
commit 0bd0ea33f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View file

@ -2,6 +2,7 @@ const { exec } = require('child-process-promise');
const fs = require('fs-extra');
const upath = require('upath');
const os = require('os');
const hostRules = require('../../util/host-rules');
module.exports = {
getArtifacts,
@ -60,12 +61,29 @@ async function getArtifacts(
cmd += envVars.map(e => `-e ${e} `).join('');
cmd += '-e CGO_ENABLED=0 ';
cmd += `-w ${cwd} `;
cmd += `renovate/go go`;
cmd += `renovate/go `;
const credentials = hostRules.find({
platform: 'github',
host: 'api.github.com',
});
if (credentials && credentials.token) {
logger.debug('Setting github.com credentials');
cmd += `bash -c "git config --global url.\\"https://${
global.appMode
? `x-access-token:${credentials.token}`
: credentials.token
}@github.com/\\".insteadOf \\"https://github.com/\\" && go`;
} else {
cmd += 'go';
}
} else {
logger.info('Running go via global command');
cmd = 'go';
}
let args = 'get';
if (cmd.includes('.insteadOf')) {
args += '"';
}
logger.debug({ cmd, args }, 'go get command');
({ stdout, stderr } = await exec(`${cmd} ${args}`, {
cwd,
@ -131,6 +149,9 @@ async function getArtifacts(
if (await platform.getFile(vendorModulesFileName)) {
if (config.gitFs) {
args = 'mod vendor';
if (cmd.includes('.insteadOf')) {
args += '"';
}
logger.debug({ cmd, args }, 'go mod vendor command');
({ stdout, stderr } = await exec(`${cmd} ${args}`, {
cwd,

View file

@ -1,9 +1,11 @@
jest.mock('fs-extra');
jest.mock('child-process-promise');
jest.mock('../../../lib/util/host-rules');
const fs = require('fs-extra');
const { exec } = require('child-process-promise');
const gomod = require('../../../lib/manager/gomod/artifacts');
const hostRules = require('../../../lib/util/host-rules');
const gomod1 = `module github.com/renovate-tests/gomod1
@ -48,7 +50,24 @@ describe('.getArtifacts()', () => {
await gomod.getArtifacts('go.mod', [], gomod1, config)
).not.toBeNull();
});
it('supports docker mode', async () => {
it('supports docker mode without credentials', async () => {
platform.getFile.mockReturnValueOnce('Current go.sum');
exec.mockReturnValueOnce({
stdout: '',
stderror: '',
});
fs.readFile = jest.fn(() => 'New go.sum');
expect(
await gomod.getArtifacts('go.mod', [], gomod1, {
...config,
binarySource: 'docker',
})
).not.toBeNull();
});
it('supports docker mode with credentials', async () => {
hostRules.find.mockReturnValue({
token: 'some-token',
});
platform.getFile.mockReturnValueOnce('Current go.sum');
exec.mockReturnValueOnce({
stdout: '',