sign.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package approve_former
  2. import "fmt"
  3. const signTemplate = `
  4. [[- if .parallel -]]
  5. - name: [[ .name ]]
  6. template: approval
  7. [[- if .when ]]
  8. when: "[[ .when ]]"
  9. [[- end -]]
  10. [[ else -]]
  11. - - name: [[ .name ]]
  12. template: approval
  13. [[- if .when ]]
  14. when: "[[ .when ]]"
  15. [[- end -]]
  16. [[- end -]]
  17. `
  18. type Sign struct {
  19. Parallel bool // 如果为true,认为和上个节点并行执行,模板渲染为-。如果为false,和上个节点顺序执行,模板渲染为- -
  20. Name string
  21. }
  22. func NewParallelSign(name string) *Sign {
  23. return &Sign{
  24. Parallel: true,
  25. Name: name,
  26. }
  27. }
  28. func NewSequenceSign(name string) *Sign {
  29. return &Sign{
  30. Parallel: false,
  31. Name: name,
  32. }
  33. }
  34. func (sign *Sign) GetWhen() string {
  35. return fmt.Sprintf(`{{ steps.%s.outputs.parameters.front-json }} == 通过`, sign.Name)
  36. }
  37. func (sign *Sign) Render(upperNode Noder) (string, error) {
  38. if upperNode == nil {
  39. return render("sign", signTemplate, map[string]any{
  40. "name": sign.Name,
  41. })
  42. }
  43. return render("sign", signTemplate, map[string]any{
  44. "parallel": sign.Parallel,
  45. "name": sign.Name,
  46. "when": upperNode.GetWhen(),
  47. })
  48. }