fake.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fake
  13. import (
  14. "net/http"
  15. gitlab "github.com/xanzy/go-gitlab"
  16. )
  17. type APIResponse[O any] struct {
  18. Output O
  19. Response *gitlab.Response
  20. Error error
  21. }
  22. type GitVariable interface {
  23. gitlab.ProjectVariable |
  24. gitlab.GroupVariable
  25. }
  26. type extractKey[V GitVariable] func(gv V) string
  27. func keyFromProjectVariable(pv gitlab.ProjectVariable) string {
  28. return pv.Key
  29. }
  30. func keyFromGroupVariable(gv gitlab.GroupVariable) string {
  31. return gv.Key
  32. }
  33. type GitlabMockProjectsClient struct {
  34. listProjectsGroups func(pid interface{}, opt *gitlab.ListProjectGroupOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectGroup, *gitlab.Response, error)
  35. }
  36. func (mc *GitlabMockProjectsClient) ListProjectsGroups(pid interface{}, opt *gitlab.ListProjectGroupOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectGroup, *gitlab.Response, error) {
  37. return mc.listProjectsGroups(pid, opt, nil)
  38. }
  39. func (mc *GitlabMockProjectsClient) WithValue(output []*gitlab.ProjectGroup, response *gitlab.Response, err error) {
  40. if mc != nil {
  41. mc.listProjectsGroups = func(pid interface{}, opt *gitlab.ListProjectGroupOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectGroup, *gitlab.Response, error) {
  42. return output, response, err
  43. }
  44. }
  45. }
  46. type GitlabMockProjectVariablesClient struct {
  47. getVariable func(pid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error)
  48. listVariables func(pid interface{}, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectVariable, *gitlab.Response, error)
  49. }
  50. func (mc *GitlabMockProjectVariablesClient) GetVariable(pid interface{}, key string, opt *gitlab.GetProjectVariableOptions, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectVariable, *gitlab.Response, error) {
  51. return mc.getVariable(pid, key, nil)
  52. }
  53. func (mc *GitlabMockProjectVariablesClient) ListVariables(pid interface{}, opt *gitlab.ListProjectVariablesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectVariable, *gitlab.Response, error) {
  54. return mc.listVariables(pid)
  55. }
  56. func (mc *GitlabMockProjectVariablesClient) WithValue(response APIResponse[[]*gitlab.ProjectVariable]) {
  57. mc.WithValues([]APIResponse[[]*gitlab.ProjectVariable]{response})
  58. }
  59. func (mc *GitlabMockProjectVariablesClient) WithValues(responses []APIResponse[[]*gitlab.ProjectVariable]) {
  60. if mc != nil {
  61. mc.getVariable = mockGetVariable(keyFromProjectVariable, responses)
  62. mc.listVariables = mockListVariable(responses)
  63. }
  64. }
  65. func mockGetVariable[V GitVariable](keyExtractor extractKey[V], responses []APIResponse[[]*V]) func(interface{}, string, ...gitlab.RequestOptionFunc) (*V, *gitlab.Response, error) {
  66. getCount := -1
  67. return func(pid interface{}, key string, options ...gitlab.RequestOptionFunc) (*V, *gitlab.Response, error) {
  68. getCount++
  69. if getCount > len(responses)-1 {
  70. return nil, make404APIResponse(), nil
  71. }
  72. var match *V
  73. for _, v := range responses[getCount].Output {
  74. if keyExtractor(*v) == key {
  75. match = v
  76. }
  77. }
  78. if match == nil {
  79. return nil, make404APIResponse(), nil
  80. }
  81. return match, responses[getCount].Response, responses[getCount].Error
  82. }
  83. }
  84. func mockListVariable[V GitVariable](responses []APIResponse[[]*V]) func(interface{}, ...gitlab.RequestOptionFunc) ([]*V, *gitlab.Response, error) {
  85. listCount := -1
  86. return func(pid interface{}, options ...gitlab.RequestOptionFunc) ([]*V, *gitlab.Response, error) {
  87. listCount++
  88. if listCount > len(responses)-1 {
  89. return nil, makeAPIResponse(listCount, len(responses)), nil
  90. }
  91. return responses[listCount].Output, responses[listCount].Response, responses[listCount].Error
  92. }
  93. }
  94. func make404APIResponse() *gitlab.Response {
  95. return &gitlab.Response{
  96. Response: &http.Response{
  97. StatusCode: http.StatusNotFound,
  98. },
  99. }
  100. }
  101. func makeAPIResponse(page, pages int) *gitlab.Response {
  102. return &gitlab.Response{
  103. Response: &http.Response{
  104. StatusCode: http.StatusOK,
  105. },
  106. CurrentPage: page,
  107. TotalPages: pages,
  108. }
  109. }
  110. type GitlabMockGroupVariablesClient struct {
  111. getVariable func(gid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error)
  112. listVariables func(gid interface{}, options ...gitlab.RequestOptionFunc) ([]*gitlab.GroupVariable, *gitlab.Response, error)
  113. }
  114. func (mc *GitlabMockGroupVariablesClient) GetVariable(gid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
  115. return mc.getVariable(gid, key, nil)
  116. }
  117. func (mc *GitlabMockGroupVariablesClient) ListVariables(gid interface{}, opt *gitlab.ListGroupVariablesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.GroupVariable, *gitlab.Response, error) {
  118. return mc.listVariables(gid)
  119. }
  120. func (mc *GitlabMockGroupVariablesClient) WithValue(output *gitlab.GroupVariable, response *gitlab.Response, err error) {
  121. if mc != nil {
  122. mc.getVariable = func(gid interface{}, key string, options ...gitlab.RequestOptionFunc) (*gitlab.GroupVariable, *gitlab.Response, error) {
  123. return output, response, err
  124. }
  125. mc.listVariables = func(gid interface{}, options ...gitlab.RequestOptionFunc) ([]*gitlab.GroupVariable, *gitlab.Response, error) {
  126. return []*gitlab.GroupVariable{output}, response, err
  127. }
  128. }
  129. }
  130. func (mc *GitlabMockGroupVariablesClient) WithValues(responses []APIResponse[[]*gitlab.GroupVariable]) {
  131. if mc != nil {
  132. mc.getVariable = mockGetVariable(keyFromGroupVariable, responses)
  133. mc.listVariables = mockListVariable(responses)
  134. }
  135. }