Skip to content

Add CacheMetadata with repository_id to cache v2 requests #2093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
25 changes: 25 additions & 0 deletions packages/cache/__tests__/saveCacheV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jest.mock('@azure/storage-blob', () => ({

beforeAll(() => {
process.env['ACTIONS_RUNTIME_TOKEN'] = 'token'
process.env['GITHUB_REPOSITORY_ID'] = '123456789'
jest.spyOn(console, 'log').mockImplementation(() => {})
jest.spyOn(core, 'debug').mockImplementation(() => {})
jest.spyOn(core, 'info').mockImplementation(() => {})
Expand Down Expand Up @@ -124,6 +125,10 @@ test('create cache entry failure on non-ok response', async () => {
)

expect(createCacheEntryMock).toHaveBeenCalledWith({
metadata: {
repositoryId: '123456789',
scope: []
},
key,
version: cacheVersion
})
Comment on lines 127 to 134
Copy link
Preview

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

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

[nitpick] To reduce duplication and improve clarity, extract the expected metadata object into a constant or use expect.objectContaining({ metadata }) so tests remain concise.

Suggested change
expect(createCacheEntryMock).toHaveBeenCalledWith({
metadata: {
repositoryId: '123456789',
scope: []
},
key,
version: cacheVersion
})
const metadata = {
repositoryId: '123456789',
scope: []
};
expect(createCacheEntryMock).toHaveBeenCalledWith({
metadata,
key,
version: cacheVersion
});

Copilot uses AI. Check for mistakes.

Expand Down Expand Up @@ -159,6 +164,10 @@ test('create cache entry fails on rejected promise', async () => {
)

expect(createCacheEntryMock).toHaveBeenCalledWith({
metadata: {
repositoryId: '123456789',
scope: []
},
key,
version: cacheUtils.getCacheVersion(paths, compression)
})
Expand Down Expand Up @@ -202,6 +211,10 @@ test('save cache fails if a signedUploadURL was not passed', async () => {

expect(cacheId).toBe(-1)
expect(createCacheEntryMock).toHaveBeenCalledWith({
metadata: {
repositoryId: '123456789',
scope: []
},
key,
version: cacheVersion
})
Expand Down Expand Up @@ -265,6 +278,10 @@ test('finalize save cache failure', async () => {
const cacheId = await saveCache([paths], key, options)

expect(createCacheEntryMock).toHaveBeenCalledWith({
metadata: {
repositoryId: '123456789',
scope: []
},
key,
version: cacheVersion
})
Expand All @@ -286,6 +303,10 @@ test('finalize save cache failure', async () => {
expect(getCompressionMock).toHaveBeenCalledTimes(1)

expect(finalizeCacheEntryMock).toHaveBeenCalledWith({
metadata: {
repositoryId: '123456789',
scope: []
},
key,
version: cacheVersion,
sizeBytes: archiveFileSize.toString()
Expand Down Expand Up @@ -351,6 +372,10 @@ test('save with valid inputs uploads a cache', async () => {
)

expect(finalizeCacheEntryMock).toHaveBeenCalledWith({
metadata: {
repositoryId: '123456789',
scope: []
},
key,
version: cacheVersion,
sizeBytes: archiveFileSize.toString()
Expand Down
14 changes: 14 additions & 0 deletions packages/cache/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
FinalizeCacheEntryUploadResponse,
GetCacheEntryDownloadURLRequest
} from './generated/results/api/v1/cache'
import {CacheMetadata} from './generated/results/entities/v1/cachemetadata'
import {CacheFileSizeLimit} from './internal/constants'
export class ValidationError extends Error {
constructor(message: string) {
Expand Down Expand Up @@ -51,6 +52,17 @@ function checkKey(key: string): void {
}
}

function getCacheMetadata(): CacheMetadata | undefined {
const repositoryId = process.env['GITHUB_REPOSITORY_ID']
if (!repositoryId) {
return undefined
}
return {
repositoryId,
scope: []
}
}

/**
* isFeatureAvailable to check the presence of Actions cache service
*
Expand Down Expand Up @@ -525,6 +537,7 @@ async function saveCacheV2(
enableCrossOsArchive
)
const request: CreateCacheEntryRequest = {
metadata: getCacheMetadata(),
Comment on lines 539 to +540
Copy link
Preview

Copilot AI Jun 18, 2025

Choose a reason for hiding this comment

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

[nitpick] Since getCacheMetadata() is called twice in this function, consider invoking it once (e.g., const metadata = getCacheMetadata()) and reusing the same object for both requests to improve readability and consistency.

Suggested change
const request: CreateCacheEntryRequest = {
metadata: getCacheMetadata(),
const metadata = getCacheMetadata();
const request: CreateCacheEntryRequest = {
metadata,

Copilot uses AI. Check for mistakes.

key,
version
}
Expand Down Expand Up @@ -553,6 +566,7 @@ async function saveCacheV2(
)

const finalizeRequest: FinalizeCacheEntryUploadRequest = {
metadata: getCacheMetadata(),
key,
version,
sizeBytes: `${archiveFileSize}`
Expand Down