Created
November 2, 2022 15:38
-
-
Save Tranquility2/5963b93c527b135762be591b32fbe6f4 to your computer and use it in GitHub Desktop.
Fix Github set-output command (Pre deprecation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import click | |
import fileinput | |
# Used to address deprecating set-outputcommand | |
# https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ | |
# Search project for relevant files | |
# egrep -r --include="*.yml" "set-output" | awk '{print $1}' | awk '!seen[$0]++' | awk -F':' '{print $1}'\ | sort > temp/github_files.txt | |
'''Samples: | |
echo "::set-output name=test::false" | |
echo "::set-output name=base_test::$(echo $base_hash)" | |
echo "::set-output name=predictions_table::$predictions_table" | |
run: echo "::set-output name=var::true" | |
echo "::set-output name=test_table::$test_table" | |
echo "::warning ::test not found in the given param." | |
echo "::set-output name=new_test_name::$NEW_TEST_NAME" | |
echo "::set-output name=test_table::$(echo $test_table)" | |
echo "::set-output name=tests_table::$(echo $tests_table)" | |
''' | |
patterns = {"base": r"echo \"::set-output name=(\w+)::(\w+)\"", | |
"param": r"echo \"::set-output name=(\w+)::(\$\w+)\"", | |
"adv": r"echo \"::set-output name=(\w+)::(\$\(echo \$?\w+\))\""} | |
compiled_patterns = {name: re.compile(pattern) for (name, pattern) in patterns.items()} | |
replace_pattern = r'echo "\1=\2" >> $GITHUB_OUTPUT' # 'echo "{name}={value}" >> $GITHUB_OUTPUT' | |
def process_file(source_file: str): | |
for original_line in fileinput.input(source_file, inplace=True): | |
results = {name: re.search(compiled_pattern, original_line) for (name, compiled_pattern) | |
in compiled_patterns.items()} | |
new_line = original_line | |
for key, value in results.items(): | |
if value: | |
new_line = re.sub(compiled_patterns.get(key), replace_pattern, original_line) | |
print(new_line, end='') | |
def dry_run(source_file: str): | |
def print_lines(kind: str): | |
print(f"{'original_line:':<15}{original_line:>10}") | |
print(f"{'new_line:[' + kind + ']':<15}{new_line:>10}") | |
with open(source_file) as file: | |
lines = [line.rstrip() for line in file] | |
for original_line in lines: | |
results = {name: re.search(compiled_pattern, original_line) for (name, compiled_pattern) | |
in compiled_patterns.items()} | |
for key, value in results.items(): | |
if value: | |
new_line = re.sub(compiled_patterns.get(key), replace_pattern, original_line) | |
print_lines(key) | |
@click.command("Process github yml files for set-output handling") | |
@click.option("--filename", "-f", "filename", required=True, help="file containing relevant file paths", | |
default='temp/github_files.txt') | |
@click.option("--dryrun", "-d", "dryrun", required=False, default=False, is_flag=True, | |
help='use this flag to see the file processing without any changes') | |
def main(filename: str, dryrun: bool): | |
with open(filename) as f: | |
lines = f.readlines() | |
for file_path in lines: | |
if dryrun: | |
dry_run(source_file=file_path.rstrip()) | |
else: | |
process_file(source_file=file_path.rstrip()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment