Skip to content

Commit d22c536

Browse files
do not send empty repo_info when not available in attest artifact cmd (#698)
this is related to kosli-dev/server#4929
1 parent 84778c7 commit d22c536

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

cmd/kosli/attestArtifact.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type AttestArtifactPayload struct {
3030
Filename string `json:"filename"`
3131
GitCommit string `json:"git_commit"`
3232
GitCommitInfo *gitview.BasicCommitInfo `json:"git_commit_info"`
33-
GitRepoInfo *gitview.GitRepoInfo `json:"repo_info"`
33+
GitRepoInfo *gitview.GitRepoInfo `json:"repo_info,omitempty"`
3434
BuildUrl string `json:"build_url"`
3535
CommitUrl string `json:"commit_url"`
3636
RepoUrl string `json:"repo_url"`

cmd/kosli/attestArtifact_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"strings"
57
"testing"
68

79
"github.com/stretchr/testify/suite"
@@ -114,6 +116,30 @@ func (suite *AttestArtifactCommandTestSuite) TestAttestArtifactCmd() {
114116
runTestCmd(suite.T(), tests)
115117
}
116118

119+
// TestAttestArtifactPayload_RepoInfoOmittedWhenNil ensures that when GitRepoInfo
120+
// is not available (nil), the JSON payload does not include the repo_info field
121+
// (omitempty behavior).
122+
func TestAttestArtifactPayload_RepoInfoOmittedWhenNil(t *testing.T) {
123+
payload := AttestArtifactPayload{
124+
Fingerprint: "abc123",
125+
Filename: "file1",
126+
GitCommit: "sha",
127+
BuildUrl: "https://build.example.com",
128+
CommitUrl: "https://commit.example.com",
129+
RepoUrl: "https://repo.example.com",
130+
Name: "cli",
131+
TrailName: "trail-1",
132+
GitRepoInfo: nil, // not available
133+
}
134+
data, err := json.Marshal(payload)
135+
if err != nil {
136+
t.Fatalf("marshal payload: %v", err)
137+
}
138+
if strings.Contains(string(data), "repo_info") {
139+
t.Errorf("payload must not include repo_info when GitRepoInfo is nil, got: %s", string(data))
140+
}
141+
}
142+
117143
// In order for 'go test' to run this suite, we need to create
118144
// a normal test function and pass our suite to suite.Run
119145
func TestAttestArtifactCommandTestSuite(t *testing.T) {

0 commit comments

Comments
 (0)