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

not able to replace char with whitespace #6

Closed
einSelbst opened this issue Jan 9, 2021 · 7 comments
Closed

not able to replace char with whitespace #6

einSelbst opened this issue Jan 9, 2021 · 7 comments

Comments

@einSelbst
Copy link

Hey!
Thanks for the action!
I would like to replace dashes with whitespace but it seems the whitespace is ignored if given as a 'replace-with'.
I also tried passing in the unicode char replace-with: "\u0020" but then end up with that actual string as a replacement.

Do you think it's possible to use your action to replace something with a whitespace?
I want to generate PR titles from branch names btw.

Thanks!

@frabert
Copy link
Owner

frabert commented Jan 9, 2021

From some preliminary testing it looks like the whitespace is being stripped from the input before it gets to the action: https://github.com/frabert/replace-string-action/runs/1673088621

This makes it look like the Actions engine is doing something like input.trim() before forwarding the data. I'll have to check with someone else for this one...

@frabert
Copy link
Owner

frabert commented Jan 13, 2021

I can't seem to get a reply for now, I'll leave the link here if someone wants to chime in: https://github.community/t/custom-action-whitespace-in-input-parameters-is-getting-trimmed/154470

I'm not closing this issue because it's a genuine bug, but I'm not able to propose a fix right now.

@Simran-B
Copy link

Simran-B commented Jun 15, 2021

I can't find a way to preserve whitespace around action inputs, but I guess that you could add another input to let the action know that the string is JSON-encoded, do replace-with: '" "' in a workflow, and parse the input with JSON.parse() to just a space. That should preserve it. Roughly like this:

inputs:
...
  json:
    description: Set to any value to indicate that the replace-with string is JSON-encoded
    required: true
    default: ''
let replaceWith = core.getInput('replace-with');
const isJsonString = core.getInput('json');
if (isJsonString) {
  replaceWith = JSON.parse(replaceWith );
}
uses: frabert/replace-string-action@master
with:
  pattern: '_'
  flags: 'g'
  string: '_replace_underscores_'
  replace-with: '" "'
  json: true

frabert added a commit that referenced this issue Jun 16, 2021
This is useful in case the string to use as replacement contains leading
or trailing whitespace. This should function as a workaround for #6.

Thanks to @Simran-B for the suggestion!
@frabert
Copy link
Owner

frabert commented Jun 16, 2021

Thanks for the suggestion @Simran-B ! I've pushed a version which includes the json flag, let me know if it works ok.

@Simran-B
Copy link

Turns out there is actually a better way to turn trimming of whitespace off: @actions/core has an option to control it since v1.3.0: actions/toolkit#802

Update dependency: npm install @actions/core

Usage example:

core.getInput('replace-with', { trimWhitespace: false })

@frabert
Copy link
Owner

frabert commented Jun 27, 2021

I've pushed a new version which does exactly that, thank you!

Since this is a possibly breaking change, I've switched to version 2.0 instead of 1.4

@frabert
Copy link
Owner

frabert commented Dec 8, 2022

Closing this as solved

@frabert frabert closed this as completed Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants