Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Daniel committed May 30, 2024
0 parents commit 3f5d5e0
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Gonar

Gonar is a Go application to check the status of a project's quality gate on SonarQube.

## Installation

To install Gonar, you can use the following steps:

1. Clone this repository:

```
git clone https://github.com/jdnielss/gonar.git
```

2. Build the executable:

```
go build -o gonar main.go
```

## Usage

You can use Gonar to check the status of a project's quality gate on SonarQube. Here's how to use it:

```
./gonar -n <PROJECT_NAME> -h <SONAR_URL> -k <SONAR_LOGIN>
```

- `<PROJECT_NAME>`: The name of the project on SonarQube.
- `<SONAR_URL>`: The URL of your SonarQube instance.
- `<SONAR_LOGIN>`: The login token for SonarQube authentication.

Example:

```
./gonar -n myproject -h https://sonar.example.com -k mytoken123
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module go-nar

go 1.22.3
89 changes: 89 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
)

type Condition struct {
MetricKey string `json:"metricKey"`
ActualValue string `json:"actualValue"`
Comparator string `json:"comparator"`
ErrorThreshold string `json:"errorThreshold"`
Status string `json:"status"`
}

type ProjectStatus struct {
Status string `json:"status"`
Conditions []Condition `json:"conditions"`
}

type Response struct {
ProjectStatus ProjectStatus `json:"projectStatus"`
}

func main() {
// Define command-line arguments
projectName := flag.String("n", "", "CI Project Name")
sonarURL := flag.String("h", "", "Sonar URL")
sonarLogin := flag.String("k", "", "Sonar Login")
flag.Parse()

// Validate arguments
if *projectName == "" || *sonarURL == "" || *sonarLogin == "" {
fmt.Println("Missing required arguments")
flag.Usage()
os.Exit(1)
}

// Construct the project URL
projectURL := fmt.Sprintf("%s/api/qualitygates/project_status?projectKey=%s", *sonarURL, *projectName)

// Make the HTTP request
client := &http.Client{}
req, err := http.NewRequest("GET", projectURL, nil)
if err != nil {
fmt.Println("Error creating request:", err)
os.Exit(1)
}
req.SetBasicAuth(*sonarLogin, "")

resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
os.Exit(1)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
os.Exit(1)
}

// Parse the JSON response
var response Response
if err := json.Unmarshal(body, &response); err != nil {
fmt.Println("Error parsing JSON response:", err)
os.Exit(1)
}

// Check the quality gate status
status := response.ProjectStatus.Status
if status == "OK" {
fmt.Println("✅ Quality Gate passed")
} else {
fmt.Println("⛔️ Quality Gate failed")
for _, condition := range response.ProjectStatus.Conditions {
if condition.Status == "OK" {
fmt.Printf("✅ %s: %s %s %s => OK\n", condition.MetricKey, condition.ActualValue, condition.Comparator, condition.ErrorThreshold)
} else {
fmt.Printf("⛔️ %s: %s %s %s => ERROR\n", condition.MetricKey, condition.ActualValue, condition.Comparator, condition.ErrorThreshold)
}
}
}
}

0 comments on commit 3f5d5e0

Please sign in to comment.