From 9e8fe2e5ec930ed1336fc1c90e4157463d82a1d5 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 14:40:15 -0500 Subject: [PATCH 01/17] add label calculation script --- devtools/scripts/git-diff-label-calc.sh | 79 +++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 devtools/scripts/git-diff-label-calc.sh diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh new file mode 100755 index 0000000000..54d7686169 --- /dev/null +++ b/devtools/scripts/git-diff-label-calc.sh @@ -0,0 +1,79 @@ +#!/bin/sh +set -e + +# Outputs a label based on the size of the diff between the current branch and the main branch. + +# env setting defaults +if [ -z "$MAIN_BRANCH" ]; then + MAIN_BRANCH=main +fi +if [ -z "$FAIL_IF_XL" ]; then + FAIL_IF_XL=0 +fi + +# const settings +IGNORE_PATTERN_FILE=.labelignore +XS_LABEL=size/xs +XS_SIZE=20 +S_LABEL=size/s +S_SIZE=100 +M_LABEL=size/m +M_SIZE=250 +L_LABEL=size/l +L_SIZE=500 +XL_LABEL=size/xl + +# Get a list of files including the "generated by" header, then filter them out by only those with DO NOT EDIT on the first line, in case we have code in the repo that does the generation. +EXCLUDE_GENERATED_FILES=$(git grep -l -e 'generated by .* DO NOT EDIT' | xargs -I{} sh -c 'head -n 1 {} | grep -q "DO NOT EDIT" && echo {}' | sed 's/^/:\(exclude\)/') + +EXCLUDE_IGNORED_PATTERNS=$(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | sed 's/^/:\(exclude\)/') + +# Perform diff for merging to the main branch: +# --ignore-blank-lines: ignore changes that only involve blank lines +# --shortstat: output only the summary of the changes +# -w: ignore whitespace +# --diff-filter=d: exclude deleted files +# --minimal: produce the smallest diff possible +# --merge-base: find the common ancestor of the current branch and the main branch +# $EXCLUDE_GENERATED_FILES: exclude generated files +# $EXCLUDE_IGNORED_PATTERNS: exclude files that match the patterns in the .labelignore file +OUTPUT=$(git diff --ignore-blank-lines --shortstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- $EXCLUDE_GENERATED_FILES $EXCLUDE_IGNORED_PATTERNS) + +sum=$( + echo "$OUTPUT" | awk ' + { + insertions = deletions = 0 + for (i = 1; i <= NF; i++) { + if ($i ~ /insertions\(\+\)/) insertions = $(i-1) + if ($i ~ /deletions\(-\)/) deletions = $(i-1) + } + print insertions + deletions + }' +) + +echo "Output: $OUTPUT" >&2 +echo "Changes: $sum" >&2 + +LABEL="" +if [ $sum -le $XS_SIZE ]; then + LABEL=$XS_LABEL +elif [ $sum -le $S_SIZE ]; then + LABEL=$S_LABEL +elif [ $sum -le $M_SIZE ]; then + LABEL=$M_LABEL +elif [ $sum -le $L_SIZE ]; then + LABEL=$L_LABEL +else + LABEL=$XL_LABEL +fi + +echo "Label: $LABEL" >&2 + +if [ $FAIL_IF_XL -eq 1 ] && [ "$LABEL" = "$XL_LABEL" ]; then + echo "" + echo "ERROR: This PR exceeds the maximum size of $L_SIZE lines." + echo "Please make sure you are NOT addressing multiple issues with one PR." + exit 1 +fi + +echo "$LABEL" From 6e503b765e0f12d6761502b763decf645856afed Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 14:40:23 -0500 Subject: [PATCH 02/17] add additional ignore files --- .labelignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .labelignore diff --git a/.labelignore b/.labelignore new file mode 100644 index 0000000000..27929ea7cf --- /dev/null +++ b/.labelignore @@ -0,0 +1,4 @@ +*.lock +go.mod +go.sum +.yarn/releases/* From 7a8aea3cf1f09f4486e29986fb8f3570db98d9f7 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 15:21:03 -0500 Subject: [PATCH 03/17] add debug mode --- devtools/scripts/git-diff-label-calc.sh | 88 +++++++++++++++++-------- 1 file changed, 62 insertions(+), 26 deletions(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index 54d7686169..4a6ab15a8e 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -14,19 +14,58 @@ fi # const settings IGNORE_PATTERN_FILE=.labelignore XS_LABEL=size/xs -XS_SIZE=20 +XS_MAX=20 S_LABEL=size/s -S_SIZE=100 +S_MAX=100 M_LABEL=size/m -M_SIZE=250 +M_MAX=250 L_LABEL=size/l -L_SIZE=500 +L_MAX=500 XL_LABEL=size/xl +if [ -z "$DEBUG" ]; then + DEBUG=0 +fi +if [ "$1" = "--debug" ]; then + DEBUG=1 +fi + +# debug works like echo but only prints when --debug is passed and to stderr +debug() { + if [ $DEBUG -eq 1 ]; then + echo "$1" >&2 + fi +} + +filter_nongen_files() { + while IFS= read -r file; do + # include only files where DO NOT EDIT appears on the very first line + if head -n 1 "$file" | grep -q "DO NOT EDIT"; then + debug "IGNORE: generated file: $file" + printf '%s\n' "$file" + fi + done +} + # Get a list of files including the "generated by" header, then filter them out by only those with DO NOT EDIT on the first line, in case we have code in the repo that does the generation. -EXCLUDE_GENERATED_FILES=$(git grep -l -e 'generated by .* DO NOT EDIT' | xargs -I{} sh -c 'head -n 1 {} | grep -q "DO NOT EDIT" && echo {}' | sed 's/^/:\(exclude\)/') +EXCLUDE_GENERATED_FILES_ARGS=$(git grep -l -e 'generated by .* DO NOT EDIT' | filter_nongen_files | sed 's/^/:\(exclude\)/') + +resolve_git_files() { + while IFS= read -r pattern; do + # Skip empty lines and comments + case "$pattern" in + '' | \#*) continue ;; + esac -EXCLUDE_IGNORED_PATTERNS=$(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | sed 's/^/:\(exclude\)/') + # Use git ls-files with the pattern + for file in $(git ls-files "$pattern"); do + debug "IGNORE: pattern '$pattern': $file" + echo "$file" + done + done +} + +EXCLUDE_IGNORED_FILES_ARGS=$(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | resolve_git_files | sed 's/^/:\(exclude\)/') # Perform diff for merging to the main branch: # --ignore-blank-lines: ignore changes that only involve blank lines @@ -35,43 +74,40 @@ EXCLUDE_IGNORED_PATTERNS=$(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | sed 's/^/:\( # --diff-filter=d: exclude deleted files # --minimal: produce the smallest diff possible # --merge-base: find the common ancestor of the current branch and the main branch -# $EXCLUDE_GENERATED_FILES: exclude generated files -# $EXCLUDE_IGNORED_PATTERNS: exclude files that match the patterns in the .labelignore file -OUTPUT=$(git diff --ignore-blank-lines --shortstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- $EXCLUDE_GENERATED_FILES $EXCLUDE_IGNORED_PATTERNS) +# $EXCLUDE_GENERATED_FILES_ARGS: exclude generated files +# $EXCLUDE_IGNORED_FILES_ARGS: exclude files that match the patterns in the .labelignore file +OUTPUT=$(git diff --ignore-blank-lines --numstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- $EXCLUDE_GENERATED_FILES_ARGS $EXCLUDE_IGNORED_FILES_ARGS) + +debug +debug "DIFF FILE STATS" +debug "===============" +debug "$OUTPUT" +debug sum=$( - echo "$OUTPUT" | awk ' - { - insertions = deletions = 0 - for (i = 1; i <= NF; i++) { - if ($i ~ /insertions\(\+\)/) insertions = $(i-1) - if ($i ~ /deletions\(-\)/) deletions = $(i-1) - } - print insertions + deletions - }' + echo "$OUTPUT" | awk '{ total += $1 + $2 } END { print total }' ) -echo "Output: $OUTPUT" >&2 -echo "Changes: $sum" >&2 +debug "TOTAL: $sum" LABEL="" -if [ $sum -le $XS_SIZE ]; then +if [ $sum -le $XS_MAX ]; then LABEL=$XS_LABEL -elif [ $sum -le $S_SIZE ]; then +elif [ $sum -le $S_MAX ]; then LABEL=$S_LABEL -elif [ $sum -le $M_SIZE ]; then +elif [ $sum -le $M_MAX ]; then LABEL=$M_LABEL -elif [ $sum -le $L_SIZE ]; then +elif [ $sum -le $L_MAX ]; then LABEL=$L_LABEL else LABEL=$XL_LABEL fi -echo "Label: $LABEL" >&2 +debug "LABEL: $LABEL" if [ $FAIL_IF_XL -eq 1 ] && [ "$LABEL" = "$XL_LABEL" ]; then echo "" - echo "ERROR: This PR exceeds the maximum size of $L_SIZE lines." + echo "ERROR: This PR exceeds the maximum size of $L_MAX lines." echo "Please make sure you are NOT addressing multiple issues with one PR." exit 1 fi From e3f3954ff21b660a73fc5868b60332ffeaa61541 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 15:44:10 -0500 Subject: [PATCH 04/17] handle spaces in filenames safely --- devtools/scripts/git-diff-label-calc.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index 4a6ab15a8e..7b91627d8d 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash set -e # Outputs a label based on the size of the diff between the current branch and the main branch. @@ -48,7 +48,7 @@ filter_nongen_files() { } # Get a list of files including the "generated by" header, then filter them out by only those with DO NOT EDIT on the first line, in case we have code in the repo that does the generation. -EXCLUDE_GENERATED_FILES_ARGS=$(git grep -l -e 'generated by .* DO NOT EDIT' | filter_nongen_files | sed 's/^/:\(exclude\)/') +readarray -t EXCLUDE_GENERATED_FILES_ARGS < <(git grep -l -e 'generated by .* DO NOT EDIT' | filter_nongen_files | sed 's/^/:\(exclude\)/') resolve_git_files() { while IFS= read -r pattern; do @@ -58,14 +58,14 @@ resolve_git_files() { esac # Use git ls-files with the pattern - for file in $(git ls-files "$pattern"); do + while IFS= read -r file; do debug "IGNORE: pattern '$pattern': $file" - echo "$file" - done + printf '%s\n' "$file" + done < <(git ls-files "$pattern") done } -EXCLUDE_IGNORED_FILES_ARGS=$(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | resolve_git_files | sed 's/^/:\(exclude\)/') +readarray -t EXCLUDE_IGNORED_FILES_ARGS < <(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | resolve_git_files | sed 's/^/:\(exclude\)/') # Perform diff for merging to the main branch: # --ignore-blank-lines: ignore changes that only involve blank lines @@ -74,9 +74,9 @@ EXCLUDE_IGNORED_FILES_ARGS=$(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | resolve_gi # --diff-filter=d: exclude deleted files # --minimal: produce the smallest diff possible # --merge-base: find the common ancestor of the current branch and the main branch -# $EXCLUDE_GENERATED_FILES_ARGS: exclude generated files -# $EXCLUDE_IGNORED_FILES_ARGS: exclude files that match the patterns in the .labelignore file -OUTPUT=$(git diff --ignore-blank-lines --numstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- $EXCLUDE_GENERATED_FILES_ARGS $EXCLUDE_IGNORED_FILES_ARGS) +# $EXCLUDE_GENERATED_FILES_ARGS[@]: exclude generated files +# $EXCLUDE_IGNORED_FILES_ARGS[@]: exclude files that match the patterns in the .labelignore file +OUTPUT=$(git diff --ignore-blank-lines --numstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- "${EXCLUDE_GENERATED_FILES_ARGS[@]}" "${EXCLUDE_IGNORED_FILES_ARGS[@]}") debug debug "DIFF FILE STATS" From 869b5e3a8385af46dc84b46e6e198a89360c22f0 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 15:55:00 -0500 Subject: [PATCH 05/17] fix compat, simplify --- devtools/scripts/git-diff-label-calc.sh | 52 ++++++++++++------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index 7b91627d8d..c7c8270e3f 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -37,35 +37,30 @@ debug() { fi } -filter_nongen_files() { - while IFS= read -r file; do - # include only files where DO NOT EDIT appears on the very first line - if head -n 1 "$file" | grep -q "DO NOT EDIT"; then - debug "IGNORE: generated file: $file" - printf '%s\n' "$file" - fi - done -} +IGNORE_ARGS=() # Get a list of files including the "generated by" header, then filter them out by only those with DO NOT EDIT on the first line, in case we have code in the repo that does the generation. -readarray -t EXCLUDE_GENERATED_FILES_ARGS < <(git grep -l -e 'generated by .* DO NOT EDIT' | filter_nongen_files | sed 's/^/:\(exclude\)/') - -resolve_git_files() { - while IFS= read -r pattern; do - # Skip empty lines and comments - case "$pattern" in - '' | \#*) continue ;; - esac - - # Use git ls-files with the pattern - while IFS= read -r file; do - debug "IGNORE: pattern '$pattern': $file" - printf '%s\n' "$file" - done < <(git ls-files "$pattern") - done -} +while IFS= read -r file; do + # include only files where DO NOT EDIT appears on the very first line + if head -n 1 "$file" | grep -q "DO NOT EDIT"; then + debug "IGNORE: generated file: $file" + IGNORE_ARGS+=(":(exclude)$file") + fi +done < <(git grep -l -e 'generated by .* DO NOT EDIT') -readarray -t EXCLUDE_IGNORED_FILES_ARGS < <(cat "$IGNORE_PATTERN_FILE" 2>/dev/null | resolve_git_files | sed 's/^/:\(exclude\)/') +# Read the .labelignore file and add the resolved files to the IGNORE_ARGS array. +while IFS= read -r pattern; do + # Skip empty lines and comments + case "$pattern" in + '' | \#*) continue ;; + esac + + # Use git ls-files with the pattern + while IFS= read -r file; do + debug "IGNORE: pattern '$pattern': $file" + IGNORE_ARGS+=(":(exclude)$file") + done < <(git ls-files "$pattern") +done < <(cat "$IGNORE_PATTERN_FILE" 2>/dev/null) # Perform diff for merging to the main branch: # --ignore-blank-lines: ignore changes that only involve blank lines @@ -76,7 +71,10 @@ readarray -t EXCLUDE_IGNORED_FILES_ARGS < <(cat "$IGNORE_PATTERN_FILE" 2>/dev/nu # --merge-base: find the common ancestor of the current branch and the main branch # $EXCLUDE_GENERATED_FILES_ARGS[@]: exclude generated files # $EXCLUDE_IGNORED_FILES_ARGS[@]: exclude files that match the patterns in the .labelignore file -OUTPUT=$(git diff --ignore-blank-lines --numstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- "${EXCLUDE_GENERATED_FILES_ARGS[@]}" "${EXCLUDE_IGNORED_FILES_ARGS[@]}") +OUTPUT=$( + if [ "$DEBUG" = "1" ]; then set -x; fi + git diff --ignore-blank-lines --numstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- "${IGNORE_ARGS[@]}" +) debug debug "DIFF FILE STATS" From f6d84163f37f4c23c9114c946953fc7cdf89fdc8 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 15:55:48 -0500 Subject: [PATCH 06/17] set correct default --- devtools/scripts/git-diff-label-calc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index c7c8270e3f..d81bde7f81 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -5,7 +5,7 @@ set -e # env setting defaults if [ -z "$MAIN_BRANCH" ]; then - MAIN_BRANCH=main + MAIN_BRANCH=master fi if [ -z "$FAIL_IF_XL" ]; then FAIL_IF_XL=0 From c78e4feb5b08aa718d8ad16bcee923d1316df881 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:01:01 -0500 Subject: [PATCH 07/17] fix shebang --- devtools/scripts/git-diff-label-calc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index d81bde7f81..8f866a7ad3 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e # Outputs a label based on the size of the diff between the current branch and the main branch. From b590d8dca5d956e27771f4e4ac4917059b74aa64 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:09:49 -0500 Subject: [PATCH 08/17] add test for new labeler script --- .github/workflows/labeler_v2.yml | 17 +++++++++++++++++ devtools/scripts/github-set-pr-label.sh | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 .github/workflows/labeler_v2.yml create mode 100755 devtools/scripts/github-set-pr-label.sh diff --git a/.github/workflows/labeler_v2.yml b/.github/workflows/labeler_v2.yml new file mode 100644 index 0000000000..b85326d24f --- /dev/null +++ b/.github/workflows/labeler_v2.yml @@ -0,0 +1,17 @@ +name: labeler v2 + +on: + pull_request: + types: ['opened', 'ready_for_review', 'reopened', 'synchronize'] + +jobs: + labeler: + runs-on: ubuntu-latest + if: github.event.pull_request.draft == false + name: Label the PR size + steps: + - uses: actions/checkout@v3 + - name: Install jq + run: sudo apt-get install -y jq + - name: Run labeler script + run: ./devtools/scripts/github-set-pr-label.sh diff --git a/devtools/scripts/github-set-pr-label.sh b/devtools/scripts/github-set-pr-label.sh new file mode 100755 index 0000000000..5c8247fc54 --- /dev/null +++ b/devtools/scripts/github-set-pr-label.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -e + +if [ -z "$GITHUB_API_URL" ]; then + GITHUB_API_URL=https://api.github.com +fi + +if [ -z "$GITHUB_TOKEN" ]; then + echo "GITHUB_TOKEN is not set" + exit 1 +fi + +PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") +LABEL=$(./devtools/scripts/git-diff-label-calc.sh --debug) + +curl -sSL \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + -X PATCH \ + -H "Content-Type: application/json" \ + -d "{\"labels\":[\"test/$LABEL\"]}" \ + "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER" From e6a3fd1b8ebbba894d8c343186583444dce6e5d3 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:18:00 -0500 Subject: [PATCH 09/17] set env vars, remove jq dep --- .github/workflows/labeler_v2.yml | 7 +++++-- devtools/scripts/github-set-pr-label.sh | 19 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.github/workflows/labeler_v2.yml b/.github/workflows/labeler_v2.yml index b85326d24f..4c9eb049b1 100644 --- a/.github/workflows/labeler_v2.yml +++ b/.github/workflows/labeler_v2.yml @@ -11,7 +11,10 @@ jobs: name: Label the PR size steps: - uses: actions/checkout@v3 - - name: Install jq - run: sudo apt-get install -y jq - name: Run labeler script + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + OWNER: ${{ github.repository_owner }} + REPO: ${{ github.event.repository.name }} + PR_NUMBER: ${{ github.event.number }} run: ./devtools/scripts/github-set-pr-label.sh diff --git a/devtools/scripts/github-set-pr-label.sh b/devtools/scripts/github-set-pr-label.sh index 5c8247fc54..0a1beb50d7 100755 --- a/devtools/scripts/github-set-pr-label.sh +++ b/devtools/scripts/github-set-pr-label.sh @@ -1,16 +1,21 @@ #!/usr/bin/env bash set -e +ensure_env() { + if [ -z "$1" ]; then + echo "$2 is not set" + exit 1 + fi +} + if [ -z "$GITHUB_API_URL" ]; then GITHUB_API_URL=https://api.github.com fi -if [ -z "$GITHUB_TOKEN" ]; then - echo "GITHUB_TOKEN is not set" - exit 1 -fi - -PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") +ensure_env "$GITHUB_API_URL" "GITHUB_API_URL" +ensure_env "$PR_NUMBER" "PR_NUMBER" +ensure_env "$OWNER" "OWNER" +ensure_env "$REPO" "REPO" LABEL=$(./devtools/scripts/git-diff-label-calc.sh --debug) curl -sSL \ @@ -19,4 +24,4 @@ curl -sSL \ -X PATCH \ -H "Content-Type: application/json" \ -d "{\"labels\":[\"test/$LABEL\"]}" \ - "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER" + "$GITHUB_API_URL/repos/$OWNER/$REPO/issues/$PR_NUMBER" From 02af88210ef77777d97c87b22378ef4c47a4868e Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:21:16 -0500 Subject: [PATCH 10/17] include history for merge check --- .github/workflows/labeler_v2.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/labeler_v2.yml b/.github/workflows/labeler_v2.yml index 4c9eb049b1..d4a51c8819 100644 --- a/.github/workflows/labeler_v2.yml +++ b/.github/workflows/labeler_v2.yml @@ -11,6 +11,8 @@ jobs: name: Label the PR size steps: - uses: actions/checkout@v3 + with: + fetch-depth: 0 - name: Run labeler script env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From c513dbad87298b7ad58b06cd28f8312e289f14e6 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:25:53 -0500 Subject: [PATCH 11/17] simplify --- .github/workflows/labeler_v2.yml | 6 ++---- devtools/scripts/github-set-pr-label.sh | 24 ++++-------------------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/.github/workflows/labeler_v2.yml b/.github/workflows/labeler_v2.yml index d4a51c8819..3499fbc9d2 100644 --- a/.github/workflows/labeler_v2.yml +++ b/.github/workflows/labeler_v2.yml @@ -10,13 +10,11 @@ jobs: if: github.event.pull_request.draft == false name: Label the PR size steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Run labeler script env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - OWNER: ${{ github.repository_owner }} - REPO: ${{ github.event.repository.name }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.number }} run: ./devtools/scripts/github-set-pr-label.sh diff --git a/devtools/scripts/github-set-pr-label.sh b/devtools/scripts/github-set-pr-label.sh index 0a1beb50d7..1cb6fd4054 100755 --- a/devtools/scripts/github-set-pr-label.sh +++ b/devtools/scripts/github-set-pr-label.sh @@ -1,27 +1,11 @@ #!/usr/bin/env bash set -e -ensure_env() { - if [ -z "$1" ]; then - echo "$2 is not set" - exit 1 - fi -} - -if [ -z "$GITHUB_API_URL" ]; then - GITHUB_API_URL=https://api.github.com +if [ -z "PR_NUMBER" ]; then + echo "PR_NUMBER is not set" + exit 1 fi -ensure_env "$GITHUB_API_URL" "GITHUB_API_URL" -ensure_env "$PR_NUMBER" "PR_NUMBER" -ensure_env "$OWNER" "OWNER" -ensure_env "$REPO" "REPO" LABEL=$(./devtools/scripts/git-diff-label-calc.sh --debug) -curl -sSL \ - -H "Authorization: token $GITHUB_TOKEN" \ - -H "Accept: application/vnd.github.v3+json" \ - -X PATCH \ - -H "Content-Type: application/json" \ - -d "{\"labels\":[\"test/$LABEL\"]}" \ - "$GITHUB_API_URL/repos/$OWNER/$REPO/issues/$PR_NUMBER" +gh pr edit "$PR_NUMBER" --add-label "test/$LABEL" From 8a36670b7cd0041c4068882b0ee4b766a2f56362 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:30:52 -0500 Subject: [PATCH 12/17] Simplify PR size labeling workflow - Replace pr-size-labeler with custom labeler script - Remove redundant labeler_v2 workflow file - Enhance labeler script to clean up existing size labels before adding new ones --- .github/workflows/labeler.yml | 41 +++++-------------------- .github/workflows/labeler_v2.yml | 20 ------------ devtools/scripts/github-set-pr-label.sh | 10 +++++- 3 files changed, 16 insertions(+), 55 deletions(-) delete mode 100644 .github/workflows/labeler_v2.yml diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index 6ac8f1b69d..b3b0992ae3 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -10,38 +10,11 @@ jobs: if: github.event.pull_request.draft == false name: Label the PR size steps: - - uses: mastercactapus/pr-size-labeler@eb6a25f40d6a3327581aca35839b60d419e8019a # https://github.com/CodelyTV/pr-size-labeler/pull/61, updated with latest from main + - uses: actions/checkout@v4 with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - xs_label: 'size/xs' - xs_max_size: '15' - s_label: 'size/s' - s_max_size: '100' - m_label: 'size/m' - m_max_size: '250' - l_label: 'size/l' - l_max_size: '500' - xl_label: 'size/xl' - fail_if_xl: 'true' - ignore_deleted: 'true' - message_if_xl: > - This PR exceeds the recommended size of 500 lines. - Please make sure you are NOT addressing multiple issues with one PR. - Note this PR might be rejected due to its size. - files_to_ignore: | - "*.lock" - "graphql2/generated.go" - "graphql2/maplimit.go" - "graphql2/mapconfig.go" - "graphql2/models_gen.go" - "pkg/sysapi/*.pb.go" - "swo/swodb/*.go" - "web/src/*.d.ts" - "Makefile.binaries.mk" - "gadb/*.go" - "migrate/schema.sql" - "go.mod" - "go.sum" - ".yarn/releases/*" - "devtools/pgdump-lite/pgd/*" - "timezone/*.txt" + fetch-depth: 0 + - name: Run labeler script + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ github.event.number }} + run: ./devtools/scripts/github-set-pr-label.sh diff --git a/.github/workflows/labeler_v2.yml b/.github/workflows/labeler_v2.yml deleted file mode 100644 index 3499fbc9d2..0000000000 --- a/.github/workflows/labeler_v2.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: labeler v2 - -on: - pull_request: - types: ['opened', 'ready_for_review', 'reopened', 'synchronize'] - -jobs: - labeler: - runs-on: ubuntu-latest - if: github.event.pull_request.draft == false - name: Label the PR size - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Run labeler script - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUMBER: ${{ github.event.number }} - run: ./devtools/scripts/github-set-pr-label.sh diff --git a/devtools/scripts/github-set-pr-label.sh b/devtools/scripts/github-set-pr-label.sh index 1cb6fd4054..7c1f1c3249 100755 --- a/devtools/scripts/github-set-pr-label.sh +++ b/devtools/scripts/github-set-pr-label.sh @@ -8,4 +8,12 @@ fi LABEL=$(./devtools/scripts/git-diff-label-calc.sh --debug) -gh pr edit "$PR_NUMBER" --add-label "test/$LABEL" +# Remove any existing test/* labels +for label in $(gh pr view "$PR_NUMBER" --json labels --jq '.labels[] | select(.name | startswith("size/")) | .name'); do + if [ "$label" == "$LABEL" ]; then + continue # Skip the label we want to add + fi + gh pr edit "$PR_NUMBER" --remove-label "$label" +done + +gh pr edit "$PR_NUMBER" --add-label "$LABEL" From 1897c4658318f87080fa5442ee593a69bc914015 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:32:18 -0500 Subject: [PATCH 13/17] test label failure --- devtools/scripts/git-diff-label-calc.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index 8f866a7ad3..d10dc057e5 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -110,4 +110,6 @@ if [ $FAIL_IF_XL -eq 1 ] && [ "$LABEL" = "$XL_LABEL" ]; then exit 1 fi +exit 1 # test failure + echo "$LABEL" From dc98d3e2154d037ce57cbbcd3644621c64ca53c4 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:33:11 -0500 Subject: [PATCH 14/17] remove test failure --- devtools/scripts/git-diff-label-calc.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index d10dc057e5..8f866a7ad3 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -110,6 +110,4 @@ if [ $FAIL_IF_XL -eq 1 ] && [ "$LABEL" = "$XL_LABEL" ]; then exit 1 fi -exit 1 # test failure - echo "$LABEL" From eb8a4a0f5b0c2c8f508e170c74ee472902dbd1f3 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:37:58 -0500 Subject: [PATCH 15/17] Update diff tool to show number of changes per file - Switched from `--shortstat` to `--numstat` for more detailed file change output --- devtools/scripts/git-diff-label-calc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index 8f866a7ad3..69563eb895 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -64,7 +64,7 @@ done < <(cat "$IGNORE_PATTERN_FILE" 2>/dev/null) # Perform diff for merging to the main branch: # --ignore-blank-lines: ignore changes that only involve blank lines -# --shortstat: output only the summary of the changes +# --numstat: output only the number of changes (per file) # -w: ignore whitespace # --diff-filter=d: exclude deleted files # --minimal: produce the smallest diff possible From a323e821b2fef9e09af926c28009e0af33d3a773 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:38:59 -0500 Subject: [PATCH 16/17] Refactor ignore arguments in git diff script - Consolidated exclude arguments into IGNORE_ARGS for clarity --- devtools/scripts/git-diff-label-calc.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/devtools/scripts/git-diff-label-calc.sh b/devtools/scripts/git-diff-label-calc.sh index 69563eb895..c89d03b5ef 100755 --- a/devtools/scripts/git-diff-label-calc.sh +++ b/devtools/scripts/git-diff-label-calc.sh @@ -69,8 +69,7 @@ done < <(cat "$IGNORE_PATTERN_FILE" 2>/dev/null) # --diff-filter=d: exclude deleted files # --minimal: produce the smallest diff possible # --merge-base: find the common ancestor of the current branch and the main branch -# $EXCLUDE_GENERATED_FILES_ARGS[@]: exclude generated files -# $EXCLUDE_IGNORED_FILES_ARGS[@]: exclude files that match the patterns in the .labelignore file +# $IGNORE_ARGS[@]: list of `:(exclude)file` patterns to ignore OUTPUT=$( if [ "$DEBUG" = "1" ]; then set -x; fi git diff --ignore-blank-lines --numstat -w --diff-filter=d --minimal --merge-base "origin/$MAIN_BRANCH" -- "${IGNORE_ARGS[@]}" From 908721632fe02246cfdbf424f8aca79d94a1ce18 Mon Sep 17 00:00:00 2001 From: Nathaniel Caza Date: Tue, 3 Sep 2024 16:40:18 -0500 Subject: [PATCH 17/17] Enforce stricter PR labeler checks - Added FAIL_IF_XL environment variable for labeler script execution --- .github/workflows/labeler.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/labeler.yml b/.github/workflows/labeler.yml index b3b0992ae3..09ab98f2ca 100644 --- a/.github/workflows/labeler.yml +++ b/.github/workflows/labeler.yml @@ -17,4 +17,5 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.number }} + FAIL_IF_XL: '1' run: ./devtools/scripts/github-set-pr-label.sh