|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + model "github.com/codefresh-io/go-sdk/pkg/codefresh/model/app-proxy" |
| 8 | +) |
| 9 | + |
| 10 | +type ( |
| 11 | + IAppProxyGitIntegrationsAPI interface { |
| 12 | + List(ctx context.Context) ([]model.GitIntegration, error) |
| 13 | + Get(ctx context.Context, name string) (*model.GitIntegration, error) |
| 14 | + Add(ctx context.Context, args *model.AddGitIntegrationArgs) (*model.GitIntegration, error) |
| 15 | + Edit(ctx context.Context, args *model.EditGitIntegrationArgs) (*model.GitIntegration, error) |
| 16 | + Remove(ctx context.Context, name string) error |
| 17 | + Register(ctx context.Context, args *model.RegisterToGitIntegrationArgs) (*model.GitIntegration, error) |
| 18 | + Deregister(ctx context.Context, name string) (*model.GitIntegration, error) |
| 19 | + } |
| 20 | + |
| 21 | + gitIntegrations struct { |
| 22 | + codefresh *codefresh |
| 23 | + } |
| 24 | + |
| 25 | + graphqlGitIntegrationsListResponse struct { |
| 26 | + Data struct { |
| 27 | + GitIntegrations []model.GitIntegration |
| 28 | + } |
| 29 | + Errors []graphqlError |
| 30 | + } |
| 31 | + |
| 32 | + graphqlGitIntegrationsGetResponse struct { |
| 33 | + Data struct { |
| 34 | + GitIntegration *model.GitIntegration |
| 35 | + } |
| 36 | + Errors []graphqlError |
| 37 | + } |
| 38 | + |
| 39 | + graphqlGitIntegrationsAddResponse struct { |
| 40 | + Data struct { |
| 41 | + AddGitIntegration *model.GitIntegration |
| 42 | + } |
| 43 | + Errors []graphqlError |
| 44 | + } |
| 45 | + |
| 46 | + graphqlGitIntegrationsEditResponse struct { |
| 47 | + Data struct { |
| 48 | + EditGitIntegration *model.GitIntegration |
| 49 | + } |
| 50 | + Errors []graphqlError |
| 51 | + } |
| 52 | + |
| 53 | + graphqlGitIntegrationsRemoveResponse struct { |
| 54 | + Errors []graphqlError |
| 55 | + } |
| 56 | + |
| 57 | + graphqlGitIntegrationsRegisterResponse struct { |
| 58 | + Data struct { |
| 59 | + RegisterToGitIntegration *model.GitIntegration |
| 60 | + } |
| 61 | + Errors []graphqlError |
| 62 | + } |
| 63 | + |
| 64 | + graphqlGitIntegrationsDeregisterResponse struct { |
| 65 | + Data struct { |
| 66 | + DeregisterFromGitIntegration *model.GitIntegration |
| 67 | + } |
| 68 | + Errors []graphqlError |
| 69 | + } |
| 70 | +) |
| 71 | + |
| 72 | +func newAppProxyGitIntegrationsAPI(c *codefresh) IAppProxyGitIntegrationsAPI { |
| 73 | + return &gitIntegrations{codefresh: c} |
| 74 | +} |
| 75 | + |
| 76 | +func (c *gitIntegrations) List(ctx context.Context) ([]model.GitIntegration, error) { |
| 77 | + jsonData := map[string]interface{}{ |
| 78 | + "query": ` |
| 79 | + { |
| 80 | + gitIntegrations { |
| 81 | + name |
| 82 | + sharingPolicy |
| 83 | + provider |
| 84 | + apiUrl |
| 85 | + registeredUsers |
| 86 | + } |
| 87 | + }`, |
| 88 | + } |
| 89 | + |
| 90 | + res := &graphqlGitIntegrationsListResponse{} |
| 91 | + err := c.codefresh.graphqlAPI(ctx, jsonData, res) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("failed getting git-integrations list: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + if len(res.Errors) > 0 { |
| 97 | + return nil, graphqlErrorResponse{errors: res.Errors} |
| 98 | + } |
| 99 | + |
| 100 | + return res.Data.GitIntegrations, nil |
| 101 | +} |
| 102 | + |
| 103 | +func (c *gitIntegrations) Get(ctx context.Context, name string) (*model.GitIntegration, error) { |
| 104 | + jsonData := map[string]interface{}{ |
| 105 | + "query": ` |
| 106 | + query GetGitIntegration($name: String!) { |
| 107 | + gitIntegration(name: $name) { |
| 108 | + name |
| 109 | + sharingPolicy |
| 110 | + provider |
| 111 | + apiUrl |
| 112 | + registeredUsers |
| 113 | + } |
| 114 | + } |
| 115 | + `, |
| 116 | + "variables": map[string]interface{}{ |
| 117 | + "name": name, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + res := &graphqlGitIntegrationsGetResponse{} |
| 122 | + err := c.codefresh.graphqlAPI(ctx, jsonData, res) |
| 123 | + |
| 124 | + if err != nil { |
| 125 | + return nil, fmt.Errorf("failed making a graphql API call while getting git integration: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + if len(res.Errors) > 0 { |
| 129 | + return nil, graphqlErrorResponse{errors: res.Errors} |
| 130 | + } |
| 131 | + |
| 132 | + return res.Data.GitIntegration, nil |
| 133 | +} |
| 134 | + |
| 135 | +func (c *gitIntegrations) Add(ctx context.Context, args *model.AddGitIntegrationArgs) (*model.GitIntegration, error) { |
| 136 | + jsonData := map[string]interface{}{ |
| 137 | + "query": ` |
| 138 | + mutation AddGitIntegration($args: AddGitIntegrationArgs!) { |
| 139 | + addGitIntegration(args: $args) { |
| 140 | + name |
| 141 | + sharingPolicy |
| 142 | + provider |
| 143 | + apiUrl |
| 144 | + registeredUsers |
| 145 | + } |
| 146 | + } |
| 147 | + `, |
| 148 | + "variables": map[string]interface{}{ |
| 149 | + "args": args, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + res := &graphqlGitIntegrationsAddResponse{} |
| 154 | + err := c.codefresh.graphqlAPI(ctx, jsonData, res) |
| 155 | + |
| 156 | + if err != nil { |
| 157 | + return nil, fmt.Errorf("failed making a graphql API call while adding git integration: %w", err) |
| 158 | + } |
| 159 | + |
| 160 | + if len(res.Errors) > 0 { |
| 161 | + return nil, graphqlErrorResponse{errors: res.Errors} |
| 162 | + } |
| 163 | + |
| 164 | + return res.Data.AddGitIntegration, nil |
| 165 | +} |
| 166 | + |
| 167 | +func (c *gitIntegrations) Edit(ctx context.Context, args *model.EditGitIntegrationArgs) (*model.GitIntegration, error) { |
| 168 | + jsonData := map[string]interface{}{ |
| 169 | + "query": ` |
| 170 | + mutation EditGitIntegration($args: EditGitIntegrationArgs!) { |
| 171 | + editGitIntegration(args: $args) { |
| 172 | + name |
| 173 | + sharingPolicy |
| 174 | + provider |
| 175 | + apiUrl |
| 176 | + registeredUsers |
| 177 | + } |
| 178 | + } |
| 179 | + `, |
| 180 | + "variables": map[string]interface{}{ |
| 181 | + "args": args, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + res := &graphqlGitIntegrationsEditResponse{} |
| 186 | + err := c.codefresh.graphqlAPI(ctx, jsonData, res) |
| 187 | + |
| 188 | + if err != nil { |
| 189 | + return nil, fmt.Errorf("failed making a graphql API call to edit a git integration: %w", err) |
| 190 | + } |
| 191 | + |
| 192 | + if len(res.Errors) > 0 { |
| 193 | + return nil, graphqlErrorResponse{errors: res.Errors} |
| 194 | + } |
| 195 | + |
| 196 | + return res.Data.EditGitIntegration, nil |
| 197 | +} |
| 198 | + |
| 199 | +func (c *gitIntegrations) Remove(ctx context.Context, name string) error { |
| 200 | + jsonData := map[string]interface{}{ |
| 201 | + "query": ` |
| 202 | + mutation RemoveGitIntegration($name: String!) { |
| 203 | + removeGitIntegration(name: $name) |
| 204 | + } |
| 205 | + `, |
| 206 | + "variables": map[string]interface{}{ |
| 207 | + "name": name, |
| 208 | + }, |
| 209 | + } |
| 210 | + |
| 211 | + res := &graphqlGitIntegrationsEditResponse{} |
| 212 | + err := c.codefresh.graphqlAPI(ctx, jsonData, res) |
| 213 | + |
| 214 | + if err != nil { |
| 215 | + return fmt.Errorf("failed making a graphql API call to remove a git integration: %w", err) |
| 216 | + } |
| 217 | + |
| 218 | + if len(res.Errors) > 0 { |
| 219 | + return graphqlErrorResponse{errors: res.Errors} |
| 220 | + } |
| 221 | + |
| 222 | + return nil |
| 223 | +} |
| 224 | + |
| 225 | +func (c *gitIntegrations) Register(ctx context.Context, args *model.RegisterToGitIntegrationArgs) (*model.GitIntegration, error) { |
| 226 | + jsonData := map[string]interface{}{ |
| 227 | + "query": ` |
| 228 | + mutation RegisterToGitIntegration($args: RegisterToGitIntegrationArgs!) { |
| 229 | + registerToGitIntegration(args: $args) { |
| 230 | + name |
| 231 | + sharingPolicy |
| 232 | + provider |
| 233 | + apiUrl |
| 234 | + registeredUsers |
| 235 | + } |
| 236 | + } |
| 237 | + `, |
| 238 | + "variables": map[string]interface{}{ |
| 239 | + "args": args, |
| 240 | + }, |
| 241 | + } |
| 242 | + |
| 243 | + res := &graphqlGitIntegrationsRegisterResponse{} |
| 244 | + err := c.codefresh.graphqlAPI(ctx, jsonData, res) |
| 245 | + |
| 246 | + if err != nil { |
| 247 | + return nil, fmt.Errorf("failed making a graphql API call to register to a git integration: %w", err) |
| 248 | + } |
| 249 | + |
| 250 | + if len(res.Errors) > 0 { |
| 251 | + return nil, graphqlErrorResponse{errors: res.Errors} |
| 252 | + } |
| 253 | + |
| 254 | + return res.Data.RegisterToGitIntegration, nil |
| 255 | +} |
| 256 | + |
| 257 | +func (c *gitIntegrations) Deregister(ctx context.Context, name string) (*model.GitIntegration, error) { |
| 258 | + jsonData := map[string]interface{}{ |
| 259 | + "query": ` |
| 260 | + mutation DeregisterToGitIntegration($name: String!) { |
| 261 | + deregisterFromGitIntegration(name: $name) { |
| 262 | + name |
| 263 | + sharingPolicy |
| 264 | + provider |
| 265 | + apiUrl |
| 266 | + registeredUsers |
| 267 | + } |
| 268 | + } |
| 269 | + `, |
| 270 | + "variables": map[string]interface{}{ |
| 271 | + "name": name, |
| 272 | + }, |
| 273 | + } |
| 274 | + |
| 275 | + res := &graphqlGitIntegrationsDeregisterResponse{} |
| 276 | + err := c.codefresh.graphqlAPI(ctx, jsonData, res) |
| 277 | + |
| 278 | + if err != nil { |
| 279 | + return nil, fmt.Errorf("failed making a graphql API call to deregister from a git integration: %w", err) |
| 280 | + } |
| 281 | + |
| 282 | + if len(res.Errors) > 0 { |
| 283 | + return nil, graphqlErrorResponse{errors: res.Errors} |
| 284 | + } |
| 285 | + |
| 286 | + return res.Data.DeregisterFromGitIntegration, nil |
| 287 | +} |
0 commit comments