| 
 | 1 | +# frozen_string_literal: true  | 
 | 2 | + | 
 | 3 | +module Travis::API::V3  | 
 | 4 | +  class InsightsClient  | 
 | 5 | +    class ConfigurationError < StandardError; end  | 
 | 6 | + | 
 | 7 | +    def initialize(user_id)  | 
 | 8 | +      @user_id = user_id  | 
 | 9 | +    end  | 
 | 10 | + | 
 | 11 | +    def user_notifications(filter, page, active, sort_by, sort_direction)  | 
 | 12 | +      query_string = query_string_from_params(  | 
 | 13 | +        value: filter,  | 
 | 14 | +        page: page || '1',  | 
 | 15 | +        active: active,  | 
 | 16 | +        order: sort_by,  | 
 | 17 | +        order_dir: sort_direction  | 
 | 18 | +      )  | 
 | 19 | +      response = connection.get("/user_notifications?#{query_string}")  | 
 | 20 | + | 
 | 21 | +      handle_errors_and_respond(response) do |body|  | 
 | 22 | +        notifications = body['data'].map do |notification|  | 
 | 23 | +          Travis::API::V3::Models::InsightsNotification.new(notification)  | 
 | 24 | +        end  | 
 | 25 | + | 
 | 26 | +        Travis::API::V3::Models::InsightsCollection.new(notifications, body.fetch('total_count'))  | 
 | 27 | +      end  | 
 | 28 | +    end  | 
 | 29 | + | 
 | 30 | +    def toggle_snooze_user_notifications(notification_ids)  | 
 | 31 | +      response = connection.put('/user_notifications/toggle_snooze', snooze_ids: notification_ids)  | 
 | 32 | + | 
 | 33 | +      handle_errors_and_respond(response)  | 
 | 34 | +    end  | 
 | 35 | + | 
 | 36 | +    def user_plugins(filter, page, active, sort_by, sort_direction)  | 
 | 37 | +      query_string = query_string_from_params(  | 
 | 38 | +        value: filter,  | 
 | 39 | +        page: page || '1',  | 
 | 40 | +        active: active,  | 
 | 41 | +        order: sort_by,  | 
 | 42 | +        order_dir: sort_direction  | 
 | 43 | +      )  | 
 | 44 | +      response = connection.get("/user_plugins?#{query_string}")  | 
 | 45 | + | 
 | 46 | +      handle_errors_and_respond(response) do |body|  | 
 | 47 | +        plugins = body['data'].map do |plugin|  | 
 | 48 | +          Travis::API::V3::Models::InsightsPlugin.new(plugin)  | 
 | 49 | +        end  | 
 | 50 | + | 
 | 51 | +        Travis::API::V3::Models::InsightsCollection.new(plugins, body.fetch('total_count'))  | 
 | 52 | +      end  | 
 | 53 | +    end  | 
 | 54 | + | 
 | 55 | +    def create_plugin(params)  | 
 | 56 | +      response = connection.post("/user_plugins", user_plugin: params)  | 
 | 57 | +      handle_errors_and_respond(response) do |body|  | 
 | 58 | +        Travis::API::V3::Models::InsightsPlugin.new(body['plugin'])  | 
 | 59 | +      end  | 
 | 60 | +    end  | 
 | 61 | + | 
 | 62 | +    def toggle_active_plugins(plugin_ids)  | 
 | 63 | +      response = connection.put('/user_plugins/toggle_active', toggle_ids: plugin_ids)  | 
 | 64 | + | 
 | 65 | +      handle_errors_and_respond(response) do |body|  | 
 | 66 | +        Travis::API::V3::Models::InsightsCollection.new([], 0)  | 
 | 67 | +      end  | 
 | 68 | +    end  | 
 | 69 | + | 
 | 70 | +    def delete_many_plugins(plugin_ids)  | 
 | 71 | +      response = connection.delete('/user_plugins/delete_many', delete_ids: plugin_ids)  | 
 | 72 | + | 
 | 73 | +      handle_errors_and_respond(response) do |body|  | 
 | 74 | +        Travis::API::V3::Models::InsightsCollection.new([], 0)  | 
 | 75 | +      end  | 
 | 76 | +    end  | 
 | 77 | + | 
 | 78 | +    def run_scan  | 
 | 79 | +      response = connection.get('/user_plugins/run_scan')  | 
 | 80 | + | 
 | 81 | +      handle_errors_and_respond(response) do |body|  | 
 | 82 | +        Travis::API::V3::Models::InsightsCollection.new([], 0)  | 
 | 83 | +      end  | 
 | 84 | +    end  | 
 | 85 | + | 
 | 86 | +    def generate_key(plugin_name, plugin_type)  | 
 | 87 | +      response = connection.get('/user_plugins/generate_key', name: plugin_name, plugin_type: plugin_type)  | 
 | 88 | + | 
 | 89 | +      handle_errors_and_respond(response) do |body|  | 
 | 90 | +        body  | 
 | 91 | +      end  | 
 | 92 | +    end  | 
 | 93 | + | 
 | 94 | +    def authenticate_key(params)  | 
 | 95 | +      response = connection.post('/user_plugins/authenticate_key', params)  | 
 | 96 | + | 
 | 97 | +      handle_errors_and_respond(response) do |body|  | 
 | 98 | +        body  | 
 | 99 | +      end  | 
 | 100 | +    end  | 
 | 101 | + | 
 | 102 | +    def template_plugin_tests(plugin_type)  | 
 | 103 | +      response = connection.get("/user_plugins/#{plugin_type}/template_plugin_tests")  | 
 | 104 | + | 
 | 105 | +      handle_errors_and_respond(response) do |body|  | 
 | 106 | +        body  | 
 | 107 | +      end  | 
 | 108 | +    end  | 
 | 109 | + | 
 | 110 | +    def get_scan_logs(plugin_id, last_id)  | 
 | 111 | +      params = last_id ? { last: last_id, poll: true } : {}  | 
 | 112 | +      response = connection.get("/user_plugins/#{plugin_id}/get_scan_logs", params)  | 
 | 113 | + | 
 | 114 | +      handle_errors_and_respond(response) do |body|  | 
 | 115 | +        body  | 
 | 116 | +      end  | 
 | 117 | +    end  | 
 | 118 | + | 
 | 119 | +    def probes(filter, page, active, sort_by, sort_direction)  | 
 | 120 | +      query_string = query_string_from_params(  | 
 | 121 | +        value: filter,  | 
 | 122 | +        page: page || '1',  | 
 | 123 | +        active: active,  | 
 | 124 | +        order: sort_by,  | 
 | 125 | +        order_dir: sort_direction  | 
 | 126 | +      )  | 
 | 127 | +      response = connection.get("/probes?#{query_string}")  | 
 | 128 | + | 
 | 129 | +      handle_errors_and_respond(response) do |body|  | 
 | 130 | +        probes = body['data'].map do |probe|  | 
 | 131 | +          Travis::API::V3::Models::InsightsProbe.new(probe)  | 
 | 132 | +        end  | 
 | 133 | + | 
 | 134 | +        Travis::API::V3::Models::InsightsCollection.new(probes, body.fetch('total_count'))  | 
 | 135 | +      end  | 
 | 136 | +    end  | 
 | 137 | + | 
 | 138 | +    def create_probe(params)  | 
 | 139 | +      response = connection.post("/probes", test_template: params)  | 
 | 140 | +      handle_errors_and_respond(response) do |body|  | 
 | 141 | +        Travis::API::V3::Models::InsightsProbe.new(body)  | 
 | 142 | +      end  | 
 | 143 | +    end  | 
 | 144 | + | 
 | 145 | +    def update_probe(params)  | 
 | 146 | +      response = connection.patch("/probes/#{params['probe_id']}", params)  | 
 | 147 | +      handle_errors_and_respond(response) do |body|  | 
 | 148 | +        Travis::API::V3::Models::InsightsProbe.new(body)  | 
 | 149 | +      end  | 
 | 150 | +    end  | 
 | 151 | + | 
 | 152 | +    def get_probe(params)  | 
 | 153 | +      response = connection.get("/probes/#{params['probe_id']}/template_test", params)  | 
 | 154 | +      handle_errors_and_respond(response) do |body|  | 
 | 155 | +        Travis::API::V3::Models::InsightsProbe.new(body)  | 
 | 156 | +      end  | 
 | 157 | +    end  | 
 | 158 | + | 
 | 159 | +    def toggle_active_probes(probe_ids)  | 
 | 160 | +      response = connection.put('/probes/toggle_active', toggle_ids: probe_ids)  | 
 | 161 | + | 
 | 162 | +      handle_errors_and_respond(response) do |body|  | 
 | 163 | +        Travis::API::V3::Models::InsightsCollection.new([], 0)  | 
 | 164 | +      end  | 
 | 165 | +    end  | 
 | 166 | + | 
 | 167 | +    def delete_many_probes(probe_ids)  | 
 | 168 | +      response = connection.delete('/probes/delete_many', delete_ids: probe_ids)  | 
 | 169 | + | 
 | 170 | +      handle_errors_and_respond(response) do |body|  | 
 | 171 | +        Travis::API::V3::Models::InsightsCollection.new([], 0)  | 
 | 172 | +      end  | 
 | 173 | +    end  | 
 | 174 | + | 
 | 175 | +    def sandbox_plugins(plugin_type)  | 
 | 176 | +      response = connection.post('/sandbox/plugins', plugin_type: plugin_type)  | 
 | 177 | + | 
 | 178 | +      handle_errors_and_respond(response) do |body|  | 
 | 179 | +        body  | 
 | 180 | +      end  | 
 | 181 | +    end  | 
 | 182 | + | 
 | 183 | +    def sandbox_plugin_data(plugin_id)  | 
 | 184 | +      response = connection.post('/sandbox/plugin_data', plugin_id: plugin_id)  | 
 | 185 | + | 
 | 186 | +      handle_errors_and_respond(response) do |body|  | 
 | 187 | +        body  | 
 | 188 | +      end  | 
 | 189 | +    end  | 
 | 190 | + | 
 | 191 | +    def sandbox_run_query(plugin_id, query)  | 
 | 192 | +      response = connection.post('/sandbox/run_query', plugin_id: plugin_id, query: query)  | 
 | 193 | + | 
 | 194 | +      handle_errors_and_respond(response) do |body|  | 
 | 195 | +        body  | 
 | 196 | +      end  | 
 | 197 | +    end  | 
 | 198 | + | 
 | 199 | +    def public_key  | 
 | 200 | +      response = connection.get('/api/v1/public_keys/latest.json')  | 
 | 201 | + | 
 | 202 | +      handle_errors_and_respond(response) do |body|  | 
 | 203 | +        Travis::API::V3::Models::InsightsPublicKey.new(body)  | 
 | 204 | +      end  | 
 | 205 | +    end  | 
 | 206 | + | 
 | 207 | +    def search_tags  | 
 | 208 | +      response = connection.get('/tags')  | 
 | 209 | + | 
 | 210 | +      handle_errors_and_respond(response) do |body|  | 
 | 211 | +        tags = body.map do |tag|  | 
 | 212 | +          Travis::API::V3::Models::InsightsTag.new(tag)  | 
 | 213 | +        end  | 
 | 214 | +      end  | 
 | 215 | +    end  | 
 | 216 | + | 
 | 217 | +    private  | 
 | 218 | + | 
 | 219 | +    def handle_errors_and_respond(response)  | 
 | 220 | +      case response.status  | 
 | 221 | +      when 200, 201  | 
 | 222 | +        yield(response.body) if block_given?  | 
 | 223 | +      when 202  | 
 | 224 | +        true  | 
 | 225 | +      when 204  | 
 | 226 | +        true  | 
 | 227 | +      when 400  | 
 | 228 | +        raise Travis::API::V3::ClientError, response.body&.fetch('error', '')  | 
 | 229 | +      when 403  | 
 | 230 | +        raise Travis::API::V3::InsufficientAccess, response.body&.fetch('rejection_code', '')  | 
 | 231 | +      when 404  | 
 | 232 | +        raise Travis::API::V3::NotFound, response.body&.fetch('error', '')  | 
 | 233 | +      when 422  | 
 | 234 | +        raise Travis::API::V3::UnprocessableEntity, response.body&.fetch('error', '')  | 
 | 235 | +      else  | 
 | 236 | +        raise Travis::API::V3::ServerError, 'Insights API failed'  | 
 | 237 | +      end  | 
 | 238 | +    end  | 
 | 239 | + | 
 | 240 | +    def connection(timeout: 20)  | 
 | 241 | +      @connection ||= Faraday.new(url: insights_url, ssl: { ca_path: '/usr/lib/ssl/certs' }) do |conn|  | 
 | 242 | +        conn.headers[:Authorization] = "Token token=\"#{insights_auth_token}\""  | 
 | 243 | +        conn.headers['X-Travis-User-Id'] = @user_id.to_s  | 
 | 244 | +        conn.headers['Content-Type'] = 'application/json'  | 
 | 245 | +        conn.request :json  | 
 | 246 | +        conn.response :json  | 
 | 247 | +        conn.options[:open_timeout] = timeout  | 
 | 248 | +        conn.options[:timeout] = timeout  | 
 | 249 | +        conn.use OpenCensus::Trace::Integrations::FaradayMiddleware if Travis::Api::App::Middleware::OpenCensus.enabled?  | 
 | 250 | +        conn.adapter :net_http  | 
 | 251 | +      end  | 
 | 252 | +    end  | 
 | 253 | + | 
 | 254 | +    def insights_url  | 
 | 255 | +      Travis.config.new_insights.insights_url || raise(ConfigurationError, 'No Insights API URL configured!')  | 
 | 256 | +    end  | 
 | 257 | + | 
 | 258 | +    def insights_auth_token  | 
 | 259 | +      Travis.config.new_insights.insights_auth_token || raise(ConfigurationError, 'No Insights Auth Token configured!')  | 
 | 260 | +    end  | 
 | 261 | + | 
 | 262 | +    def query_string_from_params(params)  | 
 | 263 | +      params.delete_if { |_, v| v.nil? || v.empty? }.to_query  | 
 | 264 | +    end  | 
 | 265 | +  end  | 
 | 266 | +end  | 
0 commit comments