From 5086ede1611f0d1e0630365df289b7b3568e339a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 04:37:54 +0000 Subject: [PATCH 1/4] Initial plan for issue From 3958110e91eda8cc41e39543dc1dd34cf00bb810 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 04:43:34 +0000 Subject: [PATCH 2/4] Add GitHub Action to label PRs by number of files changed Co-authored-by: DonnieBLT <128622481+DonnieBLT@users.noreply.github.com> --- .github/workflows/add-files-changed-label.yml | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 .github/workflows/add-files-changed-label.yml diff --git a/.github/workflows/add-files-changed-label.yml b/.github/workflows/add-files-changed-label.yml new file mode 100644 index 0000000000..6a142624db --- /dev/null +++ b/.github/workflows/add-files-changed-label.yml @@ -0,0 +1,71 @@ +name: Add Files Changed Label + +on: + pull_request: + types: + - opened + - synchronize + - reopened + +permissions: + pull-requests: write + contents: read + +jobs: + add_files_changed_label: + runs-on: ubuntu-latest + steps: + - name: Add Files Changed Label + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ github.event.pull_request.number }} + REPO_OWNER: ${{ github.repository_owner }} + REPO_NAME: ${{ github.event.repository.name }} + run: | + # Get the number of files changed in the PR + FILES_CHANGED=$(curl -s -X GET \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER/files" | jq '. | length') + + echo "Files changed in PR #$PR_NUMBER: $FILES_CHANGED" + + # Determine the label based on the number of files changed + if [ "$FILES_CHANGED" -eq 1 ]; then + LABEL="files-changed: 1" + elif [ "$FILES_CHANGED" -ge 2 ] && [ "$FILES_CHANGED" -le 5 ]; then + LABEL="files-changed: 2-5" + elif [ "$FILES_CHANGED" -ge 6 ] && [ "$FILES_CHANGED" -le 10 ]; then + LABEL="files-changed: 6-10" + else + LABEL="files-changed: 11+" + fi + + echo "Determined label: $LABEL" + + # First, get all existing labels on the PR + EXISTING_LABELS=$(curl -s -X GET \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels" | jq -r '.[].name') + + # Remove any existing files-changed labels + for EXISTING_LABEL in $EXISTING_LABELS; do + if [[ "$EXISTING_LABEL" == "files-changed:"* ]]; then + echo "Removing existing label: $EXISTING_LABEL" + curl -s -X DELETE \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels/$(echo $EXISTING_LABEL | sed 's/ /%20/g')" + fi + done + + # Add the new label + echo "Adding label: $LABEL" + curl -s -X POST \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels" \ + -d "{\"labels\":[\"$LABEL\"]}" + + echo "Successfully applied label $LABEL to PR #$PR_NUMBER" \ No newline at end of file From 28b9e109a38bad1923c971702c3fd664b3ede4d3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 04:52:09 +0000 Subject: [PATCH 3/4] Enhance PR files changed label action with auto-creation and colors Co-authored-by: DonnieBLT <128622481+DonnieBLT@users.noreply.github.com> --- .github/workflows/add-files-changed-label.yml | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/.github/workflows/add-files-changed-label.yml b/.github/workflows/add-files-changed-label.yml index 6a142624db..d01a788aa9 100644 --- a/.github/workflows/add-files-changed-label.yml +++ b/.github/workflows/add-files-changed-label.yml @@ -33,15 +33,35 @@ jobs: # Determine the label based on the number of files changed if [ "$FILES_CHANGED" -eq 1 ]; then LABEL="files-changed: 1" + LABEL_COLOR="0e8a16" # Green elif [ "$FILES_CHANGED" -ge 2 ] && [ "$FILES_CHANGED" -le 5 ]; then LABEL="files-changed: 2-5" + LABEL_COLOR="fbca04" # Yellow elif [ "$FILES_CHANGED" -ge 6 ] && [ "$FILES_CHANGED" -le 10 ]; then LABEL="files-changed: 6-10" + LABEL_COLOR="ff9800" # Orange else LABEL="files-changed: 11+" + LABEL_COLOR="e74c3c" # Red (using the project's preferred red color) fi - echo "Determined label: $LABEL" + echo "Determined label: $LABEL with color: $LABEL_COLOR" + + # Get all labels in the repository to check if our label exists + ALL_REPO_LABELS=$(curl -s -X GET \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/labels" | jq -r '.[].name') + + # Create the label if it doesn't exist + if ! echo "$ALL_REPO_LABELS" | grep -q "$LABEL"; then + echo "Creating label: $LABEL" + curl -s -X POST \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/labels" \ + -d "{\"name\":\"$LABEL\",\"color\":\"$LABEL_COLOR\",\"description\":\"PR changes $FILES_CHANGED file(s)\"}" + fi # First, get all existing labels on the PR EXISTING_LABELS=$(curl -s -X GET \ From a51fa50558a242454e5f39d6545ba2a400d53241 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 20 May 2025 04:55:15 +0000 Subject: [PATCH 4/4] Add improved error handling and logging to files changed label workflow Co-authored-by: DonnieBLT <128622481+DonnieBLT@users.noreply.github.com> --- .github/workflows/add-files-changed-label.yml | 83 +++++++++++++++---- 1 file changed, 67 insertions(+), 16 deletions(-) diff --git a/.github/workflows/add-files-changed-label.yml b/.github/workflows/add-files-changed-label.yml index d01a788aa9..4ac97ed005 100644 --- a/.github/workflows/add-files-changed-label.yml +++ b/.github/workflows/add-files-changed-label.yml @@ -22,11 +22,22 @@ jobs: REPO_OWNER: ${{ github.repository_owner }} REPO_NAME: ${{ github.event.repository.name }} run: | + echo "Starting Files Changed Label workflow for PR #$PR_NUMBER in $REPO_OWNER/$REPO_NAME" + # Get the number of files changed in the PR - FILES_CHANGED=$(curl -s -X GET \ + echo "Fetching files changed information..." + FILES_API_RESPONSE=$(curl -s -X GET \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ - "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER/files" | jq '. | length') + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER/files") + + # Check if the API request was successful + if [[ "$FILES_API_RESPONSE" == *"message"*"Not Found"* ]]; then + echo "Error: Could not fetch PR files. API response indicates resource not found." + exit 1 + fi + + FILES_CHANGED=$(echo "$FILES_API_RESPONSE" | jq '. | length') echo "Files changed in PR #$PR_NUMBER: $FILES_CHANGED" @@ -34,58 +45,98 @@ jobs: if [ "$FILES_CHANGED" -eq 1 ]; then LABEL="files-changed: 1" LABEL_COLOR="0e8a16" # Green + DESCRIPTION="PR changes 1 file" elif [ "$FILES_CHANGED" -ge 2 ] && [ "$FILES_CHANGED" -le 5 ]; then LABEL="files-changed: 2-5" LABEL_COLOR="fbca04" # Yellow + DESCRIPTION="PR changes 2-5 files" elif [ "$FILES_CHANGED" -ge 6 ] && [ "$FILES_CHANGED" -le 10 ]; then LABEL="files-changed: 6-10" LABEL_COLOR="ff9800" # Orange + DESCRIPTION="PR changes 6-10 files" else LABEL="files-changed: 11+" LABEL_COLOR="e74c3c" # Red (using the project's preferred red color) + DESCRIPTION="PR changes 11+ files" fi echo "Determined label: $LABEL with color: $LABEL_COLOR" # Get all labels in the repository to check if our label exists - ALL_REPO_LABELS=$(curl -s -X GET \ + echo "Checking if label exists in repository..." + ALL_LABELS_RESPONSE=$(curl -s -X GET \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ - "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/labels" | jq -r '.[].name') + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/labels") + + ALL_REPO_LABELS=$(echo "$ALL_LABELS_RESPONSE" | jq -r '.[].name') # Create the label if it doesn't exist if ! echo "$ALL_REPO_LABELS" | grep -q "$LABEL"; then - echo "Creating label: $LABEL" - curl -s -X POST \ + echo "Label '$LABEL' does not exist. Creating it now..." + CREATE_LABEL_RESPONSE=$(curl -s -X POST \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/labels" \ - -d "{\"name\":\"$LABEL\",\"color\":\"$LABEL_COLOR\",\"description\":\"PR changes $FILES_CHANGED file(s)\"}" + -d "{\"name\":\"$LABEL\",\"color\":\"$LABEL_COLOR\",\"description\":\"$DESCRIPTION\"}") + + # Check if label creation was successful + if [[ "$CREATE_LABEL_RESPONSE" == *"message"* ]]; then + echo "Warning: There might be an issue creating the label. Response: $CREATE_LABEL_RESPONSE" + else + echo "Label '$LABEL' created successfully." + fi + else + echo "Label '$LABEL' already exists in the repository." fi # First, get all existing labels on the PR - EXISTING_LABELS=$(curl -s -X GET \ + echo "Getting existing labels on PR #$PR_NUMBER..." + PR_LABELS_RESPONSE=$(curl -s -X GET \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ - "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels" | jq -r '.[].name') + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels") + + EXISTING_LABELS=$(echo "$PR_LABELS_RESPONSE" | jq -r '.[].name') # Remove any existing files-changed labels + echo "Checking for existing 'files-changed' labels to remove..." + FOUND_EXISTING_LABEL=false for EXISTING_LABEL in $EXISTING_LABELS; do if [[ "$EXISTING_LABEL" == "files-changed:"* ]]; then - echo "Removing existing label: $EXISTING_LABEL" - curl -s -X DELETE \ + FOUND_EXISTING_LABEL=true + ENCODED_LABEL=$(echo "$EXISTING_LABEL" | sed 's/ /%20/g') + echo "Removing existing label: $EXISTING_LABEL (encoded as: $ENCODED_LABEL)" + + REMOVE_RESPONSE=$(curl -s -X DELETE \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ - "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels/$(echo $EXISTING_LABEL | sed 's/ /%20/g')" + "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels/$ENCODED_LABEL") + + if [[ "$REMOVE_RESPONSE" == "" ]]; then + echo "Successfully removed label: $EXISTING_LABEL" + else + echo "Warning: There might be an issue removing the label. Response: $REMOVE_RESPONSE" + fi fi done + if [ "$FOUND_EXISTING_LABEL" = false ]; then + echo "No existing 'files-changed' labels found on PR #$PR_NUMBER." + fi + # Add the new label - echo "Adding label: $LABEL" - curl -s -X POST \ + echo "Adding label '$LABEL' to PR #$PR_NUMBER..." + ADD_LABEL_RESPONSE=$(curl -s -X POST \ -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/issues/$PR_NUMBER/labels" \ - -d "{\"labels\":[\"$LABEL\"]}" + -d "{\"labels\":[\"$LABEL\"]}") - echo "Successfully applied label $LABEL to PR #$PR_NUMBER" \ No newline at end of file + # Check if label was added successfully + if [[ "$ADD_LABEL_RESPONSE" == *"message"* ]]; then + echo "Error: Failed to add label. Response: $ADD_LABEL_RESPONSE" + exit 1 + else + echo "Successfully applied label '$LABEL' to PR #$PR_NUMBER" + fi \ No newline at end of file