Skip to content

Commit

Permalink
Merge pull request #525 from tj-actions/feat/add-support-for-conditio…
Browse files Browse the repository at this point in the history
…nally-escaping-default-values

feat: add support for conditionally escaping default values
  • Loading branch information
repo-ranger[bot] committed Nov 16, 2023
2 parents f4f096c + f35ce36 commit b326093
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 181 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ and/or `Description` (only supported by actions)
bin_path: ''

# Max width of a column
# Default: `"1000"`
# Default: "1000"
col_max_width: ''

# Max number of words per line in a column
# Default: `"5"`
# Default: "5"
col_max_words: ''

# Path to the yaml file
# Default: `"action.yml"`
# Default: "action.yml"
filename: ''

# List of action.yml **input** columns names to display, default
Expand All @@ -89,19 +89,19 @@ and/or `Description` (only supported by actions)

# Boolean indicating whether to output input, output and secret
# names as markdown links
# Default: `"true"`
# Default: "true"
markdown_links: ''

# Path to the output file
# Default: `"README.md"`
# Default: "README.md"
output: ''

# List of action.yml **output** column names to display, default
# (display all columns)
output_columns: ''

# Repository name with owner. For example, tj-actions/auto-doc
# Default: `"${{ github.repository }}"`
# Default: "${{ github.repository }}"
repository: ''

# Boolean Indicating whether the file is a reusable workflow
Expand All @@ -121,16 +121,16 @@ and/or `Description` (only supported by actions)

# GitHub token or Personal Access Token used to fetch
# the repository latest tag.
# Default: `"${{ github.token }}"`
# Default: "${{ github.token }}"
token: ''

# Enable code block documentation
# Default: `"false"`
# Default: "false"
use_code_blocks: ''

# Use the major version of the repository tag e.g
# v1.0.0 -> v1
# Default: `"false"`
# Default: "false"
use_major_version: ''

