Skip to content

Commit d85faba

Browse files
ayusht2810lieut-datamickmisterhanzei
authored
[MM-98] Update plugin with respect to phase 1 upgrades (#217)
* [MM-98] Update plugin with respect to phase 1 upgrades * [MM-98] Fix lint * [MM-98] Update Readme * [MM-98] Review fixes regarding constant for golangci-lint * Sync with playbooks: install-go-tools, gotestsum, and dynamic versions (#192) * Revert "Update main.go (#154)" This reverts commit be4a281d0cc791d10e6e5ae917b325b2f054e475. * Revert "[MM-33506] Use embed package to include plugin manifest (#145)" This reverts commit ca9ee3c17c6920a636a33f378e17395afd6f329f. * Revert "Don't generate manifest.ts (#127)" This reverts commit 18d30b50bc7ba800c9f05bfd82970781db0aea3e. * install-go-tools target, adopt gotestsum * bring back make apply + automatic versioning * Update build/manifest/main.go Co-authored-by: Michael Kochell <[email protected]> * suppress git describe error when no tags match * make version/release notes opt-in * fix whitespace in Makefile * document version management options --------- Co-authored-by: Michael Kochell <[email protected]> * Fetch plugin logs from server (#193) Co-authored-by: Jesse Hallam <[email protected]> --------- Co-authored-by: Jesse Hallam <[email protected]> Co-authored-by: Michael Kochell <[email protected]> Co-authored-by: Ben Schumacher <[email protected]>
1 parent 050fecc commit d85faba

27 files changed

+1038
-1470
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server/manifest.go linguist-generated=true

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist
2+
server/manifest.go

.golangci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ linters:
2323
enable:
2424
- bodyclose
2525
- errcheck
26-
- goconst
2726
- gocritic
2827
- gofmt
2928
- goimports

.nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
14.21.1
1+
16.13.1

Makefile

+165-49
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
GO ?= $(shell command -v go 2> /dev/null)
22
NPM ?= $(shell command -v npm 2> /dev/null)
33
CURL ?= $(shell command -v curl 2> /dev/null)
4-
MANIFEST_FILE ?= plugin.json
4+
MM_DEBUG ?=
55
GOPATH ?= $(shell go env GOPATH)
66
GO_TEST_FLAGS ?= -race
77
GO_BUILD_FLAGS ?=
88
MM_UTILITIES_DIR ?= ../mattermost-utilities
9+
DLV_DEBUG_PORT := 2346
10+
DEFAULT_GOOS := $(shell go env GOOS)
11+
DEFAULT_GOARCH := $(shell go env GOARCH)
912

1013
export GO111MODULE=on
1114

15+
# We need to export GOBIN to allow it to be set
16+
# for processes spawned from the Makefile
17+
export GOBIN ?= $(PWD)/bin
18+
1219
# You can include assets this directory into the bundle. This can be e.g. used to include profile pictures.
1320
ASSETS_DIR ?= assets
1421

22+
## Define the default target (make all)
23+
.PHONY: default
24+
default: all
25+
1526
# Verify environment, and define PLUGIN_ID, PLUGIN_VERSION, HAS_SERVER and HAS_WEBAPP as needed.
1627
include build/setup.mk
1728

@@ -22,122 +33,195 @@ ifneq ($(wildcard build/custom.mk),)
2233
include build/custom.mk
2334
endif
2435

36+
ifneq ($(MM_DEBUG),)
37+
GO_BUILD_GCFLAGS = -gcflags "all=-N -l"
38+
else
39+
GO_BUILD_GCFLAGS =
40+
endif
41+
2542
## Checks the code style, tests, builds and bundles the plugin.
43+
.PHONY: all
2644
all: check-style test dist
2745

28-
## Propagates plugin manifest information into the server/ and webapp/ folders as required.
46+
## Propagates plugin manifest information into the server/ and webapp/ folders.
2947
.PHONY: apply
3048
apply:
3149
./build/bin/manifest apply
3250

33-
## Runs golangci-lint and eslint.
51+
## Install go tools
52+
install-go-tools:
53+
@echo Installing go tools
54+
$(GO) install github.com/golangci/golangci-lint/cmd/[email protected]
55+
$(GO) install gotest.tools/[email protected]
56+
57+
## Runs eslint and golangci-lint
3458
.PHONY: check-style
35-
check-style: webapp/.npminstall golangci-lint
59+
check-style: apply webapp/node_modules install-go-tools
3660
@echo Checking for style guide compliance
3761

3862
ifneq ($(HAS_WEBAPP),)
3963
cd webapp && npm run lint
64+
cd webapp && npm run check-types
4065
endif
4166

42-
## Run golangci-lint on codebase.
43-
.PHONY: golangci-lint
44-
golangci-lint:
45-
@if ! [ -x "$$(command -v golangci-lint)" ]; then \
46-
echo "golangci-lint is not installed. Please see https://github.com/golangci/golangci-lint#install for installation instructions."; \
47-
exit 1; \
48-
fi; \
49-
67+
# It's highly recommended to run go-vet first
68+
# to find potential compile errors that could introduce
69+
# weird reports at golangci-lint step
70+
ifneq ($(HAS_SERVER),)
5071
@echo Running golangci-lint
51-
golangci-lint run ./...
72+
$(GO) vet ./...
73+
$(GOBIN)/golangci-lint run ./...
74+
endif
5275

53-
## Builds the server, if it exists, including support for multiple architectures.
76+
## Builds the server, if it exists, for all supported architectures, unless MM_SERVICESETTINGS_ENABLEDEVELOPER is set.
5477
.PHONY: server
5578
server:
5679
ifneq ($(HAS_SERVER),)
80+
ifneq ($(MM_DEBUG),)
81+
$(info DEBUG mode is on; to disable, unset MM_DEBUG)
82+
endif
5783
mkdir -p server/dist;
58-
cd server && env GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-linux-amd64;
59-
cd server && env GOOS=linux GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-linux-arm64;
60-
cd server && env GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-darwin-amd64;
61-
cd server && env GOOS=darwin GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-darwin-arm64;
62-
cd server && env GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-windows-amd64.exe;
84+
ifneq ($(MM_SERVICESETTINGS_ENABLEDEVELOPER),)
85+
@echo Building plugin only for $(DEFAULT_GOOS)-$(DEFAULT_GOARCH) because MM_SERVICESETTINGS_ENABLEDEVELOPER is enabled
86+
cd server && env CGO_ENABLED=0 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-$(DEFAULT_GOOS)-$(DEFAULT_GOARCH);
87+
else
88+
cd server && env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-linux-amd64;
89+
cd server && env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-linux-arm64;
90+
cd server && env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-darwin-amd64;
91+
cd server && env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-darwin-arm64;
92+
cd server && env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) $(GO_BUILD_GCFLAGS) -trimpath -o dist/plugin-windows-amd64.exe;
93+
endif
6394
endif
6495

6596
## Ensures NPM dependencies are installed without having to run this all the time.
66-
webapp/.npminstall:
97+
webapp/node_modules: $(wildcard webapp/package.json)
6798
ifneq ($(HAS_WEBAPP),)
6899
cd webapp && $(NPM) install
69100
touch $@
70101
endif
71102

72103
## Builds the webapp, if it exists.
73104
.PHONY: webapp
74-
webapp: webapp/.npminstall
105+
webapp: webapp/node_modules
75106
ifneq ($(HAS_WEBAPP),)
107+
ifeq ($(MM_DEBUG),)
76108
cd webapp && $(NPM) run build;
109+
else
110+
cd webapp && $(NPM) run debug;
77111
endif
78-
79-
## Builds the webapp in debug mode, if it exists.
80-
.PHONY: webapp-debug
81-
webapp-debug: webapp/.npminstall
82-
ifneq ($(HAS_WEBAPP),)
83-
cd webapp && \
84-
$(NPM) run debug;
85112
endif
86113

87114
## Generates a tar bundle of the plugin for install.
88115
.PHONY: bundle
89116
bundle:
90117
rm -rf dist/
91118
mkdir -p dist/$(PLUGIN_ID)
92-
cp $(MANIFEST_FILE) dist/$(PLUGIN_ID)/
119+
./build/bin/manifest dist
93120
ifneq ($(wildcard $(ASSETS_DIR)/.),)
94121
cp -r $(ASSETS_DIR) dist/$(PLUGIN_ID)/
95122
endif
96123
ifneq ($(HAS_PUBLIC),)
97-
cp -r public/ dist/$(PLUGIN_ID)/
124+
cp -r public dist/$(PLUGIN_ID)/
98125
endif
99126
ifneq ($(HAS_SERVER),)
100-
mkdir -p dist/$(PLUGIN_ID)/server/dist;
101-
cp -r server/dist/* dist/$(PLUGIN_ID)/server/dist/;
127+
mkdir -p dist/$(PLUGIN_ID)/server
128+
cp -r server/dist dist/$(PLUGIN_ID)/server/
102129
endif
103130
ifneq ($(HAS_WEBAPP),)
104-
mkdir -p dist/$(PLUGIN_ID)/webapp/dist;
105-
cp -r webapp/dist/* dist/$(PLUGIN_ID)/webapp/dist/;
131+
mkdir -p dist/$(PLUGIN_ID)/webapp
132+
cp -r webapp/dist dist/$(PLUGIN_ID)/webapp/
106133
endif
107134
cd dist && tar -cvzf $(BUNDLE_NAME) $(PLUGIN_ID)
108135

109136
@echo plugin built at: dist/$(BUNDLE_NAME)
110137

111138
## Builds and bundles the plugin.
112139
.PHONY: dist
113-
dist: apply server webapp bundle
140+
dist: apply server webapp bundle
114141

115-
## Installs the plugin to a (development) server.
116-
## It uses the API if appropriate environment variables are defined,
117-
## and otherwise falls back to trying to copy the plugin to a sibling mattermost-server directory.
142+
## Builds and installs the plugin to a server.
118143
.PHONY: deploy
119144
deploy: dist
120-
./build/bin/deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME)
145+
./build/bin/pluginctl deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME)
146+
147+
## Builds and installs the plugin to a server, updating the webapp automatically when changed.
148+
.PHONY: watch
149+
watch: apply server bundle
150+
ifeq ($(MM_DEBUG),)
151+
cd webapp && $(NPM) run build:watch
152+
else
153+
cd webapp && $(NPM) run debug:watch
154+
endif
121155

122-
.PHONY: debug-deploy
123-
debug-deploy: debug-dist deploy
156+
## Installs a previous built plugin with updated webpack assets to a server.
157+
.PHONY: deploy-from-watch
158+
deploy-from-watch: bundle
159+
./build/bin/pluginctl deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME)
124160

125-
.PHONY: debug-dist
126-
debug-dist: apply server webapp-debug bundle
161+
## Setup dlv for attaching, identifying the plugin PID for other targets.
162+
.PHONY: setup-attach
163+
setup-attach:
164+
$(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}'))
165+
$(eval NUM_PID := $(shell echo -n ${PLUGIN_PID} | wc -w))
166+
167+
@if [ ${NUM_PID} -gt 2 ]; then \
168+
echo "** There is more than 1 plugin process running. Run 'make kill reset' to restart just one."; \
169+
exit 1; \
170+
fi
171+
172+
## Check if setup-attach succeeded.
173+
.PHONY: check-attach
174+
check-attach:
175+
@if [ -z ${PLUGIN_PID} ]; then \
176+
echo "Could not find plugin PID; the plugin is not running. Exiting."; \
177+
exit 1; \
178+
else \
179+
echo "Located Plugin running with PID: ${PLUGIN_PID}"; \
180+
fi
181+
182+
## Attach dlv to an existing plugin instance.
183+
.PHONY: attach
184+
attach: setup-attach check-attach
185+
dlv attach ${PLUGIN_PID}
186+
187+
## Attach dlv to an existing plugin instance, exposing a headless instance on $DLV_DEBUG_PORT.
188+
.PHONY: attach-headless
189+
attach-headless: setup-attach check-attach
190+
dlv attach ${PLUGIN_PID} --listen :$(DLV_DEBUG_PORT) --headless=true --api-version=2 --accept-multiclient
191+
192+
## Detach dlv from an existing plugin instance, if previously attached.
193+
.PHONY: detach
194+
detach: setup-attach
195+
@DELVE_PID=$(shell ps aux | grep "dlv attach ${PLUGIN_PID}" | grep -v "grep" | awk -F " " '{print $$2}') && \
196+
if [ "$$DELVE_PID" -gt 0 ] > /dev/null 2>&1 ; then \
197+
echo "Located existing delve process running with PID: $$DELVE_PID. Killing." ; \
198+
kill -9 $$DELVE_PID ; \
199+
fi
127200

128201
## Runs any lints and unit tests defined for the server and webapp, if they exist.
129202
.PHONY: test
130-
test: webapp/.npminstall
203+
test: apply webapp/node_modules install-go-tools
131204
ifneq ($(HAS_SERVER),)
132-
$(GO) test -v $(GO_TEST_FLAGS) ./server/...
205+
$(GOBIN)/gotestsum -- -v ./...
133206
endif
134207
ifneq ($(HAS_WEBAPP),)
135-
cd webapp && $(NPM) run fix && $(NPM) run test;
208+
cd webapp && $(NPM) run test;
209+
endif
210+
211+
## Runs any lints and unit tests defined for the server and webapp, if they exist, optimized
212+
## for a CI environment.
213+
.PHONY: test-ci
214+
test-ci: apply webapp/node_modules install-go-tools
215+
ifneq ($(HAS_SERVER),)
216+
$(GOBIN)/gotestsum --format standard-verbose --junitfile report.xml -- ./...
217+
endif
218+
ifneq ($(HAS_WEBAPP),)
219+
cd webapp && $(NPM) run test;
136220
endif
137221

138222
## Creates a coverage report for the server code.
139223
.PHONY: coverage
140-
coverage: webapp/.npminstall
224+
coverage: apply webapp/node_modules
141225
ifneq ($(HAS_SERVER),)
142226
$(GO) test $(GO_TEST_FLAGS) -coverprofile=server/coverage.txt ./server/...
143227
$(GO) tool cover -html=server/coverage.txt
@@ -154,6 +238,31 @@ else
154238
endif
155239
endif
156240

241+
## Disable the plugin.
242+
.PHONY: disable
243+
disable: detach
244+
./build/bin/pluginctl disable $(PLUGIN_ID)
245+
246+
## Enable the plugin.
247+
.PHONY: enable
248+
enable:
249+
./build/bin/pluginctl enable $(PLUGIN_ID)
250+
251+
## Reset the plugin, effectively disabling and re-enabling it on the server.
252+
.PHONY: reset
253+
reset: detach
254+
./build/bin/pluginctl reset $(PLUGIN_ID)
255+
256+
## Kill all instances of the plugin, detaching any existing dlv instance.
257+
.PHONY: kill
258+
kill: detach
259+
$(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}'))
260+
261+
@for PID in ${PLUGIN_PID}; do \
262+
echo "Killing plugin pid $$PID"; \
263+
kill -9 $$PID; \
264+
done; \
265+
157266
## Clean removes all build artifacts.
158267
.PHONY: clean
159268
clean:
@@ -163,13 +272,20 @@ ifneq ($(HAS_SERVER),)
163272
rm -fr server/dist
164273
endif
165274
ifneq ($(HAS_WEBAPP),)
166-
rm -fr webapp/.npminstall
167275
rm -fr webapp/junit.xml
168276
rm -fr webapp/dist
169277
rm -fr webapp/node_modules
170278
endif
171279
rm -fr build/bin/
172280

281+
.PHONY: logs
282+
logs:
283+
./build/bin/pluginctl logs $(PLUGIN_ID)
284+
285+
.PHONY: logs-watch
286+
logs-watch:
287+
./build/bin/pluginctl logs-watch $(PLUGIN_ID)
288+
173289
# Help documentation à la https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
174290
help:
175-
@cat Makefile | grep -v '\.PHONY' | grep -v '\help:' | grep -B1 -E '^[a-zA-Z0-9_.-]+:.*' | sed -e "s/:.*//" | sed -e "s/^## //" | grep -v '\-\-' | sed '1!G;h;$$!d' | awk 'NR%2{printf "\033[36m%-30s\033[0m",$$0;next;}1' | sort
291+
@cat Makefile build/*.mk | grep -v '\.PHONY' | grep -v '\help:' | grep -B1 -E '^[a-zA-Z0-9_.-]+:.*' | sed -e "s/:.*//" | sed -e "s/^## //" | grep -v '\-\-' | sed '1!G;h;$$!d' | awk 'NR%2{printf "\033[36m%-30s\033[0m",$$0;next;}1' | sort

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,13 @@ The `/autolink` commands allow the users to easily edit the configurations.
163163

164164
## Development
165165

166-
This plugin contains a server portion.
166+
This plugin contains a server portion. Read our documentation about the [Developer Workflow](https://developers.mattermost.com/integrate/plugins/developer-workflow/) and [Developer Setup](https://developers.mattermost.com/integrate/plugins/developer-setup/) for more information about developing and extending plugins.
167167

168-
Use `make dist` to build distributions of the plugin that you can upload to a Mattermost server.
169-
Use `make check-style` to check the style.
170-
Use `make deploy` to deploy the plugin to your local server.
168+
### Releasing new versions
171169

172-
For additional information on developing plugins, refer to [our plugin developer documentation](https://developers.mattermost.com/extend/plugins/).
170+
The version of a plugin is determined at compile time, automatically populating a `version` field in the [plugin manifest](plugin.json):
171+
* If the current commit matches a tag, the version will match after stripping any leading `v`, e.g. `1.3.1`.
172+
* Otherwise, the version will combine the nearest tag with `git rev-parse --short HEAD`, e.g. `1.3.1+d06e53e1`.
173+
* If there is no version tag, an empty version will be combined with the short hash, e.g. `0.0.0+76081421`.
174+
175+
To disable this behaviour, manually populate and maintain the `version` field.

0 commit comments

Comments
 (0)