|
| 1 | +package describers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/opengovern/og-describer-github/discovery/pkg/models" |
| 8 | + model "github.com/opengovern/og-describer-github/discovery/provider" |
| 9 | + resilientbridge "github.com/opengovern/resilient-bridge" |
| 10 | + "github.com/opengovern/resilient-bridge/adapters" |
| 11 | + "net/url" |
| 12 | + "strconv" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +func ListRepositoryWebhooks(ctx context.Context, githubClient model.GitHubClient, organizationName string, stream *models.StreamSender) ([]models.Resource, error) { |
| 17 | + handler := resilientbridge.NewResilientBridge() |
| 18 | + handler.SetDebug(false) |
| 19 | + handler.RegisterProvider("github", adapters.NewGitHubAdapter(githubClient.Token), &resilientbridge.ProviderConfig{ |
| 20 | + UseProviderLimits: true, |
| 21 | + MaxRetries: 3, |
| 22 | + BaseBackoff: time.Second, |
| 23 | + }) |
| 24 | + |
| 25 | + repositories, err := getRepositories(ctx, githubClient.RestClient, organizationName) |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + |
| 30 | + var values []models.Resource |
| 31 | + for _, repo := range repositories { |
| 32 | + webhooks, err := processRepositoryWebhooks(ctx, handler, organizationName, repo.GetName()) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + for _, webhook := range webhooks { |
| 37 | + config := model.WebhookConfig{ |
| 38 | + URL: webhook.Config.URL, |
| 39 | + ContentType: webhook.Config.ContentType, |
| 40 | + Secret: webhook.Config.Secret, |
| 41 | + InsecureSSL: webhook.Config.InsecureSSL, |
| 42 | + } |
| 43 | + lastResponse := model.HookResponse{ |
| 44 | + Code: webhook.LastResponse.Code, |
| 45 | + Status: webhook.LastResponse.Status, |
| 46 | + Message: webhook.LastResponse.Message, |
| 47 | + } |
| 48 | + value := models.Resource{ |
| 49 | + ID: strconv.Itoa(int(webhook.ID)), |
| 50 | + Name: webhook.Name, |
| 51 | + Description: model.WebhookDescription{ |
| 52 | + Type: webhook.Type, |
| 53 | + ID: webhook.ID, |
| 54 | + Name: webhook.Name, |
| 55 | + Active: webhook.Active, |
| 56 | + Events: webhook.Events, |
| 57 | + Config: config, |
| 58 | + UpdatedAt: webhook.UpdatedAt, |
| 59 | + CreatedAt: webhook.CreatedAt, |
| 60 | + URL: webhook.URL, |
| 61 | + TestURL: webhook.TestURL, |
| 62 | + PingURL: webhook.PingURL, |
| 63 | + DeliveriesURL: webhook.DeliveriesURL, |
| 64 | + LastResponse: lastResponse, |
| 65 | + }, |
| 66 | + } |
| 67 | + if stream != nil { |
| 68 | + if err := (*stream)(value); err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + } else { |
| 72 | + values = append(values, value) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return values, nil |
| 78 | +} |
| 79 | + |
| 80 | +func GetRepositoryWebhook(ctx context.Context, githubClient model.GitHubClient, organizationName string, repositoryName string, resourceID string, stream *models.StreamSender) (*models.Resource, error) { |
| 81 | + handler := resilientbridge.NewResilientBridge() |
| 82 | + handler.SetDebug(false) |
| 83 | + handler.RegisterProvider("github", adapters.NewGitHubAdapter(githubClient.Token), &resilientbridge.ProviderConfig{ |
| 84 | + UseProviderLimits: true, |
| 85 | + MaxRetries: 3, |
| 86 | + BaseBackoff: time.Second, |
| 87 | + }) |
| 88 | + |
| 89 | + webhook, err := processRepositoryWebhook(ctx, handler, organizationName, repositoryName, resourceID) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + config := model.WebhookConfig{ |
| 94 | + URL: webhook.Config.URL, |
| 95 | + ContentType: webhook.Config.ContentType, |
| 96 | + Secret: webhook.Config.Secret, |
| 97 | + InsecureSSL: webhook.Config.InsecureSSL, |
| 98 | + } |
| 99 | + lastResponse := model.HookResponse{ |
| 100 | + Code: webhook.LastResponse.Code, |
| 101 | + Status: webhook.LastResponse.Status, |
| 102 | + Message: webhook.LastResponse.Message, |
| 103 | + } |
| 104 | + value := models.Resource{ |
| 105 | + ID: strconv.Itoa(int(webhook.ID)), |
| 106 | + Name: webhook.Name, |
| 107 | + Description: model.WebhookDescription{ |
| 108 | + Type: webhook.Type, |
| 109 | + ID: webhook.ID, |
| 110 | + Name: webhook.Name, |
| 111 | + Active: webhook.Active, |
| 112 | + Events: webhook.Events, |
| 113 | + Config: config, |
| 114 | + UpdatedAt: webhook.UpdatedAt, |
| 115 | + CreatedAt: webhook.CreatedAt, |
| 116 | + URL: webhook.URL, |
| 117 | + TestURL: webhook.TestURL, |
| 118 | + PingURL: webhook.PingURL, |
| 119 | + DeliveriesURL: webhook.DeliveriesURL, |
| 120 | + LastResponse: lastResponse, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + return &value, nil |
| 125 | +} |
| 126 | + |
| 127 | +func processRepositoryWebhooks(ctx context.Context, handler *resilientbridge.ResilientBridge, organization, repo string) ([]model.WebhookJSON, error) { |
| 128 | + var webhooks []model.WebhookJSON |
| 129 | + var responseWebhooks []model.WebhookJSON |
| 130 | + baseURL := "/repos/" |
| 131 | + page := 1 |
| 132 | + |
| 133 | + for { |
| 134 | + params := url.Values{} |
| 135 | + params.Set("per_page", "100") |
| 136 | + params.Set("page", strconv.Itoa(page)) |
| 137 | + finalURL := fmt.Sprintf("%s%s/%s/hooks?%s", baseURL, organization, repo, params.Encode()) |
| 138 | + |
| 139 | + req := &resilientbridge.NormalizedRequest{ |
| 140 | + Method: "GET", |
| 141 | + Endpoint: finalURL, |
| 142 | + Headers: map[string]string{"accept": "application/vnd.github+json"}, |
| 143 | + } |
| 144 | + |
| 145 | + resp, err := handler.Request("github", req) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("request execution failed: %w", err) |
| 148 | + } |
| 149 | + |
| 150 | + if resp.StatusCode >= 400 { |
| 151 | + return nil, fmt.Errorf("error %d: %s", resp.StatusCode, string(resp.Data)) |
| 152 | + } |
| 153 | + |
| 154 | + if err = json.Unmarshal(resp.Data, &responseWebhooks); err != nil { |
| 155 | + return nil, fmt.Errorf("error parsing response: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + webhooks = append(webhooks, responseWebhooks...) |
| 159 | + if len(responseWebhooks) < 100 { |
| 160 | + break |
| 161 | + } |
| 162 | + page++ |
| 163 | + } |
| 164 | + |
| 165 | + return webhooks, nil |
| 166 | +} |
| 167 | + |
| 168 | +func processRepositoryWebhook(ctx context.Context, handler *resilientbridge.ResilientBridge, organization, repo, resourceID string) (*model.WebhookJSON, error) { |
| 169 | + var webhook model.WebhookJSON |
| 170 | + baseURL := "/repos/" |
| 171 | + |
| 172 | + finalURL := fmt.Sprintf("%s%s/%s/hooks/%s", baseURL, organization, repo, resourceID) |
| 173 | + |
| 174 | + req := &resilientbridge.NormalizedRequest{ |
| 175 | + Method: "GET", |
| 176 | + Endpoint: finalURL, |
| 177 | + Headers: map[string]string{"accept": "application/vnd.github+json"}, |
| 178 | + } |
| 179 | + |
| 180 | + resp, err := handler.Request("github", req) |
| 181 | + if err != nil { |
| 182 | + return nil, fmt.Errorf("request execution failed: %w", err) |
| 183 | + } |
| 184 | + |
| 185 | + if resp.StatusCode >= 400 { |
| 186 | + return nil, fmt.Errorf("error %d: %s", resp.StatusCode, string(resp.Data)) |
| 187 | + } |
| 188 | + |
| 189 | + if err = json.Unmarshal(resp.Data, &webhook); err != nil { |
| 190 | + return nil, fmt.Errorf("error parsing response: %w", err) |
| 191 | + } |
| 192 | + |
| 193 | + return &webhook, nil |
| 194 | +} |
0 commit comments