Compare commits

...

5 Commits

Author SHA1 Message Date
Jason Karns
b6410ac24d
Merge 55b7d827be into abb238b131 2024-10-22 11:07:09 -06:00
John Wesley Walker III
abb238b131
Revise isGhes logic (#1148)
* Revise `isGhes` logic

* ran 'npm run format'

* added unit test
2024-10-21 11:41:32 -05:00
Jason Karns
55b7d827be
Refactor and test 2024-05-29 08:52:52 -04:00
Jason Karns
473cb1b506
Make linter happy 2024-05-28 16:13:11 -04:00
Jason Karns
574c6daa52
Spike to default the scope from package.json 2024-05-28 16:13:08 -04:00
6 changed files with 85 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import * as auth from '../src/authutil';
import * as cacheUtils from '../src/cache-utils';
let rcFile: string;
let pkgJson: string;
describe('authutil tests', () => {
const _runnerDir = path.join(__dirname, 'runner');
@ -25,10 +26,12 @@ describe('authutil tests', () => {
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
process.env['RUNNER_TEMP'] = tempDir;
rcFile = path.join(tempDir, '.npmrc');
pkgJson = path.join(tempDir, 'package.json');
}, 100000);
beforeEach(async () => {
await io.rmRF(rcFile);
await io.rmRF(pkgJson);
// if (fs.existsSync(rcFile)) {
// fs.unlinkSync(rcFile);
// }
@ -113,6 +116,15 @@ describe('authutil tests', () => {
expect(rc['always-auth']).toBe('false');
});
it('Automatically configures npm scope from package.json', async () => {
process.env['INPUT_SCOPE'] = '';
fs.writeFileSync(pkgJson, '{"name":"@myscope/mypackage"}');
await auth.configAuthentication('https://registry.npmjs.org', '');
const rc = readRcFile(rcFile);
expect(rc['@myscope:registry']).toBe('https://registry.npmjs.org/');
});
it('Sets up npmrc for always-auth true', async () => {
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
expect(fs.statSync(rcFile)).toBeDefined();

View File

@ -6,7 +6,7 @@ import {
PackageManagerInfo,
isCacheFeatureAvailable,
supportedPackageManagers,
getCommandOutput,
isGhes,
resetProjectDirectoriesMemoized
} from '../src/cache-utils';
import fs from 'fs';
@ -361,3 +361,41 @@ describe('cache-utils', () => {
);
});
});
describe('isGhes', () => {
const pristineEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {...pristineEnv};
});
afterAll(() => {
process.env = pristineEnv;
});
it('returns false when the GITHUB_SERVER_URL environment variable is not defined', () => {
delete process.env['GITHUB_SERVER_URL'];
expect(isGhes()).toBeFalsy();
});
it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', () => {
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
expect(isGhes()).toBeFalsy();
});
it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', () => {
process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com';
expect(isGhes()).toBeFalsy();
});
it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', () => {
process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost';
expect(isGhes()).toBeFalsy();
});
it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', () => {
process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com';
expect(isGhes()).toBeTruthy();
});
});

View File

@ -83977,7 +83977,11 @@ const repoHasYarnBerryManagedDependencies = (packageManagerInfo, cacheDependency
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
const isGitHubHost = hostname === 'GITHUB.COM';
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
const isLocalHost = hostname.endsWith('.LOCALHOST');
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
}
exports.isGhes = isGhes;
function isCacheFeatureAvailable() {

6
dist/setup/index.js vendored
View File

@ -93598,7 +93598,11 @@ const repoHasYarnBerryManagedDependencies = (packageManagerInfo, cacheDependency
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
const isGitHubHost = hostname === 'GITHUB.COM';
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
const isLocalHost = hostname.endsWith('.LOCALHOST');
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
}
exports.isGhes = isGhes;
function isCacheFeatureAvailable() {

View File

@ -25,6 +25,12 @@ function writeRegistryToFile(
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
}
if (!scope) {
const namePrefix = packageJson('name')?.match(/^(@[^/]+)\//);
if (namePrefix) {
scope = namePrefix[1];
}
}
if (scope && scope[0] != '@') {
scope = '@' + scope;
}
@ -57,3 +63,14 @@ function writeRegistryToFile(
process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX'
);
}
function packageJson(prop: string){
const pkgPath: string = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), 'package.json');
try {
const json = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
return prop ? json[prop] : json;
} catch(e) {
core.debug(`Unable to read from package.json`);
}
}

View File

@ -295,7 +295,13 @@ export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
const isGitHubHost = hostname === 'GITHUB.COM';
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
const isLocalHost = hostname.endsWith('.LOCALHOST');
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
}
export function isCacheFeatureAvailable(): boolean {