Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
kind: pipeline
name: build

steps:
- name: get tags
image: busybox
commands:
- echo "$(grep Version /drone/src/main.go | head -1 | cut -d'"' -f2),latest" > .tags
- cat .tags

- name: docker
image: plugins/docker
settings:
username:
from_secret: harbor_user
password:
from_secret: harbor_token
repo: drone-sonar-plugin
repo: registry.ver.bmfp.fr/drone-sonar/drone-sonar
registry: registry.ver.bmfp.fr

- name: trivy image
image: registry.ver.bmfp.fr/drone-trivy/drone-trivy
settings:
proxy: http://192.168.20.101:8888
mode: image
image: registry.ver.bmfp.fr/drone-sonar/drone-sonar:latest
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
.vscode
.scannerwork
.idea
drone-sonar
drone-sonar-plugin
16 changes: 7 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
FROM golang:1.13.4-alpine as build
FROM golang:1.24-alpine as build
RUN mkdir -p /go/src/github.com/aosapps/drone-sonar-plugin
WORKDIR /go/src/github.com/aosapps/drone-sonar-plugin
COPY *.go ./
COPY vendor ./vendor/
COPY *.go go.mod go.sum ./
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o drone-sonar

FROM openjdk:11.0.8-jre
FROM eclipse-temurin:17-jre-alpine

ARG SONAR_VERSION=4.5.0.2216
ARG SONAR_VERSION=7.2.0.5079
ARG SONAR_SCANNER_CLI=sonar-scanner-cli-${SONAR_VERSION}
ARG SONAR_SCANNER=sonar-scanner-${SONAR_VERSION}

RUN apt-get update \
&& apt-get install -y nodejs curl \
&& apt-get clean
RUN apk --no-cache --update-cache --update add curl nodejs unzip

COPY --from=build /go/src/github.com/aosapps/drone-sonar-plugin/drone-sonar /bin/
WORKDIR /bin

RUN curl https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/${SONAR_SCANNER_CLI}.zip -so /bin/${SONAR_SCANNER_CLI}.zip
RUN unzip ${SONAR_SCANNER_CLI}.zip \
&& rm ${SONAR_SCANNER_CLI}.zip
&& rm ${SONAR_SCANNER_CLI}.zip \
&& apk del curl unzip

ENV PATH $PATH:/bin/${SONAR_SCANNER}/bin

Expand Down
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Go parameters
BINARY_NAME=drone-sonar
GOCMD=go
GOBUILD=$(GOCMD) build -o $(BINARY_NAME) -v
GOBUILD_STATIC_ENVVARS=CGO_ENABLED=0 GOARCH=amd64
GOBUILD_STATIC_OPTIONS=-a -ldflags '-extldflags "-static"'
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMODTIDY=$(GOCMD) mod tidy

all: get test build
all-static: get test build-static
build:
$(GOBUILD)
strip $(BINARY_NAME)
build-static:
$(GOBUILD_STATIC_ENVVARS) $(GOBUILD) $(GOBUILD_STATIC_OPTIONS)
strip $(BINARY_NAME)
get:
$(GOGET) -u
$(GOMODTIDY)
test:
$(GOTEST) -v ./...
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module drone-sonar-plugin

go 1.21

require github.com/urfave/cli/v2 v2.27.1

require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
186 changes: 98 additions & 88 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,125 +2,135 @@ package main

import (
"fmt"
"github.com/codegangsta/cli"
"os"
)

var build = "1" // build number set at compile time
cli "github.com/urfave/cli/v2"
)

