1 #!/usr/bin/env bash
2 #
3 # Determines whether a PR has already been looked at by a Medusa team member.
4 # Usage: ./scripts/check_team_review.sh [pr_number]
5 #
6 # If pr_number is not provided, reads from the workflow event payload.
7 #
8 # A PR counts as "reviewed by the team" when a team member (as listed in
9 # .github/teams.yml) has either submitted a review or left a comment on it —
10 # a comment is treated as a sign that a human on the team has looked at the
11 # PR, even if they never formally submitted a review. The PR author is
12 # excluded: authoring a PR is not reviewing it.
13 #
14 # Prints `true` or `false` on stdout.
15 #
16
17 set -euo pipefail
18
19 if [[ -n "${1:-}" ]]; then
20 PR="$1"
21 else
22 PR=$(jq -r '.pull_request.number // .issue.number // empty' "${GITHUB_EVENT_PATH:?GITHUB_EVENT_PATH not set}")
23 fi
24
25 if ! [[ "$PR" =~ ^[0-9]+$ ]]; then
26 echo "Error: no valid PR number provided or found in event payload" >&2
27 exit 1
28 fi
29
30 REPO="${GITHUB_REPOSITORY:-medusajs/medusa}"
31 TEAMS_FILE="${TEAMS_FILE:-.github/teams.yml}"
32
33 if [[ ! -f "$TEAMS_FILE" ]]; then
34 echo "Error: teams file not found at $TEAMS_FILE" >&2
35 exit 1
36 fi
37
38 # teams.yml lists members as quoted "@login" entries. Extract the logins,
39 # lowercased, so the comparison below is case-insensitive (GitHub logins are).
40 TEAM_MEMBERS=$(grep -oE '"@[A-Za-z0-9-]+"' "$TEAMS_FILE" | tr -d '"@' | tr '[:upper:]' '[:lower:]' | sort -u)
41
42 if [[ -z "$TEAM_MEMBERS" ]]; then
43 echo "Error: no team members parsed from $TEAMS_FILE" >&2
44 exit 1
45 fi
46
47 AUTHOR=$(gh pr view "$PR" --repo "$REPO" --json author --jq '.author.login // ""' | tr '[:upper:]' '[:lower:]')
48
49 # Reviewers (any review state) and comment authors, minus bots.
50 REVIEWERS=$(gh api "repos/$REPO/pulls/$PR/reviews" --paginate \
51 --jq '.[] | select(.user.type != "Bot") | .user.login' 2>/dev/null | tr '[:upper:]' '[:lower:]' || true)
52 COMMENTERS=$(gh api "repos/$REPO/issues/$PR/comments" --paginate \
53 --jq '.[] | select(.user.type != "Bot") | .user.login' 2>/dev/null | tr '[:upper:]' '[:lower:]' || true)
54 REVIEW_COMMENTERS=$(gh api "repos/$REPO/pulls/$PR/comments" --paginate \
55 --jq '.[] | select(.user.type != "Bot") | .user.login' 2>/dev/null | tr '[:upper:]' '[:lower:]' || true)
56
57 PARTICIPANTS=$(printf '%s\n%s\n%s\n' "$REVIEWERS" "$COMMENTERS" "$REVIEW_COMMENTERS" \
58 | sed '/^$/d' | sort -u | grep -v "^${AUTHOR}$" || true)
59
60 if [[ -z "$PARTICIPANTS" ]]; then
61 echo "false"
62 exit 0
63 fi
64
65 if comm -12 <(printf '%s\n' "$TEAM_MEMBERS") <(printf '%s\n' "$PARTICIPANTS") | grep -q .; then
66 echo "true"
67 else
68 echo "false"
69 fi