findByProjectName finds the connection by project name and plugin name
(connection interface{}, params map[string]string, pluginName string, webhookName string)
| 355 | |
| 356 | // findByProjectName finds the connection by project name and plugin name |
| 357 | func findByProjectName(connection interface{}, params map[string]string, pluginName string, webhookName string) errors.Error { |
| 358 | projectName := params["projectName"] |
| 359 | if projectName == "" { |
| 360 | return errors.BadInput.New("missing projectName") |
| 361 | } |
| 362 | if len(projectName) > 100 { |
| 363 | return errors.BadInput.New("invalid projectName") |
| 364 | } |
| 365 | if pluginName == "" { |
| 366 | return errors.BadInput.New("missing pluginName") |
| 367 | } |
| 368 | // We need to join three tables: _tool_webhook_connections, _devlake_blueprint_connections, and _devlake_blueprints |
| 369 | // to find the connection associated with the given project name and plugin name. |
| 370 | // The SQL query would look something like this: |
| 371 | // SELECT wc.* |
| 372 | // FROM _tool_webhook_connections AS wc |
| 373 | // JOIN _devlake_blueprint_connections AS bc ON wc.id = bc.connection_id AND bc.plugin_name = ? |
| 374 | // JOIN _devlake_blueprints AS bp ON bc.blueprint_id = bp.id |
| 375 | // WHERE bp.project_name = ? and _tool_webhook_connections.name = ? |
| 376 | // LIMIT 1; |
| 377 | |
| 378 | basicRes.GetLogger().Debug("finding project webhook connection for project %s and plugin %s", projectName, pluginName) |
| 379 | // Using DAL to construct the query |
| 380 | clauses := []dal.Clause{dal.From(connection)} |
| 381 | clauses = append(clauses, |
| 382 | dal.Join("left join _devlake_blueprint_connections bc ON _tool_webhook_connections.id = bc.connection_id and bc.plugin_name = ?", pluginName), |
| 383 | dal.Join("left join _devlake_blueprints bp ON bc.blueprint_id = bp.id"), |
| 384 | dal.Where("bp.project_name = ? and _tool_webhook_connections.name = ?", projectName, webhookName), |
| 385 | ) |
| 386 | |
| 387 | dal := basicRes.GetDal() |
| 388 | return dal.First(connection, clauses...) |
| 389 | } |