# The version number to run
Expand Down
2 changes: 1 addition & 1 deletion internal/types/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func renderActionInputTableOutput(inputs map[string]ActionInput, inputColumns []
case "Required":
row = append(row, strconv.FormatBool(inputs[key].Required))
case "Default":
row = append(row, utils.FormatValue(inputs[key].Default))
row = append(row, utils.FormatValue(inputs[key].Default, true, "<br>"))
case "Description":
if inputs[key].DeprecationMessage != "" {
row = append(row, utils.WordWrap(fmt.Sprintf("**Deprecated:** %s", inputs[key].Description), maxWords, "<br>"))
Expand Down
2 changes: 1 addition & 1 deletion internal/types/code_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func renderCodeBlockActionInputs(inputs map[string]ActionInput, repository, tag
for _, key := range keys {
codeBlock.WriteString(fmt.Sprintf(" # %s\n", utils.WordWrap(inputs[key].Description, 9, "\n # ")))
if inputs[key].Default != "" {
codeBlock.WriteString(fmt.Sprintf(" # Default: %s\n", utils.FormatValue(inputs[key].Default)))
codeBlock.WriteString(fmt.Sprintf(" # Default: %s\n", utils.FormatValue(inputs[key].Default, false, "\n # ")))
}
if inputs[key].DeprecationMessage != "" {
codeBlock.WriteString(fmt.Sprintf(" # Deprecated: %s\n", inputs[key].DeprecationMessage))
Expand Down
4 changes: 2 additions & 2 deletions internal/types/reusable.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func renderReusableInputTableOutput(inputs map[string]ReusableInput, inputColumn
case "Default":
switch inputs[key].Type {
case "string":
row = append(row, utils.FormatValue(inputs[key].Default))
row = append(row, utils.FormatValue(inputs[key].Default, true, "<br>"))
default:
row = append(row, "`"+inputs[key].Default+"`")
}
Expand Down Expand Up @@ -336,7 +336,7 @@ func renderReusableOutputTableOutput(outputs map[string]ReusableOutput, reusable
row = append(row, key)
}
case "Value":
row = append(row, utils.FormatValue(outputs[key].Value))
row = append(row, utils.FormatValue(outputs[key].Value, true, "<br>"))
case "Description":
row = append(row, utils.WordWrap(outputs[key].Description, maxWords, "<br>"))
default:
Expand Down
20 changes: 16 additions & 4 deletions internal/utils/format_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

// FormatValue formats a string that would be output as markdown
func FormatValue(v string) string {
func FormatValue(v string, escape bool, lineSeparator string) string {
if len(v) == 0 {
return ""
}
Expand All @@ -36,16 +36,28 @@ func FormatValue(v string) string {
if len(parts) > 1 && inputDefault != internal.NewLineSeparator {
for _, part := range parts {
if part != "" {
defaultValue += "`\"" + part + "\"`" + "<br>"
if escape {
defaultValue += "`\"" + part + "\"`" + lineSeparator
} else {
defaultValue += part + lineSeparator
}
}
}
} else {
if strings.Contains(inputDefault, internal.PipeSeparator) {
inputDefault = strings.Replace(inputDefault, internal.PipeSeparator, "\"\\"+internal.PipeSeparator+"\"", -1)
if escape {
inputDefault = strings.Replace(inputDefault, internal.PipeSeparator, "\"\\"+internal.PipeSeparator+"\"", -1)
} else {
inputDefault = strings.Replace(inputDefault, internal.PipeSeparator, "\""+internal.PipeSeparator+"\"", -1)
}
} else {
inputDefault = fmt.Sprintf("%#v", inputDefault)
}
defaultValue = "`" + inputDefault + "`"
if escape {
defaultValue = "`" + inputDefault + "`"
} else {
defaultValue = inputDefault
}
}

return defaultValue
Expand Down
42 changes: 23 additions & 19 deletions test/README-codeBlocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Output unique changed directories instead of filenames. **NOTE:** This
# returns `.` for changed files located in the root
# of the project.
# Default: `"false"`
# Default: "false"
dir_names: ''

# Maximum depth of directories to output. e.g `test/test1/test2` with
Expand All @@ -26,12 +26,16 @@

# Depth of additional branch history fetched. **NOTE**: This can
# be adjusted to resolve errors with insufficient history.
# Default: `"50"`
# Default: "50"
fetch_depth: ''

# File and directory patterns to detect changes using only
# these list of file(s) (Defaults to the entire repo) **NOTE:** Multiline file/directory patterns
# should not include quotes.
# Default: a.txt
# b.txt
# test.txt
#
files: ''

# Source file(s) used to populate the `files` input.
Expand All @@ -45,68 +49,68 @@
files_ignore_from_source_file: ''

# Separator used to split the `files_ignore` input
# Default: `"\n"`
# Default: "\n"
files_ignore_separator: ''

# Separator used to split the `files` input
# Default: `"\n"`
# Default: "\n"
files_separator: ''

# Include `all_old_new_renamed_files` output. Note this can generate a large
# output See: [#501](https://github.com/tj-actions/changed-files/issues/501).
# Default: `"false"`
# Default: "false"
include_all_old_new_renamed_files: ''

# Output list of changed files in a JSON formatted
# string which can be used for matrix jobs.
# Default: `"false"`
# Default: "false"
json: ''

# Output list of changed files in a raw format
# which means that the output will not be surrounded
# by quotes and special characters will not be escaped.
# Default: `"false"`
# Default: "false"
# Deprecated: Use `json_unescaped` instead.
json_raw_format: ''

# Output list of changed files in a JSON formatted
# string without escaping special characters.
# Default: `"false"`
# Default: "false"
json_unescaped: ''

# Boolean indicating whether to output input, output and secret
# names as markdown links
# Default: `"false"`
# Default: "false"
markdown_links: ''

# Indicates whether to include match directories
# Default: `"true"`
# Default: "true"
match_directories: ''

# Split character for old and new renamed filename pairs.
# Default: `" "`
# Default: " "
old_new_files_separator: ''

# Split character for old and new filename pairs.
# Default: `","`
# Default: ","
old_new_separator: ''

# Directory to store output files.
# Default: `".github/outputs"`
# Default: ".github/outputs"
output_dir: ''

# Specify a relative path under `$GITHUB_WORKSPACE` to locate the
# repository.
# Default: `"."`
# Default: "."
path: ''

# Use non ascii characters to match files and output
# the filenames completely verbatim by setting this to `false`
# Default: `"true"`
# Default: "true"
quotepath: ''

# Split character for output strings.
# Default: `" "`
# Default: "|"
separator: ''

# Specify a different commit SHA used for comparing changes
Expand All @@ -121,11 +125,11 @@
# on the target branch for pull request events and
# the previous remote commit of the current branch for
# push events.
# Default: `"false"`
# Default: "false"
since_last_remote_commit: ''

# The GitHub token to use for authentication.
# Default: `"${{ github.token }}"`
# Default: "${{ github.token }}"
token: ''

# Get changed files for commits whose timestamp is earlier
Expand All @@ -134,7 +138,7 @@

# Write outputs to files in the `.github/outputs` folder by
# default.
# Default: `"false"`
# Default: "false"
write_output_files: ''

```
Expand Down

0 comments on commit b326093

Please sign in to comment.