/
codecov.go
51 lines (42 loc) · 1.03 KB
/
codecov.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package adapter
import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/tidwall/gjson"
"github.com/zcong1993/badge-service/utils"
)
var (
codecovUnknown = BadgeInput{
Subject: "codecov",
Status: "unknown",
Color: "grey",
}
codecovTypes = []string{"github", "bitbucket", "gitlab"}
)
// CodecovApi is codecov api provider
func CodecovApi(args ...string) BadgeInput {
if len(args) != 4 {
return ErrorInput
}
vcsType := args[0]
username := args[1]
project := args[2]
branch := args[3]
endpoint := fmt.Sprintf("https://codecov.io/api/%s/%s/%s", vcsType, username, project)
if branch != "" {
endpoint += fmt.Sprintf("/branch/%s", branch)
}
resp, err := utils.Get(endpoint)
if err != nil {
return ErrorInput
}
if gjson.Get(string(resp), "error").String() != "" {
return codecovUnknown
}
percent := gjson.Get(string(resp), "commit.totals.c").Float()
return BadgeInput{
Subject: "codecov",
Status: utils.StringOrDefault(humanize.FtoaWithDigits(percent, 2), "0") + "%",
Color: utils.CovColor(percent),
}
}