dmorfav opened a new issue, #9418:
URL: https://github.com/apache/devlake/issues/9418

   ### Search before asking
   
   - [X] I had searched in the 
[issues](https://github.com/apache/devlake/issues?q=is%3Aissue) and found no 
similar issues.
   
   ### What happened
   
   The endpoints that post to a webhook by project name (`POST 
/plugins/webhook/projects/:projectName/deployments`, and the `issues` and 
`pull_requests` equivalents added in #8915) create the webhook connection on 
the first call through `getOrCreateConnection` 
(`backend/plugins/webhook/api/deployments.go`). That function has two problems.
   
   **1. The first call returns 404 and the payload is not stored.**
   `err` is first set by `findByProjectName`, which returns a not-found error 
when the connection does not exist yet. Inside the `if err != nil` block, 
`projectOutput, err := services.GetProject(projectName)` declares a new `err` 
that shadows the outer one. The function then ends with `return connection, 
err, false`, which returns the original not-found error. `postDeployments` (and 
`postIssue`, `postPullRequests`) return early when `err != nil`, so the caller 
gets a 404 although the connection and its blueprint connection were created, 
and the deployment sent in that first call is never stored. Sending the same 
payload again works.
   
   **2. The scheduled sync does not see the new connection until the blueprint 
is saved or the server restarts.**
   The connection is attached to the project's blueprint with a direct 
`dal.Create(blueprintConnection)`. The cron job, however, runs a copy of the 
blueprint that `reloadBlueprint` loaded into memory (`BlueprintJob{blueprint}` 
in `backend/server/services/blueprint.go`), and nothing reloads it here. A 
manual "Collect Data" reads the blueprint from the database and includes the 
webhook, so everything looks fine at first. The next scheduled run plans from 
the stale copy, rebuilds `project_mapping` without the webhook's 
`cicd_scopes`/`boards`/`repos` rows, and the posted deployments stop counting 
for the project (DORA metrics drop) until someone saves the blueprint or 
restarts DevLake.
   
   ### What do you expect to happen
   
   1. The first call stores the payload and returns 200, like the following 
ones.
   2. After the connection is attached, scheduled runs include it.
   
   ### How to reproduce
   
   1. Create a project with a blueprint and a daily schedule, without any 
webhook connection.
   2. `POST /api/rest/plugins/webhook/projects/<project>/deployments` with a 
valid deployment. It returns 404, but a connection named 
`<project>_deployments` appears in the blueprint.
   3. Send the same request again: it returns 200 and the deployment is stored.
   4. Trigger "Collect Data" manually: the deployment is mapped to the project 
(`project_mapping` has `webhook:<id>` rows).
   5. Wait for the next scheduled run: `project_mapping` no longer has the 
`webhook:<id>` rows, and the deployment no longer counts for the project.
   6. Save the blueprint without changes (`PATCH /blueprints/<id>`) and trigger 
again: the rows come back and later scheduled runs keep them.
   
   ### Anything else
   
   Possible fix, both in `getOrCreateConnection`:
   
   1. Avoid the shadowing (for example `projectOutput, getErr := 
services.GetProject(projectName)`) and return `nil` as the error once the 
connection has been created.
   2. After `dal.Create(blueprintConnection)`, reload the scheduled copy of the 
blueprint: either attach the connection through `services.PatchBlueprint` 
(which saves and reloads it, like the UI does) instead of inserting the row 
directly, or call `services.ReloadBlueprints()` afterwards.
   
   Workaround: after the first call, send it again, and save the blueprint once 
(`PATCH /blueprints/<id>` with its current `connections`) so the scheduler 
picks up the new connection.
   
   ### Version
   
   v1.0.3-beta19 (the same code is on `main`)
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to