Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ require (
go.uber.org/zap v1.13.0
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -340,6 +342,7 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/nleeper/goment v1.4.2 h1:r4c8KkCrsBJUnVi/IJ5HEqev5QY8aCWOXQtu+eYXtnI=
github.com/nleeper/goment v1.4.2/go.mod h1:zDl5bAyDhqxwQKAvkSXMRLOdCowrdZz53ofRJc4VhTo=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
Expand Down Expand Up @@ -758,6 +761,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
Expand Down
153 changes: 54 additions & 99 deletions internal/server/slack/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,109 +173,64 @@ func buildDeployView(callbackID string, c *vo.Config, perms []*ent.Perm) slack.M
return slack.ModalViewRequest{
Type: slack.VTModal,
CallbackID: callbackID,
Title: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Deploy",
},
Submit: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Submit",
},
Close: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Close",
},
Title: slack.NewTextBlockObject(slack.PlainTextType, "Deploy", false, false),
Submit: slack.NewTextBlockObject(slack.PlainTextType, "Submit", false, false),
Close: slack.NewTextBlockObject(slack.PlainTextType, "Close", false, false),
Blocks: slack.Blocks{
BlockSet: []slack.Block{
slack.NewInputBlock(
blockEnv,
slack.NewTextBlockObject(slack.PlainTextType, "Environment", false, false),
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select target environment", false, false),
actionEnv,
envs...,
),
),
slack.NewInputBlock(
blockType,
slack.NewTextBlockObject(slack.PlainTextType, "Type", false, false),
slack.NewOptionsSelectBlockElement(
slack.OptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select your ref type", false, false),
actionType,
slack.NewOptionBlockObject(
"commit",
slack.NewTextBlockObject(slack.PlainTextType, "Commit", false, false),
nil,
),
slack.NewOptionBlockObject(
"branch",
slack.NewTextBlockObject(slack.PlainTextType, "Branch", false, false),
nil,
),
slack.NewOptionBlockObject(
"tag",
slack.NewTextBlockObject(slack.PlainTextType, "Tag", false, false),
nil,
),
),
),
slack.NewInputBlock(
blockRef,
slack.NewTextBlockObject(slack.PlainTextType, "Ref", false, false),
slack.NewPlainTextInputBlockElement(
slack.NewTextBlockObject(slack.PlainTextType, "E.g. Commit - 25a667d6, Branch - main, Tag - v0.1.2", false, false),
actionRef,
),
),
slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockEnv,
Label: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Environment",
},
Element: slack.SelectBlockElement{
Type: slack.OptTypeStatic,
ActionID: actionEnv,
Placeholder: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Select target environment",
},
Options: envs,
},
},
slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockType,
Label: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Reference Type",
},
Element: slack.SelectBlockElement{
Type: slack.OptTypeStatic,
ActionID: actionType,
Placeholder: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Select your reference type",
},
Options: []*slack.OptionBlockObject{
{
Text: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Commit",
},
Value: "commit",
},
{
Text: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Branch",
},
Value: "branch",
},
{
Text: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Tag",
},
Value: "tag",
},
},
},
},
slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockRef,
Label: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Reference",
},
Element: slack.PlainTextInputBlockElement{
Type: slack.METPlainTextInput,
ActionID: actionRef,
Placeholder: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "E.g. Commit - 25a667d6, Branch - main, Tag - v0.1.2",
},
},
},
slack.InputBlock{
Type: slack.MBTInput,
BlockID: blockApprovers,
Label: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Approvers",
},
Type: slack.MBTInput,
BlockID: blockApprovers,
Label: slack.NewTextBlockObject(slack.PlainTextType, "Approvers", false, false),
Optional: true,
Element: slack.SelectBlockElement{
Type: slack.MultiOptTypeStatic,
ActionID: actionApprovers,
Placeholder: &slack.TextBlockObject{
Type: slack.PlainTextType,
Text: "Select approvers",
},
Options: approvers,
},
Element: slack.NewOptionsSelectBlockElement(
slack.MultiOptTypeStatic,
slack.NewTextBlockObject(slack.PlainTextType, "Select approvers", false, false),
actionApprovers,
approvers...,
),
},
},
},
Expand Down
86 changes: 81 additions & 5 deletions internal/server/slack/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,93 @@ import (
"testing"

"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
"github.com/slack-go/slack"
"go.uber.org/zap"
"gopkg.in/h2non/gock.v1"

"github.com/gitploy-io/gitploy/ent"
"github.com/gitploy-io/gitploy/ent/deployment"
"github.com/gitploy-io/gitploy/internal/server/slack/mock"
"github.com/gitploy-io/gitploy/vo"
"github.com/golang/mock/gomock"
"go.uber.org/zap"
)

const (
pathPostMessage string = "chat.postMessage"
)

func TestSlack_interactDeploy(t *testing.T) {
t.Run("Post a message when the repository is locked.", func(t *testing.T) {
m := mock.NewMockInteractor(gomock.NewController(t))

// These values are in "./testdata/deploy-interact.json"
const (
callbackID = "nafyVuEqzcchuVmV"
chatUserID = "U025KUBB2"
branch = "main"
env = "prod"
)

t.Log("Find the callback which was stored by the Slash command.")
m.
EXPECT().
FindCallbackByHash(gomock.Any(), callbackID).
Return(&ent.Callback{
Edges: ent.CallbackEdges{
Repo: &ent.Repo{
ID: "1",
Locked: true,
},
},
}, nil)

t.Log("Find the chat-user who sent the payload.")
m.
EXPECT().
FindChatUserByID(gomock.Any(), chatUserID).
Return(&ent.ChatUser{}, nil)

t.Log("Get branch to validate the payload.")
m.
EXPECT().
GetBranch(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), branch).
Return(&vo.Branch{
Name: branch,
}, nil)

t.Log("Post a message when the repository is locked.")
gock.
New(slack.APIURL).
Post(pathPostMessage).
Reply(200)

s := &Slack{i: m, log: zap.L()}

gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.POST("/interact", s.interactDeploy)

// Build the Slack payload.
bytes, err := ioutil.ReadFile("./testdata/deploy-interact.json")
if err != nil {
t.Errorf("It has failed to open the JSON file: %s", err)
t.FailNow()
}

form := url.Values{}
form.Add("payload", string(bytes))

req, _ := http.NewRequest("POST", "/interact", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

w := httptest.NewRecorder()
router.ServeHTTP(w, req)

if w.Code != http.StatusOK {
t.Fatalf("w.Code = %d, wanted %d. Body = %v", w.Code, http.StatusOK, w.Body)
}
})

t.Run("Create a new deployment with payload.", func(t *testing.T) {
m := mock.NewMockInteractor(gomock.NewController(t))

Expand Down Expand Up @@ -114,9 +192,7 @@ func TestSlack_interactDeploy(t *testing.T) {
router.ServeHTTP(w, req)

if w.Code != http.StatusOK {
t.Errorf("w.Code = %d, wanted %d", w.Code, http.StatusOK)
t.Logf("w.Body = %v", w.Body)
t.FailNow()
t.Fatalf("w.Code = %d, wanted %d. Body = %v", w.Code, http.StatusOK, w.Body)
}
})
}
Loading