Skip to content

Commit

Permalink
Improve API (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
moolmanruan authored Jan 13, 2023
1 parent 0ec6924 commit fa0ec06
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 41 deletions.
59 changes: 59 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package danger

type T struct {
Results Results
}

func New() *T {
return &T{
Results: Results{
Fails: []Violation{},
Messages: []Violation{},
Warnings: []Violation{},
Markdowns: []Violation{},
},
}
}

// Message adds the message to the Danger table. The only difference between
// this and Warn is the emoji which shows in the table.
func (s *T) Message(message string, file string, line int) {
s.Results.Messages = append(s.Results.Messages,
Violation{
Message: message,
File: file,
Line: line,
})
}

// Warn adds the message to the Danger table. The message highlights
// low-priority issues, but does not fail the build.
func (s *T) Warn(message string, file string, line int) {
s.Results.Warnings = append(s.Results.Warnings,
Violation{
Message: message,
File: file,
Line: line,
})
}

// Fail a build, outputting a specific reason for failing into an HTML table.
func (s *T) Fail(message string, file string, line int) {
s.Results.Fails = append(s.Results.Fails,
Violation{
Message: message,
File: file,
Line: line,
})
}

// Markdown adds the message as raw markdown into the Danger comment, under the
// table.
func (s *T) Markdown(message string, file string, line int) {
s.Results.Markdowns = append(s.Results.Markdowns,
Violation{
Message: message,
File: file,
Line: line,
})
}
55 changes: 55 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package danger_test

import (
"testing"

"github.com/moolmanruan/danger-go"
"github.com/stretchr/testify/require"
)

func TestMessage(t *testing.T) {
d := danger.New()

d.Message("a message", "", 0)

require.Equal(t,
[]danger.Violation{
{Message: "a message"},
},
d.Results.Messages)
}

func TestWarn(t *testing.T) {
d := danger.New()

d.Warn("a warning", "", 0)

require.Equal(t,
[]danger.Violation{
{Message: "a warning"},
},
d.Results.Warnings)
}

func TestFail(t *testing.T) {
d := danger.New()
d.Fail("a failure", "", 0)

require.Equal(t,
[]danger.Violation{
{Message: "a failure"},
},
d.Results.Fails)
}

func TestMarkdown(t *testing.T) {
d := danger.New()

d.Markdown("some markdown", "", 0)

require.Equal(t,
[]danger.Violation{
{Message: "some markdown"},
},
d.Results.Markdowns)
}
1 change: 1 addition & 0 deletions build/ci/dangerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/moolmanruan/danger-go/cmd/danger-go/runner"
danger_js "github.com/moolmanruan/danger-go/danger-js"
)
Expand Down
5 changes: 5 additions & 0 deletions build/ci/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module danger-go/dangerfile

go 1.19

require github.com/moolmanruan/danger-go v0.0.2
2 changes: 2 additions & 0 deletions build/ci/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/moolmanruan/danger-go v0.0.2 h1:BGoqAgholwSWdZqzmxLjWSBLtLoDk+//L6FdpVUVcJ4=
github.com/moolmanruan/danger-go v0.0.2/go.mod h1:JIzQHXs5iGbWszdpQfwuEiVPd0Ees93yjzDqBUMQl6c=
15 changes: 8 additions & 7 deletions cmd/danger-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
"github.com/moolmanruan/danger-go/cmd/danger-go/runner"
danger_js "github.com/moolmanruan/danger-go/danger-js"
"log"
"os"

"github.com/moolmanruan/danger-go/cmd/danger-go/runner"
dangerJs "github.com/moolmanruan/danger-go/danger-js"
)

const version = "v0.0.2"
const version = "v0.0.3"

