Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-55060 - Calls: Add PushTypeCalls for calls-specific push notifications #110

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ PROTECTED_BRANCH := master
CURRENT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
# Use repository name as application name
APP_NAME := $(shell basename -s .git `git config --get remote.origin.url`)
# Get current commit
APP_COMMIT := $(shell git log --pretty=format:'%h' -n 1)
# Check if we are in protected branch, if yes use `protected_branch_name-sha` as app version.
# Else check if we are in a release tag, if yes use the tag as app version, else use `dev-sha` as app version.
APP_VERSION ?= $(shell if [ $(PROTECTED_BRANCH) = $(CURRENT_BRANCH) ]; then echo $(PROTECTED_BRANCH); else (git describe --abbrev=0 --exact-match --tags 2>/dev/null || echo dev-$(APP_COMMIT)) ; fi)
Expand All @@ -23,6 +21,11 @@ endif
# Get current date and format like: 2022-04-27 11:32
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M)

# Get version information for plugins that depend on a semver version
BUILD_HASH = $(shell git rev-parse --short HEAD)
BUILD_TAG_LATEST = $(shell git describe --tags --match 'v*' --abbrev=0)
BUILD_TAG_CURRENT = $(shell git tag --points-at HEAD)

## General Configuration Variables
# We don't need make's built-in rules.
MAKEFLAGS += --no-builtin-rules
Expand Down Expand Up @@ -69,7 +72,9 @@ GO := $(shell which go)
GO_VERSION ?= $(shell grep -E '^go' go.mod | awk {'print $$2'})
# LDFLAGS
GO_LDFLAGS += -X "github.com/mattermost/${APP_NAME}/internal/version.gitVersion=$(GIT_VERSION)"
GO_LDFLAGS += -X "github.com/mattermost/${APP_NAME}/internal/version.gitCommit=$(APP_COMMIT)"
GO_LDFLAGS += -X "github.com/mattermost/${APP_NAME}/internal/version.buildHash=$(BUILD_HASH)"
GO_LDFLAGS += -X "github.com/mattermost/${APP_NAME}/internal/version.buildTagLatest=$(BUILD_TAG_LATEST)"
GO_LDFLAGS += -X "github.com/mattermost/${APP_NAME}/internal/version.buildTagCurrent=$(BUILD_TAG_CURRENT)"
GO_LDFLAGS += -X "github.com/mattermost/${APP_NAME}/internal/version.gitTreeState=$(GIT_TREESTATE)"
GO_LDFLAGS += -X "github.com/mattermost/${APP_NAME}/internal/version.buildDate=$(BUILD_DATE)"

