-
Notifications
You must be signed in to change notification settings - Fork 0
115 lines (106 loc) · 3.98 KB
/
python_imports.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
##########################
## Pydeps ##
## Reusable Workflow ##
##########################
# 0. Never run
# 1. Always run
# 2. Draw Imports on conditions
# - Triggered on a Long-living branch (ie main)
# - Triggered on a v* tag (ie v1.0.0)
# - Source (business logic) Code (no tests) changed
# 3. Draw if Source (business logic) Code (no tests) changed
on:
workflow_call:
inputs:
# Defaults to Always Run
run_policy:
required: false
type: string
default: '2'
branches:
required: false
type: string
default: 'master, main, dev'
source_code_targets:
required: false
type: string
default: 'src'
python_version:
required: false
type: string
default: '3.10'
artifacts_dir:
required: false
type: string
default: 'dependency-graphs'
jobs:
check_trigger_draw_dependency_graphs:
name: Run Draw Job ?
runs-on: ubuntu-latest
if: always() && inputs.run_policy != 0
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- if: ${{ !contains('1, 2, 3', inputs.run_policy) }}
run: 'echo "Invalid run_policy: ${{ inputs.run_policy }}. Must be >0 and <4" && exit 1'
- if: inputs.run_policy == 1
name: 'POLICY: 1 -> Trigger'
run: echo "SHOULD_DRAW_GRAPHS=true" >> $GITHUB_ENV
- if: inputs.run_policy == 2 && contains(inputs.branches, github.ref_name)
name: 'POLICY: 2 & Branch: ${{ github.ref_name }} -> Trigger'
run: echo "SHOULD_DRAW_GRAPHS=true" >> $GITHUB_ENV
- if: inputs.run_policy == 2 && startsWith(github.ref, format('refs/tags/{0}', inputs.tags))
name: 'POLICY: 2 & Tag: ${{ github.ref_name }} -> Trigger'
run: echo "SHOULD_DRAW_GRAPHS=true" >> $GITHUB_ENV
- if: ${{ !env.SHOULD_DRAW_GRAPHS && contains('2, 3', inputs.run_policy) }}
name: 'POLICY: 2, 3 -> Check if Source Code Changed'
run: |
echo "=============== list modified files ==============="
git diff --name-only HEAD^ HEAD
echo "========== check paths of modified files =========="
git diff --name-only HEAD^ HEAD > files.txt
# if the diff contains any of the source code targets, then should draw graphs
IFS=',' read -ra TARGETS <<< "${{ inputs.source_code_targets }}"
while read file; do
for target in "${TARGETS[@]}"; do
if [[ "${file}" == *"${target}"* ]]; then
echo "SHOULD_DRAW_GRAPHS=true" >> $GITHUB_ENV
break
fi
done
done < files.txt
### OUTPUT of STEP ###
- name: "Set 'Draw Graphs' to ${{ env.SHOULD_DRAW_GRAPHS }}"
id: set_draw_signal
run: |
echo "DRAW_GRAPHS=${{ env.SHOULD_DRAW_GRAPHS }}" >> $GITHUB_OUTPUT
outputs:
DRAW_GRAPHS: ${{ steps.set_draw_signal.outputs.DRAW_GRAPHS }}
draw-dependencies:
runs-on: ubuntu-latest
needs: check_trigger_draw_dependency_graphs
if: needs.check_trigger_draw_dependency_graphs.outputs.DRAW_GRAPHS == 'true'
name: Draw Python Dependencies as Graphs, in .svg
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ inputs.python_version }}
uses: actions/setup-python@v4
with:
python-version: '${{ inputs.python_version }}'
- name: Install tox
run: |
python -m pip install --upgrade pip
python -m pip install tox==3.28
- name: Install dependencies (ie dot binary of graphviz)
run: |
sudo apt-get update -y --allow-releaseinfo-change
sudo apt-get install -y graphviz
- name: Draw Dependency Graphs as .svg files
run: tox -e pydeps -vv -s false
- name: Upload Dependency Graphs as artifacts
uses: actions/upload-artifact@v3
with:
name: ${{ inputs.artifacts_dir }}
path: pydeps/
if-no-files-found: warn # 'error' or 'ignore' are also available, defaults to `warn`