Skip to content

Commit 3ba438e

Browse files
authored
Add a workflow that labels issues and PRs that mention an AGPL project (#7985)
Cortex is Apache 2.0 licensed and must not take code or ideas from any AGPL licensed project, so a maintainer must review every issue and pull request that refers to one. The new license-check workflow adds the license-issue label when the name of a known AGPL project (case-insensitive, which includes links to its repository) is in the title, the body, a comment, a review, or the added lines of a pull request. Changes in vendor/ are skipped, because upstream libraries mention the project in comments. The script keeps the literal name out of its own source (the regex uses brackets), so that this change and later changes to the script do not get the label. The workflow runs on issue, comment and pull request events, every hour for items that changed recently (review comments on fork pull requests get a read-only token), and on demand. It uses pull_request_target to label fork pull requests, but it never checks out or runs their code. The pull request template gets a checkbox to confirm that the author did not look at code from AGPL licensed projects. Signed-off-by: Friedrich Gonzalez <1517449+friedrichg@users.noreply.github.com>
1 parent e7d65c8 commit 3ba438e

4 files changed

Lines changed: 186 additions & 0 deletions

File tree

‎.github/pull_request_template.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ Fixes #<issue number>
1414
- [ ] Documentation added
1515
- [ ] `CHANGELOG.md` updated - the order of entries should be `[CHANGE]`, `[FEATURE]`, `[ENHANCEMENT]`, `[BUGFIX]`
1616
- [ ] `docs/configuration/v1-guarantees.md` updated if this PR introduces experimental flags
17+
- [ ] I did not look at code from AGPL licensed projects to make this PR

‎.github/workflows-doc.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ If you wish to add a new CI or CD job, add it to the existing current test-build
1111
test-build-deploy.yml specifies a workflow that runs all Cortex continuous integration and continuous deployment jobs. The workflow is triggered on every pull request and commit to master, however the CD jobs only run when changes are merged onto master . The workflow combines both CI and CD jobs, because the CD jobs are dependent on artifacts produced the CI jobs.
1212

1313

14+
## License Check
15+
16+
license-check.yml adds the `license-issue` label to issues and pull requests that mention any AGPL licensed project. Cortex must not take code or ideas from such a project, so a maintainer must review each labeled item. The script `scripts/license-check.sh` looks for the name of the project (see `PATTERN` in the script) in the title, the body, all comments and reviews, and the added lines of a pull request (not in `vendor/`). The workflow runs on issue, comment and pull request events, every hour for items that changed recently, and on demand with `workflow_dispatch`. It uses `pull_request_target` so that it can label pull requests from forks, but it never checks out or runs code from the pull request. To test the script locally, run `DRY_RUN=true GITHUB_REPOSITORY=cortexproject/cortex .github/workflows/scripts/license-check.sh <number>`.
17+
1418
## Specific Jobs
1519

1620
| Job | Description | Type |
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: license-check
2+
# Adds the license-issue label to issues and pull requests that mention an
3+
# AGPL project. See .github/workflows/scripts/license-check.sh.
4+
#
5+
# This workflow uses pull_request_target so that it can label pull requests
6+
# from forks. It never checks out or runs code from the pull request. It only
7+
# reads the pull request through the GitHub API.
8+
permissions: read-all
9+
on:
10+
issues:
11+
types: [opened, edited, reopened]
12+
issue_comment:
13+
types: [created, edited]
14+
pull_request_target:
15+
types: [opened, edited, reopened, synchronize]
16+
# Review comments on pull requests from forks get a read-only token, so an
17+
# event for them cannot add a label. The scheduled run finds them instead.
18+
schedule:
19+
- cron: '17 * * * *'
20+
workflow_dispatch:
21+
inputs:
22+
numbers:
23+
description: 'Issue or pull request numbers, separated by spaces'
24+
required: true
25+
26+
concurrency:
27+
group: license-check-${{ github.event.issue.number || github.event.pull_request.number || github.run_id }}
28+
cancel-in-progress: false
29+
30+
jobs:
31+
label:
32+
if: github.repository == 'cortexproject/cortex'
33+
runs-on: ubuntu-24.04
34+
permissions:
35+
issues: write
36+
pull-requests: write
37+
steps:
38+
- name: Checkout Repo
39+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
40+
with:
41+
# The base branch, never the pull request head.
42+
ref: ${{ github.event.repository.default_branch }}
43+
sparse-checkout: .github/workflows/scripts
44+
persist-credentials: false
45+
- name: Check for AGPL Project References
46+
env:
47+
GH_TOKEN: ${{ github.token }}
48+
EVENT_NAME: ${{ github.event_name }}
49+
ITEM_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
50+
INPUT_NUMBERS: ${{ github.event.inputs.numbers }}
51+
run: |
52+
case "${EVENT_NAME}" in
53+
schedule)
54+
.github/workflows/scripts/license-check.sh --since "$(date -u -d '-2 hours' +%Y-%m-%dT%H:%M:%SZ)"
55+
;;
56+
workflow_dispatch)
57+
# shellcheck disable=SC2086 # split the list into arguments
58+
.github/workflows/scripts/license-check.sh ${INPUT_NUMBERS}
59+
;;
60+
*)
61+
.github/workflows/scripts/license-check.sh "${ITEM_NUMBER}"
62+
;;
63+
esac
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/bin/bash
2+
#
3+
# Adds the license-issue label to issues and pull requests that mention any
4+
# AGPL licensed project. Cortex is Apache 2.0 licensed, so Cortex must not take
5+
# code or ideas from such a project. A maintainer must review every item that
6+
# gets this label.
7+
#
8+
# Usage: license-check.sh <number>...
9+
# license-check.sh --since <ISO 8601 timestamp>
10+
#
11+
# The script looks for the name of the project (case-insensitive, see
12+
# PATTERN) in:
13+
# - the title and the body
14+
# - the issue comments, the review comments and the review bodies
15+
# - for a pull request: the changed file names and the added lines,
16+
# except in vendor/ (upstream libraries mention the project in comments)
17+
#
18+
# A link to the GitHub repository of the project also contains the name, so
19+
# the script finds it too.
20+
#
21+
# Environment: GH_TOKEN and GITHUB_REPOSITORY must be set. Set DRY_RUN=true
22+
# to print the result and not change the label.
23+
24+
set -euo pipefail
25+
26+
readonly LABEL="license-issue"
27+
readonly LABEL_COLOR="b60205"
28+
readonly LABEL_DESCRIPTION="Mentions an AGPL project. A maintainer must check for a license issue."
29+
# The brackets keep the literal name out of this file, so that changes to this
30+
# script do not get the label. The regex matches the plain name.
31+
readonly PATTERN="m[i]m[i]r"
32+
33+
: "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY must be set}"
34+
readonly REPO="${GITHUB_REPOSITORY}"
35+
36+
ensure_label() {
37+
if ! gh label list --repo "${REPO}" --search "${LABEL}" --json name --jq '.[].name' | grep -qx "${LABEL}"; then
38+
gh label create "${LABEL}" --repo "${REPO}" --color "${LABEL_COLOR}" --description "${LABEL_DESCRIPTION}"
39+
fi
40+
}
41+
42+
# Writes all the text of one issue or pull request to stdout.
43+
item_text() {
44+
local number="$1"
45+
local is_pr="$2"
46+
47+
gh api "repos/${REPO}/issues/${number}" --jq '.title, (.body // "")'
48+
gh api --paginate "repos/${REPO}/issues/${number}/comments" --jq '.[].body // ""'
49+
50+
if [[ "${is_pr}" == "true" ]]; then
51+
gh api --paginate "repos/${REPO}/pulls/${number}/reviews" --jq '.[].body // ""'
52+
gh api --paginate "repos/${REPO}/pulls/${number}/comments" --jq '.[].body // ""'
53+
# The files API returns a patch for each file. Keep the file name and the
54+
# added lines only. Context lines and removed lines are old code.
55+
gh api --paginate "repos/${REPO}/pulls/${number}/files" \
56+
--jq '.[] | select(.filename | startswith("vendor/") | not) | .filename, (.patch // "")' \
57+
| grep -v -e '^-' -e '^ ' -e '^@@' || true
58+
fi
59+
}
60+
61+
check_item() {
62+
local number="$1"
63+
local is_pr
64+
is_pr="$(gh api "repos/${REPO}/issues/${number}" --jq 'has("pull_request")')"
65+
66+
# Read all the text first. With pipefail, "item_text | grep -q" can fail
67+
# when grep stops early and gh gets SIGPIPE.
68+
local text
69+
text="$(item_text "${number}" "${is_pr}")"
70+
71+
if grep -qi "${PATTERN}" <<< "${text}"; then
72+
if [[ "${DRY_RUN:-false}" == "true" ]]; then
73+
echo "#${number}: found '${PATTERN}', dry run, label ${LABEL} not added"
74+
else
75+
echo "#${number}: found '${PATTERN}', adding label ${LABEL}"
76+
gh api --method POST "repos/${REPO}/issues/${number}/labels" -f "labels[]=${LABEL}" > /dev/null
77+
fi
78+
else
79+
echo "#${number}: no match"
80+
fi
81+
}
82+
83+
main() {
84+
if [[ $# -eq 0 ]]; then
85+
echo "usage: $0 <number>... | --since <timestamp>" >&2
86+
exit 2
87+
fi
88+
89+
local numbers=()
90+
if [[ "$1" == "--since" ]]; then
91+
local since="${2:?--since needs a timestamp}"
92+
local n
93+
while read -r n; do
94+
numbers+=("${n}")
95+
done < <(gh api --paginate "repos/${REPO}/issues?state=all&since=${since}&per_page=100" --jq '.[].number')
96+
if [[ ${#numbers[@]} -eq 0 ]]; then
97+
echo "no issues or pull requests changed since ${since}"
98+
exit 0
99+
fi
100+
else
101+
numbers=("$@")
102+
fi
103+
104+
if [[ "${DRY_RUN:-false}" != "true" ]]; then
105+
ensure_label
106+
fi
107+
108+
local number
109+
for number in "${numbers[@]}"; do
110+
if [[ ! "${number}" =~ ^[0-9]+$ ]]; then
111+
echo "not a number: ${number}" >&2
112+
exit 2
113+
fi
114+
check_item "${number}"
115+
done
116+
}
117+
118+
main "$@"

0 commit comments

Comments
 (0)