4
0

repos_forks.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.
  51. // A follow up request, after a delay of a second or so, should result
  52. // in a successful request.
  53. //
  54. // GitHub API docs: https://developer.github.com/v3/repos/forks/#create-a-fork
  55. func (s *RepositoriesService) CreateFork(ctx context.Context, owner, repo string, opt *RepositoryCreateForkOptions) (*Repository, *Response, error) {
  56. u := fmt.Sprintf("repos/%v/%v/forks", owner, repo)
  57. u, err := addOptions(u, opt)
  58. if err != nil {
  59. return nil, nil, err
  60. }
  61. req, err := s.client.NewRequest("POST", u, nil)
  62. if err != nil {
  63. return nil, nil, err
  64. }
  65. fork := new(Repository)
  66. resp, err := s.client.Do(ctx, req, fork)
  67. if err != nil {
  68. return nil, resp, err
  69. }
  70. return fork, resp, nil
  71. }