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

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