chore: publish IntegraCI build modules 732ec6c83925
This commit is contained in:
1 parent
7a84930dea
commit
5f8a24d014
133 files changed
+18990
-2
No files matched your search
@@ -0,0 +1,164 @@
|
||||
package main
|
||||
|
||||
// The SonarQube quality gate, as the SERVER evaluated it for this analysis.
|
||||
//
|
||||
// Why this exists. The scanner used to run as `sonar-scanner … || true` with no
|
||||
// gate wait, and nothing read the verdict afterwards. MEASURED 2026-09-15 on an
|
||||
// on-prem tenant: four projects analysed on every build, their gate status sat on
|
||||
// the server (`api/qualitygates/project_status`), and no pipeline, gate or screen
|
||||
// ever looked at it. A build that failed the tenant's own quality gate deployed
|
||||
// exactly like one that passed.
|
||||
//
|
||||
// Why the web API and not the scanner's log. The scanner's console wording is not
|
||||
// a contract and changes between scanner versions; the web API is documented and
|
||||
// is what SonarQube's own CI integrations read. The analysis is processed
|
||||
// asynchronously by the server's Compute Engine, so the verdict is read in two
|
||||
// steps: the CE task named in `report-task.txt` (which the scanner writes into its
|
||||
// working directory) yields the analysis id once it finishes, and the analysis id
|
||||
// yields the gate status of exactly THIS analysis — not whatever the project's
|
||||
// latest one is by the time we ask.
|
||||
//
|
||||
// Nothing here imports the Dagger SDK, so it is unit-testable on its own.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
qualityGatePassed = "passed"
|
||||
qualityGateFailed = "failed"
|
||||
qualityGateUnavailable = "unavailable"
|
||||
)
|
||||
|
||||
// ceTaskIDPattern is the shape of a Compute Engine task id. The id comes out of a
|
||||
// file inside a container that ran the customer's repository, so it is data until
|
||||
// it matches.
|
||||
var ceTaskIDPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`)
|
||||
|
||||
// ceTaskIDFromReport reads `ceTaskId` out of a scanner `report-task.txt`
|
||||
// (`key=value` lines). "" when absent or malformed.
|
||||
func ceTaskIDFromReport(report string) string {
|
||||
for _, line := range strings.Split(report, "\n") {
|
||||
key, value, ok := strings.Cut(strings.TrimSpace(line), "=")
|
||||
if ok && key == "ceTaskId" {
|
||||
value = strings.TrimSpace(value)
|
||||
if ceTaskIDPattern.MatchString(value) {
|
||||
return value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// gateFromProjectStatus maps `projectStatus.status` onto the platform's words.
|
||||
// OK and WARN (older servers) pass; ERROR fails; NONE (no gate) and anything
|
||||
// unknown is unavailable — never "passed".
|
||||
func gateFromProjectStatus(status string) string {
|
||||
switch strings.ToUpper(strings.TrimSpace(status)) {
|
||||
case "OK", "WARN":
|
||||
return qualityGatePassed
|
||||
case "ERROR":
|
||||
return qualityGateFailed
|
||||
default:
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
}
|
||||
|
||||
type sonarGateClient struct {
|
||||
// hostURL is the server the pipeline was rendered with (the SonarQube
|
||||
// connector's address, which a spec override can also set) — never the one a
|
||||
// repository's own sonar-project.properties may name.
|
||||
hostURL string
|
||||
// expectedProjectKey, when set, is the IntegraCI-injected project this
|
||||
// analysis MUST belong to. Without the check the verdict could be read from
|
||||
// any project the token can see.
|
||||
expectedProjectKey string
|
||||
token string
|
||||
http *http.Client
|
||||
poll time.Duration
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (c sonarGateClient) getJSON(ctx context.Context, path string, query url.Values, out any) error {
|
||||
endpoint := strings.TrimRight(c.hostURL, "/") + path + "?" + query.Encode()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// A SonarQube user token is the basic-auth USERNAME with an empty password.
|
||||
req.SetBasicAuth(c.token, "")
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%s answered %d", path, resp.StatusCode)
|
||||
}
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
|
||||
// qualityGate waits for the analysis in `report` to be processed and returns its
|
||||
// gate verdict. Any failure to learn it is `unavailable`, which the deploy gate
|
||||
// does NOT read as a pass or a fail — it is recorded so the gap is visible.
|
||||
func (c sonarGateClient) qualityGate(ctx context.Context, report string) string {
|
||||
taskID := ceTaskIDFromReport(report)
|
||||
if taskID == "" || c.hostURL == "" || c.token == "" {
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
deadline := time.Now().Add(c.timeout)
|
||||
var task struct {
|
||||
Task struct {
|
||||
Status string `json:"status"`
|
||||
AnalysisID string `json:"analysisId"`
|
||||
ComponentKey string `json:"componentKey"`
|
||||
} `json:"task"`
|
||||
}
|
||||
for {
|
||||
if err := c.getJSON(ctx, "/api/ce/task", url.Values{"id": {taskID}}, &task); err != nil {
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
switch task.Task.Status {
|
||||
case "SUCCESS":
|
||||
if c.expectedProjectKey != "" && task.Task.ComponentKey != c.expectedProjectKey {
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
var status struct {
|
||||
ProjectStatus struct {
|
||||
Status string `json:"status"`
|
||||
} `json:"projectStatus"`
|
||||
}
|
||||
if task.Task.AnalysisID == "" {
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
err := c.getJSON(ctx, "/api/qualitygates/project_status",
|
||||
url.Values{"analysisId": {task.Task.AnalysisID}}, &status)
|
||||
if err != nil {
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
return gateFromProjectStatus(status.ProjectStatus.Status)
|
||||
case "FAILED", "CANCELED":
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return qualityGateUnavailable
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return qualityGateUnavailable
|
||||
case <-time.After(c.poll):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// noRedirects keeps the token on the one host it was meant for: a redirect is
|
||||
// answered as-is instead of being followed with the credential attached.
|
||||
func noRedirects(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
||||
Reference in new issue
Block a user