repos_keys.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // The Key type is defined in users_keys.go
  11. // ListKeys lists the deploy keys for a repository.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/repos/keys/#list
  14. func (s *RepositoriesService) ListKeys(ctx context.Context, owner string, repo string, opt *ListOptions) ([]*Key, *Response, error) {
  15. u := fmt.Sprintf("repos/%v/%v/keys", owner, repo)
  16. u, err := addOptions(u, opt)
  17. if err != nil {
  18. return nil, nil, err
  19. }
  20. req, err := s.client.NewRequest("GET", u, nil)
  21. if err != nil {
  22. return nil, nil, err
  23. }
  24. var keys []*Key
  25. resp, err := s.client.Do(ctx, req, &keys)
  26. if err != nil {
  27. return nil, resp, err
  28. }
  29. return keys, resp, nil
  30. }
  31. // GetKey fetches a single deploy key.
  32. //
  33. // GitHub API docs: https://developer.github.com/v3/repos/keys/#get
  34. func (s *RepositoriesService) GetKey(ctx context.Context, owner string, repo string, id int64) (*Key, *Response, error) {
  35. u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id)
  36. req, err := s.client.NewRequest("GET", u, nil)
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. key := new(Key)
  41. resp, err := s.client.Do(ctx, req, key)
  42. if err != nil {
  43. return nil, resp, err
  44. }
  45. return key, resp, nil
  46. }
  47. // CreateKey adds a deploy key for a repository.
  48. //
  49. // GitHub API docs: https://developer.github.com/v3/repos/keys/#create
  50. func (s *RepositoriesService) CreateKey(ctx context.Context, owner string, repo string, key *Key) (*Key, *Response, error) {
  51. u := fmt.Sprintf("repos/%v/%v/keys", owner, repo)
  52. req, err := s.client.NewRequest("POST", u, key)
  53. if err != nil {
  54. return nil, nil, err
  55. }
  56. k := new(Key)
  57. resp, err := s.client.Do(ctx, req, k)
  58. if err != nil {
  59. return nil, resp, err
  60. }
  61. return k, resp, nil
  62. }
  63. // EditKey edits a deploy key.
  64. //
  65. // GitHub API docs: https://developer.github.com/v3/repos/keys/#edit
  66. func (s *RepositoriesService) EditKey(ctx context.Context, owner string, repo string, id int64, key *Key) (*Key, *Response, error) {
  67. u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id)
  68. req, err := s.client.NewRequest("PATCH", u, key)
  69. if err != nil {
  70. return nil, nil, err
  71. }
  72. k := new(Key)
  73. resp, err := s.client.Do(ctx, req, k)
  74. if err != nil {
  75. return nil, resp, err
  76. }
  77. return k, resp, nil
  78. }
  79. // DeleteKey deletes a deploy key.
  80. //
  81. // GitHub API docs: https://developer.github.com/v3/repos/keys/#delete
  82. func (s *RepositoriesService) DeleteKey(ctx context.Context, owner string, repo string, id int64) (*Response, error) {
  83. u := fmt.Sprintf("repos/%v/%v/keys/%v", owner, repo, id)
  84. req, err := s.client.NewRequest("DELETE", u, nil)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return s.client.Do(ctx, req, nil)
  89. }