|
| 1 | +# Configuration |
| 2 | +SONAR_HOST_URL="https://sonarqube-developers.rootstrap.net" |
| 3 | +SONAR_API_TOKEN="squ_21cf7e64993c3df2b83511539c16429a28ec1d17" |
| 4 | +#!/bin/bash |
| 5 | + |
| 6 | +#!/bin/bash |
| 7 | + |
| 8 | +# Check if fzf is installed, and install it if it's missing |
| 9 | +if ! command -v fzf &>/dev/null; then |
| 10 | + echo "fzf is not installed. Installing fzf via Homebrew..." |
| 11 | + |
| 12 | + # Check if Homebrew is installed |
| 13 | + if command -v brew &>/dev/null; then |
| 14 | + brew install fzf |
| 15 | + else |
| 16 | + echo "Homebrew is not installed. Please install Homebrew f irst: https://brew.sh/" |
| 17 | + exit 1 |
| 18 | + fi |
| 19 | +fi |
| 20 | + |
| 21 | +### Step 1: Retrieve the project name ### |
| 22 | +echo "" |
| 23 | +read -p "Please enter the project name: " project_name |
| 24 | + |
| 25 | +# Confirm the entered project name |
| 26 | +if [[ -z "$project_name" ]]; then |
| 27 | + echo "No project name provided. Exiting." |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +echo "You have entered project name: $project_name" |
| 32 | + |
| 33 | +### Step 2: Retrieve and select Quality Profile ### |
| 34 | +# Fetch the list of quality profiles |
| 35 | +quality_profiles_response=$(curl -s -X GET "$SONAR_HOST_URL/api/qualityprofiles/search" \ |
| 36 | + -H "Authorization: Bearer $SONAR_API_TOKEN") |
| 37 | + |
| 38 | +# Check if the response contains any profiles |
| 39 | +if ! echo "$quality_profiles_response" | grep -q '"profiles"'; then |
| 40 | + echo "Failed to retrieve quality profiles. Response:" |
| 41 | + echo "$quality_profiles_response" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +# Extract and display the names of the quality profiles using jq and fzf for selection |
| 46 | +selected_quality_profile=$(echo "$quality_profiles_response" | jq -r '.profiles[] | "\(.languageName) (\(.name))"' | fzf --prompt="Select a quality profile: ") |
| 47 | + |
| 48 | +# Check if a selection was made |
| 49 | +if [[ -z "$selected_quality_profile" ]]; then |
| 50 | + echo "No selection made. Exiting." |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | + |
| 54 | +# Extract both key and language in a single query based on the fzf selection |
| 55 | +profile_info=$(echo "$quality_profiles_response" | jq -r --arg selected "$selected_quality_profile" '.profiles[] | select("\(.languageName) (\(.name))" == $selected) | {key, language, name}') |
| 56 | + |
| 57 | +# Extract key and language from the JSON result |
| 58 | +selected_quality_profile_name=$(echo "$profile_info" | jq -r '.name') |
| 59 | +selected_quality_profile_language=$(echo "$profile_info" | jq -r '.language') |
| 60 | + |
| 61 | +# Display the selected quality profile |
| 62 | +echo "You have selected Quality Profile: $selected_quality_profile" |
| 63 | + |
| 64 | +### Step 3: Retrieve and select Quality Gate ### |
| 65 | +# Fetch the list of quality gates |
| 66 | +quality_gates_response=$(curl -s -X GET "$SONAR_HOST_URL/api/qualitygates/list" \ |
| 67 | + -H "Authorization: Bearer $SONAR_API_TOKEN") |
| 68 | + |
| 69 | +# Check if the response contains any quality gates |
| 70 | +if ! echo "$quality_gates_response" | grep -q '"qualitygates"'; then |
| 71 | + echo "Failed to retrieve quality gates. Response:" |
| 72 | + echo "$quality_gates_response" |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +# Extract and display the names of the quality gates using jq and fzf for selection |
| 77 | +selected_quality_gate=$(echo "$quality_gates_response" | jq -r '.qualitygates[].name' | fzf --prompt="Select a quality gate: ") |
| 78 | + |
| 79 | +# Check if a selection was made |
| 80 | +if [[ -z "$selected_quality_gate" ]]; then |
| 81 | + echo "No selection made. Exiting." |
| 82 | + exit 1 |
| 83 | +fi |
| 84 | + |
| 85 | +# Extract the ID of the selected quality gate for API usage |
| 86 | +selected_quality_gate_name=$(echo "$quality_gates_response" | jq -r --arg selected "$selected_quality_gate" '.qualitygates[] | select(.name == $selected) | .name') |
| 87 | + |
| 88 | +# Display the selected quality gate |
| 89 | +echo "You have selected Quality Gate: $selected_quality_gate" |
| 90 | + |
| 91 | +### Step 4: Show summary of selected fields ### |
| 92 | +echo "" |
| 93 | +echo "########################################" |
| 94 | +echo "Project Name: $project_name" |
| 95 | +echo "Quality Profile: $selected_quality_profile" |
| 96 | +echo "Quality Profile Language: $selected_quality_profile_language" |
| 97 | +echo "Quality Gate: $selected_quality_gate" |
| 98 | +echo "########################################" |
| 99 | +echo "" |
| 100 | + |
| 101 | +# Confirm choices |
| 102 | +read -p "Are you sure you want to proceed with these selections? (y/n): " confirmation |
| 103 | + |
| 104 | +if [[ "$confirmation" != "y" ]]; then |
| 105 | + echo "Project creation canceled. Exiting." |
| 106 | + exit 1 |
| 107 | +fi |
| 108 | + |
| 109 | +### Step 5: Create the project ### |
| 110 | +create_project_response=$(curl -s -X POST "$SONAR_HOST_URL/api/projects/create" \ |
| 111 | + -H "Authorization: Bearer $SONAR_API_TOKEN" \ |
| 112 | + -d "name=$project_name&project=$project_name") |
| 113 | + |
| 114 | +if echo "$create_project_response" | grep -q '"errors"'; then |
| 115 | + echo "Failed to create project. Response:" |
| 116 | + echo "$create_project_response" |
| 117 | + exit 1 |
| 118 | +else |
| 119 | + echo "Project $project_name created successfully." |
| 120 | +fi |
| 121 | + |
| 122 | +### Step 6: Associate the Quality Profile with the project ### |
| 123 | +assign_quality_profile_response=$(curl -s -X POST "$SONAR_HOST_URL/api/qualityprofiles/add_project" \ |
| 124 | + -H "Authorization: Bearer $SONAR_API_TOKEN" \ |
| 125 | + -d "project=$project_name&qualityProfile=$selected_quality_profile_name&language=$selected_quality_profile_language") |
| 126 | + |
| 127 | +if echo "$assign_quality_profile_response" | grep -q '"errors"'; then |
| 128 | + echo "Failed to assign quality profile. Response:" |
| 129 | + echo "$assign_quality_profile_response" |
| 130 | + exit 1 |
| 131 | +else |
| 132 | + echo "Quality Profile $selected_quality_profile (Language: $selected_quality_profile_language) assigned to project $project_name successfully." |
| 133 | +fi |
| 134 | + |
| 135 | +### Step 7: Assign the Quality Gate to the project ### |
| 136 | +assign_quality_gate_response=$(curl -s -X POST "$SONAR_HOST_URL/api/qualitygates/select" \ |
| 137 | + -H "Authorization: Bearer $SONAR_API_TOKEN" \ |
| 138 | + -d "projectKey=$project_name&gateName=$selected_quality_gate_name") |
| 139 | + |
| 140 | +if echo "$assign_quality_gate_response" | grep -q '"errors"'; then |
| 141 | + echo "Failed to assign quality gate. Response:" |
| 142 | + echo "$assign_quality_gate_response" |
| 143 | + exit 1 |
| 144 | +else |
| 145 | + echo "Quality Gate $selected_quality_gate assigned to project $project_name successfully." |
| 146 | +fi |
| 147 | + |
| 148 | +### Final Confirmation Echo ### |
| 149 | +echo "" |
| 150 | +echo "########################################" |
| 151 | +echo "SUCCESS!" |
| 152 | +echo "Project '$project_name' was created successfully." |
| 153 | +echo "Quality Profile '$selected_quality_profile' (Language: $selected_quality_profile_language) and Quality Gate '$selected_quality_gate' have been assigned to the project." |
| 154 | +echo "########################################" |
| 155 | +echo "" |
0 commit comments