Skip to content

Commit f559866

Browse files
authored
feat(cli): adds pr event (#177)
1 parent 191fd48 commit f559866

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

cli/pkg/events/events.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"cuelang.org/go/cue"
77
"github.com/input-output-hk/catalyst-forge/lib/project/project"
8+
"github.com/input-output-hk/catalyst-forge/lib/providers/github"
89
)
910

1011
//go:generate go run github.com/matryer/moq@latest -pkg mocks -out mocks/handler.go . EventHandler
@@ -15,6 +16,7 @@ type EventType string
1516
const (
1617
AlwaysEventName EventType = "always"
1718
MergeEventName EventType = "merge"
19+
PREventName EventType = "pr"
1820
TagEventName EventType = "tag"
1921
)
2022

@@ -63,13 +65,22 @@ func (r *DefaultEventHandler) Firing(p *project.Project, events map[string]cue.V
6365

6466
// NewDefaultEventHandler returns a new default event handler.
6567
func NewDefaultEventHandler(logger *slog.Logger) DefaultEventHandler {
68+
// This should never error
69+
gc, err := github.NewDefaultGithubClient("", "")
70+
if err != nil {
71+
panic(err)
72+
}
73+
6674
return DefaultEventHandler{
6775
logger: logger,
6876
store: map[EventType]Event{
6977
AlwaysEventName: &AlwaysEvent{},
7078
MergeEventName: &MergeEvent{
7179
logger: logger,
7280
},
81+
PREventName: &PREvent{
82+
gc: gc,
83+
},
7384
TagEventName: &TagEvent{
7485
logger: logger,
7586
},

cli/pkg/events/pr.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package events
2+
3+
import (
4+
"cuelang.org/go/cue"
5+
"github.com/input-output-hk/catalyst-forge/lib/project/project"
6+
"github.com/input-output-hk/catalyst-forge/lib/providers/github"
7+
)
8+
9+
// PREvent fires when the current operation is within a PR.
10+
type PREvent struct {
11+
gc github.GithubClient
12+
}
13+
14+
func (m *PREvent) Firing(p *project.Project, config cue.Value) (bool, error) {
15+
if m.gc.Env().IsPR() {
16+
return true, nil
17+
}
18+
19+
return false, nil
20+
}

cli/pkg/events/pr_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package events
2+
3+
import (
4+
"testing"
5+
6+
"cuelang.org/go/cue"
7+
"github.com/input-output-hk/catalyst-forge/lib/providers/github"
8+
gm "github.com/input-output-hk/catalyst-forge/lib/providers/github/mocks"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestPREventFiring(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
inPR bool
17+
expected bool
18+
}{
19+
{
20+
name: "firing on pr",
21+
inPR: true,
22+
expected: true,
23+
},
24+
{
25+
name: "not firing on pr",
26+
inPR: false,
27+
expected: false,
28+
},
29+
}
30+
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
em := gm.GithubEnvMock{
34+
IsPRFunc: func() bool {
35+
return tt.inPR
36+
},
37+
}
38+
gc := gm.GithubClientMock{
39+
EnvFunc: func() github.GithubEnv {
40+
return &em
41+
},
42+
}
43+
44+
event := PREvent{
45+
gc: &gc,
46+
}
47+
firing, err := event.Firing(nil, cue.Value{})
48+
require.NoError(t, err)
49+
assert.Equal(t, tt.expected, firing)
50+
})
51+
}
52+
}

docs/blueprint.cue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ project: {
44
release: {
55
docs: {
66
on: {
7-
always: {}
7+
merge: {}
8+
pr: {}
89
}
910

1011
config: {

0 commit comments

Comments
 (0)