Skip to content

Commit 391990a

Browse files
authored
Reduce context usage for list_tags (#2088)
1 parent c1ac64f commit 391990a

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

pkg/github/minimal_types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ type MinimalBranch struct {
122122
Protected bool `json:"protected"`
123123
}
124124

125+
// MinimalTag is the trimmed output type for tag objects.
126+
type MinimalTag struct {
127+
Name string `json:"name"`
128+
SHA string `json:"sha"`
129+
}
130+
125131
// MinimalResponse represents a minimal response for all CRUD operations.
126132
// Success is implicit in the HTTP response status, and all other information
127133
// can be derived from the URL or fetched separately if needed.
@@ -702,6 +708,18 @@ func convertToMinimalBranch(branch *github.Branch) MinimalBranch {
702708
}
703709
}
704710

711+
func convertToMinimalTag(tag *github.RepositoryTag) MinimalTag {
712+
m := MinimalTag{
713+
Name: tag.GetName(),
714+
}
715+
716+
if commit := tag.GetCommit(); commit != nil {
717+
m.SHA = commit.GetSHA()
718+
}
719+
720+
return m
721+
}
722+
705723
// MinimalCheckRun is the trimmed output type for check run objects.
706724
type MinimalCheckRun struct {
707725
ID int64 `json:"id"`

pkg/github/repositories.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,14 @@ func ListTags(t translations.TranslationHelperFunc) inventory.ServerTool {
14971497
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to list tags", resp, body), nil, nil
14981498
}
14991499

1500-
r, err := json.Marshal(tags)
1500+
minimalTags := make([]MinimalTag, 0, len(tags))
1501+
for _, tag := range tags {
1502+
if tag != nil {
1503+
minimalTags = append(minimalTags, convertToMinimalTag(tag))
1504+
}
1505+
}
1506+
1507+
r, err := json.Marshal(minimalTags)
15011508
if err != nil {
15021509
return nil, nil, fmt.Errorf("failed to marshal response: %w", err)
15031510
}

pkg/github/repositories_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,15 +2791,15 @@ func Test_ListTags(t *testing.T) {
27912791
textContent := getTextResult(t, result)
27922792

27932793
// Parse and verify the result
2794-
var returnedTags []*github.RepositoryTag
2794+
var returnedTags []MinimalTag
27952795
err = json.Unmarshal([]byte(textContent.Text), &returnedTags)
27962796
require.NoError(t, err)
27972797

27982798
// Verify each tag
27992799
require.Equal(t, len(tc.expectedTags), len(returnedTags))
28002800
for i, expectedTag := range tc.expectedTags {
2801-
assert.Equal(t, *expectedTag.Name, *returnedTags[i].Name)
2802-
assert.Equal(t, *expectedTag.Commit.SHA, *returnedTags[i].Commit.SHA)
2801+
assert.Equal(t, *expectedTag.Name, returnedTags[i].Name)
2802+
assert.Equal(t, *expectedTag.Commit.SHA, returnedTags[i].SHA)
28032803
}
28042804
})
28052805
}

0 commit comments

Comments
 (0)