repos_forks.go 2.6 KB

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