Skip to content

Commit 752bae6

Browse files
committed
fix: only needed artifacts are pushed
1 parent 857072a commit 752bae6

File tree

7 files changed

+84
-31
lines changed

7 files changed

+84
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ dist
22
node_modules
33
coverage
44
.DS_Store
5+
.env
56
/test/transform/generated-*/
67
/test/transform/ecosystem-repos
78
# Transform

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"dependencies": {
4444
"@babel/parser": "^7.21.4",
4545
"@babel/traverse": "^7.21.4",
46+
"@babel/types": "^7.22.5",
4647
"c12": "^1.4.1",
4748
"copy-anything": "^3.0.5",
4849
"fast-equals": "^5.0.1",
@@ -57,7 +58,6 @@
5758
"unplugin": "^1.3.1"
5859
},
5960
"devDependencies": {
60-
"@babel/types": "^7.21.4",
6161
"@happy-dom/global-registrator": "^9.20.3",
6262
"@nuxtjs/eslint-config-typescript": "latest",
6363
"@rollup/plugin-commonjs": "^25.0.0",
@@ -72,6 +72,7 @@
7272
"@vitest/browser": "^0.30.1",
7373
"acorn": "8.8.2",
7474
"degit": "^2.8.4",
75+
"dotenv": "^16.3.1",
7576
"eslint": "8.36.0",
7677
"eslint-config-prettier": "latest",
7778
"eslint-plugin-prettier": "latest",

pnpm-lock.yaml

Lines changed: 16 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ export type Artifact = {
157157
functionId?: string
158158
}
159159

160+
export type DatabaseArtifact = Artifact & {
161+
id: string
162+
createdAt: string
163+
projectId: string
164+
functionId: null | undefined | string
165+
}
166+
160167
export type ArtifactCacheEntry = {
161168
timestamp: number
162169
uploadStatus: 'not-uploaded' | 'uploaded' | 'upload-failed'

src/transform/artifacts/batchedArtifactsUpload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function batchedArtifactsUpload(
1717

1818
const uploadedArtifactBatches = await Promise.all(
1919
batches.map(async (batch) => {
20-
const { data, error } = await post<Artifact>(
20+
const { data, error } = await post<string[]>(
2121
`${FLYTRAP_API_BASE}/api/v1/artifacts/${projectId}`,
2222
JSON.stringify({
2323
artifacts: batch
@@ -35,5 +35,5 @@ export async function batchedArtifactsUpload(
3535
return data
3636
})
3737
)
38-
return uploadedArtifactBatches
38+
return uploadedArtifactBatches.reduce((acc, curr) => [...acc, ...curr], [] as string[])
3939
}

src/transform/artifacts/cache.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dirname, join } from 'path'
22
import { homedir } from 'os'
33
import { deepEqual } from 'fast-equals'
44
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
5-
import { Artifact, CacheFile } from '../../exports'
5+
import { Artifact, CacheFile, DatabaseArtifact } from '../../exports'
66
import { cwd } from 'process'
77
import { formatBytes, get } from '../../core/util'
88
import { FLYTRAP_API_BASE } from '../../core/config'
@@ -57,7 +57,13 @@ const _populateCache = async (projectId: string, secretApiKey: string): Promise<
5757
return cache
5858
}
5959

60-
async function _getArtifactsToUpload(
60+
function _removeDatabaseKeys(artifact: DatabaseArtifact): Artifact {
61+
const { id, createdAt, projectId, ...rest } = artifact
62+
if (rest.functionId === null) delete rest.functionId
63+
return rest
64+
}
65+
66+
export async function _getArtifactsToUpload(
6167
projectId: string,
6268
secretApiKey: string,
6369
artifacts: Artifact[]
@@ -70,8 +76,11 @@ async function _getArtifactsToUpload(
7076
artifacts[i].functionOrCallId
7177
)
7278
if (existingArtifactIndex !== -1) {
79+
const withoutDatabaseKeys = _removeDatabaseKeys(
80+
alreadyUploadedArtifacts[existingArtifactIndex]
81+
)
7382
// Exists, check if they're the same
74-
if (!deepEqual(alreadyUploadedArtifacts[existingArtifactIndex], artifacts[i])) {
83+
if (!deepEqual(withoutDatabaseKeys, artifacts[i])) {
7584
artifactsToUpload.push(artifacts[i])
7685
continue
7786
}
@@ -161,8 +170,8 @@ export function getArtifactsToUpload(projectId: string) {
161170
.reduce((acc, curr) => [...acc, curr.artifact], [] as Artifact[])
162171
}
163172

164-
async function _fetchUploadedArtifacts(projectId: string, secretApiKey: string) {
165-
const { data, error } = await get<Artifact[]>(
173+
export async function _fetchUploadedArtifacts(projectId: string, secretApiKey: string) {
174+
const { data, error } = await get<DatabaseArtifact[]>(
166175
`${FLYTRAP_API_BASE}/api/v1/artifacts/${projectId}`,
167176
undefined,
168177
{

test/artifacts.test.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ import babelTsParser from 'recast/parsers/babel-ts.js'
1111
import { Identifier } from '@babel/types'
1212
import { flytrapTransformArtifacts } from '../src/transform/index'
1313
import {
14+
_fetchUploadedArtifacts,
15+
_getArtifactsToUpload,
1416
getArtifactsToUpload,
1517
getCacheFilePath,
1618
markArtifactAsUploaded,
17-
upsertArtifact
19+
upsertArtifact,
20+
upsertArtifacts
1821
} from '../src/transform/artifacts/cache'
1922
import { Artifact } from '../src/exports'
2023
import { rmSync } from 'fs'
24+
import { config } from 'dotenv'
25+
config()
2126

2227
describe('Artifacts for functions', () => {
2328
const arrowFunctionFixture = `
@@ -261,9 +266,17 @@ it('generates values same as transform', () => {
261266
})
262267

263268
describe('Cache', () => {
269+
const testEnvProjectId = process.env['FLYTRAP_PROJECT_ID']
270+
const testEnvSecretApiKey = process.env['FLYTRAP_SECRET_KEY']
271+
if (!testEnvProjectId) {
272+
throw new Error(`Flytrap Testing Environment project ID not defined.`)
273+
}
274+
if (!testEnvSecretApiKey) {
275+
throw new Error(`Flytrap Testing Environment secret API key not defined.`)
276+
}
264277
const cacheFilePath = getCacheFilePath('mock-project-id')
265278
const mockArtifact: Artifact = {
266-
functionOrCallId: '',
279+
functionOrCallId: `mock-call-id-${new Date().toISOString()}`,
267280
functionOrCallName: '',
268281
params: '',
269282
scopes: [],
@@ -278,6 +291,33 @@ describe('Cache', () => {
278291
rmSync(cacheFilePath, { recursive: true })
279292
})
280293

294+
it('_fetchUploadedArtifacts', async () => {
295+
const uploadedArtifacts = await _fetchUploadedArtifacts(testEnvProjectId, testEnvSecretApiKey)
296+
expect(
297+
uploadedArtifacts.find((u) => u.functionOrCallId === mockArtifact.functionOrCallId) !==
298+
undefined
299+
)
300+
})
301+
302+
it('_getArtifactsToUpload', async () => {
303+
const artifactsToUpload = await _getArtifactsToUpload(testEnvProjectId, testEnvSecretApiKey, [
304+
mockArtifact
305+
])
306+
expect(artifactsToUpload).toEqual([mockArtifact])
307+
})
308+
309+
it('upsertArtifacts', async () => {
310+
const response = await upsertArtifacts(testEnvProjectId, testEnvSecretApiKey, [mockArtifact])
311+
expect(response.includes(mockArtifact.functionOrCallId))
312+
})
313+
314+
it('_getArtifactsToUpload no longer include the mock artifact', async () => {
315+
const artifactsToUpload = await _getArtifactsToUpload(testEnvProjectId, testEnvSecretApiKey, [
316+
mockArtifact
317+
])
318+
expect(artifactsToUpload).toEqual([])
319+
})
320+
281321
it('upserts artifact', () => {
282322
upsertArtifact('mock-project-id', mockArtifact)
283323
})

0 commit comments

Comments
 (0)