Skip to content

Commit

Permalink
Add git LFS - large file storage - control (#2429)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsm-kb committed Aug 13, 2022
1 parent 56a9096 commit 7a7c657
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
49 changes: 49 additions & 0 deletions github/repos_lfs.go
@@ -0,0 +1,49 @@
// Copyright 2022 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"fmt"
)

// EnableLFS turns the LFS (Large File Storage) feature ON for the selected repo.
//
// GitHub API docs: https://docs.github.com/en/rest/repos/lfs#enable-git-lfs-for-a-repository
func (s *RepositoriesService) EnableLFS(ctx context.Context, owner, repo string) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/lfs", owner, repo)

req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}

resp, err := s.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}

return resp, nil
}

// DisableLFS turns the LFS (Large File Storage) feature OFF for the selected repo.
//
// GitHub API docs: https://docs.github.com/en/rest/repos/lfs#disable-git-lfs-for-a-repository
func (s *RepositoriesService) DisableLFS(ctx context.Context, owner, repo string) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/lfs", owner, repo)

req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}

resp, err := s.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}

return resp, nil
}
64 changes: 64 additions & 0 deletions github/repos_lfs_test.go
@@ -0,0 +1,64 @@
// Copyright 2022 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"net/http"
"testing"
)

func TestRepositoriesService_EnableLFS(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/lfs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")

w.WriteHeader(http.StatusNoContent)
})

ctx := context.Background()
if _, err := client.Repositories.EnableLFS(ctx, "o", "r"); err != nil {
t.Errorf("Repositories.EnableLFS returned error: %v", err)
}

const methodName = "EnableLFS"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Repositories.EnableLFS(ctx, "\n", "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Repositories.EnableLFS(ctx, "o", "r")
})
}

func TestRepositoriesService_DisableLFS(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/lfs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")

w.WriteHeader(http.StatusNoContent)
})

ctx := context.Background()
if _, err := client.Repositories.DisableLFS(ctx, "o", "r"); err != nil {
t.Errorf("Repositories.DisableLFS returned error: %v", err)
}

const methodName = "DisableLFS"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Repositories.DisableLFS(ctx, "\n", "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Repositories.DisableLFS(ctx, "o", "r")
})
}

0 comments on commit 7a7c657

Please sign in to comment.