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

Commit 159dfe0

Browse files
authored
Merge pull request #26 from fastapi-mvc/workflow_dispatch_monkeypatch
Implement monkeypatch for return-dispatch action
2 parents c25c552 + 49e009d commit 159dfe0

File tree

6 files changed

+69
-48
lines changed

6 files changed

+69
-48
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ jobs:
4242
nix_path: nixpkgs=channel:nixos-22.05
4343
- name: Generate test project
4444
run: nix-shell -p cookiecutter --run "cookiecutter --config-file test-config.yml --no-input ."
45+
- name: Apply monkeypatch
46+
run: nix-shell -p python3 --run "python monkeypatch.py example/.github/workflows/main.yml example/.github/workflows/nix.yml example/.github/workflows/docs.yml"
4547
- name: Setup poetry.lock cache
4648
uses: actions/cache@v3
4749
id: lock-cache

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This file documents changes to [fastapi-mvc/cookiecutter](https://github.com/fas
1414

1515
### Internal
1616

17+
* 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)
1718
* Drop mock dev-dependency [#12](https://github.com/fastapi-mvc/cookiecutter/issues/12). PR [#25](https://github.com/fastapi-mvc/cookiecutter/pull/25)
1819
* Update dependencies. PR [#24](https://github.com/fastapi-mvc/cookiecutter/pull/24)
1920
* fastapi (0.82.0 -> 0.85.0)

monkeypatch.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""https://github.com/fastapi-mvc/cookiecutter/issues/17"""
2+
import os
3+
import re
4+
import sys
5+
import argparse
6+
7+
8+
trigger_patch = """\
9+
on:
10+
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
11+
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
12+
# and await its result. NOTE! This is not included in the template.
13+
workflow_dispatch:
14+
inputs:
15+
distinct_id:
16+
required: true
17+
description: "Input required by codex-/return-dispatch@v1"\
18+
"""
19+
echo_patch = """\
20+
jobs:
21+
meta:
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
26+
# NOTE! This is not included in the template.
27+
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
28+
run: echo ${{ github.event.inputs.distinct_id }}\
29+
"""
30+
31+
32+
def apply_patch(file):
33+
if not os.path.exists(file):
34+
print(f"Path: '{file}' does not exist.")
35+
sys.exit(1)
36+
elif not os.path.isfile(file):
37+
print(f"Path: '{file}' is not a file.")
38+
sys.exit(1)
39+
40+
with open(file, "r+") as f:
41+
content = f.read()
42+
patched = content.replace("on:", trigger_patch, 1)
43+
patched = patched.replace("jobs:", echo_patch, 1)
44+
f.seek(0)
45+
f.write(patched)
46+
f.truncate()
47+
48+
49+
def main():
50+
parser = argparse.ArgumentParser()
51+
parser.add_argument(
52+
"files",
53+
metavar="FILE",
54+
type=str,
55+
nargs="+",
56+
help="File to apply monkeypatch",
57+
)
58+
args = parser.parse_args()
59+
60+
for file in args.files:
61+
print(f"Patching: '{file}'")
62+
apply_patch(file)
63+
64+
65+
if __name__ == "__main__":
66+
main()

{{cookiecutter.folder_name}}/.github/workflows/docs.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ on:
55
push:
66
branches:
77
- master
8-
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
9-
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
10-
# and await its result. By no means should this be included in the template this file was generated from.
11-
# It's just a temporary monkey patch to fulfill the GH automation use case.
12-
# Feel free to remove it.
13-
workflow_dispatch:
14-
inputs:
15-
distinct_id:
16-
required: true
17-
description: "Input required by codex-/return-dispatch@v1"
188

199
env:
2010
POETRY_HOME: /opt/poetry
@@ -27,12 +17,6 @@ jobs:
2717
runs-on: ubuntu-latest
2818

2919
steps:
30-
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
31-
# By no means should this be included in the template this file was generated from.
32-
# It's just a temporary monkey patch to fulfill the GH automation use case.
33-
# Feel free to remove it.
34-
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
35-
run: echo ${{ github.event.inputs.distinct_id }}
3620
- uses: actions/checkout@v3
3721
- name: Set up Python
3822
uses: actions/setup-python@v4

{{cookiecutter.folder_name}}/.github/workflows/main.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
on:
44
push:
55
pull_request:
6-
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
7-
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
8-
# and await its result. By no means should this be included in the template this file was generated from.
9-
# It's just a temporary monkey patch to fulfill the GH automation use case.
10-
# Feel free to remove it.
11-
workflow_dispatch:
12-
inputs:
13-
distinct_id:
14-
required: true
15-
description: "Input required by codex-/return-dispatch@v1"
166

177
env:
188
POETRY_HOME: /opt/poetry
@@ -29,12 +19,6 @@ jobs:
2919
outputs:
3020
should_skip: ${{ steps.skip_check.outputs.should_skip }}
3121
steps:
32-
# This echo is required by codex-/return-dispatch@v1 in order to identify dispatched workflow.
33-
# By no means should this be included in the template this file was generated from.
34-
# It's just a temporary monkey patch to fulfill the GH automation use case.
35-
# Feel free to remove it.
36-
- name: echo distinct ID ${{ github.event.inputs.distinct_id }}
37-
run: echo ${{ github.event.inputs.distinct_id }}
3822
- id: skip_check
3923
uses: fkirc/skip-duplicate-actions@v3.4.1
4024
with:

{{cookiecutter.folder_name}}/.github/workflows/nix.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ on:
88
pull_request:
99
branches:
1010
- master
11-
# This trigger is required by fastapi-mvc automation to dispatch this concrete workflow
12-
# from fastapi-mvc 'CI workflow' (https://github.com/fastapi-mvc/cookiecutter/actions/workflows/main.yml),
13-
# and await its result. By no means should this be included in the template this file was generated from.
14-
# It's just a temporary monkey patch to fulfill the GH automation use case.
15-
# Feel free to remove it.
16-
workflow_dispatch:
17-
inputs:
18-
distinct_id:
19-
required: true
20-
description: "Input required by codex-/return-dispatch@v1"
2111

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

0 commit comments

Comments
 (0)