Skip to content
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

feat: allow update function to trigger file deletion #130

Merged
merged 1 commit into from Jun 4, 2023

Conversation

stefanbuck
Copy link
Contributor

@stefanbuck stefanbuck commented May 31, 2023

Fixes #129

I raised this PR to discuss some challenges I phased with TypeScript see comments.

@@ -54,7 +54,7 @@ export type UpdateFunctionFile =

export type UpdateFunction = (
file: UpdateFunctionFile
) => string | File | null | Promise<string | File | null>;
) => string | File | null | Symbol | Promise<string | File | null | Symbol>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the update function returns null it means no file changes.
If the update function return a symbol it triggers a file deletion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to type the Symbol to ensure it is the actual deleteFile symbol?

Copy link
Owner

Choose a reason for hiding this comment

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

I don't know, I actually never worked with types for Symbols

src/index.ts Outdated Show resolved Hide resolved
@@ -20,6 +21,8 @@ export async function createTree(
for (const path of Object.keys(changes.files)) {
const value = changes.files[path];

// TODO use Symbol here which will result in a breaking change
// if (value === deleteFile) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

or we keep it as is at the drawback of having inconsistency between return values from an update function vs this code snipped

files: {
  "file/to/delete.txt": null
}

Copy link
Owner

Choose a reason for hiding this comment

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

I'd rather go with a breaking change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Documented in the readme and commit message

});
continue;
} catch (error) {
// istanbul ignore next
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought this is fine since I saw basically the same for other catch blocks, but please let me know if you would prefer a test case for this.

@stefanbuck stefanbuck marked this pull request as ready for review June 3, 2023 11:13
@stefanbuck
Copy link
Contributor Author

@gr2m this PR is ready for review.

  • I addressed your comments regarding TS
  • Added a unit test
  • Updated the readme
  • Added a breaking change note in the commit footer

Copy link
Owner

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

Besides the note about the deleteFile export name, I really love your pull request, thank you so much!! I'm okay with merging it as is, but wanted to at least try to come up with a better name than deleteFile. Let me know what you think

README.md Outdated
Comment on lines 41 to 44
const {
createPullRequest,
deleteFile,
} = require("octokit-plugin-create-pull-request");
Copy link
Owner

Choose a reason for hiding this comment

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

Seeing these two imports, I really don't like deleteFile. At first sight it suggests that it's a separate method to delete a file, don't you think?

Node.js has a convention to uses a k prefix for constants, see example: https://github.com/nodejs/node/blob/4bb06dbd0a87e5b2430780ddceec08015f8c496a/lib/_http_client.js#LL107C7-L107C13

Maybe we could use kDeleteFile? Or how about DELETE_FILE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good shout. I changed it to DELETE_FILE which is well understood in our industry

BREAKING CHANGE: The meaning of `null` has changed!

Previously the following snippet was causing a file deletion.

files: {
  "file/to/delete.txt": null
}

If you want to remain this behavior you must use the deleteFile Symbol

import { createPullRequest, deleteFile } from 'octokit-plugin-create-pull-request';

files: {
  "file/to/delete.txt": deleteFile
}

If you want to trigger a file deletion from an update function, you can now do so by returning  the deleteFile Symbol.

import { createPullRequest, deleteFile } from 'octokit-plugin-create-pull-request';

files: {
  "file/to/delete.txt": ({ exists, encoding, content }) => {
    const fileContent = Buffer.from(content, encoding).toString("utf-8")

    if (fileContent.includes('abc')) {
      // trigger file deletion
      return deleteFile
    }

    // do not alter file content
    return null;
  }
}
Copy link
Owner

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

Thank you!

@gr2m gr2m merged commit ac571dc into gr2m:main Jun 4, 2023
5 checks passed
@github-actions
Copy link

github-actions bot commented Jun 4, 2023

🎉 This PR is included in version 5.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@stefanbuck stefanbuck deleted the delete-file branch June 4, 2023 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow programmatic file deletion
2 participants