Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The install-analyzers task interacts with NuGet via two APIs:
Key design decisions:
- `parseRegistrationIndex()` is a pure function (no I/O) for easy testing
- `queryNuGetRegistration()` is a shared module usable by any task needing NuGet version info
- `User-Agent: ALCops-AzureDevOps` is set on all HTTP requests for NuGet.org statistics tracking
- `User-Agent: vsts-task-installer/{version}` is set on NuGet HTTP requests, matching a known client pattern in NuGet.org's CDN log parser for download statistics visibility
- Unlisted versions are filtered out during version resolution
- `resolveVersion()` returns a `ResolvedVersion` with both the version string and the `packageContentUrl` from the Registration API (avoids redundant URL construction)

Expand Down
10 changes: 10 additions & 0 deletions shared/user-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as os from 'os';

/**
* Build the User-Agent string sent on NuGet HTTP requests.
* Uses the `vsts-task-installer` known-client pattern recognised by NuGet.org's
* CDN log parser so downloads appear in per-package statistics.
*/
export function getUserAgent(version: string): string {
return `vsts-task-installer/${version} (Node.js ${process.version}; ${os.type()} ${os.release()})`;
}
5 changes: 4 additions & 1 deletion tasks/install-analyzers/src/nuget-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { NUGET_PACKAGE_NAME, NUGET_FLAT_CONTAINER, RegistrationVersion } from '.
import { Logger, nullLogger } from '../../../shared/logger';
import { queryNuGetRegistration } from '../../../shared/nuget-registration';
import { httpsGetBuffer } from '../../../shared/http-client';
import { getUserAgent } from '../../../shared/user-agent';
import taskJson from '../task.json';

const packageId = NUGET_PACKAGE_NAME.toLowerCase();
const USER_AGENT = 'ALCops-AzureDevOps';
const { Major, Minor, Patch } = taskJson.version;
const USER_AGENT = getUserAgent(`${Major}.${Minor}.${Patch}`);

export interface ResolvedVersion {
version: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/install-analyzers/nuget-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('downloadPackage', () => {
try {
await downloadPackage('1.0.0', tmpDir);
const calledOpts = mockRequest.mock.calls[0][1] as { headers?: Record<string, string> };
expect(calledOpts.headers?.['User-Agent']).toBe('ALCops-AzureDevOps');
expect(calledOpts.headers?.['User-Agent']).toMatch(/^vsts-task-installer\/\d+\.\d+\.\d+ \(Node\.js v/);
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
Expand Down
20 changes: 20 additions & 0 deletions tests/shared/user-agent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect } from 'vitest';
import { getUserAgent } from '../../shared/user-agent';

describe('getUserAgent', () => {
it('returns vsts-task-installer format with version', () => {
const ua = getUserAgent('1.2.3');
expect(ua).toMatch(/^vsts-task-installer\/1\.2\.3 \(Node\.js v\d+\.\d+\.\d+; \w+ .+\)$/);
});

it('includes process.version and os info', () => {
const ua = getUserAgent('0.0.1');
expect(ua).toContain(`Node.js ${process.version}`);
expect(ua).toContain('vsts-task-installer/0.0.1');
});

it('works with pre-release style versions', () => {
const ua = getUserAgent('2.0.0-beta.1');
expect(ua).toMatch(/^vsts-task-installer\/2\.0\.0-beta\.1 \(/);
});
});