Skip to content

Commit fa0ec06

Browse files
authored
Improve API (#4)
1 parent 0ec6924 commit fa0ec06

File tree

13 files changed

+166
-41
lines changed

13 files changed

+166
-41
lines changed

api.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package danger
2+
3+
type T struct {
4+
Results Results
5+
}
6+
7+
func New() *T {
8+
return &T{
9+
Results: Results{
10+
Fails: []Violation{},
11+
Messages: []Violation{},
12+
Warnings: []Violation{},
13+
Markdowns: []Violation{},
14+
},
15+
}
16+
}
17+
18+
// Message adds the message to the Danger table. The only difference between
19+
// this and Warn is the emoji which shows in the table.
20+
func (s *T) Message(message string, file string, line int) {
21+
s.Results.Messages = append(s.Results.Messages,
22+
Violation{
23+
Message: message,
24+
File: file,
25+
Line: line,
26+
})
27+
}
28+
29+
// Warn adds the message to the Danger table. The message highlights
30+
// low-priority issues, but does not fail the build.
31+
func (s *T) Warn(message string, file string, line int) {
32+
s.Results.Warnings = append(s.Results.Warnings,
33+
Violation{
34+
Message: message,
35+
File: file,
36+
Line: line,
37+
})
38+
}
39+
40+
// Fail a build, outputting a specific reason for failing into an HTML table.
41+
func (s *T) Fail(message string, file string, line int) {
42+
s.Results.Fails = append(s.Results.Fails,
43+
Violation{
44+
Message: message,
45+
File: file,
46+
Line: line,
47+
})
48+
}
49+
50+
// Markdown adds the message as raw markdown into the Danger comment, under the
51+
// table.
52+
func (s *T) Markdown(message string, file string, line int) {
53+
s.Results.Markdowns = append(s.Results.Markdowns,
54+
Violation{
55+
Message: message,
56+
File: file,
57+
Line: line,
58+
})
59+
}

api_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package danger_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/moolmanruan/danger-go"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestMessage(t *testing.T) {
11+
d := danger.New()
12+
13+
d.Message("a message", "", 0)
14+
15+
require.Equal(t,
16+
[]danger.Violation{
17+
{Message: "a message"},
18+
},
19+
d.Results.Messages)
20+
}
21+
22+
func TestWarn(t *testing.T) {
23+
d := danger.New()
24+
25+
d.Warn("a warning", "", 0)
26+
27+
require.Equal(t,
28+
[]danger.Violation{
29+
{Message: "a warning"},
30+
},
31+
d.Results.Warnings)
32+
}
33+
34+
func TestFail(t *testing.T) {
35+
d := danger.New()
36+
d.Fail("a failure", "", 0)
37+
38+
require.Equal(t,
39+
[]danger.Violation{
40+
{Message: "a failure"},
41+
},
42+
d.Results.Fails)
43+
}
44+
45+
func TestMarkdown(t *testing.T) {
46+
d := danger.New()
47+
48+
d.Markdown("some markdown", "", 0)
49+
50+
require.Equal(t,
51+
[]danger.Violation{
52+
{Message: "some markdown"},
53+
},
54+
d.Results.Markdowns)
55+
}

build/ci/dangerfile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
56
"github.com/moolmanruan/danger-go/cmd/danger-go/runner"
67
danger_js "github.com/moolmanruan/danger-go/danger-js"
78
)

build/ci/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module danger-go/dangerfile
2+
3+
go 1.19
4+
5+
require github.com/moolmanruan/danger-go v0.0.2

build/ci/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/moolmanruan/danger-go v0.0.2 h1:BGoqAgholwSWdZqzmxLjWSBLtLoDk+//L6FdpVUVcJ4=
2+
github.com/moolmanruan/danger-go v0.0.2/go.mod h1:JIzQHXs5iGbWszdpQfwuEiVPd0Ees93yjzDqBUMQl6c=

cmd/danger-go/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/moolmanruan/danger-go/cmd/danger-go/runner"
6-
danger_js "github.com/moolmanruan/danger-go/danger-js"
75
"log"
86
"os"
7+
8+
"github.com/moolmanruan/danger-go/cmd/danger-go/runner"
9+
dangerJs "github.com/moolmanruan/danger-go/danger-js"
910
)
1011

11-
const version = "v0.0.2"
12+
const version = "v0.0.3"
1213

