Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Implement monkeypatch for return-dispatch action #26

Merged
merged 5 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
nix_path: nixpkgs=channel:nixos-22.05
- name: Generate test project
run: nix-shell -p cookiecutter --run "cookiecutter --config-file test-config.yml --no-input ."
- name: Apply monkeypatch
run: nix-shell -p python3 --run "python monkeypatch.py example/.github/workflows/main.yml example/.github/workflows/nix.yml example/.github/workflows/docs.yml"
- name: Setup poetry.lock cache
uses: actions/cache@v3
id: lock-cache
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This file documents changes to [fastapi-mvc/cookiecutter](https://github.com/fas

### Internal

* Remove hardcoded workflow_dispatch for return-dispatch action in GH workflow templates [#17](https://github.com/fastapi-mvc/cookiecutter/issues/17). PR [#26](https://github.com/fastapi-mvc/cookiecutter/pull/26)
* Drop mock dev-dependency [#12](https://github.com/fastapi-mvc/cookiecutter/issues/12). PR [#25](https://github.com/fastapi-mvc/cookiecutter/pull/25)
* Update dependencies. PR [#24](https://github.com/fastapi-mvc/cookiecutter/pull/24)
* fastapi (0.82.0 -> 0.85.0)
Expand Down
66 changes: 66 additions & 0 deletions monkeypatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""https://github.com/fastapi-mvc/cookiecutter/issues/17"""
import os
import re
import sys
import argparse


trigger_patch = """\
on:
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
# and await its result. NOTE! This is not included in the template.
workflow_dispatch:
inputs:
distinct_id:
required: true
description: "Input required by codex-/return-dispatch@v1"\
"""
echo_patch = """\
jobs:
meta:
runs-on: ubuntu-latest

steps:
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
# NOTE! This is not included in the template.
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
run: echo ${{ github.event.inputs.distinct_id }}\
"""


def apply_patch(file):
if not os.path.exists(file):
print(f"Path: '{file}' does not exist.")
sys.exit(1)
elif not os.path.isfile(file):
print(f"Path: '{file}' is not a file.")
sys.exit(1)

with open(file, "r+") as f:
content = f.read()
patched = content.replace("on:", trigger_patch, 1)
patched = patched.replace("jobs:", echo_patch, 1)
f.seek(0)
f.write(patched)
f.truncate()


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"files",
metavar="FILE",
type=str,
nargs="+",
help="File to apply monkeypatch",
)
args = parser.parse_args()

for file in args.files:
print(f"Patching: '{file}'")
apply_patch(file)


if __name__ == "__main__":
main()
16 changes: 0 additions & 16 deletions {{cookiecutter.folder_name}}/.github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ on:
push:
branches:
- master
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
# and await its result. By no means should this be included in the template this file was generated from.
# It's just a temporary monkey patch to fulfill the GH automation use case.
# Feel free to remove it.
workflow_dispatch:
inputs:
distinct_id:
required: true
description: "Input required by codex-/return-dispatch@v1"

env:
POETRY_HOME: /opt/poetry
Expand All @@ -27,12 +17,6 @@ jobs:
runs-on: ubuntu-latest

steps:
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
# By no means should this be included in the template this file was generated from.
# It's just a temporary monkey patch to fulfill the GH automation use case.
# Feel free to remove it.
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
run: echo ${{ github.event.inputs.distinct_id }}
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
Expand Down
16 changes: 0 additions & 16 deletions {{cookiecutter.folder_name}}/.github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
on:
push:
pull_request:
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
# and await its result. By no means should this be included in the template this file was generated from.
# It's just a temporary monkey patch to fulfill the GH automation use case.
# Feel free to remove it.
workflow_dispatch:
inputs:
distinct_id:
required: true
description: "Input required by codex-/return-dispatch@v1"

env:
POETRY_HOME: /opt/poetry
Expand All @@ -29,12 +19,6 @@ jobs:
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
# By no means should this be included in the template this file was generated from.
# It's just a temporary monkey patch to fulfill the GH automation use case.
# Feel free to remove it.
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
run: echo ${{ github.event.inputs.distinct_id }}
- id: skip_check
uses: fkirc/skip-duplicate-actions@v3.4.1
with:
Expand Down
16 changes: 0 additions & 16 deletions {{cookiecutter.folder_name}}/.github/workflows/nix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ on:
pull_request:
branches:
- master
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
# and await its result. By no means should this be included in the template this file was generated from.
# It's just a temporary monkey patch to fulfill the GH automation use case.
# Feel free to remove it.
workflow_dispatch:
inputs:
distinct_id:
required: true
description: "Input required by codex-/return-dispatch@v1"

jobs:
# This job checks if an identical workflow is being triggered by different
Expand All @@ -28,12 +18,6 @@ jobs:
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
# By no means should this be included in the template this file was generated from.
# It's just a temporary monkey patch to fulfill the GH automation use case.
# Feel free to remove it.
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
run: echo ${{ github.event.inputs.distinct_id }}
- id: skip_check
uses: fkirc/skip-duplicate-actions@v3.4.1
with:
Expand Down