Skip to content

Commit a991e37

Browse files
appleboylunny
andcommitted
feat: implement commit comparison feature in Gitea client (#659)
See the API: go-gitea/gitea#30349 - Add a new file `repo_compare.go` with package `gitea` and `Compare` struct - Implement `CompareCommits` method in `Client` struct in `repo_compare.go` - Add `version1_22_0` constant in `version.go` Signed-off-by: appleboy <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/659 Reviewed-by: Lunny Xiao <[email protected]> Co-authored-by: appleboy <[email protected]> Co-committed-by: appleboy <[email protected]>
1 parent bb25c98 commit a991e37

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

gitea/repo_compare.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gitea
6+
7+
import "fmt"
8+
9+
// Compare represents a comparison between two commits.
10+
type Compare struct {
11+
TotalCommits int `json:"total_commits"` // Total number of commits in the comparison.
12+
Commits []*Commit `json:"commits"` // List of commits in the comparison.
13+
}
14+
15+
// CompareCommits compares two commits in a repository.
16+
func (c *Client) CompareCommits(user, repo, prev, current string) (*Compare, *Response, error) {
17+
if err := c.checkServerVersionGreaterThanOrEqual(version1_22_0); err != nil {
18+
return nil, nil, err
19+
}
20+
if err := escapeValidatePathSegments(&user, &repo, &prev, &current); err != nil {
21+
return nil, nil, err
22+
}
23+
24+
basehead := fmt.Sprintf("%s...%s", prev, current)
25+
26+
apiResp := new(Compare)
27+
resp, err := c.getParsedResponse(
28+
"GET",
29+
fmt.Sprintf("/repos/%s/%s/compare/%s", user, repo, basehead),
30+
nil, nil, apiResp,
31+
)
32+
return apiResp, resp, err
33+
}

gitea/version.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var (
6969
version1_15_0 = version.Must(version.NewVersion("1.15.0"))
7070
version1_16_0 = version.Must(version.NewVersion("1.16.0"))
7171
version1_17_0 = version.Must(version.NewVersion("1.17.0"))
72+
version1_22_0 = version.Must(version.NewVersion("1.22.0"))
7273
)
7374

7475
// ErrUnknownVersion is an unknown version from the API

0 commit comments

Comments
 (0)