// main entrypoint of the danger-go command
func main() {
Expand All @@ -33,7 +34,7 @@ func main() {
if len(os.Args) > 2 {
rest = os.Args[2:]
}
err := danger_js.Process(command, rest)
err := dangerJs.Process(command, rest)
if err != nil {
log.Fatalf(err.Error())
}
Expand Down Expand Up @@ -64,8 +65,8 @@ Options:
-h, --help Output usage information
Commands:
init Helps you get started with Danger
ci Runs Danger on CI
pr Runs your local Dangerfile against an existing GitHub PR. Will not post on the PR
init Helps you get started with DSL
ci Runs DSL on CI
pr Runs your local Dangerfile against an existing GitHub DSL. Will not post on the DSL
runner Runs a dangerfile against a DSL passed in via STDIN [You probably don't need this]
local Runs danger standalone on a repo, useful for git hooks`
30 changes: 17 additions & 13 deletions cmd/danger-go/runner/main.go → cmd/danger-go/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"encoding/json"
"errors"
"fmt"
danger_js "github.com/moolmanruan/danger-go/danger-js"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"plugin"
"strings"

"github.com/moolmanruan/danger-go"
"github.com/moolmanruan/danger-go/danger-js"
)

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

if !strings.HasPrefix(in, dangerURLPrefix) {
log.Fatalf("did not receive a Danger URL")
log.Fatalf("did not receive a DSL URL")
}

jsonPath := strings.Replace(in, dangerURLPrefix, "", 1)
prJSON, err := os.ReadFile(jsonPath)
jsonBytes, err := os.ReadFile(jsonPath)
if err != nil {
log.Fatalf("failed to read JSON file at %s", jsonPath)
}

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

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

resp := fn(pr)
respJSON, err := json.Marshal(resp)
d := danger.New()
fn(d, jsonData.Danger)
respJSON, err := json.Marshal(d.Results)
if err != nil {
log.Fatalf("marshalling response: %s", err.Error())
}
Expand All @@ -82,7 +86,7 @@ func buildPlugin(dangerFilePath string) (string, func() error, error) {
if err != nil {
return "", nil, fmt.Errorf("creating temp directory: %w", err)
}
var clearTempDir = func() error {
clearTempDir := func() error {
return os.RemoveAll(tempDir)
}

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

type MainFunc = func(pr danger_js.PR) DangerResults
type MainFunc = func(d *danger.T, pr dangerJs.DSL)

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

dangerFn, ok := dangerSymbol.(MainFunc)
if !ok {
return nil, errors.New("failed to cast Danger function")
return nil, errors.New("failed to cast DSL function")
}

return dangerFn, nil
Expand Down
12 changes: 6 additions & 6 deletions danger-js/danger-js.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package danger_js
package dangerJs

import (
"encoding/json"
Expand All @@ -20,24 +20,24 @@ func findBinary(name string) (string, error) {
return strings.TrimSpace(string(dangerBin)), nil
}

func GetPR(url string, dangerBin string) (PR, error) {
func GetPR(url string, dangerBin string) (DSL, error) {
var err error
if dangerBin == "" {
dangerBin, err = findBinary(dangerJsBinary)
if err != nil {
return PR{}, err
return DSL{}, err
}
}

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

var pr PR
var pr DSL
if err = json.Unmarshal([]byte(prJSON), &pr); err != nil {
return PR{}, err
return DSL{}, err
}
return pr, nil
}
Expand Down
2 changes: 1 addition & 1 deletion danger-js/types_commit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package danger_js
package dangerJs

type GitCommit struct {
SHA string `json:"sha"`
Expand Down
18 changes: 8 additions & 10 deletions danger-js/types_danger.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package danger_js
package dangerJs

type PR struct {
Danger struct {
Git Git `json:"git"`
GitHub GitHub `json:"github,omitempty"`
GitLab GitLab `json:"gitlab,omitempty"`
// TODO: bitbucket_server
// TODO: bitbucket_cloud
Settings Settings `json:"settings"`
} `json:"danger"`
type DSL struct {
Git Git `json:"git"`
GitHub GitHub `json:"github,omitempty"`
GitLab GitLab `json:"gitlab,omitempty"`
// TODO: bitbucket_server
// TODO: bitbucket_cloud
Settings Settings `json:"settings"`
}

type FilePath = string
Expand Down
2 changes: 1 addition & 1 deletion danger-js/types_github.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package danger_js
package dangerJs

import "time"

Expand Down
2 changes: 1 addition & 1 deletion danger-js/types_gitlab.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package danger_js
package dangerJs

import "time"

Expand Down
4 changes: 2 additions & 2 deletions cmd/danger-go/runner/types.go → types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package runner
package danger

type DangerResults struct {
type Results struct {
Fails []Violation `json:"fails"`
Warnings []Violation `json:"warnings"`
Messages []Violation `json:"messages"`
Expand Down

0 comments on commit fa0ec06

Please sign in to comment.