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
30 changes: 29 additions & 1 deletion packages/cloudflare/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import type { CloudflareOptions } from './client';

/**
* Cloudflare's version metadata binding structure.
* @see https://developers.cloudflare.com/workers/runtime-apis/bindings/version-metadata/
*/
interface CfVersionMetadata {
id: string;
tag?: string;
timestamp?: string;
}

/**
* Checks if the value is a valid CF_VERSION_METADATA binding.
*/
function isVersionMetadata(value: unknown): value is CfVersionMetadata {
return typeof value === 'object' && value !== null && 'id' in value && typeof value.id === 'string';
}

/**
* Merges the options passed in from the user with the options we read from
* the Cloudflare `env` environment variable object.
*
* Release is determined with the following priority (highest to lowest):
* 1. User-provided release option
* 2. SENTRY_RELEASE environment variable
* 3. CF_VERSION_METADATA.id binding (if configured in the wrangler config)
*
* @param userOptions - The options passed in from the user.
* @param env - The environment variables.
*
Expand All @@ -14,7 +36,13 @@ export function getFinalOptions(userOptions: CloudflareOptions, env: unknown): C
return userOptions;
}

const release = 'SENTRY_RELEASE' in env && typeof env.SENTRY_RELEASE === 'string' ? env.SENTRY_RELEASE : undefined;
// Priority: userOptions.release > SENTRY_RELEASE > CF_VERSION_METADATA.id
const release =
'SENTRY_RELEASE' in env && typeof env.SENTRY_RELEASE === 'string'
? env.SENTRY_RELEASE
: 'CF_VERSION_METADATA' in env && isVersionMetadata(env.CF_VERSION_METADATA)
? env.CF_VERSION_METADATA.id
: undefined;

return { release, ...userOptions };
}
71 changes: 71 additions & 0 deletions packages/cloudflare/test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,75 @@ describe('getFinalOptions', () => {

expect(result).toEqual(userOptions);
});

describe('CF_VERSION_METADATA', () => {
it('uses CF_VERSION_METADATA.id as release when no other release is set', () => {
const userOptions = { dsn: 'test-dsn' };
const env = { CF_VERSION_METADATA: { id: 'version-123', tag: 'v1.0.0' } };

const result = getFinalOptions(userOptions, env);

expect(result).toEqual({ dsn: 'test-dsn', release: 'version-123' });
});

it('prefers SENTRY_RELEASE over CF_VERSION_METADATA.id', () => {
const userOptions = { dsn: 'test-dsn' };
const env = {
SENTRY_RELEASE: 'env-release',
CF_VERSION_METADATA: { id: 'version-123' },
};

const result = getFinalOptions(userOptions, env);

expect(result).toEqual({ dsn: 'test-dsn', release: 'env-release' });
});

it('prefers user release over CF_VERSION_METADATA.id', () => {
const userOptions = { dsn: 'test-dsn', release: 'user-release' };
const env = { CF_VERSION_METADATA: { id: 'version-123' } };

const result = getFinalOptions(userOptions, env);

expect(result).toEqual({ dsn: 'test-dsn', release: 'user-release' });
});

it('prefers user release over both SENTRY_RELEASE and CF_VERSION_METADATA.id', () => {
const userOptions = { dsn: 'test-dsn', release: 'user-release' };
const env = {
SENTRY_RELEASE: 'env-release',
CF_VERSION_METADATA: { id: 'version-123' },
};

const result = getFinalOptions(userOptions, env);

expect(result).toEqual({ dsn: 'test-dsn', release: 'user-release' });
});

it('ignores CF_VERSION_METADATA when it is not an object', () => {
const userOptions = { dsn: 'test-dsn' };
const env = { CF_VERSION_METADATA: 'not-an-object' };

const result = getFinalOptions(userOptions, env);

expect(result).toEqual({ dsn: 'test-dsn', release: undefined });
});

it('ignores CF_VERSION_METADATA when id is not a string', () => {
const userOptions = { dsn: 'test-dsn' };
const env = { CF_VERSION_METADATA: { id: 123 } };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: can this happen/is it common to provide a non-string id? If so, we could stringify the release. Though only worth doing if this is actually a thing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not defined what the version ID can be. Right now the id is always a UUID, so a string. But I'm not sure if this is always the case.


const result = getFinalOptions(userOptions, env);

expect(result).toEqual({ dsn: 'test-dsn', release: undefined });
});

it('ignores CF_VERSION_METADATA when id is missing', () => {
const userOptions = { dsn: 'test-dsn' };
const env = { CF_VERSION_METADATA: { tag: 'v1.0.0' } };

const result = getFinalOptions(userOptions, env);

expect(result).toEqual({ dsn: 'test-dsn', release: undefined });
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing integration or E2E test for new feature

Low Severity · Bugbot Rules

This feat PR adds support for CF_VERSION_METADATA as a release source but only includes unit tests. The user-specified review rules require that feat PRs include at least one integration or E2E test. While the unit tests thoroughly cover the getFinalOptions logic, an integration test validating the feature works correctly in an actual Cloudflare environment would strengthen confidence in the change.

Fix in Cursor Fix in Web

});
});
Loading