@@ -1541,49 +1541,96 @@ func Test_DeleteFile(t *testing.T) {
1541
1541
assert .Contains (t , tool .InputSchema .Properties , "path" )
1542
1542
assert .Contains (t , tool .InputSchema .Properties , "message" )
1543
1543
assert .Contains (t , tool .InputSchema .Properties , "branch" )
1544
- assert . Contains ( t , tool . InputSchema . Properties , "sha" )
1545
- assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" , "path" , "message" , "branch" , "sha" })
1544
+ // SHA is no longer required since we're using Git Data API
1545
+ assert .ElementsMatch (t , tool .InputSchema .Required , []string {"owner" , "repo" , "path" , "message" , "branch" })
1546
1546
1547
- // Setup mock delete response
1548
- mockDeleteResponse := & github.RepositoryContentResponse {
1549
- Content : & github.RepositoryContent {
1550
- Name : github .Ptr ("example.md" ),
1551
- Path : github .Ptr ("docs/example.md" ),
1552
- SHA : github .Ptr ("abc123def456" ),
1553
- HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/docs/example.md" ),
1547
+ // Setup mock objects for Git Data API
1548
+ mockRef := & github.Reference {
1549
+ Ref : github .Ptr ("refs/heads/main" ),
1550
+ Object : & github.GitObject {
1551
+ SHA : github .Ptr ("abc123" ),
1554
1552
},
1555
- Commit : github.Commit {
1556
- SHA : github .Ptr ("def456abc789" ),
1557
- Message : github .Ptr ("Delete example file" ),
1558
- Author : & github.CommitAuthor {
1559
- Name : github .Ptr ("Test User" ),
1560
- Email : github .Ptr ("test@example.com" ),
1561
- Date : & github.Timestamp {Time : time .Now ()},
1562
- },
1563
- HTMLURL : github .Ptr ("https://github.com/owner/repo/commit/def456abc789" ),
1553
+ }
1554
+
1555
+ mockCommit := & github.Commit {
1556
+ SHA : github .Ptr ("abc123" ),
1557
+ Tree : & github.Tree {
1558
+ SHA : github .Ptr ("def456" ),
1564
1559
},
1565
1560
}
1566
1561
1562
+ mockTree := & github.Tree {
1563
+ SHA : github .Ptr ("ghi789" ),
1564
+ }
1565
+
1566
+ mockNewCommit := & github.Commit {
1567
+ SHA : github .Ptr ("jkl012" ),
1568
+ Message : github .Ptr ("Delete example file" ),
1569
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/commit/jkl012" ),
1570
+ }
1571
+
1567
1572
tests := []struct {
1568
- name string
1569
- mockedClient * http.Client
1570
- requestArgs map [string ]interface {}
1571
- expectError bool
1572
- expectedResult * github. RepositoryContentResponse
1573
- expectedErrMsg string
1573
+ name string
1574
+ mockedClient * http.Client
1575
+ requestArgs map [string ]interface {}
1576
+ expectError bool
1577
+ expectedCommitSHA string
1578
+ expectedErrMsg string
1574
1579
}{
1575
1580
{
1576
- name : "successful file deletion" ,
1581
+ name : "successful file deletion using Git Data API " ,
1577
1582
mockedClient : mock .NewMockedHTTPClient (
1583
+ // Get branch reference
1584
+ mock .WithRequestMatch (
1585
+ mock .GetReposGitRefByOwnerByRepoByRef ,
1586
+ mockRef ,
1587
+ ),
1588
+ // Get commit
1589
+ mock .WithRequestMatch (
1590
+ mock .GetReposGitCommitsByOwnerByRepoByCommitSha ,
1591
+ mockCommit ,
1592
+ ),
1593
+ // Create tree
1594
+ mock .WithRequestMatchHandler (
1595
+ mock .PostReposGitTreesByOwnerByRepo ,
1596
+ expectRequestBody (t , map [string ]interface {}{
1597
+ "base_tree" : "def456" ,
1598
+ "tree" : []interface {}{
1599
+ map [string ]interface {}{
1600
+ "path" : "docs/example.md" ,
1601
+ "mode" : "100644" ,
1602
+ "type" : "blob" ,
1603
+ "sha" : nil ,
1604
+ },
1605
+ },
1606
+ }).andThen (
1607
+ mockResponse (t , http .StatusCreated , mockTree ),
1608
+ ),
1609
+ ),
1610
+ // Create commit
1578
1611
mock .WithRequestMatchHandler (
1579
- mock .DeleteReposContentsByOwnerByRepoByPath ,
1612
+ mock .PostReposGitCommitsByOwnerByRepo ,
1580
1613
expectRequestBody (t , map [string ]interface {}{
1581
1614
"message" : "Delete example file" ,
1582
- "branch" : "main" ,
1583
- "sha" : "abc123def456" ,
1584
- "content" : interface {}(nil ),
1615
+ "tree" : "ghi789" ,
1616
+ "parents" : []interface {}{"abc123" },
1617
+ }).andThen (
1618
+ mockResponse (t , http .StatusCreated , mockNewCommit ),
1619
+ ),
1620
+ ),
1621
+ // Update reference
1622
+ mock .WithRequestMatchHandler (
1623
+ mock .PatchReposGitRefsByOwnerByRepoByRef ,
1624
+ expectRequestBody (t , map [string ]interface {}{
1625
+ "sha" : "jkl012" ,
1626
+ "force" : false ,
1585
1627
}).andThen (
1586
- mockResponse (t , http .StatusOK , mockDeleteResponse ),
1628
+ mockResponse (t , http .StatusOK , & github.Reference {
1629
+ Ref : github .Ptr ("refs/heads/main" ),
1630
+ Object : & github.GitObject {
1631
+ SHA : github .Ptr ("jkl012" ),
1632
+ },
1633
+ }),
1587
1634
),
1588
1635
),
1589
1636
),
@@ -1593,19 +1640,18 @@ func Test_DeleteFile(t *testing.T) {
1593
1640
"path" : "docs/example.md" ,
1594
1641
"message" : "Delete example file" ,
1595
1642
"branch" : "main" ,
1596
- "sha" : "abc123def456" ,
1597
1643
},
1598
- expectError : false ,
1599
- expectedResult : mockDeleteResponse ,
1644
+ expectError : false ,
1645
+ expectedCommitSHA : "jkl012" ,
1600
1646
},
1601
1647
{
1602
- name : "file deletion fails" ,
1648
+ name : "file deletion fails - branch not found " ,
1603
1649
mockedClient : mock .NewMockedHTTPClient (
1604
1650
mock .WithRequestMatchHandler (
1605
- mock .DeleteReposContentsByOwnerByRepoByPath ,
1651
+ mock .GetReposGitRefByOwnerByRepoByRef ,
1606
1652
http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
1607
1653
w .WriteHeader (http .StatusNotFound )
1608
- _ , _ = w .Write ([]byte (`{"message": "Not Found "}` ))
1654
+ _ , _ = w .Write ([]byte (`{"message": "Reference not found "}` ))
1609
1655
}),
1610
1656
),
1611
1657
),
@@ -1614,11 +1660,10 @@ func Test_DeleteFile(t *testing.T) {
1614
1660
"repo" : "repo" ,
1615
1661
"path" : "docs/nonexistent.md" ,
1616
1662
"message" : "Delete nonexistent file" ,
1617
- "branch" : "main" ,
1618
- "sha" : "abc123def456" ,
1663
+ "branch" : "nonexistent-branch" ,
1619
1664
},
1620
1665
expectError : true ,
1621
- expectedErrMsg : "failed to delete file " ,
1666
+ expectedErrMsg : "failed to get branch reference " ,
1622
1667
},
1623
1668
}
1624
1669
@@ -1647,20 +1692,16 @@ func Test_DeleteFile(t *testing.T) {
1647
1692
textContent := getTextResult (t , result )
1648
1693
1649
1694
// Unmarshal and verify the result
1650
- var returnedContent github. RepositoryContentResponse
1651
- err = json .Unmarshal ([]byte (textContent .Text ), & returnedContent )
1695
+ var response map [ string ] interface {}
1696
+ err = json .Unmarshal ([]byte (textContent .Text ), & response )
1652
1697
require .NoError (t , err )
1653
1698
1654
- // Verify content
1655
- if tc .expectedResult .Content != nil {
1656
- assert .Equal (t , * tc .expectedResult .Content .Name , * returnedContent .Content .Name )
1657
- assert .Equal (t , * tc .expectedResult .Content .Path , * returnedContent .Content .Path )
1658
- assert .Equal (t , * tc .expectedResult .Content .SHA , * returnedContent .Content .SHA )
1659
- }
1660
-
1661
- // Verify commit
1662
- assert .Equal (t , * tc .expectedResult .Commit .SHA , * returnedContent .Commit .SHA )
1663
- assert .Equal (t , * tc .expectedResult .Commit .Message , * returnedContent .Commit .Message )
1699
+ // Verify the response contains the expected commit
1700
+ commit , ok := response ["commit" ].(map [string ]interface {})
1701
+ require .True (t , ok )
1702
+ commitSHA , ok := commit ["sha" ].(string )
1703
+ require .True (t , ok )
1704
+ assert .Equal (t , tc .expectedCommitSHA , commitSHA )
1664
1705
})
1665
1706
}
1666
1707
}
0 commit comments