Skip to content

Commit

Permalink
Merge pull request #353 from yc-actions/feature/clear
Browse files Browse the repository at this point in the history
feat: add `clear` input to delete all objects in bucket before upload
  • Loading branch information
nikolaymatrosov authored Jun 23, 2024
2 parents a7f1eb1 + 925f9b3 commit 67a78ab
Show file tree
Hide file tree
Showing 8 changed files with 1,700 additions and 2,351 deletions.
93 changes: 91 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
CompleteMultipartUploadCommand,
CreateMultipartUploadCommand,
DeleteObjectsCommand,
ListObjectsV2Command,
ListObjectsV2Output,
PutObjectCommand,
S3Client,
UploadPartCommand
Expand All @@ -9,7 +12,7 @@ import { expect, test } from '@jest/globals'
import * as fs from 'fs'
import * as path from 'path'
import * as process from 'process'
import { run, upload, UploadInputs } from '../src/main'
import { clearBucket, run, upload, UploadInputs } from '../src/main'
import * as core from '@actions/core'

const strCompare = (a: string | undefined, b: string | undefined): number => {
Expand All @@ -29,7 +32,8 @@ const requiredInputs: Record<string, string> = {
"public_key": "public_key"
}`,
bucket: 'bucket',
root: '.'
root: '.',
clear: 'false'
}

describe('upload', () => {
Expand All @@ -40,6 +44,85 @@ describe('upload', () => {
mockedSendFn.mockReset()
})

test('it should clear bucket', async () => {
mockedSendFn.mockImplementation(async cmd => {
if (cmd instanceof ListObjectsV2Command) {
const output: ListObjectsV2Output = {
Contents: [
{
Key: 'src/func.js'
}
],
IsTruncated: false
}
return Promise.resolve(output)
}
return Promise.resolve({})
})
await clearBucket(s3client, 'bucket')

const expected = [expect.any(ListObjectsV2Command), expect.any(DeleteObjectsCommand)]

for (let i = 0; i < expected.length; i++) {
expect(mockedSendFn).toHaveBeenNthCalledWith(i + 1, expected[i])
}
})

test('it should clear bucket with a lot objects', async () => {
const listCommnds: ListObjectsV2Output[] = [
{
Contents: [
{
Key: 'src/func.js'
}
],
IsTruncated: true,
NextContinuationToken: 'token'
},
{
Contents: [
{
Key: 'src/func.js'
}
],
IsTruncated: true,
NextContinuationToken: 'token'
},
{
Contents: [
{
Key: 'src/func.js'
}
],
IsTruncated: false
}
]
let listCommandIndex = 0
mockedSendFn.mockImplementation(async cmd => {
if (cmd instanceof ListObjectsV2Command) {
const output = listCommnds[listCommandIndex]
listCommandIndex += 1
return Promise.resolve(output)
}
return Promise.resolve({})
})
await clearBucket(s3client, 'bucket')

const expected = [
expect.any(ListObjectsV2Command),
expect.any(DeleteObjectsCommand),
expect.any(ListObjectsV2Command),
expect.any(DeleteObjectsCommand),
expect.any(ListObjectsV2Command),
expect.any(DeleteObjectsCommand)
]

for (let i = 0; i < expected.length; i++) {
expect(mockedSendFn).toHaveBeenNthCalledWith(i + 1, expected[i])
}
expect(mockedSendFn).toHaveBeenCalledTimes(expected.length)
})

test('it should add files from include', async () => {
const inputs: UploadInputs = {
bucket: 'bucket',
Expand Down Expand Up @@ -246,6 +329,7 @@ describe('upload', () => {
describe('run', () => {
// Mock the GitHub Actions core library
let getInputMock: jest.SpyInstance
let getBooleanInputMock: jest.SpyInstance
let setFailedMock: jest.SpyInstance

const s3client = new S3Client({})
Expand All @@ -254,8 +338,13 @@ describe('run', () => {
beforeEach(() => {
jest.clearAllMocks()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
getBooleanInputMock = jest.spyOn(core, 'getBooleanInput').mockImplementation()
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()

getBooleanInputMock.mockImplementation((): boolean => {
return false
})

mockedSendFn.mockReset()
})

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ inputs:
description: 'Exclude patterns. Multiline'
default: ''
required: false
clear:
description: 'Clear bucket before upload.'
default: 'false'
required: false

branding:
color: blue
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 67a78ab

Please sign in to comment.