As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit b99c3183 authored by Archish's avatar Archish
Browse files

Lint fixes for audit event packages

parent e02f4bb0
No related branches found
No related tags found
No related merge requests found
// Package gitauditevent handles Git audit events for GitLab.
package gitauditevent
import (
Loading
Loading
Loading
Loading
@@ -7,6 +7,7 @@ import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/command/commandargs"
Loading
Loading
@@ -30,13 +31,13 @@ func TestGitAudit(t *testing.T) {
called = true
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
assert.NoError(t, err)
defer r.Body.Close()
var request *gitauditevent.Request
require.NoError(t, json.Unmarshal(body, &request))
require.Equal(t, testUsername, request.Username)
require.Equal(t, testRepo, request.Repo)
assert.NoError(t, json.Unmarshal(body, &request))
assert.Equal(t, testUsername, request.Username)
assert.Equal(t, testRepo, request.Repo)
w.WriteHeader(http.StatusOK)
},
Loading
Loading
// Package gitauditevent handles Git audit events for GitLab.
package gitauditevent
import (
Loading
Loading
@@ -13,11 +14,13 @@ import (
const uri = "/api/v4/internal/shellhorse/git_audit_event"
// Client handles communication with the GitLab audit event API.
type Client struct {
config *config.Config
client *client.GitlabNetClient
}
// NewClient creates a new Client for sending audit events.
func NewClient(config *config.Config) (*Client, error) {
client, err := gitlabnet.GetClient(config)
if err != nil {
Loading
Loading
@@ -27,6 +30,7 @@ func NewClient(config *config.Config) (*Client, error) {
return &Client{config: config, client: client}, nil
}
// Request represents the data for a Git audit event.
type Request struct {
Action commandargs.CommandType `json:"action"`
Protocol string `json:"protocol"`
Loading
Loading
@@ -35,6 +39,7 @@ type Request struct {
PackfileStats *pb.PackfileNegotiationStatistics `json:"packfile_stats,omitempty"`
}
// Audit sends an audit event to the GitLab API.
func (c *Client) Audit(ctx context.Context, username string, action commandargs.CommandType, repo string, packfileStats *pb.PackfileNegotiationStatistics) error {
request := &Request{
Action: action,
Loading
Loading
@@ -48,6 +53,10 @@ func (c *Client) Audit(ctx context.Context, username string, action commandargs.
if err != nil {
return err
}
defer response.Body.Close()
defer func() {
if err := response.Body.Close(); err != nil {
_ = fmt.Errorf("unable to close response body: %v", err)
}
}()
return nil
}
Loading
Loading
@@ -7,6 +7,7 @@ import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
pb "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
Loading
Loading
@@ -48,17 +49,17 @@ func setup(t *testing.T, responseStatus int) *Client {
Path: uri,
Handler: func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
assert.NoError(t, err)
defer r.Body.Close()
var request *Request
require.NoError(t, json.Unmarshal(body, &request))
require.Equal(t, testUsername, request.Username)
require.Equal(t, testAction, request.Action)
require.Equal(t, testRepo, request.Repo)
require.Equal(t, "ssh", request.Protocol)
require.Equal(t, testPackfileWants, request.PackfileStats.Wants)
require.Equal(t, testPackfileHaves, request.PackfileStats.Haves)
assert.NoError(t, json.Unmarshal(body, &request))
assert.Equal(t, testUsername, request.Username)
assert.Equal(t, testAction, request.Action)
assert.Equal(t, testRepo, request.Repo)
assert.Equal(t, "ssh", request.Protocol)
assert.Equal(t, testPackfileWants, request.PackfileStats.Wants)
assert.Equal(t, testPackfileHaves, request.PackfileStats.Haves)
w.WriteHeader(responseStatus)
},
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment