repo.go 4.4 KB

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