Skip to content

Commit

Permalink
A0-3280: Add replace-string action (#18)
Browse files Browse the repository at this point in the history
* Add replace-string action
  • Loading branch information
mikogs authored Oct 25, 2023
1 parent c893c59 commit 827c356
Show file tree
Hide file tree
Showing 6 changed files with 30,504 additions and 0 deletions.
87 changes: 87 additions & 0 deletions .github/workflows/test-replace-string.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
name: Test replace-string action

on:
pull_request:
branches:
- main
push:
branches:
- main

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true

jobs:
main:
name: Test replace-string action
runs-on: ubuntu-20.04
steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Replace string
id: str
uses: ./replace-string
with:
string: 'Apple Redberry'
replace-regex: 'Redberry'
flags: 'g'
replace-with: 'Apricot'

- name: Check replaced string
shell: bash
run: |
if [[ '${{ steps.str.outputs.replaced-string }}' != 'Apple Apricot' ]]; then
echo '!!! Action failed to make simple string replacement'
exit 1
fi
- name: Create a dummy file to read from
shell: bash
run: |
echo -n 'Apricot Blueberry' > tmp-read-from-file.txt
- name: Replace string from file
id: from-file
uses: ./replace-string
with:
read-from-file: 'tmp-read-from-file.txt'
replace-regex: 'Blue'
flags: 'g'
replace-with: 'Red'

- name: Check replaced string
shell: bash
run: |
if [[ '${{ steps.from-file.outputs.replaced-string }}' != 'Apricot Redberry' ]]; then
echo '!!! Action failed to replace string from a file'
exit 1
fi
- name: Create an output to read from
shell: bash
id: output
run: |
echo "read-me=Cranberry and green apple" >> $GITHUB_OUTPUT
- name: Replace string from output and write to file
uses: ./replace-string
with:
string: ${{ steps.output.outputs.read-me }}
replace-regex: 'green'
flags: 'g'
replace-with: 'red'
write-to-file: 'tmp-write-to-file.txt'

- name: Check replaced string
shell: bash
run: |
if [[ ! -f tmp-write-to-file.txt ]]; then
echo "!!! Action failed to create an output file"
fi
output_from_file=$(cat tmp-write-to-file.txt);
if [[ "${output_from_file}" != 'Cranberry and red apple' ]]; then
echo "!!! Action failed to create an output file with replaced string"
fi
34 changes: 34 additions & 0 deletions replace-string/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
name: Replace string
description: |
This action replaces a specified string using regular expression.
It can be passed as an input or read from a file.
Also, the output string can be additional written to file.
inputs:
string:
description: String to be replaced
required: false
read-from-file:
description: Read from a file instead of taking 'string' input
required: false
read-encoding:
description: When reading from a file, encoding can be set
required: false
replace-regex:
description: Regular expression for replace function
required: true
replace-with:
description: String to be replaced with
required: true
write-to-file:
description: Target file where replaced string should be saved. When empty, file is not created.
required: false
flags:
description: Replace flags
required: false
outputs:
replaced-string:
description: Output string
runs:
using: 'node20'
main: 'dist/index.js'
Loading

0 comments on commit 827c356

Please sign in to comment.