Skip to content

Commit

Permalink
Resolve all lint issues, add e2e test on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Nov 23, 2022
1 parent bd9b856 commit 8cb33a3
Show file tree
Hide file tree
Showing 19 changed files with 293 additions and 112 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/branch-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
with:
go-version-file: 'go.mod'
cache: true

- name: Install Helm
uses: azure/setup-helm@v1
with:
Expand All @@ -103,13 +103,24 @@ jobs:
--set image.repository="${IMAGE_REPOSITORY}" \
--set image.tag="${IMAGE_TAG}" \
- name: Install GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
install-only: true
version: latest

- name: Build all plugins into dist directory
run: |
make build-plugins
- name: Run ${{ matrix.integration }} tests
env:
SLACK_TESTER_APP_TOKEN: ${{ secrets.SLACK_TESTER_APP_TOKEN }}
SLACK_ADDITIONAL_CONTEXT_MESSAGE: "Branch test - commit SHA: ${GITHUB_SHA} - https://github.com/kubeshop/botkube/commit/${GITHUB_SHA}"
DISCORD_TESTER_APP_TOKEN: ${{ secrets.DISCORD_TESTER_APP_TOKEN }}
DISCORD_GUILD_ID: ${{ secrets.DISCORD_GUILD_ID }}
DISCORD_ADDITIONAL_CONTEXT_MESSAGE: "Branch test - commit SHA: ${GITHUB_SHA} - https://github.com/kubeshop/botkube/commit/${GITHUB_SHA}"
PLUGINS_BINARIES_DIRECTORY: ${{ github.workspace }}/dist
run: |
KUBECONFIG=$(k3d kubeconfig write ${{ matrix.integration }}-test-cluster) \
make test-integration-${{ matrix.integration }}
32 changes: 32 additions & 0 deletions .github/workflows/pr-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ jobs:
ENDOFFILE
check-generated-resources:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Run generating gRPC resources
run: make gen-grpc-resources

- name: Detect git changes
run: |
if [[ $(git diff --stat) != '' ]]; then
echo -e '❌ \033[0;31mGenerated gRPC resources are outdated. Run 'make gen-grpc-resources'.\033[0m'
git diff --color
exit 1
else
echo '✔ No issues detected. Have a nice day :-)'
fi
integration-tests:
name: Integration tests
runs-on: ubuntu-latest
Expand Down Expand Up @@ -174,13 +195,24 @@ jobs:
--set image.repository="${IMAGE_REPOSITORY}" \
--set image.tag="${IMAGE_TAG}" \
- name: Install GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
install-only: true
version: latest

- name: Build all plugins into dist directory
run: |
make build-plugins
- name: Run ${{ matrix.integration }} tests
env:
SLACK_TESTER_APP_TOKEN: ${{ secrets.SLACK_TESTER_APP_TOKEN }}
SLACK_ADDITIONAL_CONTEXT_MESSAGE: "Pull request: ${PR_NUMBER} - https://github.com/kubeshop/botkube/pull/${PR_NUMBER}"
DISCORD_TESTER_APP_TOKEN: ${{ secrets.DISCORD_TESTER_APP_TOKEN }}
DISCORD_GUILD_ID: ${{ secrets.DISCORD_GUILD_ID }}
DISCORD_ADDITIONAL_CONTEXT_MESSAGE: "Pull request: ${PR_NUMBER} - https://github.com/kubeshop/botkube/pull/${PR_NUMBER}"
PLUGINS_BINARIES_DIRECTORY: ${{ github.workspace }}/dist
run: |
KUBECONFIG=$(k3d kubeconfig write ${{ matrix.integration }}-test-cluster) \
make test-integration-${{ matrix.integration }}
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.DEFAULT_GOAL := build
.PHONY: container-image test test-integration-slack test-integration-discord build pre-build publish lint lint-fix go-import-fmt system-check save-images load-and-push-images
.PHONY: container-image test test-integration-slack test-integration-discord build pre-build publish lint lint-fix go-import-fmt system-check save-images load-and-push-images gen-grpc-resources build-plugins

# Show this help.
help:
Expand Down Expand Up @@ -69,6 +69,10 @@ system-check:
echo 'System information checks passed.'; \
fi ;

# Generate gRPC Go code for client and server.
gen-grpc-resources:
@./hack/gen-grpc-resources.sh

# Pre-build checks
pre-build: system-check