Expand Down
33 changes: 28 additions & 5 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ var (
// Output of "git describe". The prerequisite is that the branch should be
// tagged using the correct versioning strategy.
gitVersion string = "devel"
// SHA1 from git, output of $(git rev-parse HEAD)
gitCommit = "unknown"
// short SHA1 from git, output of $(git rev-parse --short HEAD)
buildHash = "unknown"
// the most recent v* tag in the current branch (or its ancestors)
buildTagLatest = "unknown"
// the current commit's v* tag
buildTagCurrent = "unknown"
// State of git tree, either "clean" or "dirty"
gitTreeState = "unknown"
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
Expand All @@ -33,7 +37,8 @@ func GetVersion() error {

type Info struct {
GitVersion string
GitCommit string
cpoile marked this conversation as resolved.
Show resolved Hide resolved
BuildHash string
BuildVersion string
GitTreeState string
BuildDate string
GoVersion string
Expand All @@ -44,9 +49,26 @@ type Info struct {
func VersionInfo() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the global defaults set above.

// Create the semver version based on the state of the current commit or its branch.
// Use the first version we find.
var version string
tags := strings.Fields(buildTagCurrent)
for _, t := range tags {
if strings.HasPrefix(t, "v") {
version = t
break
}
}
if version == "" {
version = buildTagLatest + "+" + buildHash
}
version = strings.TrimPrefix(version, "v")

return Info{
GitVersion: gitVersion,
GitCommit: gitCommit,
BuildHash: buildHash,
BuildVersion: version,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Expand All @@ -61,7 +83,8 @@ func (i *Info) String() string {
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)

fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
fmt.Fprintf(w, "BuildHash:\t%s\n", i.BuildHash)
fmt.Fprintf(w, "BuildVersion:\t%s\n", i.BuildVersion)
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState)
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
Expand Down
4 changes: 2 additions & 2 deletions server/android_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus
data["sender_id"] = msg.SenderID
data["sender_name"] = "Someone"
data["team_id"] = msg.TeamID
} else if pushType == PushTypeMessage || pushType == PushTypeSession {
} else if pushType == PushTypeMessage || pushType == PushTypeSession || pushType == PushTypeCalls {
data["team_id"] = msg.TeamID
data["sender_id"] = msg.SenderID
data["sender_name"] = msg.SenderName
Expand All @@ -162,7 +162,7 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus
ctx, cancel := context.WithTimeout(context.Background(), me.sendTimeout)
defer cancel()

me.logger.Infof("Sending android push notification for device=%v type=%v ackId=%v", me.AndroidPushSettings.Type, msg.Type, msg.AckID)
me.logger.Infof("Sending android push notification for device=%v type=%v ackId=%v", me.AndroidPushSettings.Type, pushType, msg.AckID)

start := time.Now()
_, err := me.client.Send(ctx, fcmMsg)
Expand Down
4 changes: 2 additions & 2 deletions server/apple_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ func (me *AppleNotificationServer) SendNotification(msg *PushNotification) PushR
data.ContentAvailable()
} else {
switch msg.Type {
case PushTypeMessage, PushTypeSession:
case PushTypeMessage, PushTypeSession, PushTypeCalls:
data.Category(msg.Category)
data.Sound("default")
data.Custom("version", msg.Version)
data.MutableContent()
if msg.Type == PushTypeMessage {
if msg.Type == PushTypeMessage || msg.Type == PushTypeCalls {
data.ContentAvailable()
}

Expand Down
1 change: 1 addition & 0 deletions server/push_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
PushTypeUpdateBadge = "update_badge"
PushTypeSession = "session"
PushTypeTest = "test"
PushTypeCalls = "calls"

PushMessageV2 = "v2"

Expand Down
17 changes: 17 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (s *Server) Start() {
handler := th.Throttle(router)

router.HandleFunc("/", root).Methods("GET")
router.HandleFunc("/version", s.version).Methods("GET")

metricCompatibleSendNotificationHandler := s.handleSendNotification
metricCompatibleAckNotificationHandler := s.handleAckNotification
Expand Down Expand Up @@ -150,6 +151,22 @@ func root(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("<html><body>Mattermost Push Proxy</body></html>"))
}

func (s *Server) version(w http.ResponseWriter, _ *http.Request) {
agnivade marked this conversation as resolved.
Show resolved Hide resolved
info := version.VersionInfo()
ret := map[string]interface{}{
"version": info.BuildVersion,
"hash": info.BuildHash,
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(ret); err != nil {
cpoile marked this conversation as resolved.
Show resolved Hide resolved
s.logger.Errorf("Failed to write response: %v", err)
if s.metrics != nil {
s.metrics.incrementBadRequest()
}
}
}

func (s *Server) responseTimeMiddleware(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
Expand Down
26 changes: 26 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package server
import (
"bytes"
"encoding/json"
"github.com/mattermost/mattermost-push-proxy/internal/version"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"

Expand Down Expand Up @@ -111,3 +114,26 @@ func TestAndroidSend(t *testing.T) {
srv.Stop()
time.Sleep(time.Second * 2)
}

func TestServer_version(t *testing.T) {
fileName := FindConfigFile("mattermost-push-proxy.sample.json")
cfg, err := LoadConfig(fileName)
require.NoError(t, err)
logger := NewLogger(cfg)
srv := New(cfg, logger)

req := httptest.NewRequest(http.MethodGet, "/version", nil)
res := httptest.NewRecorder()
srv.version(res, req)
assert.Equal(t, res.Code, http.StatusOK)

info := version.VersionInfo()
ret := struct {
Version string
Hash string
}{}
err = json.NewDecoder(res.Body).Decode(&ret)
assert.NoError(t, err)
assert.Equal(t, info.BuildVersion, ret.Version)
assert.Equal(t, info.BuildHash, ret.Hash)
}