Skip to content

Commit 8c87df5

Browse files
committed
docker: add REPO_URL and ORG_NAME support
* REPO_URL allows to fetch rules from the target repo * ORG_NAME allows to prefix review groups correctly
1 parent 9a571c9 commit 8c87df5

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ reviewers string is formatted for use with [GitHub's gh
7777
CLI](https://cli.github.com/).
7878

7979
$ docker run --rm -i reviewer-selector < samples/sample.diff
80+
No REPO_URL in environment, using built-in rules ...
8081
No DIFF_URL in environment, reading from stdin ...
82+
No ORG_NAME in environment, using # as group prefix ...
83+
No REPO_NAME or TARGET_BRANCH_NAME in environment, not matching repository-based rules ...
8184
No PR_URL or GITHUB_TOKEN in environment, outputing to stdout ...
8285
@example-group,shtrom
8386

@@ -91,7 +94,10 @@ piped into the container.
9194
| docker run --rm -i \
9295
-v ./herald_rules.real.json:/app/herald_rules.json \
9396
reviewer-selector
97+
No REPO_URL in environment, using built-in rules ...
9498
No DIFF_URL in environment, reading from stdin ...
99+
No ORG_NAME in environment, using # as group prefix ...
100+
No REPO_NAME or TARGET_BRANCH_NAME in environment, not matching repository-based rules ...
95101
No PR_URL or GITHUB_TOKEN in environment, outputing to stdout ...
96102
@android-reviewers
97103

@@ -104,6 +110,16 @@ The container's behaviour can be entirely parametrised via environment variables
104110
-e GITHUB_TOKEN=[REDACTED] reviewer-selector
105111
Adding reviewers to https://github.com/mozilla-firefox/infra-testing/pull/30 ...
106112

107-
If `DIFF_URL` is given, it will be fetched and passed into the selector's
108-
stdin. If `GITHUB_TOKEN` and `PR_URL` are provided, the container will attempt
109-
to set the reviewers on the target PR.
113+
* If `DIFF_URL` is given, it will be fetched and passed into the selector's
114+
stdin.
115+
* If `GITHUB_TOKEN` and `PR_URL` are provided, the container will attempt
116+
117+
Some other optional behaviours can be triggered by providing additional context
118+
in environment variables:
119+
* If `ORG_NAME` is passed, it will be used to scope reviewers groups to that
120+
org.
121+
* If `REPO_NAME` and `TARGET_BRANCH_NAME` are provided, rules that specifically
122+
match a given repository and/or branch will also be applied.
123+
* If `REPO_URL` is given, the script will attempt to fetch a rules file from
124+
the `main` branch of the repository. If it fails, it will fallback to
125+
built-in rules. to set the reviewers on the target PR.

docker/entrypoint.sh

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,65 @@
11
#!/bin/sh -eu
2+
# Variables expected in environment:
3+
# * DIFF_URL (optional, will read stdin otherwise)
4+
# * GITHUB_TOKEN (optional, will output reviewers to stdout otherwise)
5+
# * PR_URL (optional, will output reviewers to stdout otherwise)
6+
# * ORG_NAME (optional, will use # as a group prefix otherwise)
7+
# * REPO_NAME (optional, for more rule matching with TARGET_BRANCH_NAME)
8+
# * TARGET_BRANCH_NAME (optional, for more rule matching with REPO_NAME)
9+
# * REPO_URL (optional, to fetch specific rules)
210

3-
HERALD_RULES_JSON="${1}"
11+
CURL="curl --fail --show-error --silent --location"
12+
13+
if [ -n "${REPO_URL:-}" ]; then
14+
HERALD_RULES_JSON=$(mktemp)
15+
# We always fetch the rules from the main branch.
16+
RULES_URL="${REPO_URL}/refs/heads/main/herald_rules.json"
17+
18+
if ! ${CURL} "${RULES_URL}" --output "${HERALD_RULES_JSON}"; then
19+
echo "Failed to fetch rules from ${RULES_URL}, using built-in rules ..." >&2
20+
HERALD_RULES_JSON=""
21+
22+
fi
23+
24+
else
25+
echo "No REPO_URL in environment, using built-in rules ..." >&2
26+
27+
fi
28+
29+
if [ -z "${HERALD_RULES_JSON:-}" ]; then
30+
HERALD_RULES_JSON="${1}"
31+
fi
432

533
DIFF=$(mktemp)
634
if [ -n "${DIFF_URL:-}" ]; then
7-
curl --show-error --silent --location "${DIFF_URL}" --output "${DIFF}"
35+
${CURL} "${DIFF_URL}" --output "${DIFF}"
836

937
else
1038
echo "No DIFF_URL in environment, reading from stdin ..." >&2
1139
cat > "${DIFF}"
1240

1341
fi
1442

43+
if [ -n "${ORG_NAME:-}" ]; then
44+
GROUP_PREFIX="${ORG_NAME}/"
45+
46+
else
47+
GROUP_PREFIX="#"
48+
echo "No ORG_NAME in environment, using ${GROUP_PREFIX} as group prefix ..." >&2
49+
50+
fi
51+
52+
if [ -n "${REPO_NAME:-}" ] && [ -n "${TARGET_BRANCH_NAME:-}" ]; then
53+
REPO_BRANCH=${REPO_NAME}-${TARGET_BRANCH_NAME}
54+
55+
else
56+
echo "No REPO_NAME or TARGET_BRANCH_NAME in environment, not matching repository-based rules ..." >&2
57+
fi
58+
1559
REVIEWERS=$(cat "${DIFF}" \
1660
| /app/reviewer_selector.py \
17-
${REPO_NAME:+--repo "${REPO_NAME}"} \
18-
--group-prefix @ --reviewer-separator , \
61+
${REPO_BRANCH:+--repo "${REPO_BRANCH}"} \
62+
--group-prefix "${GROUP_PREFIX}" --reviewer-separator , \
1963
"${HERALD_RULES_JSON}" \
2064
)
2165

0 commit comments

Comments
 (0)