Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/git-track-command.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"io"
10
"os"
11
"time"
12
13
log "github.com/sirupsen/logrus"
14
"github.com/spf13/cobra"
15
16
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
17
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
18
serverapi "github.com/gitpod-io/gitpod/gitpod-protocol"
19
)
20
21
var gitTrackCommandOpts struct {
22
GitCommand string
23
}
24
25
var gitTrackCommand = &cobra.Command{
26
Use: "git-track-command",
27
Short: "Gitpod's Git command tracker",
28
Long: "Sending anonymous statistics about the executed git commands inside a workspace",
29
Args: cobra.ExactArgs(0),
30
Hidden: true,
31
RunE: func(cmd *cobra.Command, args []string) error {
32
// ignore trace
33
utils.TrackCommandUsageEvent.Command = nil
34
35
log.SetOutput(io.Discard)
36
f, err := os.OpenFile(os.TempDir()+"/gitpod-git-credential-helper.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
37
if err == nil {
38
defer f.Close()
39
log.SetOutput(f)
40
}
41
42
log.Infof("gp git-track-command")
43
44
ctx, cancel := context.WithTimeout(cmd.Context(), 1*time.Minute)
45
defer cancel()
46
wsInfo, err := gitpod.GetWSInfo(ctx)
47
if err != nil {
48
return err
49
}
50
51
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{"function:trackEvent"})
52
53
if err != nil {
54
log.WithError(err).Fatal("error connecting to supervisor")
55
}
56
57
defer client.Close()
58
59
type GitEventParams struct {
60
Command string `json:"command,omitempty"`
61
WorkspaceId string `json:"workspaceId,omitempty"`
62
// most often used across all events
63
InstanceId string `json:"instanceId,omitempty"`
64
// deprecated for backward compatibility
65
WorkspaceInstanceId string `json:"workspaceInstanceId,omitempty"`
66
Timestamp int64 `json:"timestamp,omitempty"`
67
}
68
69
params := &GitEventParams{
70
Command: gitTrackCommandOpts.GitCommand,
71
WorkspaceId: wsInfo.WorkspaceId,
72
InstanceId: wsInfo.InstanceId,
73
WorkspaceInstanceId: wsInfo.InstanceId,
74
Timestamp: time.Now().Unix(),
75
}
76
event := &serverapi.RemoteTrackMessage{
77
Event: "git_command",
78
Properties: *params,
79
}
80
log.WithField("command", gitTrackCommandOpts.GitCommand).
81
Info("tracking the GitCommand event")
82
83
// TODO(ak) use segment directly + supervisor info to get workspace and isntance IDs, don't use server
84
err = client.TrackEvent(ctx, event)
85
if err != nil {
86
log.WithError(err).Fatal("error tracking git event")
87
}
88
return nil
89
},
90
}
91
92
func init() {
93
rootCmd.AddCommand(gitTrackCommand)
94
gitTrackCommand.Flags().StringVarP(&gitTrackCommandOpts.GitCommand, "gitCommand", "c", "", "The Git command to be recorded")
95
gitTrackCommand.MarkFlagRequired("gitCommand")
96
}
97
98