Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ jobs:
- name: Run tests
shell: bash
run: ./check_email_pr.sh --test
- name: Test event handling
shell: bash
run: bash test/check_event_types.sh
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Qualcomm PR email addresses checker
For each commit in a PR, validates that the commit's author and committer email
addresses are appropriate for the repo.

**NOTE:** This action should be used with `pull_request` events.
**NOTE:** This action should be used with `pull_request` or
`pull_request_target` events. It exits successfully without checking anything
for other event types during normal execution. The `--test` option intentionally
uses fixture data regardless of the event type.

## Example Usage

Expand Down
13 changes: 13 additions & 0 deletions check_email_pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ EOF
exit 10
}

is_pr_event() {
case "${GITHUB_EVENT_NAME:-}" in
pull_request|pull_request_target) return 0 ;;
*) return 1 ;;
esac
}

TEST_MODE=()
while [ $# -gt 0 ] ; do
case "$1" in
--test) shift ; TEST_MODE=("--verbose") ;;
Expand All @@ -55,6 +63,11 @@ while [ $# -gt 0 ] ; do
shift
done

if [ "${#TEST_MODE[@]}" -eq 0 ] && ! is_pr_event ; then
debug "Skipping email check for non-PR event: ${GITHUB_EVENT_NAME:-unknown}"
exit 0
fi

RESULT=0
while read -r pr_commit ; do
debug "Running check on: $pr_commit"
Expand Down
56 changes: 56 additions & 0 deletions test/check_event_types.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
set -euo pipefail

ROOT_DIR=$(cd -- "$(dirname -- "$0")/.." && pwd)

run_non_pr_event_test() {
local event output status
event=$1

if [ "$event" = "unset" ] ; then
if output=$(env -u GITHUB_EVENT_NAME -u PULL_NUMBER -u COMMITS_COUNT \
GITHUB_API_URL= \
PATH="$ROOT_DIR/test/fake_bin:$PATH" \
"$ROOT_DIR/check_email_pr.sh" 2>&1) ; then
status=0
else
status=$?
fi
elif output=$(env -u PULL_NUMBER -u COMMITS_COUNT \
GITHUB_API_URL= \
GITHUB_EVENT_NAME="$event" \
PATH="$ROOT_DIR/test/fake_bin:$PATH" \
"$ROOT_DIR/check_email_pr.sh" 2>&1) ; then
status=0
else
status=$?
fi

[ "$status" -eq 0 ]
! grep -q 'curl was called' <<< "$output"
! grep -q 'jq was called' <<< "$output"
! grep -q 'integer expression expected' <<< "$output"
}

run_pull_request_event_test() {
local event output
event=$1

output=$(env -u TEST_MODE \
PULL_NUMBER=1 \
COMMITS_COUNT=2 \
GITHUB_API_URL=https://api.example.com \
GITHUB_EVENT_NAME="$event" \
PATH="$ROOT_DIR/test/fake_bin/pr_target:$PATH" \
"$ROOT_DIR/check_email_pr.sh" 2>&1)

grep -q 'Running check on:' <<< "$output"
}

for event in push workflow_dispatch schedule unset ; do
run_non_pr_event_test "$event"
done
run_pull_request_event_test pull_request
run_pull_request_event_test pull_request_target
3 changes: 3 additions & 0 deletions test/fake_bin/curl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo 'curl was called' >&2
exit 1
3 changes: 3 additions & 0 deletions test/fake_bin/jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo 'jq was called' >&2
exit 1
3 changes: 3 additions & 0 deletions test/fake_bin/pr_target/curl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
ROOT_DIR=$(cd -- "$(dirname -- "$0")/../../.." && pwd)
cat "$ROOT_DIR/test/pr_list_commits.json"
Loading