You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 gitea
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. // Permission represents a set of permissions
  12. type Permission struct {
  13. Admin bool `json:"admin"`
  14. Push bool `json:"push"`
  15. Pull bool `json:"pull"`
  16. }
  17. // Repository represents a 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. Empty bool `json:"empty"`
  25. Private bool `json:"private"`
  26. Fork bool `json:"fork"`
  27. Parent *Repository `json:"parent"`
  28. Mirror bool `json:"mirror"`
  29. Size int `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. Archived bool `json:"archived"`
  40. // swagger:strfmt date-time
  41. Created time.Time `json:"created_at"`
  42. // swagger:strfmt date-time
  43. Updated time.Time `json:"updated_at"`
  44. Permissions *Permission `json:"permissions,omitempty"`
  45. }
  46. // ListMyRepos lists all repositories for the authenticated user that has access to.
  47. func (c *Client) ListMyRepos() ([]*Repository, error) {
  48. repos := make([]*Repository, 0, 10)
  49. return repos, c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
  50. }
  51. // ListUserRepos list all repositories of one user by user's name
  52. func (c *Client) ListUserRepos(user string) ([]*Repository, error) {
  53. repos := make([]*Repository, 0, 10)
  54. return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos", user), nil, nil, &repos)
  55. }
  56. // ListOrgRepos list all repositories of one organization by organization's name
  57. func (c *Client) ListOrgRepos(org string) ([]*Repository, error) {
  58. repos := make([]*Repository, 0, 10)
  59. return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos)
  60. }
  61. // CreateRepoOption options when creating repository
  62. // swagger:model
  63. type CreateRepoOption struct {
  64. // Name of the repository to create
  65. //
  66. // required: true
  67. // unique: true
  68. Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  69. // Description of the repository to create
  70. Description string `json:"description" binding:"MaxSize(255)"`
  71. // Whether the repository is private
  72. Private bool `json:"private"`
  73. // Whether the repository should be auto-intialized?
  74. AutoInit bool `json:"auto_init"`
  75. // Gitignores to use
  76. Gitignores string `json:"gitignores"`
  77. // License to use
  78. License string `json:"license"`
  79. // Readme of the repository to create
  80. Readme string `json:"readme"`
  81. }
  82. // CreateRepo creates a repository for authenticated user.
  83. func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
  84. body, err := json.Marshal(&opt)
  85. if err != nil {
  86. return nil, err
  87. }
  88. repo := new(Repository)
  89. return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
  90. }
  91. // CreateOrgRepo creates an organization repository for authenticated user.
  92. func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, error) {
  93. body, err := json.Marshal(&opt)
  94. if err != nil {
  95. return nil, err
  96. }
  97. repo := new(Repository)
  98. return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
  99. }
  100. // GetRepo returns information of a repository of given owner.
  101. func (c *Client) GetRepo(owner, reponame string) (*Repository, error) {
  102. repo := new(Repository)
  103. return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), nil, nil, repo)
  104. }
  105. // DeleteRepo deletes a repository of user or organization.
  106. func (c *Client) DeleteRepo(owner, repo string) error {
  107. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil)
  108. return err
  109. }
  110. // MigrateRepoOption options for migrating a repository from an external service
  111. type MigrateRepoOption struct {
  112. // required: true
  113. CloneAddr string `json:"clone_addr" binding:"Required"`
  114. AuthUsername string `json:"auth_username"`
  115. AuthPassword string `json:"auth_password"`
  116. // required: true
  117. UID int `json:"uid" binding:"Required"`
  118. // required: true
  119. RepoName string `json:"repo_name" binding:"Required"`
  120. Mirror bool `json:"mirror"`
  121. Private bool `json:"private"`
  122. Description string `json:"description"`
  123. }
  124. // MigrateRepo migrates a repository from other Git hosting sources for the
  125. // authenticated user.
  126. //
  127. // To migrate a repository for a organization, the authenticated user must be a
  128. // owner of the specified organization.
  129. func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
  130. body, err := json.Marshal(&opt)
  131. if err != nil {
  132. return nil, err
  133. }
  134. repo := new(Repository)
  135. return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
  136. }
  137. // MirrorSync adds a mirrored repository to the mirror sync queue.
  138. func (c *Client) MirrorSync(owner, repo string) error {
  139. _, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/mirror-sync", owner, repo), nil, nil)
  140. return err
  141. }