Expand Down
5 changes: 2 additions & 3 deletions cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ func run() error {

errGroup, ctx := errgroup.WithContext(ctx)

// FIXME: collect enabled executors
// it can be done by creating collector for enabled bindings and shared with router
pluginManager := plugin.NewManager(logger, conf.Plugins, conf.Executors)
enabledPluginExecutors := plugin.GetAllEnabledAndUsedPluginExecutors(conf)
pluginManager := plugin.NewManager(logger, conf.Plugins, enabledPluginExecutors)

err = pluginManager.Start(ctx)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions cmd/executor/echo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Echo executor

Echo is the example Botkube executor used during [e2e tests](../../../test/e2e).

## Configuration parameters

The Echo configuration should be specified in YAML format. It accepts such parameters:

```yaml
changeResponseToUpperCase: true # default is 'false'.
```
20 changes: 19 additions & 1 deletion cmd/executor/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"strings"

"github.com/hashicorp/go-plugin"

Expand All @@ -10,10 +11,27 @@ import (

const pluginName = "echo"

// Config holds executor configuration.
type Config struct {
ChangeResponseToUpperCase bool
}

// EchoExecutor implements Botkube executor plugin
type EchoExecutor struct{}

// Execute returns a given command as response.
func (EchoExecutor) Execute(_ context.Context, req *executor.ExecuteRequest) (*executor.ExecuteResponse, error) {
return &executor.ExecuteResponse{Data: req.Command}, nil
// TODO: in request we should receive the executor configuration.
cfg := Config{}

data := req.Command
if cfg.ChangeResponseToUpperCase {
data = strings.ToUpper(data)
}

return &executor.ExecuteResponse{
Data: data,
}, nil
}

func main() {
Expand Down
11 changes: 4 additions & 7 deletions hack/fmt-imports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -E # needs to be set if we want the ERR trap

CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO_ROOT_DIR=$(cd "${CURRENT_DIR}/.." && pwd)
GOIMPORTS_REVISER_VERSION=2.5.3
GOIMPORTS_REVISER_VERSION=3.3.0

host::os() {
local host_os
Expand Down Expand Up @@ -86,12 +86,9 @@ imports::format() {
echo "Executing goimports-reviser..."
pushd "$REPO_ROOT_DIR" > /dev/null

paths=$(find . -name '*.go')

# TODO: Consider to run it in parallel to speed up the execution.
for file in $paths; do
goimports-reviser -file-path "$file" -rm-unused -local github.com/kubeshop/botkube -project-name github.com/kubeshop/botkube
done
goimports-reviser -rm-unused -project-name github.com/kubeshop/botkube -recursive ./pkg
goimports-reviser -rm-unused -project-name github.com/kubeshop/botkube -recursive ./cmd
goimports-reviser -rm-unused -project-name github.com/kubeshop/botkube -recursive ./internal

popd > /dev/null
}
Expand Down
7 changes: 1 addition & 6 deletions hack/goreleaser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,7 @@ build() {

build_plugins() {
prepare
docker run --rm --privileged \
-v $PWD:/go/src/github.com/kubeshop/botkube \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /go/src/github.com/kubeshop/botkube \
-e ANALYTICS_API_KEY="${ANALYTICS_API_KEY}" \
goreleaser/goreleaser build -f .goreleaser.plugin.yaml --rm-dist --snapshot
goreleaser build -f .goreleaser.plugin.yaml --rm-dist --snapshot
}

build_single() {
Expand Down
65 changes: 65 additions & 0 deletions internal/plugin/bindings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package plugin

import (
"github.com/kubeshop/botkube/pkg/config"
)

// GetAllEnabledAndUsedPluginExecutors returns the list of all plugins executors that are both enabled and bind to at
// least one communicator that is also enabled.
func GetAllEnabledAndUsedPluginExecutors(cfg *config.Config) []string {
// Collect all used executor bindings
bindExecutors := map[string]struct{}{}
collect := func(channels config.IdentifiableMap[config.ChannelBindingsByName]) {
for _, bindings := range channels {
for _, name := range bindings.Bindings.Executors {
bindExecutors[name] = struct{}{}
}
}
}

for _, commGroupCfg := range cfg.Communications {
if commGroupCfg.Slack.Enabled {
collect(commGroupCfg.Slack.Channels)
}

if commGroupCfg.SocketSlack.Enabled {
collect(commGroupCfg.SocketSlack.Channels)
}

if commGroupCfg.Mattermost.Enabled {
collect(commGroupCfg.Mattermost.Channels)
}

if commGroupCfg.Teams.Enabled {
for _, name := range commGroupCfg.Teams.Bindings.Executors {
bindExecutors[name] = struct{}{}
}
}

if commGroupCfg.Discord.Enabled {
for _, bindings := range commGroupCfg.Discord.Channels {
for _, name := range bindings.Bindings.Executors {
bindExecutors[name] = struct{}{}
}
}
}
}

// Collect all executors that are both enabled and bind to at least one communicator that is enabled.
var usedExecutorPlugins []string
for groupName, groupItems := range cfg.Executors {
for name, executor := range groupItems.Plugins {
if !executor.Enabled {
continue
}
_, found := bindExecutors[groupName]
if !found {
continue
}

usedExecutorPlugins = append(usedExecutorPlugins, name)
}
}

return usedExecutorPlugins
}
12 changes: 6 additions & 6 deletions internal/plugin/index.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package plugin

// Type represents the plugin type.
type Type string

const (
TypeSource Type = "source"
// TypeSource represents the source plugin.
TypeSource Type = "source"
// TypeExecutor represents the executor plugin.
TypeExecutor Type = "executor"
)

type (
// Index defines the plugin repository index.
Index struct {
Entries []IndexEntry
}
// IndexEntry defines the plugin definition.
IndexEntry struct {
Name string
Type Type
Expand All @@ -19,8 +24,3 @@ type (
Links []string
}
)

type Plugin struct {
Name string
Version string
}
Loading

0 comments on commit 8cb33a3

Please sign in to comment.