Add and configure ESLint and update configuration for Prettier (#391)
* Apply ESLint config and update Prettier * Update dependencies and rebuild * Update docspull/395/head
parent
926f442022
commit
c82240598b
@ -0,0 +1,6 @@
|
|||||||
|
# Ignore list
|
||||||
|
/*
|
||||||
|
|
||||||
|
# Do not ignore these folders:
|
||||||
|
!__tests__/
|
||||||
|
!src/
|
@ -0,0 +1,49 @@
|
|||||||
|
module.exports = {
|
||||||
|
extends: [
|
||||||
|
'eslint:recommended',
|
||||||
|
'plugin:@typescript-eslint/recommended',
|
||||||
|
'plugin:eslint-plugin-jest/recommended',
|
||||||
|
'eslint-config-prettier'
|
||||||
|
],
|
||||||
|
parser: '@typescript-eslint/parser',
|
||||||
|
plugins: ['@typescript-eslint', 'eslint-plugin-jest'],
|
||||||
|
rules: {
|
||||||
|
'@typescript-eslint/no-require-imports': 'error',
|
||||||
|
'@typescript-eslint/no-non-null-assertion': 'off',
|
||||||
|
'@typescript-eslint/no-explicit-any': 'off',
|
||||||
|
'@typescript-eslint/no-empty-function': 'off',
|
||||||
|
'@typescript-eslint/ban-ts-comment': [
|
||||||
|
'error',
|
||||||
|
{
|
||||||
|
'ts-ignore': 'allow-with-description'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'no-console': 'error',
|
||||||
|
'yoda': 'error',
|
||||||
|
'prefer-const': [
|
||||||
|
'error',
|
||||||
|
{
|
||||||
|
destructuring: 'all'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'no-control-regex': 'off',
|
||||||
|
'no-constant-condition': ['error', {checkLoops: false}]
|
||||||
|
},
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
files: ['**/*{test,spec}.ts'],
|
||||||
|
rules: {
|
||||||
|
'@typescript-eslint/no-unused-vars': 'off',
|
||||||
|
'jest/no-standalone-expect': 'off',
|
||||||
|
'jest/no-conditional-expect': 'off',
|
||||||
|
'no-console': 'off',
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
env: {
|
||||||
|
node: true,
|
||||||
|
es6: true,
|
||||||
|
'jest/globals': true
|
||||||
|
}
|
||||||
|
};
|
@ -1 +1,2 @@
|
|||||||
|
* text=auto eol=lf
|
||||||
.licenses/** -diff linguist-generated=true
|
.licenses/** -diff linguist-generated=true
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
# Ignore list
|
||||||
|
/*
|
||||||
|
|
||||||
|
# Do not ignore these folders:
|
||||||
|
!__tests__/
|
||||||
|
!.github/
|
||||||
|
!src/
|
@ -0,0 +1,10 @@
|
|||||||
|
module.exports = {
|
||||||
|
printWidth: 80,
|
||||||
|
tabWidth: 2,
|
||||||
|
useTabs: false,
|
||||||
|
semi: true,
|
||||||
|
singleQuote: true,
|
||||||
|
trailingComma: 'none',
|
||||||
|
bracketSpacing: false,
|
||||||
|
arrowParens: 'avoid'
|
||||||
|
};
|
@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"printWidth": 80,
|
|
||||||
"tabWidth": 2,
|
|
||||||
"useTabs": false,
|
|
||||||
"semi": true,
|
|
||||||
"singleQuote": true,
|
|
||||||
"trailingComma": "none",
|
|
||||||
"bracketSpacing": false,
|
|
||||||
"arrowParens": "avoid",
|
|
||||||
"parser": "typescript",
|
|
||||||
"endOfLine": "auto"
|
|
||||||
}
|
|
@ -1,336 +1,336 @@
|
|||||||
import * as io from '@actions/io';
|
import * as io from '@actions/io';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
const fakeSourcesDirForTesting = path.join(
|
const fakeSourcesDirForTesting = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
'runner',
|
'runner',
|
||||||
path.join(Math.random().toString(36).substring(7)),
|
path.join(Math.random().toString(36).substring(7)),
|
||||||
's'
|
's'
|
||||||
);
|
);
|
||||||
|
|
||||||
const invalidNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>`;
|
const invalidNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>`;
|
||||||
|
|
||||||
const emptyNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const emptyNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
const nugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const nugetorgNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
const gprnugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const gprnugetorgNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
||||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
const gprNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const gprNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
const twogprNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const twogprNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="GPR-GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
<add key="GPR-GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
||||||
<add key="GPR-Actions" value="https://nuget.pkg.github.com/actions/index.json" protocolVersion="3" />
|
<add key="GPR-Actions" value="https://nuget.pkg.github.com/actions/index.json" protocolVersion="3" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
const spaceNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const spaceNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="GPR GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
<add key="GPR GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
const azureartifactsNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const azureartifactsNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
|
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
const azureartifactsnugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
|
const azureartifactsnugetorgNuGetConfig = `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<packageSources>
|
<packageSources>
|
||||||
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
|
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
|
||||||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||||
</packageSources>
|
</packageSources>
|
||||||
</configuration>`;
|
</configuration>`;
|
||||||
|
|
||||||
// We want a NuGet.config one level above the sources directory, so it doesn't trample a user's NuGet.config but is still picked up by NuGet/dotnet.
|
// We want a NuGet.config one level above the sources directory, so it doesn't trample a user's NuGet.config but is still picked up by NuGet/dotnet.
|
||||||
const nugetConfigFile = path.join(fakeSourcesDirForTesting, '../nuget.config');
|
const nugetConfigFile = path.join(fakeSourcesDirForTesting, '../nuget.config');
|
||||||
|
|
||||||
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
|
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
|
||||||
import * as auth from '../src/authutil';
|
import * as auth from '../src/authutil';
|
||||||
|
|
||||||
describe('authutil tests', () => {
|
describe('authutil tests', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await io.rmRF(fakeSourcesDirForTesting);
|
await io.rmRF(fakeSourcesDirForTesting);
|
||||||
await io.mkdirP(fakeSourcesDirForTesting);
|
await io.mkdirP(fakeSourcesDirForTesting);
|
||||||
}, 30000);
|
}, 30000);
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await io.rmRF(fakeSourcesDirForTesting);
|
await io.rmRF(fakeSourcesDirForTesting);
|
||||||
}, 30000);
|
}, 30000);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
if (fs.existsSync(nugetConfigFile)) {
|
if (fs.existsSync(nugetConfigFile)) {
|
||||||
fs.unlinkSync(nugetConfigFile);
|
fs.unlinkSync(nugetConfigFile);
|
||||||
}
|
}
|
||||||
process.env['INPUT_OWNER'] = '';
|
process.env['INPUT_OWNER'] = '';
|
||||||
process.env['NUGET_AUTH_TOKEN'] = '';
|
process.env['NUGET_AUTH_TOKEN'] = '';
|
||||||
});
|
});
|
||||||
|
|
||||||
it('No existing config, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
|
it('No existing config, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('No existing config, auth token environment variable not provided, throws', async () => {
|
it('No existing config, auth token environment variable not provided, throws', async () => {
|
||||||
let thrown = false;
|
let thrown = false;
|
||||||
try {
|
try {
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
} catch {
|
} catch {
|
||||||
thrown = true;
|
thrown = true;
|
||||||
}
|
}
|
||||||
expect(thrown).toBe(true);
|
expect(thrown).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('No existing config, sets up a full NuGet.config with URL and other owner/PAT for GPR', async () => {
|
it('No existing config, sets up a full NuGet.config with URL and other owner/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
process.env['INPUT_OWNER'] = 'otherorg';
|
process.env['INPUT_OWNER'] = 'otherorg';
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/otherorg/index.json',
|
'https://nuget.pkg.github.com/otherorg/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config (invalid), tries to parse an invalid NuGet.config and throws', async () => {
|
it('Existing config (invalid), tries to parse an invalid NuGet.config and throws', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, invalidNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, invalidNuGetConfig);
|
||||||
let thrown = false;
|
let thrown = false;
|
||||||
try {
|
try {
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
} catch {
|
} catch {
|
||||||
thrown = true;
|
thrown = true;
|
||||||
}
|
}
|
||||||
expect(thrown).toBe(true);
|
expect(thrown).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ no sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
|
it('Existing config w/ no sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, emptyNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, emptyNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ no GPR sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
|
it('Existing config w/ no GPR sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, nugetorgNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, nugetorgNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ only GPR source, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
it('Existing config w/ only GPR source, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ GPR source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
it('Existing config w/ GPR source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, gprnugetorgNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, gprnugetorgNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ two GPR sources, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
it('Existing config w/ two GPR sources, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, twogprNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, twogprNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com',
|
'https://nuget.pkg.github.com',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ spaces in key, throws for now', async () => {
|
it('Existing config w/ spaces in key, throws for now', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, spaceNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, spaceNuGetConfig);
|
||||||
let thrown = false;
|
let thrown = false;
|
||||||
try {
|
try {
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
} catch {
|
} catch {
|
||||||
thrown = true;
|
thrown = true;
|
||||||
}
|
}
|
||||||
expect(thrown).toBe(true);
|
expect(thrown).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config not in repo root, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
it('Existing config not in repo root, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigDirectory: string = path.join(
|
const inputNuGetConfigDirectory: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'subfolder'
|
'subfolder'
|
||||||
);
|
);
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
inputNuGetConfigDirectory,
|
inputNuGetConfigDirectory,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.mkdirSync(inputNuGetConfigDirectory, {recursive: true});
|
fs.mkdirSync(inputNuGetConfigDirectory, {recursive: true});
|
||||||
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://nuget.pkg.github.com/OwnerName/index.json',
|
'https://nuget.pkg.github.com/OwnerName/index.json',
|
||||||
'subfolder/nuget.config',
|
'subfolder/nuget.config',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ only Azure Artifacts source, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
it('Existing config w/ only Azure Artifacts source, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, azureartifactsNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, azureartifactsNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
|
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Existing config w/ Azure Artifacts source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
it('Existing config w/ Azure Artifacts source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
const inputNuGetConfigPath: string = path.join(
|
const inputNuGetConfigPath: string = path.join(
|
||||||
fakeSourcesDirForTesting,
|
fakeSourcesDirForTesting,
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
fs.writeFileSync(inputNuGetConfigPath, azureartifactsnugetorgNuGetConfig);
|
fs.writeFileSync(inputNuGetConfigPath, azureartifactsnugetorgNuGetConfig);
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
|
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('No existing config, sets up a full NuGet.config with URL and token for other source', async () => {
|
it('No existing config, sets up a full NuGet.config with URL and token for other source', async () => {
|
||||||
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
|
||||||
await auth.configAuthentication(
|
await auth.configAuthentication(
|
||||||
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
|
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
|
||||||
'',
|
'',
|
||||||
fakeSourcesDirForTesting
|
fakeSourcesDirForTesting
|
||||||
);
|
);
|
||||||
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
expect(fs.existsSync(nugetConfigFile)).toBe(true);
|
||||||
expect(
|
expect(
|
||||||
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
|
||||||
).toMatchSnapshot();
|
).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,196 +1,196 @@
|
|||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as github from '@actions/github';
|
import * as github from '@actions/github';
|
||||||
import {XMLParser, XMLBuilder} from 'fast-xml-parser';
|
import {XMLParser, XMLBuilder} from 'fast-xml-parser';
|
||||||
|
|
||||||
export function configAuthentication(
|
export function configAuthentication(
|
||||||
feedUrl: string,
|
feedUrl: string,
|
||||||
existingFileLocation: string = '',
|
existingFileLocation = '',
|
||||||
processRoot: string = process.cwd()
|
processRoot: string = process.cwd()
|
||||||
) {
|
) {
|
||||||
const existingNuGetConfig: string = path.resolve(
|
const existingNuGetConfig: string = path.resolve(
|
||||||
processRoot,
|
processRoot,
|
||||||
existingFileLocation === ''
|
existingFileLocation === ''
|
||||||
? getExistingNugetConfig(processRoot)
|
? getExistingNugetConfig(processRoot)
|
||||||
: existingFileLocation
|
: existingFileLocation
|
||||||
);
|
);
|
||||||
|
|
||||||
const tempNuGetConfig: string = path.resolve(
|
const tempNuGetConfig: string = path.resolve(
|
||||||
processRoot,
|
processRoot,
|
||||||
'../',
|
'../',
|
||||||
'nuget.config'
|
'nuget.config'
|
||||||
);
|
);
|
||||||
|
|
||||||
writeFeedToFile(feedUrl, existingNuGetConfig, tempNuGetConfig);
|
writeFeedToFile(feedUrl, existingNuGetConfig, tempNuGetConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidKey(key: string): boolean {
|
function isValidKey(key: string): boolean {
|
||||||
return /^[\w\-\.]+$/i.test(key);
|
return /^[\w\-.]+$/i.test(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getExistingNugetConfig(processRoot: string) {
|
function getExistingNugetConfig(processRoot: string) {
|
||||||
const defaultConfigName = 'nuget.config';
|
const defaultConfigName = 'nuget.config';
|
||||||
const configFileNames = fs
|
const configFileNames = fs
|
||||||
.readdirSync(processRoot)
|
.readdirSync(processRoot)
|
||||||
.filter(filename => filename.toLowerCase() === defaultConfigName);
|
.filter(filename => filename.toLowerCase() === defaultConfigName);
|
||||||
if (configFileNames.length) {
|
if (configFileNames.length) {
|
||||||
return configFileNames[0];
|
return configFileNames[0];
|
||||||
}
|
}
|
||||||
return defaultConfigName;
|
return defaultConfigName;
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeFeedToFile(
|
function writeFeedToFile(
|
||||||
feedUrl: string,
|
feedUrl: string,
|
||||||
existingFileLocation: string,
|
existingFileLocation: string,
|
||||||
tempFileLocation: string
|
tempFileLocation: string
|
||||||
) {
|
) {
|
||||||
core.info(
|
core.info(
|
||||||
`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`
|
`dotnet-auth: Finding any source references in ${existingFileLocation}, writing a new temporary configuration file with credentials to ${tempFileLocation}`
|
||||||
);
|
);
|
||||||
let sourceKeys: string[] = [];
|
const sourceKeys: string[] = [];
|
||||||
let owner: string = core.getInput('owner');
|
let owner: string = core.getInput('owner');
|
||||||
let sourceUrl: string = feedUrl;
|
const sourceUrl: string = feedUrl;
|
||||||
if (!owner) {
|
if (!owner) {
|
||||||
owner = github.context.repo.owner;
|
owner = github.context.repo.owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!process.env.NUGET_AUTH_TOKEN) {
|
if (!process.env.NUGET_AUTH_TOKEN) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'The NUGET_AUTH_TOKEN environment variable was not provided. In this step, add the following: \r\nenv:\r\n NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}'
|
'The NUGET_AUTH_TOKEN environment variable was not provided. In this step, add the following: \r\nenv:\r\n NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fs.existsSync(existingFileLocation)) {
|
if (fs.existsSync(existingFileLocation)) {
|
||||||
// get key from existing NuGet.config so NuGet/dotnet can match credentials
|
// get key from existing NuGet.config so NuGet/dotnet can match credentials
|
||||||
const curContents: string = fs.readFileSync(existingFileLocation, 'utf8');
|
const curContents: string = fs.readFileSync(existingFileLocation, 'utf8');
|
||||||
|
|
||||||
const parserOptions = {
|
const parserOptions = {
|
||||||
ignoreAttributes: false
|
ignoreAttributes: false
|
||||||
};
|
};
|
||||||
const parser = new XMLParser(parserOptions);
|
const parser = new XMLParser(parserOptions);
|
||||||
const json = parser.parse(curContents);
|
const json = parser.parse(curContents);
|
||||||
|
|
||||||
if (typeof json.configuration === 'undefined') {
|
if (typeof json.configuration === 'undefined') {
|
||||||
throw new Error(`The provided NuGet.config seems invalid.`);
|
throw new Error(`The provided NuGet.config seems invalid.`);
|
||||||
}
|
}
|
||||||
if (json.configuration?.packageSources?.add) {
|
if (json.configuration?.packageSources?.add) {
|
||||||
const packageSources = json.configuration.packageSources.add;
|
const packageSources = json.configuration.packageSources.add;
|
||||||
|
|
||||||
if (Array.isArray(packageSources)) {
|
if (Array.isArray(packageSources)) {
|
||||||
packageSources.forEach(source => {
|
packageSources.forEach(source => {
|
||||||
const value = source['@_value'];
|
const value = source['@_value'];
|
||||||
core.debug(`source '${value}'`);
|
core.debug(`source '${value}'`);
|
||||||
if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
|
if (value.toLowerCase().includes(feedUrl.toLowerCase())) {
|
||||||
const key = source['@_key'];
|
const key = source['@_key'];
|
||||||
sourceKeys.push(key);
|
sourceKeys.push(key);
|
||||||
core.debug(`Found a URL with key ${key}`);
|
core.debug(`Found a URL with key ${key}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (
|
if (
|
||||||
packageSources['@_value']
|
packageSources['@_value']
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.includes(feedUrl.toLowerCase())
|
.includes(feedUrl.toLowerCase())
|
||||||
) {
|
) {
|
||||||
const key = packageSources['@_key'];
|
const key = packageSources['@_key'];
|
||||||
sourceKeys.push(key);
|
sourceKeys.push(key);
|
||||||
core.debug(`Found a URL with key ${key}`);
|
core.debug(`Found a URL with key ${key}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const xmlSource: any[] = [
|
const xmlSource: any[] = [
|
||||||
{
|
{
|
||||||
'?xml': [
|
'?xml': [
|
||||||
{
|
{
|
||||||
'#text': ''
|
'#text': ''
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
':@': {
|
':@': {
|
||||||
'@_version': '1.0'
|
'@_version': '1.0'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
configuration: [
|
configuration: [
|
||||||
{
|
{
|
||||||
config: [
|
config: [
|
||||||
{
|
{
|
||||||
add: [],
|
add: [],
|
||||||
':@': {
|
':@': {
|
||||||
'@_key': 'defaultPushSource',
|
'@_key': 'defaultPushSource',
|
||||||
'@_value': sourceUrl
|
'@_value': sourceUrl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!sourceKeys.length) {
|
if (!sourceKeys.length) {
|
||||||
let keystring = 'Source';
|
const keystring = 'Source';
|
||||||
|
|
||||||
xmlSource[1].configuration.push({
|
xmlSource[1].configuration.push({
|
||||||
packageSources: [
|
packageSources: [
|
||||||
{
|
{
|
||||||
add: [],
|
add: [],
|
||||||
':@': {
|
':@': {
|
||||||
'@_key': keystring,
|
'@_key': keystring,
|
||||||
'@_value': sourceUrl
|
'@_value': sourceUrl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
sourceKeys.push(keystring);
|
sourceKeys.push(keystring);
|
||||||
}
|
}
|
||||||
|
|
||||||
const packageSourceCredentials: any[] = [];
|
const packageSourceCredentials: any[] = [];
|
||||||
sourceKeys.forEach(key => {
|
sourceKeys.forEach(key => {
|
||||||
if (!isValidKey(key)) {
|
if (!isValidKey(key)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again."
|
"Source name can contain letters, numbers, and '-', '_', '.' symbols only. Please, fix source name in NuGet.config and try again."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
packageSourceCredentials.push({
|
packageSourceCredentials.push({
|
||||||
[key]: [
|
[key]: [
|
||||||
{
|
{
|
||||||
add: [],
|
add: [],
|
||||||
':@': {
|
':@': {
|
||||||
'@_key': 'Username',
|
'@_key': 'Username',
|
||||||
'@_value': owner
|
'@_value': owner
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
add: [],
|
add: [],
|
||||||
':@': {
|
':@': {
|
||||||
'@_key': 'ClearTextPassword',
|
'@_key': 'ClearTextPassword',
|
||||||
'@_value': process.env.NUGET_AUTH_TOKEN
|
'@_value': process.env.NUGET_AUTH_TOKEN
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
xmlSource[1].configuration.push({
|
xmlSource[1].configuration.push({
|
||||||
packageSourceCredentials
|
packageSourceCredentials
|
||||||
});
|
});
|
||||||
|
|
||||||
const xmlBuilderOptions = {
|
const xmlBuilderOptions = {
|
||||||
format: true,
|
format: true,
|
||||||
ignoreAttributes: false,
|
ignoreAttributes: false,
|
||||||
preserveOrder: true,
|
preserveOrder: true,
|
||||||
allowBooleanAttributes: true,
|
allowBooleanAttributes: true,
|
||||||
suppressBooleanAttributes: true,
|
suppressBooleanAttributes: true,
|
||||||
suppressEmptyNode: true
|
suppressEmptyNode: true
|
||||||
};
|
};
|
||||||
|
|
||||||
const builder = new XMLBuilder(xmlBuilderOptions);
|
const builder = new XMLBuilder(xmlBuilderOptions);
|
||||||
|
|
||||||
const output = builder.build(xmlSource).trim();
|
const output = builder.build(xmlSource).trim();
|
||||||
|
|
||||||
fs.writeFileSync(tempFileLocation, output);
|
fs.writeFileSync(tempFileLocation, output);
|
||||||
}
|
}
|
||||||
|
@ -1,265 +1,265 @@
|
|||||||
// Load tempDirectory before it gets wiped by tool-cache
|
// Load tempDirectory before it gets wiped by tool-cache
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as exec from '@actions/exec';
|
import * as exec from '@actions/exec';
|
||||||
import * as io from '@actions/io';
|
import * as io from '@actions/io';
|
||||||
import * as hc from '@actions/http-client';
|
import * as hc from '@actions/http-client';
|
||||||
import {chmodSync} from 'fs';
|
import {chmodSync} from 'fs';
|
||||||
import {readdir} from 'fs/promises';
|
import {readdir} from 'fs/promises';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import semver from 'semver';
|
import semver from 'semver';
|
||||||
import {IS_LINUX, IS_WINDOWS} from './utils';
|
import {IS_LINUX, IS_WINDOWS} from './utils';
|
||||||
import {QualityOptions} from './setup-dotnet';
|
import {QualityOptions} from './setup-dotnet';
|
||||||
|
|
||||||
export interface DotnetVersion {
|
export interface DotnetVersion {
|
||||||
type: string;
|
type: string;
|
||||||
value: string;
|
value: string;
|
||||||
qualityFlag: boolean;
|
qualityFlag: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DotnetVersionResolver {
|
export class DotnetVersionResolver {
|
||||||
private inputVersion: string;
|
private inputVersion: string;
|
||||||
private resolvedArgument: DotnetVersion;
|
private resolvedArgument: DotnetVersion;
|
||||||
|
|
||||||
constructor(version: string) {
|
constructor(version: string) {
|
||||||
this.inputVersion = version.trim();
|
this.inputVersion = version.trim();
|
||||||
this.resolvedArgument = {type: '', value: '', qualityFlag: false};
|
this.resolvedArgument = {type: '', value: '', qualityFlag: false};
|
||||||
}
|
}
|
||||||
|
|
||||||
private async resolveVersionInput(): Promise<void> {
|
private async resolveVersionInput(): Promise<void> {
|
||||||
if (!semver.validRange(this.inputVersion)) {
|
if (!semver.validRange(this.inputVersion)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`'dotnet-version' was supplied in invalid format: ${this.inputVersion}! Supported syntax: A.B.C, A.B, A.B.x, A, A.x`
|
`'dotnet-version' was supplied in invalid format: ${this.inputVersion}! Supported syntax: A.B.C, A.B, A.B.x, A, A.x`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (semver.valid(this.inputVersion)) {
|
if (semver.valid(this.inputVersion)) {
|
||||||
this.resolvedArgument.type = 'version';
|
this.resolvedArgument.type = 'version';
|
||||||
this.resolvedArgument.value = this.inputVersion;
|
this.resolvedArgument.value = this.inputVersion;
|
||||||
} else {
|
} else {
|
||||||
const [major, minor] = this.inputVersion.split('.');
|
const [major, minor] = this.inputVersion.split('.');
|
||||||
|
|
||||||
if (this.isNumericTag(major)) {
|
if (this.isNumericTag(major)) {
|
||||||
this.resolvedArgument.type = 'channel';
|
this.resolvedArgument.type = 'channel';
|
||||||
if (this.isNumericTag(minor)) {
|
if (this.isNumericTag(minor)) {
|
||||||
this.resolvedArgument.value = `${major}.${minor}`;
|
this.resolvedArgument.value = `${major}.${minor}`;
|
||||||
} else {
|
} else {
|
||||||
const httpClient = new hc.HttpClient('actions/setup-dotnet', [], {
|
const httpClient = new hc.HttpClient('actions/setup-dotnet', [], {
|
||||||
allowRetries: true,
|
allowRetries: true,
|
||||||
maxRetries: 3
|
maxRetries: 3
|
||||||
});
|
});
|
||||||
this.resolvedArgument.value = await this.getLatestVersion(
|
this.resolvedArgument.value = await this.getLatestVersion(
|
||||||
httpClient,
|
httpClient,
|
||||||
[major, minor]
|
[major, minor]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.resolvedArgument.qualityFlag = +major >= 6 ? true : false;
|
this.resolvedArgument.qualityFlag = +major >= 6 ? true : false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private isNumericTag(versionTag): boolean {
|
private isNumericTag(versionTag): boolean {
|
||||||
return /^\d+$/.test(versionTag);
|
return /^\d+$/.test(versionTag);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createDotNetVersion(): Promise<{
|
public async createDotNetVersion(): Promise<{
|
||||||
type: string;
|
type: string;
|
||||||
value: string;
|
value: string;
|
||||||
qualityFlag: boolean;
|
qualityFlag: boolean;
|
||||||
}> {
|
}> {
|
||||||
await this.resolveVersionInput();
|
await this.resolveVersionInput();
|
||||||
if (!this.resolvedArgument.type) {
|
if (!this.resolvedArgument.type) {
|
||||||
return this.resolvedArgument;
|
return this.resolvedArgument;
|
||||||
}
|
}
|
||||||
if (IS_WINDOWS) {
|
if (IS_WINDOWS) {
|
||||||
this.resolvedArgument.type =
|
this.resolvedArgument.type =
|
||||||
this.resolvedArgument.type === 'channel' ? '-Channel' : '-Version';
|
this.resolvedArgument.type === 'channel' ? '-Channel' : '-Version';
|
||||||
} else {
|
} else {
|
||||||
this.resolvedArgument.type =
|
this.resolvedArgument.type =
|
||||||
this.resolvedArgument.type === 'channel' ? '--channel' : '--version';
|
this.resolvedArgument.type === 'channel' ? '--channel' : '--version';
|
||||||
}
|
}
|
||||||
return this.resolvedArgument;
|
return this.resolvedArgument;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getLatestVersion(
|
private async getLatestVersion(
|
||||||
httpClient: hc.HttpClient,
|
httpClient: hc.HttpClient,
|
||||||
versionParts: string[]
|
versionParts: string[]
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const response = await httpClient.getJson<any>(
|
const response = await httpClient.getJson<any>(
|
||||||
DotnetVersionResolver.DotNetCoreIndexUrl
|
DotnetVersionResolver.DotNetCoreIndexUrl
|
||||||
);
|
);
|
||||||
const result = response.result || {};
|
const result = response.result || {};
|
||||||
let releasesInfo: any[] = result['releases-index'];
|
const releasesInfo: any[] = result['releases-index'];
|
||||||
|
|
||||||
let releaseInfo = releasesInfo.find(info => {
|
const releaseInfo = releasesInfo.find(info => {
|
||||||
let sdkParts: string[] = info['channel-version'].split('.');
|
const sdkParts: string[] = info['channel-version'].split('.');
|
||||||
return sdkParts[0] === versionParts[0];
|
return sdkParts[0] === versionParts[0];
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!releaseInfo) {
|
if (!releaseInfo) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Could not find info for version ${versionParts.join('.')} at ${
|
`Could not find info for version ${versionParts.join('.')} at ${
|
||||||
DotnetVersionResolver.DotNetCoreIndexUrl
|
DotnetVersionResolver.DotNetCoreIndexUrl
|
||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return releaseInfo['channel-version'];
|
return releaseInfo['channel-version'];
|
||||||
}
|
}
|
||||||
|
|
||||||
static DotNetCoreIndexUrl: string =
|
static DotNetCoreIndexUrl =
|
||||||
'https://dotnetcli.azureedge.net/dotnet/release-metadata/releases-index.json';
|
'https://dotnetcli.azureedge.net/dotnet/release-metadata/releases-index.json';
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DotnetCoreInstaller {
|
export class DotnetCoreInstaller {
|
||||||
private version: string;
|
private version: string;
|
||||||
private quality: QualityOptions;
|
private quality: QualityOptions;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
const installationDirectoryWindows = path.join(
|
const installationDirectoryWindows = path.join(
|
||||||
process.env['PROGRAMFILES'] + '',
|
process.env['PROGRAMFILES'] + '',
|
||||||
'dotnet'
|
'dotnet'
|
||||||
);
|
);
|
||||||
const installationDirectoryLinux = '/usr/share/dotnet';
|
const installationDirectoryLinux = '/usr/share/dotnet';
|
||||||
const installationDirectoryMac = path.join(
|
const installationDirectoryMac = path.join(
|
||||||
process.env['HOME'] + '',
|
process.env['HOME'] + '',
|
||||||
'.dotnet'
|
'.dotnet'
|
||||||
);
|
);
|
||||||
const dotnetInstallDir: string | undefined =
|
const dotnetInstallDir: string | undefined =
|
||||||
process.env['DOTNET_INSTALL_DIR'];
|
process.env['DOTNET_INSTALL_DIR'];
|
||||||
if (dotnetInstallDir) {
|
if (dotnetInstallDir) {
|
||||||
process.env['DOTNET_INSTALL_DIR'] =
|
process.env['DOTNET_INSTALL_DIR'] =
|
||||||
this.convertInstallPathToAbsolute(dotnetInstallDir);
|
this.convertInstallPathToAbsolute(dotnetInstallDir);
|
||||||
} else {
|
} else {
|
||||||
if (IS_WINDOWS) {
|
if (IS_WINDOWS) {
|
||||||
process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows;
|
process.env['DOTNET_INSTALL_DIR'] = installationDirectoryWindows;
|
||||||
} else {
|
} else {
|
||||||
process.env['DOTNET_INSTALL_DIR'] = IS_LINUX
|
process.env['DOTNET_INSTALL_DIR'] = IS_LINUX
|
||||||
? installationDirectoryLinux
|
? installationDirectoryLinux
|
||||||
: installationDirectoryMac;
|
: installationDirectoryMac;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(version: string, quality: QualityOptions) {
|
constructor(version: string, quality: QualityOptions) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
this.quality = quality;
|
this.quality = quality;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static convertInstallPathToAbsolute(installDir: string): string {
|
private static convertInstallPathToAbsolute(installDir: string): string {
|
||||||
let transformedPath;
|
let transformedPath;
|
||||||
if (path.isAbsolute(installDir)) {
|
if (path.isAbsolute(installDir)) {
|
||||||
transformedPath = installDir;
|
transformedPath = installDir;
|
||||||
} else {
|
} else {
|
||||||
transformedPath = installDir.startsWith('~')
|
transformedPath = installDir.startsWith('~')
|
||||||
? path.join(os.homedir(), installDir.slice(1))
|
? path.join(os.homedir(), installDir.slice(1))
|
||||||
: (transformedPath = path.join(process.cwd(), installDir));
|
: (transformedPath = path.join(process.cwd(), installDir));
|
||||||
}
|
}
|
||||||
return path.normalize(transformedPath);
|
return path.normalize(transformedPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
static addToPath() {
|
static addToPath() {
|
||||||
core.addPath(process.env['DOTNET_INSTALL_DIR']!);
|
core.addPath(process.env['DOTNET_INSTALL_DIR']!);
|
||||||
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
|
core.exportVariable('DOTNET_ROOT', process.env['DOTNET_INSTALL_DIR']);
|
||||||
}
|
}
|
||||||
|
|
||||||
private setQuality(
|
private setQuality(
|
||||||
dotnetVersion: DotnetVersion,
|
dotnetVersion: DotnetVersion,
|
||||||
scriptArguments: string[]
|
scriptArguments: string[]
|
||||||
): void {
|
): void {
|
||||||
const option = IS_WINDOWS ? '-Quality' : '--quality';
|
const option = IS_WINDOWS ? '-Quality' : '--quality';
|
||||||
if (dotnetVersion.qualityFlag) {
|
if (dotnetVersion.qualityFlag) {
|
||||||
scriptArguments.push(option, this.quality);
|
scriptArguments.push(option, this.quality);
|
||||||
} else {
|
} else {
|
||||||
core.warning(
|
core.warning(
|
||||||
`'dotnet-quality' input can be used only with .NET SDK version in A.B, A.B.x, A and A.x formats where the major tag is higher than 5. You specified: ${this.version}. 'dotnet-quality' input is ignored.`
|
`'dotnet-quality' input can be used only with .NET SDK version in A.B, A.B.x, A and A.x formats where the major tag is higher than 5. You specified: ${this.version}. 'dotnet-quality' input is ignored.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async installDotnet(): Promise<string> {
|
public async installDotnet(): Promise<string> {
|
||||||
const windowsDefaultOptions = [
|
const windowsDefaultOptions = [
|
||||||
'-NoLogo',
|
'-NoLogo',
|
||||||
'-Sta',
|
'-Sta',
|
||||||
'-NoProfile',
|
'-NoProfile',
|
||||||
'-NonInteractive',
|
'-NonInteractive',
|
||||||
'-ExecutionPolicy',
|
'-ExecutionPolicy',
|
||||||
'Unrestricted',
|
'Unrestricted',
|
||||||
'-Command'
|
'-Command'
|
||||||
];
|
];
|
||||||
const scriptName = IS_WINDOWS ? 'install-dotnet.ps1' : 'install-dotnet.sh';
|
const scriptName = IS_WINDOWS ? 'install-dotnet.ps1' : 'install-dotnet.sh';
|
||||||
const escapedScript = path
|
const escapedScript = path
|
||||||
.join(__dirname, '..', 'externals', scriptName)
|
.join(__dirname, '..', 'externals', scriptName)
|
||||||
.replace(/'/g, "''");
|
.replace(/'/g, "''");
|
||||||
let scriptArguments: string[];
|
let scriptArguments: string[];
|
||||||
let scriptPath = '';
|
let scriptPath = '';
|
||||||
|
|
||||||
const versionResolver = new DotnetVersionResolver(this.version);
|
const versionResolver = new DotnetVersionResolver(this.version);
|
||||||
const dotnetVersion = await versionResolver.createDotNetVersion();
|
const dotnetVersion = await versionResolver.createDotNetVersion();
|
||||||
|
|
||||||
if (IS_WINDOWS) {
|
if (IS_WINDOWS) {
|
||||||
scriptArguments = ['&', `'${escapedScript}'`];
|
scriptArguments = ['&', `'${escapedScript}'`];
|
||||||
|
|
||||||
if (dotnetVersion.type) {
|
if (dotnetVersion.type) {
|
||||||
scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
|
scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.quality) {
|
if (this.quality) {
|
||||||
this.setQuality(dotnetVersion, scriptArguments);
|
this.setQuality(dotnetVersion, scriptArguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.env['https_proxy'] != null) {
|
if (process.env['https_proxy'] != null) {
|
||||||
scriptArguments.push(`-ProxyAddress ${process.env['https_proxy']}`);
|
scriptArguments.push(`-ProxyAddress ${process.env['https_proxy']}`);
|
||||||
}
|
}
|
||||||
// This is not currently an option
|
// This is not currently an option
|
||||||
if (process.env['no_proxy'] != null) {
|
if (process.env['no_proxy'] != null) {
|
||||||
scriptArguments.push(`-ProxyBypassList ${process.env['no_proxy']}`);
|
scriptArguments.push(`-ProxyBypassList ${process.env['no_proxy']}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
scriptPath =
|
scriptPath =
|
||||||
(await io.which('pwsh', false)) || (await io.which('powershell', true));
|
(await io.which('pwsh', false)) || (await io.which('powershell', true));
|
||||||
scriptArguments = windowsDefaultOptions.concat(scriptArguments);
|
scriptArguments = windowsDefaultOptions.concat(scriptArguments);
|
||||||
} else {
|
} else {
|
||||||
chmodSync(escapedScript, '777');
|
chmodSync(escapedScript, '777');
|
||||||
scriptPath = await io.which(escapedScript, true);
|
scriptPath = await io.which(escapedScript, true);
|
||||||
scriptArguments = [];
|
scriptArguments = [];
|
||||||
|
|
||||||
if (dotnetVersion.type) {
|
if (dotnetVersion.type) {
|
||||||
scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
|
scriptArguments.push(dotnetVersion.type, dotnetVersion.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.quality) {
|
if (this.quality) {
|
||||||
this.setQuality(dotnetVersion, scriptArguments);
|
this.setQuality(dotnetVersion, scriptArguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
|
// process.env must be explicitly passed in for DOTNET_INSTALL_DIR to be used
|
||||||
const getExecOutputOptions = {
|
const getExecOutputOptions = {
|
||||||
ignoreReturnCode: true,
|
ignoreReturnCode: true,
|
||||||
env: process.env as {string: string}
|
env: process.env as {string: string}
|
||||||
};
|
};
|
||||||
const {exitCode, stderr} = await exec.getExecOutput(
|
const {exitCode, stderr} = await exec.getExecOutput(
|
||||||
`"${scriptPath}"`,
|
`"${scriptPath}"`,
|
||||||
scriptArguments,
|
scriptArguments,
|
||||||
getExecOutputOptions
|
getExecOutputOptions
|
||||||
);
|
);
|
||||||
if (exitCode) {
|
if (exitCode) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to install dotnet, exit code: ${exitCode}. ${stderr}`
|
`Failed to install dotnet, exit code: ${exitCode}. ${stderr}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.outputDotnetVersion(dotnetVersion.value);
|
return this.outputDotnetVersion(dotnetVersion.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async outputDotnetVersion(version): Promise<string> {
|
private async outputDotnetVersion(version): Promise<string> {
|
||||||
const installationPath = process.env['DOTNET_INSTALL_DIR']!;
|
const installationPath = process.env['DOTNET_INSTALL_DIR']!;
|
||||||
let versionsOnRunner: string[] = await readdir(
|
const versionsOnRunner: string[] = await readdir(
|
||||||
path.join(installationPath.replace(/'/g, ''), 'sdk')
|
path.join(installationPath.replace(/'/g, ''), 'sdk')
|
||||||
);
|
);
|
||||||
|
|
||||||
let installedVersion = semver.maxSatisfying(versionsOnRunner, version, {
|
const installedVersion = semver.maxSatisfying(versionsOnRunner, version, {
|
||||||
includePrerelease: true
|
includePrerelease: true
|
||||||
})!;
|
})!;
|
||||||
|
|
||||||
return installedVersion;
|
return installedVersion;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue