Skip to content

Commit 5a53046

Browse files
committed
handle lightweight tags in get_tag
1 parent 4bded57 commit 5a53046

2 files changed

Lines changed: 66 additions & 14 deletions

File tree

pkg/github/repositories.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,15 @@ func GetTag(t translations.TranslationHelperFunc) inventory.ServerTool {
16321632
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to get tag reference", resp, body), nil, nil
16331633
}
16341634

1635-
// Then get the tag object
1635+
// Differentiate between lightweight and annotated tags since lightweight ones don't have a fetchable object
1636+
if ref.Object.GetType() == "commit" {
1637+
r, err := json.Marshal(ref)
1638+
if err != nil {
1639+
return nil, nil, fmt.Errorf("failed to marshal response: %w", err)
1640+
}
1641+
return utils.NewToolResultText(string(r)), nil, nil
1642+
}
1643+
16361644
tagObj, resp, err := client.Git.GetTag(ctx, owner, repo, *ref.Object.SHA)
16371645
if err != nil {
16381646
return ghErrors.NewGitHubAPIErrorResponse(ctx,

pkg/github/repositories_test.go

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,10 +2914,19 @@ func Test_GetTag(t *testing.T) {
29142914
assert.Contains(t, schema.Properties, "tag")
29152915
assert.ElementsMatch(t, schema.Required, []string{"owner", "repo", "tag"})
29162916

2917-
mockTagRef := &github.Reference{
2917+
mockAnnotatedTagRef := &github.Reference{
29182918
Ref: github.Ptr("refs/tags/v1.0.0"),
29192919
Object: &github.GitObject{
2920-
SHA: github.Ptr("v1.0.0-tag-sha"),
2920+
Type: github.Ptr("tag"),
2921+
SHA: github.Ptr("v1.0.0-tag-sha"),
2922+
},
2923+
}
2924+
2925+
mockLightweightTagRef := &github.Reference{
2926+
Ref: github.Ptr("refs/tags/v1.0.1"),
2927+
Object: &github.GitObject{
2928+
Type: github.Ptr("commit"),
2929+
SHA: github.Ptr("abc123"),
29212930
},
29222931
}
29232932

@@ -2937,6 +2946,7 @@ func Test_GetTag(t *testing.T) {
29372946
requestArgs map[string]any
29382947
expectError bool
29392948
expectedTag *github.Tag
2949+
expectedRef *github.Reference
29402950
expectedErrMsg string
29412951
}{
29422952
{
@@ -2948,7 +2958,7 @@ func Test_GetTag(t *testing.T) {
29482958
t,
29492959
"/repos/owner/repo/git/ref/tags/v1.0.0",
29502960
).andThen(
2951-
mockResponse(t, http.StatusOK, mockTagRef),
2961+
mockResponse(t, http.StatusOK, mockAnnotatedTagRef),
29522962
),
29532963
),
29542964
WithRequestMatchHandler(
@@ -2993,7 +3003,7 @@ func Test_GetTag(t *testing.T) {
29933003
mockedClient: NewMockedHTTPClient(
29943004
WithRequestMatch(
29953005
GetReposGitRefByOwnerByRepoByRef,
2996-
mockTagRef,
3006+
mockAnnotatedTagRef,
29973007
),
29983008
WithRequestMatchHandler(
29993009
GetReposGitTagsByOwnerByRepoByTagSHA,
@@ -3011,6 +3021,27 @@ func Test_GetTag(t *testing.T) {
30113021
expectError: true,
30123022
expectedErrMsg: "failed to get tag object",
30133023
},
3024+
{
3025+
name: "successful lightweight tag retrieval",
3026+
mockedClient: NewMockedHTTPClient(
3027+
WithRequestMatchHandler(
3028+
GetReposGitRefByOwnerByRepoByRef,
3029+
expectPath(
3030+
t,
3031+
"/repos/owner/repo/git/ref/tags/v1.0.1",
3032+
).andThen(
3033+
mockResponse(t, http.StatusOK, mockLightweightTagRef),
3034+
),
3035+
),
3036+
),
3037+
requestArgs: map[string]any{
3038+
"owner": "owner",
3039+
"repo": "repo",
3040+
"tag": "v1.0.1",
3041+
},
3042+
expectError: false,
3043+
expectedRef: mockLightweightTagRef,
3044+
},
30143045
}
30153046

30163047
for _, tc := range tests {
@@ -3043,16 +3074,29 @@ func Test_GetTag(t *testing.T) {
30433074
// Parse the result and get the text content if no error
30443075
textContent := getTextResult(t, result)
30453076

3046-
// Parse and verify the result
3047-
var returnedTag github.Tag
3048-
err = json.Unmarshal([]byte(textContent.Text), &returnedTag)
3049-
require.NoError(t, err)
3077+
// Parse and verify the result - annotated tag (full tag object)
3078+
if tc.expectedTag != nil {
3079+
var returnedTag github.Tag
3080+
err = json.Unmarshal([]byte(textContent.Text), &returnedTag)
3081+
require.NoError(t, err)
3082+
3083+
assert.Equal(t, tc.expectedTag.GetSHA(), returnedTag.GetSHA())
3084+
assert.Equal(t, tc.expectedTag.GetTag(), returnedTag.GetTag())
3085+
assert.Equal(t, tc.expectedTag.GetMessage(), returnedTag.GetMessage())
3086+
assert.Equal(t, tc.expectedTag.Object.GetType(), returnedTag.Object.GetType())
3087+
assert.Equal(t, tc.expectedTag.Object.GetSHA(), returnedTag.Object.GetSHA())
3088+
}
30503089

3051-
assert.Equal(t, *tc.expectedTag.SHA, *returnedTag.SHA)
3052-
assert.Equal(t, *tc.expectedTag.Tag, *returnedTag.Tag)
3053-
assert.Equal(t, *tc.expectedTag.Message, *returnedTag.Message)
3054-
assert.Equal(t, *tc.expectedTag.Object.Type, *returnedTag.Object.Type)
3055-
assert.Equal(t, *tc.expectedTag.Object.SHA, *returnedTag.Object.SHA)
3090+
// Parse and verify the result - lightweight tag (reference only)
3091+
if tc.expectedRef != nil {
3092+
var returnedRef github.Reference
3093+
err = json.Unmarshal([]byte(textContent.Text), &returnedRef)
3094+
require.NoError(t, err)
3095+
3096+
assert.Equal(t, tc.expectedRef.GetRef(), returnedRef.GetRef())
3097+
assert.Equal(t, tc.expectedRef.Object.GetType(), returnedRef.Object.GetType())
3098+
assert.Equal(t, tc.expectedRef.Object.GetSHA(), returnedRef.Object.GetSHA())
3099+
}
30563100
})
30573101
}
30583102
}

0 commit comments

Comments
 (0)