Skip to content

fix: link not getting highlighted #460

fix: link not getting highlighted

fix: link not getting highlighted #460

name: Add Files Changed Label
on:
pull_request:
types:
- opened
- synchronize
- reopened
permissions:
pull-requests: write
contents: read
issues: write
repository-projects: write
jobs:
add_files_changed_label:
runs-on: ubuntu-latest
steps:
- name: Add Files Changed Label
env:
GITHUB_TOKEN: ${{ secrets.CUSTOM_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO_OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
PR_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
BASE_REPO: ${{ github.repository }}
run: |
echo "Starting Files Changed Label workflow for PR #$PR_NUMBER in $REPO_OWNER/$REPO_NAME"
# Check if PR is from a fork (external repository)
if [[ "$PR_HEAD_REPO" != "$BASE_REPO" ]]; then
echo "This PR is from an external repository: $PR_HEAD_REPO"
echo "Skipping label operations for external repository PR to avoid permission issues."
exit 0
fi
# Get the number of files changed in the PR
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")
# Check if the API request was successful
if [[ "$FILES_API_RESPONSE" == *"message"*"Not Found"* ]] || [[ "$FILES_API_RESPONSE" == *"Resource not accessible by integration"* ]]; then
echo "Error: Could not fetch PR files. Response: $FILES_API_RESPONSE"
exit 1
fi
FILES_CHANGED=$(echo "$FILES_API_RESPONSE" | jq '. | length')
echo "Files changed in PR #$PR_NUMBER: $FILES_CHANGED"
# Determine the label based on the exact number of files changed
LABEL="files-changed: $FILES_CHANGED"
# Set color based on the number of files changed
if [ "$FILES_CHANGED" -eq 0 ]; then
LABEL_COLOR="cccccc" # Gray
elif [ "$FILES_CHANGED" -eq 1 ]; then
LABEL_COLOR="0e8a16" # Green
elif [ "$FILES_CHANGED" -ge 2 ] && [ "$FILES_CHANGED" -le 5 ]; then
LABEL_COLOR="fbca04" # Yellow
elif [ "$FILES_CHANGED" -ge 6 ] && [ "$FILES_CHANGED" -le 10 ]; then
LABEL_COLOR="ff9800" # Orange
else
LABEL_COLOR="e74c3c" # Red (using the project's preferred red color)
fi
# Set grammatically correct description
if [ "$FILES_CHANGED" -eq 1 ]; then
DESCRIPTION="PR changes 1 file"
else
DESCRIPTION="PR changes $FILES_CHANGED files"
fi
echo "Determined label: $LABEL with color: $LABEL_COLOR"
# Get all labels in the repository to check if our label exists
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")
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 "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\":\"$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"
# Provide more detailed guidance for permission errors
if [[ "$CREATE_LABEL_RESPONSE" == *"Resource not accessible by integration"* ]]; then
echo "This appears to be a permissions issue with creating labels."
echo "Please check the .github/README.md file for instructions on setting up a CUSTOM_GITHUB_TOKEN with proper permissions."
fi
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
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")
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
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/$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' 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\"]}")
# Check if label was added successfully
if [[ "$ADD_LABEL_RESPONSE" == *"message"* ]]; then
echo "Error: Failed to add label. Response: $ADD_LABEL_RESPONSE"
# Check if it's a permissions issue and suggest using a custom token
if [[ "$ADD_LABEL_RESPONSE" == *"Resource not accessible by integration"* ]]; then
echo "This appears to be a permissions issue. Please follow these steps:"
echo "1. Create a Personal Access Token (PAT) with 'repo' scope"
echo "2. Add the token to your repository secrets as CUSTOM_GITHUB_TOKEN"
echo "3. See the .github/README.md file for detailed instructions on setting up the token"
echo ""
echo "Note: The workflow is configured to use CUSTOM_GITHUB_TOKEN if available, falling back to GITHUB_TOKEN"
fi
exit 1
else
echo "Successfully applied label '$LABEL' to PR #$PR_NUMBER"
fi