| 544 | } |
| 545 | |
| 546 | func DryRunTaskPlugin(c *gin.Context) { |
| 547 | var request taskPluginDryRunRequest |
| 548 | if err := c.ShouldBindJSON(&request); err != nil { |
| 549 | common.ApiErrorMsg(c, err.Error()) |
| 550 | return |
| 551 | } |
| 552 | detailSource := "" |
| 553 | plugin, err := model.GetTaskPluginVersion(c.Param("key"), "") |
| 554 | if err == nil { |
| 555 | detailSource = string(plugin.Source) |
| 556 | } else if errors.Is(err, gorm.ErrRecordNotFound) { |
| 557 | detailSource, err = plugins.Source(c.Param("key")) |
| 558 | } |
| 559 | if err != nil { |
| 560 | common.ApiErrorMsg(c, "task plugin not found") |
| 561 | return |
| 562 | } |
| 563 | loaded, err := jsplugin.NewRegistry().Register(detailSource, jsplugin.Options{Key: c.Param("key")}) |
| 564 | if err != nil { |
| 565 | taskPluginCompileError(c, err) |
| 566 | return |
| 567 | } |
| 568 | args := make([]any, len(request.Args)) |
| 569 | for index, raw := range request.Args { |
| 570 | if err = common.Unmarshal(raw, &args[index]); err != nil { |
| 571 | common.ApiErrorMsg(c, fmt.Sprintf("invalid argument %d: %v", index+1, err)) |
| 572 | return |
| 573 | } |
| 574 | } |
| 575 | var output any |
| 576 | if request.Member == "" { |
| 577 | output, err = loaded.Engine.Call(context.Background(), request.Hook, args...) |
| 578 | } else { |
| 579 | output, err = loaded.Engine.CallMember(context.Background(), request.Hook, request.Member, args...) |
| 580 | } |
| 581 | if err != nil { |
| 582 | common.ApiErrorMsg(c, err.Error()) |
| 583 | return |
| 584 | } |
| 585 | common.ApiSuccess(c, output) |
| 586 | } |
| 587 | |
| 588 | func DeleteTaskPluginVersion(c *gin.Context) { |
| 589 | key := c.Param("key") |