1314
// main entrypoint of the danger-go command
1415
func main() {
@@ -33,7 +34,7 @@ func main() {
3334
if len(os.Args) > 2 {
3435
rest = os.Args[2:]
3536
}
36-
err := danger_js.Process(command, rest)
37+
err := dangerJs.Process(command, rest)
3738
if err != nil {
3839
log.Fatalf(err.Error())
3940
}
@@ -64,8 +65,8 @@ Options:
6465
-h, --help Output usage information
6566
6667
Commands:
67-
init Helps you get started with Danger
68-
ci Runs Danger on CI
69-
pr Runs your local Dangerfile against an existing GitHub PR. Will not post on the PR
68+
init Helps you get started with DSL
69+
ci Runs DSL on CI
70+
pr Runs your local Dangerfile against an existing GitHub DSL. Will not post on the DSL
7071
runner Runs a dangerfile against a DSL passed in via STDIN [You probably don't need this]
7172
local Runs danger standalone on a repo, useful for git hooks`

cmd/danger-go/runner/main.go renamed to cmd/danger-go/runner/runner.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
danger_js "github.com/moolmanruan/danger-go/danger-js"
1110
"io"
1211
"log"
1312
"os"
1413
"os/exec"
1514
"path/filepath"
1615
"plugin"
1716
"strings"
17+
18+
"github.com/moolmanruan/danger-go"
19+
"github.com/moolmanruan/danger-go/danger-js"
1820
)
1921

2022
const dangerURLPrefix = "danger://dsl/"
@@ -26,21 +28,22 @@ func Run() {
2628
in = strings.TrimSpace(in)
2729

2830
if !strings.HasPrefix(in, dangerURLPrefix) {
29-
log.Fatalf("did not receive a Danger URL")
31+
log.Fatalf("did not receive a DSL URL")
3032
}
3133

3234
jsonPath := strings.Replace(in, dangerURLPrefix, "", 1)
33-
prJSON, err := os.ReadFile(jsonPath)
35+
jsonBytes, err := os.ReadFile(jsonPath)
3436
if err != nil {
3537
log.Fatalf("failed to read JSON file at %s", jsonPath)
3638
}
3739

38-
//TODO: Figure out why ci doesn't work, but pr and local does?
39-
var pr danger_js.PR
40-
err = json.Unmarshal(prJSON, &pr)
40+
var jsonData struct {
41+
Danger dangerJs.DSL `json:"danger"`
42+
}
43+
err = json.Unmarshal(jsonBytes, &jsonData)
4144
if err != nil {
42-
fmt.Println("JSON\n", string(prJSON))
43-
log.Fatalf("failed to unmarshal PR JSON: %s", err.Error())
45+
fmt.Println("JSON\n", string(jsonBytes))
46+
log.Fatalf("failed to unmarshal DSL JSON: %s", err.Error())
4447
}
4548

4649
dangerFile := "dangerfile.go"
@@ -58,8 +61,9 @@ func Run() {
5861
log.Fatalf("loading dangerfile plugin: %s", err.Error())
5962
}
6063

61-
resp := fn(pr)
62-
respJSON, err := json.Marshal(resp)
64+
d := danger.New()
65+
fn(d, jsonData.Danger)
66+
respJSON, err := json.Marshal(d.Results)
6367
if err != nil {
6468
log.Fatalf("marshalling response: %s", err.Error())
6569
}
@@ -82,7 +86,7 @@ func buildPlugin(dangerFilePath string) (string, func() error, error) {
8286
if err != nil {
8387
return "", nil, fmt.Errorf("creating temp directory: %w", err)
8488
}
85-
var clearTempDir = func() error {
89+
clearTempDir := func() error {
8690
return os.RemoveAll(tempDir)
8791
}
8892

@@ -101,7 +105,7 @@ func buildPlugin(dangerFilePath string) (string, func() error, error) {
101105
return outputFile, clearTempDir, nil
102106
}
103107

104-
type MainFunc = func(pr danger_js.PR) DangerResults
108+
type MainFunc = func(d *danger.T, pr dangerJs.DSL)
105109

106110
func loadPlugin(libPath string) (MainFunc, error) {
107111
fmt.Println("Loading dangerfile plugin:", libPath)
@@ -118,7 +122,7 @@ func loadPlugin(libPath string) (MainFunc, error) {
118122

119123
dangerFn, ok := dangerSymbol.(MainFunc)
120124
if !ok {
121-
return nil, errors.New("failed to cast Danger function")
125+
return nil, errors.New("failed to cast DSL function")
122126
}
123127

124128
return dangerFn, nil

danger-js/danger-js.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package danger_js
1+
package dangerJs
22

33
import (
44
"encoding/json"
@@ -20,24 +20,24 @@ func findBinary(name string) (string, error) {
2020
return strings.TrimSpace(string(dangerBin)), nil
2121
}
2222

23-
func GetPR(url string, dangerBin string) (PR, error) {
23+
func GetPR(url string, dangerBin string) (DSL, error) {
2424
var err error
2525
if dangerBin == "" {
2626
dangerBin, err = findBinary(dangerJsBinary)
2727
if err != nil {
28-
return PR{}, err
28+
return DSL{}, err
2929
}
3030
}
3131

3232
cmd := exec.Command(dangerBin, "pr", url, "--json")
3333
prJSON, err := cmd.CombinedOutput()
3434
if err != nil {
35-
return PR{}, fmt.Errorf("could not download PR JSON with danger-js: %w", err)
35+
return DSL{}, fmt.Errorf("could not download DSL JSON with danger-js: %w", err)
3636
}
3737

38-
var pr PR
38+
var pr DSL
3939
if err = json.Unmarshal([]byte(prJSON), &pr); err != nil {
40-
return PR{}, err
40+
return DSL{}, err
4141
}
4242
return pr, nil
4343
}

danger-js/types_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package danger_js
1+
package dangerJs
22

33
type GitCommit struct {
44
SHA string `json:"sha"`

danger-js/types_danger.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package danger_js
1+
package dangerJs
22

3-
type PR struct {
4-
Danger struct {
5-
Git Git `json:"git"`
6-
GitHub GitHub `json:"github,omitempty"`
7-
GitLab GitLab `json:"gitlab,omitempty"`
8-
// TODO: bitbucket_server
9-
// TODO: bitbucket_cloud
10-
Settings Settings `json:"settings"`
11-
} `json:"danger"`
3+
type DSL struct {
4+
Git Git `json:"git"`
5+
GitHub GitHub `json:"github,omitempty"`
6+
GitLab GitLab `json:"gitlab,omitempty"`
7+
// TODO: bitbucket_server
8+
// TODO: bitbucket_cloud
9+
Settings Settings `json:"settings"`
1210
}
1311

1412
type FilePath = string

0 commit comments

Comments
 (0)