diff --git a/gitea/repo.go b/gitea/repo.go index 0acc6ee..afd6e40 100644 --- a/gitea/repo.go +++ b/gitea/repo.go @@ -376,3 +376,17 @@ func (c *Client) MirrorSync(owner, repo string) error { _, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/mirror-sync", owner, repo), nil, nil) return err } + +// GetRepoLanguages return language stats of a repo +func (c *Client) GetRepoLanguages(owner, repo string) (map[string]int64, error) { + langMap := make(map[string]int64) + + data, err := c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/languages", owner, repo), jsonHeader, nil) + if err != nil { + return nil, err + } + if err = json.Unmarshal(data, &langMap); err != nil { + return nil, err + } + return langMap, nil +} diff --git a/gitea/repo_test.go b/gitea/repo_test.go index 9c8d033..709a218 100644 --- a/gitea/repo_test.go +++ b/gitea/repo_test.go @@ -7,6 +7,7 @@ package gitea import ( "log" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -31,6 +32,43 @@ func TestCreateRepo(t *testing.T) { assert.NoError(t, err) } +func TestRepoMigrateAndLanguages(t *testing.T) { + log.Println("== TestMigrateRepo ==") + c := newTestClient() + user, uErr := c.GetMyUserInfo() + assert.NoError(t, uErr) + _, err := c.GetRepo(user.UserName, "sdk-mirror") + if err == nil { + _ = c.DeleteRepo(user.UserName, "sdk-mirror") + } + + repoM, err := c.MigrateRepo(MigrateRepoOption{ + CloneAddr: "https://gitea.com/gitea/go-sdk.git", + RepoName: "sdk-mirror", + UID: int(user.ID), + Mirror: true, + Private: false, + Description: "mirror sdk", + }) + assert.NoError(t, err) + + repoG, err := c.GetRepo(repoM.Owner.UserName, repoM.Name) + assert.NoError(t, err) + assert.EqualValues(t, repoM.ID, repoG.ID) + assert.EqualValues(t, "master", repoG.DefaultBranch) + assert.True(t, repoG.Mirror) + assert.False(t, repoG.Empty) + assert.EqualValues(t, 1, repoG.Watchers) + + log.Println("== TestRepoLanguages ==") + time.Sleep(time.Second) + lang, err := c.GetRepoLanguages(repoM.Owner.UserName, repoM.Name) + assert.NoError(t, err) + assert.Len(t, lang, 2) + assert.True(t, 217441 < lang["Go"]) + assert.EqualValues(t, 3578, lang["Makefile"]) +} + func TestSearchRepo(t *testing.T) { log.Println("== TestSearchRepo ==") c := newTestClient()