Repository navigation
Expand file tree
/
Copy pathaction.yaml
More file actions
134 lines (123 loc) · 5.32 KB
/
Copy pathaction.yaml
File metadata and controls
134 lines (123 loc) · 5.32 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2024 The Linux Foundation
name: '💬 GIT Commit Message'
description: 'Retrieve GIT commit message and check for Change-Id and DCO'
inputs:
commit_number:
# Starting at one, which is the last/latest commit
description: 'Commit position/number (1=latest, 2=second-to-last, etc.)'
required: false
default: '1'
outputs:
commit_sha:
description: 'SHA value corresponding to GIT commit number'
value: ${{ steps.process-commit.outputs.commit_sha }}
commit_message:
description: 'GIT commit message body'
value: ${{ steps.process-commit.outputs.commit_message }}
# See: https://gerrit-review.googlesource.com/Documentation/user-changeid.html
change_id:
description: 'Whether the commit message contains a Gerrit Change-Id'
value: ${{ steps.process-commit.outputs.change_id }}
change_id_value:
description: 'The extracted Change-Id value/string'
value: ${{ steps.process-commit.outputs.change_id_value }}
# See: https://en.wikipedia.org/wiki/Developer_Certificate_of_Origin
dco_signed_off:
description: 'Whether the commit message contains a DCO statement'
value: ${{ steps.process-commit.outputs.dco_signed_off }}
dco_name:
description: 'Name provided in DCO statement, if present'
value: ${{ steps.process-commit.outputs.dco_name }}
dco_email:
description: 'E-mail address from DCO statement, if present '
value: ${{ steps.process-commit.outputs.dco_email }}
runs:
using: 'composite'
steps:
- name: 'Retrieve GIT commit'
id: process-commit
shell: bash
env:
INPUT_COMMIT_NUMBER: ${{ inputs.commit_number }}
run: |
# Process GIT commit
COMMIT_NUMBER="$INPUT_COMMIT_NUMBER"
CHECK_REGEX='^[0-9]+$'
if ! [[ "$COMMIT_NUMBER" =~ $CHECK_REGEX ]] ; then
echo 'Error: commit number/input must be numeric ❌'
exit 1
fi
COMMIT_SHA=$(git log --pretty=format:"%H" | awk "NR==$COMMIT_NUMBER")
COMMIT_MESSAGE=$(git log --format=%B -n 1 "$COMMIT_SHA")
CHANGE_ID_LINE=$(echo "$COMMIT_MESSAGE" | grep 'Change-Id:' || true)
if [ -z "$CHANGE_ID_LINE" ]; then
echo 'Gerrit Change-Id NOT found in commit message/body'
CHANGE_ID='false'
else
echo 'Gerrit Change-Id found in commit message/body'
CHANGE_ID='true'
CHANGE_ID_VALUE=$(echo "$CHANGE_ID_LINE" | awk '{ print $2 }')
fi
DCO_LINE=$(echo "$COMMIT_MESSAGE" | grep 'Signed-off-by:' || true)
if [ -z "$DCO_LINE" ]; then
echo 'DCO statement NOT found in commit message/body ⚠️'
DCO_SIGNED_OFF='false'
else
echo 'DCO statement found in commit message/body ✅'
DCO_SIGNED_OFF='true'
# Tested: https://regex101.com/r/LFOLgS/1
DCO_NAME=$(echo "$DCO_LINE" |\
grep -oP '^Signed-off-by: \K.*.+?(?= <)')
# Tested: https://regex101.com/r/LMK94y/1
DCO_EMAIL=$(echo "$DCO_LINE" |\
grep -oP '(?<=<).*(?=>)')
fi
if [ -z "$COMMIT_MESSAGE" ]; then
echo 'GIT commit message string was empty ⚠️'
echo 'Ensure checkout step was performed'
echo 'Ensure fetch depth covers request range'
fi
# Set outputs (GITHUB_OUTPUT is the documented interface; writing
# commit-derived values to GITHUB_ENV is avoided as it can allow
# injected variables to influence later steps).
#
# The commit message is attacker-controlled, so use a random
# per-run heredoc delimiter: a fixed delimiter could appear as a
# line in the message and prematurely terminate the value,
# allowing additional outputs to be injected.
#
# Draw the random suffix from the first available strong source
# so the action does not hard-depend on any single tool, then
# fail fast if none are available or the value is empty (a
# predictable delimiter could re-enable the injection).
RANDOM_HEX=""
if command -v openssl >/dev/null 2>&1; then
RANDOM_HEX=$(openssl rand -hex 16)
elif [ -r /dev/urandom ] && command -v od >/dev/null 2>&1; then
RANDOM_HEX=$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')
elif command -v uuidgen >/dev/null 2>&1; then
RANDOM_HEX=$(uuidgen | tr -d '-' | tr 'A-F' 'a-f')
fi
if [ -z "$RANDOM_HEX" ]; then
echo 'Error: no random source available for output delimiter ❌'
exit 1
fi
EOF_DELIM="COMMIT_EOF_${RANDOM_HEX}"
# Use printf (not echo) for output framing so values beginning
# with '-n'/'-e' are not misinterpreted as options, which could
# drop the trailing newline and corrupt the delimiter framing.
{
printf 'commit_sha=%s\n' "$COMMIT_SHA"
printf 'commit_message<<%s\n' "$EOF_DELIM"
printf '%s\n' "$COMMIT_MESSAGE"
printf '%s\n' "$EOF_DELIM"
printf 'change_id=%s\n' "$CHANGE_ID"
printf 'change_id_value=%s\n' "$CHANGE_ID_VALUE"
printf 'dco_signed_off=%s\n' "$DCO_SIGNED_OFF"
printf 'dco_name=%s\n' "$DCO_NAME"
printf 'dco_email=%s\n' "$DCO_EMAIL"
} >> "$GITHUB_OUTPUT"
echo 'GIT Commit Message 💬'
echo "$COMMIT_MESSAGE"