repo.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2014 The Gogs 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. package gogs
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. // Permission represents a API permission.
  12. type Permission struct {
  13. Admin bool `json:"admin"`
  14. Push bool `json:"push"`
  15. Pull bool `json:"pull"`
  16. }
  17. // Repository represents a API repository.
  18. type Repository struct {
  19. ID int64 `json:"id"`
  20. Owner *User `json:"owner"`
  21. Name string `json:"name"`
  22. FullName string `json:"full_name"`
  23. Description string `json:"description"`
  24. Private bool `json:"private"`
  25. Fork bool `json:"fork"`
  26. Parent *Repository `json:"parent"`
  27. Empty bool `json:"empty"`
  28. Mirror bool `json:"mirror"`
  29. Size int64 `json:"size"`
  30. HTMLURL string `json:"html_url"`
  31. SSHURL string `json:"ssh_url"`
  32. CloneURL string `json:"clone_url"`
  33. Website string `json:"website"`
  34. Stars int `json:"stars_count"`
  35. Forks int `json:"forks_count"`
  36. Watchers int `json:"watchers_count"`
  37. OpenIssues int `json:"open_issues_count"`
  38. DefaultBranch string `json:"default_branch"`
  39. Created time.Time `json:"created_at"`
  40. Updated time.Time `json:"updated_at"`
  41. Permissions *Permission `json:"permissions,omitempty"`
  42. }
  43. // ListMyRepos lists all repositories for the authenticated user that has access to.
  44. func (c *Client) ListMyRepos() ([]*Repository, error) {
  45. repos := make([]*Repository, 0, 10)
  46. return repos, c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
  47. }
  48. func (c *Client) ListUserRepos(user string) ([]*Repository, error) {
  49. repos := make([]*Repository, 0, 10)
  50. return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos", user), nil, nil, &repos)
  51. }
  52. func (c *Client) ListOrgRepos(org string) ([]*Repository, error) {
  53. repos := make([]*Repository, 0, 10)
  54. return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos)
  55. }
  56. type CreateRepoOption struct {
  57. Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  58. Description string `json:"description" binding:"MaxSize(255)"`
  59. Private bool `json:"private"`
  60. AutoInit bool `json:"auto_init"`
  61. Gitignores string `json:"gitignores"`
  62. License string `json:"license"`
  63. Readme string `json:"readme"`
  64. }
  65. // CreateRepo creates a repository for authenticated user.
  66. func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
  67. body, err := json.Marshal(&opt)
  68. if err != nil {
  69. return nil, err
  70. }
  71. repo := new(Repository)
  72. return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
  73. }
  74. // CreateOrgRepo creates an organization repository for authenticated user.
  75. func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, error) {
  76. body, err := json.Marshal(&opt)
  77. if err != nil {
  78. return nil, err
  79. }
  80. repo := new(Repository)
  81. return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
  82. }
  83. // GetRepo returns information of a repository of given owner.
  84. func (c *Client) GetRepo(owner, reponame string) (*Repository, error) {
  85. repo := new(Repository)
  86. return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), nil, nil, repo)
  87. }
  88. // DeleteRepo deletes a repository of user or organization.
  89. func (c *Client) DeleteRepo(owner, repo string) error {
  90. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil)
  91. return err
  92. }
  93. type MigrateRepoOption struct {
  94. CloneAddr string `json:"clone_addr" binding:"Required"`
  95. AuthUsername string `json:"auth_username"`
  96. AuthPassword string `json:"auth_password"`
  97. UID int `json:"uid" binding:"Required"`
  98. RepoName string `json:"repo_name" binding:"Required"`
  99. Mirror bool `json:"mirror"`
  100. Private bool `json:"private"`
  101. Description string `json:"description"`
  102. }
  103. // MigrateRepo migrates a repository from other Git hosting sources for the
  104. // authenticated user.
  105. //
  106. // To migrate a repository for a organization, the authenticated user must be a
  107. // owner of the specified organization.
  108. func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
  109. body, err := json.Marshal(&opt)
  110. if err != nil {
  111. return nil, err
  112. }
  113. repo := new(Repository)
  114. return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
  115. }