func main() {
app := cli.NewApp()
app.Name = "Drone-Sonar-Plugin"
app.Usage = "Drone plugin to integrate with SonarQube."
app.Action = run
app.Version = fmt.Sprintf("1.0.%s", build)
app.Version = "1.0.1"
app.Flags = []cli.Flag{

cli.StringFlag{
Name: "key",
Usage: "project key",
EnvVar: "DRONE_REPO",
},
cli.StringFlag{
Name: "name",
Usage: "project name",
EnvVar: "DRONE_REPO",
},
cli.StringFlag{
Name: "host",
Usage: "SonarQube host",
EnvVar: "PLUGIN_SONAR_HOST",
},
cli.StringFlag{
Name: "token",
Usage: "SonarQube token",
EnvVar: "PLUGIN_SONAR_TOKEN",
&cli.StringFlag{
Name: "key",
Usage: "project key",
EnvVars: []string{"DRONE_REPO"},
},
&cli.StringFlag{
Name: "name",
Usage: "project name",
EnvVars: []string{"DRONE_REPO"},
},
&cli.StringFlag{
Name: "host",
Usage: "SonarQube host",
EnvVars: []string{"PLUGIN_SONAR_HOST"},
},
&cli.StringFlag{
Name: "token",
Usage: "SonarQube token",
EnvVars: []string{"PLUGIN_SONAR_TOKEN"},
},

// advanced parameters
cli.StringFlag{
Name: "ver",
Usage: "Project version",
EnvVar: "DRONE_BUILD_NUMBER",
},
cli.StringFlag{
Name: "branch",
Usage: "Project branch",
EnvVar: "DRONE_BRANCH",
},
cli.StringFlag{
Name: "timeout",
Usage: "Web request timeout",
Value: "60",
EnvVar: "PLUGIN_TIMEOUT",
},
cli.StringFlag{
Name: "sources",
Usage: "analysis sources",
Value: ".",
EnvVar: "PLUGIN_SOURCES",
},
cli.StringFlag{
Name: "inclusions",
Usage: "code inclusions",
EnvVar: "PLUGIN_INCLUSIONS",
},
cli.StringFlag{
Name: "exclusions",
Usage: "code exclusions",
EnvVar: "PLUGIN_EXCLUSIONS",
},
cli.StringFlag{
Name: "level",
Usage: "log level",
Value: "INFO",
EnvVar: "PLUGIN_LEVEL",
},
cli.StringFlag{
Name: "showProfiling",
Usage: "showProfiling during analysis",
Value: "false",
EnvVar: "PLUGIN_SHOWPROFILING",
},
cli.BoolFlag{
Name: "branchAnalysis",
Usage: "execute branchAnalysis",
EnvVar: "PLUGIN_BRANCHANALYSIS",
},
cli.BoolFlag{
Name: "usingProperties",
Usage: "using sonar-project.properties",
EnvVar: "PLUGIN_USINGPROPERTIES",
&cli.StringFlag{
Name: "ver",
Usage: "Project version",
EnvVars: []string{"DRONE_BUILD_NUMBER"},
},
&cli.StringFlag{
Name: "branch",
Usage: "Project branch",
EnvVars: []string{"DRONE_BRANCH"},
},
&cli.StringFlag{
Name: "timeout",
Usage: "Web request timeout",
Value: "60",
EnvVars: []string{"PLUGIN_TIMEOUT"},
},
&cli.StringFlag{
Name: "sources",
Usage: "analysis sources",
Value: ".",
EnvVars: []string{"PLUGIN_SOURCES"},
},
&cli.StringFlag{
Name: "inclusions",
Usage: "code inclusions",
EnvVars: []string{"PLUGIN_INCLUSIONS"},
},
&cli.StringFlag{
Name: "exclusions",
Usage: "code exclusions",
EnvVars: []string{"PLUGIN_EXCLUSIONS"},
},
&cli.StringFlag{
Name: "level",
Usage: "log level",
Value: "INFO",
EnvVars: []string{"PLUGIN_LEVEL"},
},
&cli.StringFlag{
Name: "showProfiling",
Usage: "showProfiling during analysis",
Value: "false",
EnvVars: []string{"PLUGIN_SHOWPROFILING"},
},
&cli.BoolFlag{
Name: "branchAnalysis",
Usage: "execute branchAnalysis",
EnvVars: []string{"PLUGIN_BRANCHANALYSIS"},
},
&cli.BoolFlag{
Name: "usingProperties",
Usage: "using sonar-project.properties",
EnvVars: []string{"PLUGIN_USINGPROPERTIES"},
},
&cli.BoolFlag{
Name: "trustServerCert",
Usage: "trust sonar server certificate",
EnvVars: []string{"PLUGIN_TRUSTSERVERCERT"},
},
}

app.Run(os.Args)
}

func run(c *cli.Context) {
func run(c *cli.Context) error {
plugin := Plugin{
Config: Config{
Key: c.String("key"),
Name: c.String("name"),
Host: c.String("host"),
Token: c.String("token"),

Version: c.String("ver"),
Branch: c.String("branch"),
Timeout: c.String("timeout"),
Sources: c.String("sources"),
Inclusions: c.String("inclusions"),
Exclusions: c.String("exclusions"),
Level: c.String("level"),
ShowProfiling: c.String("showProfiling"),
BranchAnalysis: c.Bool("branchAnalysis"),
Version: c.String("ver"),
Branch: c.String("branch"),
Timeout: c.String("timeout"),
Sources: c.String("sources"),
Inclusions: c.String("inclusions"),
Exclusions: c.String("exclusions"),
Level: c.String("level"),
ShowProfiling: c.String("showProfiling"),
BranchAnalysis: c.Bool("branchAnalysis"),
UsingProperties: c.Bool("usingProperties"),

TrustServerCert: c.Bool("trustServerCert"),
},
}

if plugin.Config.TrustServerCert {
if err := plugin.TrustServerCert(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
if err := plugin.Exec(); err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
}
Loading