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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 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. // swagger:response Repository
  19. type Repository struct {
  20. ID int64 `json:"id"`
  21. Owner *User `json:"owner"`
  22. Name string `json:"name"`
  23. FullName string `json:"full_name"`
  24. Description string `json:"description"`
  25. Empty bool `json:"empty"`
  26. Private bool `json:"private"`
  27. Fork bool `json:"fork"`
  28. Parent *Repository `json:"parent"`
  29. Mirror bool `json:"mirror"`
  30. Size int `json:"size"`
  31. HTMLURL string `json:"html_url"`
  32. SSHURL string `json:"ssh_url"`
  33. CloneURL string `json:"clone_url"`
  34. Website string `json:"website"`
  35. Stars int `json:"stars_count"`
  36. Forks int `json:"forks_count"`
  37. Watchers int `json:"watchers_count"`
  38. OpenIssues int `json:"open_issues_count"`
  39. DefaultBranch string `json:"default_branch"`
  40. Created time.Time `json:"created_at"`
  41. Updated time.Time `json:"updated_at"`
  42. Permissions *Permission `json:"permissions,omitempty"`
  43. }
  44. // RepositoryList represents a list of API repository.
  45. // swagger:response RepositoryList
  46. type RepositoryList []*Repository
  47. // ListMyRepos lists all repositories for the authenticated user that has access to.
  48. func (c *Client) ListMyRepos() ([]*Repository, error) {
  49. repos := make([]*Repository, 0, 10)
  50. return repos, c.getParsedResponse("GET", "/user/repos", nil, nil, &repos)
  51. }
  52. // ListUserRepos list all repositories of one user by user's name
  53. func (c *Client) ListUserRepos(user string) ([]*Repository, error) {
  54. repos := make([]*Repository, 0, 10)
  55. return repos, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/repos", user), nil, nil, &repos)
  56. }
  57. // ListOrgRepos list all repositories of one organization by organization's name
  58. func (c *Client) ListOrgRepos(org string) ([]*Repository, error) {
  59. repos := make([]*Repository, 0, 10)
  60. return repos, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s/repos", org), nil, nil, &repos)
  61. }
  62. // CreateRepoOption options when creating repository
  63. //swagger:parameters createOrgRepo
  64. type CreateRepoOption struct {
  65. // Name of the repository to create
  66. //
  67. // in: body
  68. // unique: true
  69. Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  70. // Description of the repository to create
  71. //
  72. // in: body
  73. Description string `json:"description" binding:"MaxSize(255)"`
  74. // Is the repository to create private ?
  75. //
  76. // in: body
  77. Private bool `json:"private"`
  78. // Init the repository to create ?
  79. //
  80. // in: body
  81. AutoInit bool `json:"auto_init"`
  82. // Gitignores to use
  83. //
  84. // in: body
  85. Gitignores string `json:"gitignores"`
  86. // License to use
  87. //
  88. // in: body
  89. License string `json:"license"`
  90. // Readme of the repository to create
  91. //
  92. // in: body
  93. Readme string `json:"readme"`
  94. }
  95. // CreateRepo creates a repository for authenticated user.
  96. func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
  97. body, err := json.Marshal(&opt)
  98. if err != nil {
  99. return nil, err
  100. }
  101. repo := new(Repository)
  102. return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
  103. }
  104. // CreateOrgRepo creates an organization repository for authenticated user.
  105. func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*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", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
  112. }
  113. // GetRepo returns information of a repository of given owner.
  114. func (c *Client) GetRepo(owner, reponame string) (*Repository, error) {
  115. repo := new(Repository)
  116. return repo, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s", owner, reponame), nil, nil, repo)
  117. }
  118. // DeleteRepo deletes a repository of user or organization.
  119. func (c *Client) DeleteRepo(owner, repo string) error {
  120. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s", owner, repo), nil, nil)
  121. return err
  122. }
  123. // MigrateRepoOption options when migrate repository from an external place
  124. type MigrateRepoOption struct {
  125. CloneAddr string `json:"clone_addr" binding:"Required"`
  126. AuthUsername string `json:"auth_username"`
  127. AuthPassword string `json:"auth_password"`
  128. UID int `json:"uid" binding:"Required"`
  129. RepoName string `json:"repo_name" binding:"Required"`
  130. Mirror bool `json:"mirror"`
  131. Private bool `json:"private"`
  132. Description string `json:"description"`
  133. }
  134. // MigrateRepo migrates a repository from other Git hosting sources for the
  135. // authenticated user.
  136. //
  137. // To migrate a repository for a organization, the authenticated user must be a
  138. // owner of the specified organization.
  139. func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
  140. body, err := json.Marshal(&opt)
  141. if err != nil {
  142. return nil, err
  143. }
  144. repo := new(Repository)
  145. return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
  146. }
  147. // MirrorSync adds a mirrored repository to the mirror sync queue.
  148. func (c *Client) MirrorSync(owner, repo string) error {
  149. _, err := c.getResponse("POST", fmt.Sprintf("/repos/%s/%s/mirror-sync", owner, repo), nil, nil)
  150. return err
  151. }