diff --git a/repos.go b/repos.go index fdabd79..b40af7f 100644 --- a/repos.go +++ b/repos.go @@ -383,6 +383,22 @@ func (s *RepoService) GetRawFileOrLFS(ctx context.Context, owner, repo, filepath return s.client.GetRaw(ctx, path) } +// GetEditorConfig returns the EditorConfig definitions for a repository file. +func (s *RepoService) GetEditorConfig(ctx context.Context, owner, repo, filepath, ref string) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/editorconfig/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath)) + if ref != "" { + u, err := url.Parse(path) + if err != nil { + return err + } + q := u.Query() + q.Set("ref", ref) + u.RawQuery = q.Encode() + path = u.String() + } + return s.client.Get(ctx, path, nil) +} + // ApplyDiffPatch applies a diff patch to a repository. func (s *RepoService) ApplyDiffPatch(ctx context.Context, owner, repo string, opts *types.UpdateFileOptions) (*types.FileResponse, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/diffpatch", pathParams("owner", owner, "repo", repo)) diff --git a/repos_test.go b/repos_test.go index 9c39ab9..6fca0ce 100644 --- a/repos_test.go +++ b/repos_test.go @@ -573,6 +573,42 @@ func TestRepoService_GetRawFileOrLFS_Good(t *testing.T) { } } +func TestRepoService_GetEditorConfig_Good(t *testing.T) { + srv := 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/core/go-forge/editorconfig/README.md" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + if got := r.URL.Query().Get("ref"); got != "main" { + t.Errorf("wrong ref: %s", got) + } + w.WriteHeader(http.StatusOK) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Repos.GetEditorConfig(context.Background(), "core", "go-forge", "README.md", "main"); err != nil { + t.Fatal(err) + } +} + +func TestRepoService_GetEditorConfig_Bad_NotFound(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(map[string]string{"message": "not found"}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Repos.GetEditorConfig(context.Background(), "core", "go-forge", "README.md", "main"); !IsNotFound(err) { + t.Fatalf("expected not found, got %v", err) + } +} + func TestRepoService_ApplyDiffPatch_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost {