From ebbdd6412b14120b2828bcdde709447f4d6583a3 Mon Sep 17 00:00:00 2001 From: Martin Lablans Date: Fri, 15 Jul 2022 21:45:58 +0200 Subject: [PATCH] Allow to replace patterns in readme file --- README.md | 14 ++++++++++++++ action.yml | 18 ++++++++++++++++++ docker_readme.py | 9 +++++++++ 3 files changed, 41 insertions(+) diff --git a/README.md b/README.md index ba4200a..10942ed 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,20 @@ Sync to Dockerhub ``` +Optionally, you can replace strings in your readme file. For example, to avoid broken images, you can exchange relative image URLs like `![MyImage](./doc/image.svg)` with absolute URLs like `![MyImage](https://github.com/myuser/myrepo/raw/main/doc/image.svg))`: + +```yaml +- name: Sync + uses: ms-jpq/sync-dockerhub-readme@v1 + with: + username: + password: + repository: + readme: "./README.md" + replace_pattern: "](./" + replace_with: "](${{ github.server_url }}/${{ github.repository }}/raw/${{ github.ref_name }}/" +``` + ## Docker Image [![Docker Pulls](https://img.shields.io/docker/pulls/msjpq/sync-dockerhub-readme.svg)](https://hub.docker.com/r/msjpq/sync-dockerhub-readme/) diff --git a/action.yml b/action.yml index b9b713a..e3b69c7 100644 --- a/action.yml +++ b/action.yml @@ -26,6 +26,20 @@ inputs: list of repositories to update (without username) + replace_pattern: + required: False + description: |- + if set, all occurences of "replace_pattern" + will be replaced with "replace_with" + default: '' + + replace_with: + required: False + description: |- + if set, all occurences of "replace_pattern" + will be replaced with "replace_with" + default: '' + runs: using: docker image: "Dockerfile" @@ -37,6 +51,10 @@ runs: - "${{ inputs.readme }}" - "--repo" - "${{ inputs.repository }}" + - "--replace-pattern" + - "${{ inputs.replace_pattern }}" + - "--replace-with" + - "${{ inputs.replace_with }}" branding: icon: anchor diff --git a/docker_readme.py b/docker_readme.py index 665fa35..ced4c5e 100755 --- a/docker_readme.py +++ b/docker_readme.py @@ -30,6 +30,8 @@ def parse_args() -> Namespace: parser.add_argument("--readme", required=True) parser.add_argument("--repo", required=True) + parser.add_argument("--replace-pattern", type=str, required=False) + parser.add_argument("--replace-with", type=str, required=False) args = parser.parse_args() return args @@ -66,9 +68,16 @@ def set_repo(token: str, repo: str, readme: str) -> str: return loads(msg)["full_description"] +def replace_patterns(readme: str, replace_pattern: str, replace_with: str) -> str: + return readme.replace(replace_pattern, replace_with) + + def main() -> None: args = parse_args() + print(args) readme = slurp(args.readme) + if args.replace_pattern and args.replace_with: + readme = replace_patterns(readme, args.replace_pattern, args.replace_with) token = login(args.username, args.password) desc = set_repo(token=token, repo=args.repo, readme=readme,) big_print(desc)