From a267fc874440442fce4298c97559b48f55e7ff19 Mon Sep 17 00:00:00 2001 From: lddsb Date: Sun, 10 Mar 2019 13:50:59 +0800 Subject: [PATCH] start on the road --- .drone.yml | 36 ++++++++++++++++++++++++++++++++++++ .gitignore | 1 + Dockerfile | 10 ++++++++++ README.md | 16 ++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ plugin.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 162 insertions(+) create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 plugin.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..7cdd689 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,36 @@ +--- +kind: pipeline +name: build + +steps: + - name: build + pull: always + image: golang + commands: + - go build -a -o drone-goreportcard . + environment: + - GO111MODULE=on + - CGO_ENABLED=0 + + - name: dryrun + pull: always + image: plugins/docker + settings: + cache_from: lddsb/drone-goreportcard + dockerfile: Dockerfile + dryrun: true + repo: lddsb/drone-goreportcard + tags: latest + + - name: publish + pull: always + image: plugins/docker + settings: + cache_from: lddsb/drone-goreportcard + dockerfile: Dockerfile + repo: lddsb/drone-goreportcard + tags: latest + username: + from_secret: docker_username + password: + from_secret: docker_password diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5e339b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +drone-goreportcard \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f311eab --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM alpine:latest + +RUN apk update && \ + apk add \ + ca-certificates && \ + rm -rf /var/cache/apk/* + +COPY drone-goreportcard /bin + +ENTRYPOINT ["/bin/drone-goreportcard"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..e57b015 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# Go Report Card Drone Plugin +[![Build Status](https://drone.lddsb.com/api/badges/lddsb/drone-goreportcard/status.svg)](https://drone.lddsb.com/lddsb/drone-goreportcard) +[Drone CI](https://drone.io) plugin to auto refresh your project status in [go report card](https://goreportcard.com) +### Usage + +```yaml +kind: pipeline +name: default + +steps: +... +- name: goreportcard + pull: always + image: lddsb/drone-goreportcard + +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a27c638 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module drone-goreportcard + +go 1.12 + +require github.com/urfave/cli v1.20.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..03e08fc --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= +github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= diff --git a/main.go b/main.go new file mode 100644 index 0000000..98c8e2d --- /dev/null +++ b/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "log" + "os" + + "github.com/urfave/cli" +) + +// Version `cli version` +var Version = "20190309" + +func main() { + app := cli.NewApp() + app.Name = "Drone Plugin GoReportCard" + app.Usage = "fresh go report card message" + app.Copyright = "© 2019 Dee Luo" + app.Authors = []cli.Author{ + { + Name: "Dee Luo", + Email: "luodi0128@gmail.com", + }, + } + app.Action = run + app.Version = Version + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "config.url", + Usage: "repository http url", + EnvVar: "DRONE_GIT_HTTP_URL", + }, + } + + if err := app.Run(os.Args); nil != err { + log.Fatal(err) + } +} + +func run(c *cli.Context) error { + p := Plugin{ + URL: c.String("config.url"), + } + return p.Exec() +} diff --git a/plugin.go b/plugin.go new file mode 100644 index 0000000..5b400c9 --- /dev/null +++ b/plugin.go @@ -0,0 +1,48 @@ +package main + +import ( + "errors" + "io/ioutil" + "log" + "net/http" +) + +type ( + // Plugin `plugin struct` + Plugin struct { + URL string + } +) + +var baseURL = "https://goreportcard.com/" + +// Exec `exec plugin` +func (p *Plugin) Exec() error { + urlLen := len(p.URL) + if 4 > urlLen { + return errors.New("invalid repository url") + } + + suffix := p.URL[urlLen-4:] + if ".git" == suffix { + p.URL = p.URL[:urlLen-4] + } + resp, err := http.Get(baseURL + "checks?repo=" + p.URL) + if nil != err { + return err + } + + defer resp.Body.Close() + + if http.StatusOK != resp.StatusCode { + bs, err := ioutil.ReadAll(resp.Body) + if nil == err { + return errors.New("API response error: " + string(bs)) + } + + return err + } + + log.Println("Everything is OK") + return err +}