Move Python code into a dedicated module

Resolves #2
This commit is contained in:
Sviatoslav Sydorenko
2021-12-13 03:12:43 +01:00
parent d4e67eb8d6
commit aee8fc20e3
2 changed files with 112 additions and 89 deletions
+12 -89
View File
@@ -40,94 +40,17 @@ runs:
steps:
- name: Decide whether the input jobs succeeded or failed
id: outcome
env:
PYTHONPATH: ${{ github.action_path }}/src
run: |
import json
import sys
allowed_failures_raw_input = """${{ inputs.allowed-failures }}"""
try:
allowed_failures_input = json.loads(allowed_failures_raw_input)
except json.decoder.JSONDecodeError:
allowed_failures_input = list(
map(str.strip, allowed_failures_raw_input.split(',')),
)
inputs = {
'allowed_failures': allowed_failures_input,
'jobs': json.loads("""${{ inputs.jobs }}"""),
}
jobs = inputs['jobs'] or {}
jobs_allowed_to_fail = inputs['allowed_failures'] or []
if not jobs:
sys.exit(
'❌ Invalid input jobs matrix, '
'please provide a non-empty `needs` context',
)
job_matrix_succeeded = all(
job['result'] == 'success' for name, job in jobs.items()
if name not in jobs_allowed_to_fail
)
print(
'::set-output name=failure::{failed}'.
format(failed=not job_matrix_succeeded),
file=sys.stderr,
)
print(
'::set-output name=result::{result}'.
format(result='success' if job_matrix_succeeded else 'failure'),
file=sys.stderr,
)
print(
'::set-output name=success::{succeeded}'.
format(succeeded=job_matrix_succeeded),
file=sys.stderr,
)
allowed_to_fail_jobs_succeeded = all(
job['result'] == 'success' for name, job in jobs.items()
if name in jobs_allowed_to_fail
)
if job_matrix_succeeded:
print(
'🎉 All of the required dependency jobs succeeded.',
file=sys.stderr,
)
else:
print(
'😢 Some of the required to succeed jobs failed.',
file=sys.stderr,
)
if jobs_allowed_to_fail and allowed_to_fail_jobs_succeeded:
print(
'🛈 All of the allowed to fail dependency jobs succeeded.',
file=sys.stderr,
)
elif jobs_allowed_to_fail:
print(
'🛈 Some of the allowed to fail jobs did not succeed.',
file=sys.stderr,
)
print('📝 Job statuses:', file=sys.stderr)
for name, job in jobs.items():
print(
'📝 {name} → {emoji} {result} [{status}]'.
format(
emoji='✓' if job['result'] == 'success' else '❌',
name=name,
result=job['result'],
status='allowed to fail' if name in jobs_allowed_to_fail
else 'required to succeed',
),
file=sys.stderr,
)
sys.exit(int(not job_matrix_succeeded))
shell: python
python -m normalize_needed_jobs_status \
"$(cat << EOM
${{ inputs.allowed-failures }}
EOM
)" \
"$(cat << EOM
${{ inputs.jobs }}
EOM
)"
shell: bash
...
+100
View File
@@ -0,0 +1,100 @@
#! /usr/bin/env python
import json
import sys
allowed_failures_raw_input = sys.argv[1]
try:
allowed_failures_input = json.loads(allowed_failures_raw_input)
except json.decoder.JSONDecodeError:
allowed_failures_input = list(
map(str.strip, allowed_failures_raw_input.split(',')),
)
inputs = {
'allowed_failures': allowed_failures_input,
'jobs': json.loads(sys.argv[2]),
}
jobs = inputs['jobs'] or {}
jobs_allowed_to_fail = inputs['allowed_failures'] or []
if not jobs:
sys.exit(
'❌ Invalid input jobs matrix, '
'please provide a non-empty `needs` context',
)
job_matrix_succeeded = all(
job['result'] == 'success' for name, job in jobs.items()
if name not in jobs_allowed_to_fail
)
print(
'::set-output name=failure::{failed}'.
format(failed=not job_matrix_succeeded),
file=sys.stderr,
)
print(
'::set-output name=result::{result}'.
format(result='success' if job_matrix_succeeded else 'failure'),
file=sys.stderr,
)
print(
'::set-output name=success::{succeeded}'.
format(succeeded=job_matrix_succeeded),
file=sys.stderr,
)
allowed_to_fail_jobs_succeeded = all(
job['result'] == 'success' for name, job in jobs.items()
if name in jobs_allowed_to_fail
)
if job_matrix_succeeded:
print(
'🎉 All of the required dependency jobs succeeded.',
file=sys.stderr,
)
else:
print(
'😢 Some of the required to succeed jobs failed.',
file=sys.stderr,
)
if jobs_allowed_to_fail and allowed_to_fail_jobs_succeeded:
print(
'🛈 All of the allowed to fail dependency jobs succeeded.',
file=sys.stderr,
)
elif jobs_allowed_to_fail:
print(
'🛈 Some of the allowed to fail jobs did not succeed.',
file=sys.stderr,
)
print('📝 Job statuses:', file=sys.stderr)
for name, job in jobs.items():
print(
'📝 {name} → {emoji} {result} [{status}]'.
format(
emoji='✓' if job['result'] == 'success' else '❌',
name=name,
result=job['result'],
status='allowed to fail' if name in jobs_allowed_to_fail
else 'required to succeed',
),
file=sys.stderr,
)
sys.exit(int(not job_matrix_succeeded))