Skip to content

Commit

Permalink
feat(delete): add ignore-missing (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbioteau authored Jun 24, 2024
1 parent 9cba154 commit 00a0a18
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ jobs:
parent-folder-id: ${{ vars.GDRIVE_GITHUB_ACTION_FOLDER }}
target-filepath: output/community/

- name: Test Local Delete Action With Ignore Missing
id: test-delete-action
uses: ./delete
with:
credentials: ${{ env.GDRIVE_SERVICE_ACCOUNT_CREDENTIALS }}
parent-folder-id: ${{ vars.GDRIVE_GITHUB_ACTION_FOLDER }}
target-filepath: a_file_that_does_not_exist.txt
ignore-missing: true

- name: Print Output
id: output
run: echo "${{ steps.test-action.outputs.file-id }}"
2 changes: 2 additions & 0 deletions delete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| `credentials` | Google API credentials in base64 format. | `true` |
| `parent-folder-id` | The parent folder ID in Google Drive. | `true` |
| `target-filepath` | The remote file path in Google Drive of the uploaded file relative to the given parent folder. Use parent folder root with source filename when not set. | `true` |
| `ignore-missing` | Ignore if the target file does not exist. | `false` |

### Outputs

Expand All @@ -31,6 +32,7 @@ steps:
credentials: ${{ secrets.GDRIVE_CREDENTIALS }} # credentials stored as a GitHub secret
parent-folder-id: ${{ vars.GDRIVE_FOLDER_ID }} # folder id stored as a GitHub variable
target-filepath: test/hello_1.txt
ignore-missing: true # Does not fail the step if target file does not exists

- name: Print Output
id: output
Expand Down
3 changes: 3 additions & 0 deletions delete/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
description:
'The remote file path in Google Drive of the file to delete relative to the
given parent folder.'
ignore-missing:
description: 'Ignore if the target file does not exist.'
default: 'false'

outputs:
file-id:
Expand Down
12 changes: 9 additions & 3 deletions delete/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51615,7 +51615,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.OUTPUT_FILE_ID = exports.INPUT_TARGET_PARENT_FOLDER_ID = exports.INPUT_ELEMENT_NAME = exports.INPUT_SOURCE_PARENT_FOLDER_ID = exports.INPUT_CREATE_CHECKSUM = exports.INPUT_OVERWRITE = exports.INPUT_TARGET_FILEPATH = exports.INPUT_SOURCE_FILEPATH = exports.INPUT_PARENT_FOLDER_ID = exports.INPUT_CREDENTIALS = void 0;
exports.OUTPUT_FILE_ID = exports.INPUT_IGNORE_MISSING = exports.INPUT_TARGET_PARENT_FOLDER_ID = exports.INPUT_ELEMENT_NAME = exports.INPUT_SOURCE_PARENT_FOLDER_ID = exports.INPUT_CREATE_CHECKSUM = exports.INPUT_OVERWRITE = exports.INPUT_TARGET_FILEPATH = exports.INPUT_SOURCE_FILEPATH = exports.INPUT_PARENT_FOLDER_ID = exports.INPUT_CREDENTIALS = void 0;
exports.runDelete = runDelete;
exports.runUpload = runUpload;
exports.runMove = runMove;
Expand All @@ -51636,6 +51636,7 @@ exports.INPUT_CREATE_CHECKSUM = 'create-checksum';
exports.INPUT_SOURCE_PARENT_FOLDER_ID = 'source-parent-folder-id';
exports.INPUT_ELEMENT_NAME = 'element-name';
exports.INPUT_TARGET_PARENT_FOLDER_ID = 'target-parent-folder-id';
exports.INPUT_IGNORE_MISSING = 'ignore-missing';
// Outputs
exports.OUTPUT_FILE_ID = 'file-id';
/**
Expand All @@ -51648,9 +51649,10 @@ async function runDelete() {
const credentials = core.getInput(exports.INPUT_CREDENTIALS, { required: true });
const parentFolderId = core.getInput(exports.INPUT_PARENT_FOLDER_ID, { required: true });
const targetFilePath = core.getInput(exports.INPUT_TARGET_FILEPATH, { required: true });
const ignoreMissing = core.getBooleanInput(exports.INPUT_IGNORE_MISSING);
// Init Google Drive API instance
const drive = initDriveAPI(credentials);
const fileId = await deleteFile(drive, parentFolderId, targetFilePath);
const fileId = await deleteFile(drive, parentFolderId, targetFilePath, ignoreMissing);
// Set outputs
core.setOutput(exports.OUTPUT_FILE_ID, fileId);
}
Expand All @@ -51664,7 +51666,7 @@ async function runDelete() {
}
}
}
async function deleteFile(drive, parentId, targetFilePath) {
async function deleteFile(drive, parentId, targetFilePath, ignoreMissing) {
if (targetFilePath.endsWith('/')) {
targetFilePath = targetFilePath.substring(0, targetFilePath.length - 1);
}
Expand All @@ -51682,6 +51684,10 @@ async function deleteFile(drive, parentId, targetFilePath) {
const fileName = targetPaths[targetPaths.length - 1];
const fileId = await getFileId(drive, parentId, fileName);
if (!fileId) {
if (ignoreMissing) {
core.warning(`File '${fileName}' does not exist in folder '${parentId}'`);
return null;
}
throw new Error(`File '${fileName}' does not exist in folder '${parentId}'`);
}
core.debug(`Deleting file '${fileName}' in folder '${parentId}'`);
Expand Down
12 changes: 9 additions & 3 deletions move/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51615,7 +51615,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.OUTPUT_FILE_ID = exports.INPUT_TARGET_PARENT_FOLDER_ID = exports.INPUT_ELEMENT_NAME = exports.INPUT_SOURCE_PARENT_FOLDER_ID = exports.INPUT_CREATE_CHECKSUM = exports.INPUT_OVERWRITE = exports.INPUT_TARGET_FILEPATH = exports.INPUT_SOURCE_FILEPATH = exports.INPUT_PARENT_FOLDER_ID = exports.INPUT_CREDENTIALS = void 0;
exports.OUTPUT_FILE_ID = exports.INPUT_IGNORE_MISSING = exports.INPUT_TARGET_PARENT_FOLDER_ID = exports.INPUT_ELEMENT_NAME = exports.INPUT_SOURCE_PARENT_FOLDER_ID = exports.INPUT_CREATE_CHECKSUM = exports.INPUT_OVERWRITE = exports.INPUT_TARGET_FILEPATH = exports.INPUT_SOURCE_FILEPATH = exports.INPUT_PARENT_FOLDER_ID = exports.INPUT_CREDENTIALS = void 0;
exports.runDelete = runDelete;
exports.runUpload = runUpload;
exports.runMove = runMove;
Expand All @@ -51636,6 +51636,7 @@ exports.INPUT_CREATE_CHECKSUM = 'create-checksum';
exports.INPUT_SOURCE_PARENT_FOLDER_ID = 'source-parent-folder-id';
exports.INPUT_ELEMENT_NAME = 'element-name';
exports.INPUT_TARGET_PARENT_FOLDER_ID = 'target-parent-folder-id';
exports.INPUT_IGNORE_MISSING = 'ignore-missing';
// Outputs
exports.OUTPUT_FILE_ID = 'file-id';
/**
Expand All @@ -51648,9 +51649,10 @@ async function runDelete() {
const credentials = core.getInput(exports.INPUT_CREDENTIALS, { required: true });
const parentFolderId = core.getInput(exports.INPUT_PARENT_FOLDER_ID, { required: true });
const targetFilePath = core.getInput(exports.INPUT_TARGET_FILEPATH, { required: true });
const ignoreMissing = core.getBooleanInput(exports.INPUT_IGNORE_MISSING);
// Init Google Drive API instance
const drive = initDriveAPI(credentials);
const fileId = await deleteFile(drive, parentFolderId, targetFilePath);
const fileId = await deleteFile(drive, parentFolderId, targetFilePath, ignoreMissing);
// Set outputs
core.setOutput(exports.OUTPUT_FILE_ID, fileId);
}
Expand All @@ -51664,7 +51666,7 @@ async function runDelete() {
}
}
}
async function deleteFile(drive, parentId, targetFilePath) {
async function deleteFile(drive, parentId, targetFilePath, ignoreMissing) {
if (targetFilePath.endsWith('/')) {
targetFilePath = targetFilePath.substring(0, targetFilePath.length - 1);
}
Expand All @@ -51682,6 +51684,10 @@ async function deleteFile(drive, parentId, targetFilePath) {
const fileName = targetPaths[targetPaths.length - 1];
const fileId = await getFileId(drive, parentId, fileName);
if (!fileId) {
if (ignoreMissing) {
core.warning(`File '${fileName}' does not exist in folder '${parentId}'`);
return null;
}
throw new Error(`File '${fileName}' does not exist in folder '${parentId}'`);
}
core.debug(`Deleting file '${fileName}' in folder '${parentId}'`);
Expand Down
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const INPUT_CREATE_CHECKSUM = 'create-checksum'
export const INPUT_SOURCE_PARENT_FOLDER_ID = 'source-parent-folder-id'
export const INPUT_ELEMENT_NAME = 'element-name'
export const INPUT_TARGET_PARENT_FOLDER_ID = 'target-parent-folder-id'
export const INPUT_IGNORE_MISSING = 'ignore-missing'

// Outputs
export const OUTPUT_FILE_ID = 'file-id'
Expand All @@ -31,11 +32,12 @@ export async function runDelete(): Promise<void> {
const credentials = core.getInput(INPUT_CREDENTIALS, { required: true })
const parentFolderId = core.getInput(INPUT_PARENT_FOLDER_ID, { required: true })
const targetFilePath = core.getInput(INPUT_TARGET_FILEPATH, { required: true })
const ignoreMissing = core.getBooleanInput(INPUT_IGNORE_MISSING)

// Init Google Drive API instance
const drive = initDriveAPI(credentials)

const fileId = await deleteFile(drive, parentFolderId, targetFilePath)
const fileId = await deleteFile(drive, parentFolderId, targetFilePath, ignoreMissing)

// Set outputs
core.setOutput(OUTPUT_FILE_ID, fileId)
Expand All @@ -52,11 +54,13 @@ export async function runDelete(): Promise<void> {
async function deleteFile(
drive: google.drive_v3.Drive,
parentId: string,
targetFilePath: string
targetFilePath: string,
ignoreMissing: boolean
): Promise<string | null> {
if (targetFilePath.endsWith('/')) {
targetFilePath = targetFilePath.substring(0, targetFilePath.length - 1)
}

const targetPaths = targetFilePath.split(path.sep)
while (targetPaths.length > 1) {
const folderName = targetPaths.shift()
Expand All @@ -72,6 +76,10 @@ async function deleteFile(
const fileName = targetPaths[targetPaths.length - 1]
const fileId = await getFileId(drive, parentId, fileName)
if (!fileId) {
if (ignoreMissing) {
core.warning(`File '${fileName}' does not exist in folder '${parentId}'`)
return null
}
throw new Error(`File '${fileName}' does not exist in folder '${parentId}'`)
}

Expand Down
12 changes: 9 additions & 3 deletions upload/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51615,7 +51615,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.OUTPUT_FILE_ID = exports.INPUT_TARGET_PARENT_FOLDER_ID = exports.INPUT_ELEMENT_NAME = exports.INPUT_SOURCE_PARENT_FOLDER_ID = exports.INPUT_CREATE_CHECKSUM = exports.INPUT_OVERWRITE = exports.INPUT_TARGET_FILEPATH = exports.INPUT_SOURCE_FILEPATH = exports.INPUT_PARENT_FOLDER_ID = exports.INPUT_CREDENTIALS = void 0;
exports.OUTPUT_FILE_ID = exports.INPUT_IGNORE_MISSING = exports.INPUT_TARGET_PARENT_FOLDER_ID = exports.INPUT_ELEMENT_NAME = exports.INPUT_SOURCE_PARENT_FOLDER_ID = exports.INPUT_CREATE_CHECKSUM = exports.INPUT_OVERWRITE = exports.INPUT_TARGET_FILEPATH = exports.INPUT_SOURCE_FILEPATH = exports.INPUT_PARENT_FOLDER_ID = exports.INPUT_CREDENTIALS = void 0;
exports.runDelete = runDelete;
exports.runUpload = runUpload;
exports.runMove = runMove;
Expand All @@ -51636,6 +51636,7 @@ exports.INPUT_CREATE_CHECKSUM = 'create-checksum';
exports.INPUT_SOURCE_PARENT_FOLDER_ID = 'source-parent-folder-id';
exports.INPUT_ELEMENT_NAME = 'element-name';
exports.INPUT_TARGET_PARENT_FOLDER_ID = 'target-parent-folder-id';
exports.INPUT_IGNORE_MISSING = 'ignore-missing';
// Outputs
exports.OUTPUT_FILE_ID = 'file-id';
/**
Expand All @@ -51648,9 +51649,10 @@ async function runDelete() {
const credentials = core.getInput(exports.INPUT_CREDENTIALS, { required: true });
const parentFolderId = core.getInput(exports.INPUT_PARENT_FOLDER_ID, { required: true });
const targetFilePath = core.getInput(exports.INPUT_TARGET_FILEPATH, { required: true });
const ignoreMissing = core.getBooleanInput(exports.INPUT_IGNORE_MISSING);
// Init Google Drive API instance
const drive = initDriveAPI(credentials);
const fileId = await deleteFile(drive, parentFolderId, targetFilePath);
const fileId = await deleteFile(drive, parentFolderId, targetFilePath, ignoreMissing);
// Set outputs
core.setOutput(exports.OUTPUT_FILE_ID, fileId);
}
Expand All @@ -51664,7 +51666,7 @@ async function runDelete() {
}
}
}
async function deleteFile(drive, parentId, targetFilePath) {
async function deleteFile(drive, parentId, targetFilePath, ignoreMissing) {
if (targetFilePath.endsWith('/')) {
targetFilePath = targetFilePath.substring(0, targetFilePath.length - 1);
}
Expand All @@ -51682,6 +51684,10 @@ async function deleteFile(drive, parentId, targetFilePath) {
const fileName = targetPaths[targetPaths.length - 1];
const fileId = await getFileId(drive, parentId, fileName);
if (!fileId) {
if (ignoreMissing) {
core.warning(`File '${fileName}' does not exist in folder '${parentId}'`);
return null;
}
throw new Error(`File '${fileName}' does not exist in folder '${parentId}'`);
}
core.debug(`Deleting file '${fileName}' in folder '${parentId}'`);
Expand Down

0 comments on commit 00a0a18

Please sign in to comment.