Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

repos_forks.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "encoding/json"
  10. )
  11. // RepositoryListForksOptions specifies the optional parameters to the
  12. // RepositoriesService.ListForks method.
  13. type RepositoryListForksOptions struct {
  14. // How to sort the forks list. Possible values are: newest, oldest,
  15. // watchers. Default is "newest".
  16. Sort string `url:"sort,omitempty"`
  17. ListOptions
  18. }
  19. // ListForks lists the forks of the specified repository.
  20. //
  21. // GitHub API docs: https://developer.github.com/v3/repos/forks/#list-forks
  22. func (s *RepositoriesService) ListForks(ctx context.Context, owner, repo string, opt *RepositoryListForksOptions) ([]*Repository, *Response, error) {
  23. u := fmt.Sprintf("repos/%v/%v/forks", owner, repo)
  24. u, err := addOptions(u, opt)
  25. if err != nil {
  26. return nil, nil, err
  27. }
  28. req, err := s.client.NewRequest("GET", u, nil)
  29. if err != nil {
  30. return nil, nil, err
  31. }
  32. // TODO: remove custom Accept header when topics API fully launches.
  33. req.Header.Set("Accept", mediaTypeTopicsPreview)
  34. var repos []*Repository
  35. resp, err := s.client.Do(ctx, req, &repos)
  36. if err != nil {
  37. return nil, resp, err
  38. }
  39. return repos, resp, nil
  40. }
  41. // RepositoryCreateForkOptions specifies the optional parameters to the
  42. // RepositoriesService.CreateFork method.
  43. type RepositoryCreateForkOptions struct {
  44. // The organization to fork the repository into.
  45. Organization string `url:"organization,omitempty"`
  46. }
  47. // CreateFork creates a fork of the specified repository.
  48. //
  49. // This method might return an *AcceptedError and a status code of
  50. // 202. This is because this is the status that GitHub returns to signify that
  51. // it is now computing creating the fork in a background task. In this event,
  52. // the Repository value will be returned, which includes the details about the pending fork.
  53. // A follow up request, after a delay of a second or so, should result
  54. // in a successful request.
  55. //
  56. // GitHub API docs: https://developer.github.com/v3/repos/forks/#create-a-fork
  57. func (s *RepositoriesService) CreateFork(ctx context.Context, owner, repo string, opt *RepositoryCreateForkOptions) (*Repository, *Response, error) {
  58. u := fmt.Sprintf("repos/%v/%v/forks", owner, repo)
  59. u, err := addOptions(u, opt)
  60. if err != nil {
  61. return nil, nil, err
  62. }
  63. req, err := s.client.NewRequest("POST", u, nil)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. fork := new(Repository)
  68. resp, err := s.client.Do(ctx, req, fork)
  69. if err != nil {
  70. // Persist AcceptedError's metadata to the Repository object.
  71. if aerr, ok := err.(*AcceptedError); ok {
  72. if err := json.Unmarshal(aerr.Raw, fork); err != nil {
  73. return fork, resp, err
  74. }
  75. return fork, resp, err
  76. }
  77. return nil, resp, err
  78. }
  79. return fork, resp, nil
  80. }