repo.go 4.4 KB

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