Merge pull request 'feat: add GetChangedFiles() to Gitea client (#205)' (#208) from feat/get-changed-files-205 into master
This commit was merged in pull request #208.
This commit is contained in:
@@ -760,6 +760,33 @@ func (c *Client) GetPull(ctx context.Context, token, owner, repo string, index i
|
||||
return &pr, nil
|
||||
}
|
||||
|
||||
// ChangedFile represents a file changed in a pull request.
|
||||
type ChangedFile struct {
|
||||
Filename string `json:"filename"`
|
||||
Status string `json:"status"` // "added", "modified", "removed", "renamed"
|
||||
Additions int `json:"additions"`
|
||||
Deletions int `json:"deletions"`
|
||||
Changes int `json:"changes"`
|
||||
PreviousFilename string `json:"previous_filename,omitempty"`
|
||||
}
|
||||
|
||||
// GetChangedFiles fetches the list of files changed in a pull request.
|
||||
func (c *Client) GetChangedFiles(ctx context.Context, token, owner, repo string, index int64) ([]ChangedFile, error) {
|
||||
path := fmt.Sprintf("/repos/%s/%s/pulls/%d/files?limit=50", owner, repo, index)
|
||||
resp, err := c.doRequest(ctx, token, http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetching changed files: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var files []ChangedFile
|
||||
if err := json.NewDecoder(resp.Body).Decode(&files); err != nil {
|
||||
return nil, fmt.Errorf("decoding changed files: %w", err)
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// GetIssueComments fetches comments for an issue or pull request.
|
||||
func (c *Client) GetIssueComments(ctx context.Context, token, owner, repo string, index int64) ([]Comment, error) {
|
||||
path := fmt.Sprintf("/repos/%s/%s/issues/%d/comments?limit=50", owner, repo, index)
|
||||
|
||||
@@ -1456,3 +1456,63 @@ func TestRetryDelay_ExponentialBackoff(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetChangedFiles(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("expected GET, got %s", r.Method)
|
||||
}
|
||||
if r.URL.Path != "/api/v1/repos/owner1/repo1/pulls/5/files" {
|
||||
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
if r.Header.Get("Authorization") != "token test-token" {
|
||||
t.Error("missing or wrong Authorization header")
|
||||
}
|
||||
|
||||
files := []ChangedFile{
|
||||
{Filename: "main.go", Status: "modified", Additions: 10, Deletions: 3, Changes: 13},
|
||||
{Filename: "new_file.go", Status: "added", Additions: 25, Deletions: 0, Changes: 25},
|
||||
{Filename: "old_file.go", Status: "removed", Additions: 0, Deletions: 15, Changes: 15},
|
||||
}
|
||||
json.NewEncoder(w).Encode(files)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c := NewClient(server.URL)
|
||||
files, err := c.GetChangedFiles(context.Background(), "test-token", "owner1", "repo1", 5)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(files) != 3 {
|
||||
t.Fatalf("got %d files, want 3", len(files))
|
||||
}
|
||||
if files[0].Filename != "main.go" {
|
||||
t.Errorf("files[0].Filename = %q, want %q", files[0].Filename, "main.go")
|
||||
}
|
||||
if files[0].Status != "modified" {
|
||||
t.Errorf("files[0].Status = %q, want %q", files[0].Status, "modified")
|
||||
}
|
||||
if files[1].Status != "added" {
|
||||
t.Errorf("files[1].Status = %q, want %q", files[1].Status, "added")
|
||||
}
|
||||
if files[2].Status != "removed" {
|
||||
t.Errorf("files[2].Status = %q, want %q", files[2].Status, "removed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetChangedFiles_Error(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
fmt.Fprintln(w, `{"message":"pull request not found"}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
c := NewClient(server.URL)
|
||||
_, err := c.GetChangedFiles(context.Background(), "test-token", "owner1", "repo1", 999)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for 404 response, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "404") {
|
||||
t.Errorf("error should contain status code 404, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user