mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed?
Align the Nexus URL with the UI URL
## Why?
Talking with @bergundy we want the Nexus link to match the UI link for
consistency.
## How did you test it?
- [x] built
- [] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
---
> [!NOTE]
> **Medium Risk**
> Changes the wire format of Nexus operation links emitted by the
server; anything still expecting `?runID=` may break until SDK/UI are
aligned (noted duplicate in sdk-go).
>
> **Overview**
> **Nexus operation temporal links** now use the same path shape as the
UI: run ID is a path segment under `/details` instead of a `runID` query
parameter.
>
> `ConvertLinkNexusOperationToNexusLink` in
`common/nexus/link_converter.go` updates the template and drops
`RawQuery`; tests in `link_converter_test.go` and the async-start
assertion in `operation_tasks_test.go` expect URLs like
`temporal:///namespaces/.../nexus-operations/{operationId}/{runId}/details`.
>
> <sup>Reviewed by [Cursor Bugbot]() for commit
fa07126bbe9fd94c6b611834c782fd83284df9b6. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
229 lines
8.1 KiB
Go
229 lines
8.1 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
// This file is duplicated in sdk-go/temporalnexus/link_converter.go.
|
|
// Any changes here or there must be replicated. This is temporary until the
|
|
// temporal repo updates to the most recent SDK version.
|
|
|
|
package nexus
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"github.com/nexus-rpc/sdk-go/nexus"
|
|
commonpb "go.temporal.io/api/common/v1"
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
)
|
|
|
|
const (
|
|
urlSchemeTemporalKey = "temporal"
|
|
urlPathNamespaceKey = "namespace"
|
|
urlPathWorkflowIDKey = "workflowID"
|
|
urlPathRunIDKey = "runID"
|
|
urlPathWorkflowEventTemplate = "/namespaces/%s/workflows/%s/%s/history"
|
|
urlPathNexusOperationTemplate = "/namespaces/%s/nexus-operations/%s/%s/details"
|
|
|
|
linkWorkflowEventReferenceTypeKey = "referenceType"
|
|
linkEventIDKey = "eventID"
|
|
linkEventTypeKey = "eventType"
|
|
linkRequestIDKey = "requestID"
|
|
)
|
|
|
|
var (
|
|
rePatternNamespace = fmt.Sprintf(`(?P<%s>[^/]+)`, urlPathNamespaceKey)
|
|
rePatternWorkflowID = fmt.Sprintf(`(?P<%s>[^/]+)`, urlPathWorkflowIDKey)
|
|
rePatternRunID = fmt.Sprintf(`(?P<%s>[^/]+)`, urlPathRunIDKey)
|
|
urlPathRE = regexp.MustCompile(fmt.Sprintf(
|
|
`^/namespaces/%s/workflows/%s/%s/history$`,
|
|
rePatternNamespace,
|
|
rePatternWorkflowID,
|
|
rePatternRunID,
|
|
))
|
|
eventReferenceType = string((&commonpb.Link_WorkflowEvent_EventReference{}).ProtoReflect().Descriptor().Name())
|
|
requestIDReferenceType = string((&commonpb.Link_WorkflowEvent_RequestIdReference{}).ProtoReflect().Descriptor().Name())
|
|
)
|
|
|
|
// ConvertLinkNexusOperationToNexusLink converts a Link_NexusOperation type to Nexus Link.
|
|
func ConvertLinkNexusOperationToNexusLink(no *commonpb.Link_NexusOperation) nexus.Link {
|
|
u := &url.URL{
|
|
Scheme: urlSchemeTemporalKey,
|
|
Path: fmt.Sprintf(urlPathNexusOperationTemplate, no.GetNamespace(), no.GetOperationId(), no.GetRunId()),
|
|
RawPath: fmt.Sprintf(
|
|
urlPathNexusOperationTemplate,
|
|
url.PathEscape(no.GetNamespace()),
|
|
url.PathEscape(no.GetOperationId()),
|
|
url.PathEscape(no.GetRunId()),
|
|
),
|
|
}
|
|
|
|
return nexus.Link{
|
|
URL: u,
|
|
Type: string(no.ProtoReflect().Descriptor().FullName()),
|
|
}
|
|
}
|
|
|
|
// ConvertLinkWorkflowEventToNexusLink converts a Link_WorkflowEvent type to Nexus Link.
|
|
//
|
|
// NOTE: Experimental
|
|
func ConvertLinkWorkflowEventToNexusLink(we *commonpb.Link_WorkflowEvent) nexus.Link {
|
|
u := &url.URL{
|
|
Scheme: urlSchemeTemporalKey,
|
|
Path: fmt.Sprintf(urlPathWorkflowEventTemplate, we.GetNamespace(), we.GetWorkflowId(), we.GetRunId()),
|
|
RawPath: fmt.Sprintf(
|
|
urlPathWorkflowEventTemplate,
|
|
url.PathEscape(we.GetNamespace()),
|
|
url.PathEscape(we.GetWorkflowId()),
|
|
url.PathEscape(we.GetRunId()),
|
|
),
|
|
}
|
|
|
|
switch ref := we.GetReference().(type) {
|
|
case *commonpb.Link_WorkflowEvent_EventRef:
|
|
u.RawQuery = convertLinkWorkflowEventEventReferenceToURLQuery(ref.EventRef)
|
|
case *commonpb.Link_WorkflowEvent_RequestIdRef:
|
|
u.RawQuery = convertLinkWorkflowEventRequestIdReferenceToURLQuery(ref.RequestIdRef)
|
|
}
|
|
return nexus.Link{
|
|
URL: u,
|
|
Type: string(we.ProtoReflect().Descriptor().FullName()),
|
|
}
|
|
}
|
|
|
|
// ConvertNexusLinkToLinkWorkflowEvent converts a Nexus Link to Link_WorkflowEvent.
|
|
//
|
|
// NOTE: Experimental
|
|
func ConvertNexusLinkToLinkWorkflowEvent(link nexus.Link) (*commonpb.Link_WorkflowEvent, error) {
|
|
we := &commonpb.Link_WorkflowEvent{}
|
|
if link.Type != string(we.ProtoReflect().Descriptor().FullName()) {
|
|
return nil, fmt.Errorf(
|
|
"cannot parse link type %q to %q",
|
|
link.Type,
|
|
we.ProtoReflect().Descriptor().FullName(),
|
|
)
|
|
}
|
|
|
|
if link.URL.Scheme != urlSchemeTemporalKey {
|
|
return nil, fmt.Errorf(
|
|
"failed to parse link to Link_WorkflowEvent: invalid scheme: %s",
|
|
link.URL.Scheme,
|
|
)
|
|
}
|
|
|
|
matches := urlPathRE.FindStringSubmatch(link.URL.EscapedPath())
|
|
if len(matches) != 4 {
|
|
return nil, errors.New("failed to parse link to Link_WorkflowEvent: malformed URL path")
|
|
}
|
|
|
|
var err error
|
|
we.Namespace, err = url.PathUnescape(matches[urlPathRE.SubexpIndex(urlPathNamespaceKey)])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
|
|
we.WorkflowId, err = url.PathUnescape(matches[urlPathRE.SubexpIndex(urlPathWorkflowIDKey)])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
|
|
we.RunId, err = url.PathUnescape(matches[urlPathRE.SubexpIndex(urlPathRunIDKey)])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
|
|
switch refType := link.URL.Query().Get(linkWorkflowEventReferenceTypeKey); refType {
|
|
case eventReferenceType:
|
|
eventRef, err := convertURLQueryToLinkWorkflowEventEventReference(link.URL.Query())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
we.Reference = &commonpb.Link_WorkflowEvent_EventRef{
|
|
EventRef: eventRef,
|
|
}
|
|
case requestIDReferenceType:
|
|
requestIDRef, err := convertURLQueryToLinkWorkflowEventRequestIdReference(link.URL.Query())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse link to Link_WorkflowEvent: %w", err)
|
|
}
|
|
we.Reference = &commonpb.Link_WorkflowEvent_RequestIdRef{
|
|
RequestIdRef: requestIDRef,
|
|
}
|
|
default:
|
|
return nil, fmt.Errorf(
|
|
"failed to parse link to Link_WorkflowEvent: unknown reference type: %q",
|
|
refType,
|
|
)
|
|
}
|
|
|
|
return we, nil
|
|
}
|
|
|
|
func convertLinkWorkflowEventEventReferenceToURLQuery(eventRef *commonpb.Link_WorkflowEvent_EventReference) string {
|
|
values := url.Values{}
|
|
values.Set(linkWorkflowEventReferenceTypeKey, eventReferenceType)
|
|
if eventRef.GetEventId() > 0 {
|
|
values.Set(linkEventIDKey, strconv.FormatInt(eventRef.GetEventId(), 10))
|
|
}
|
|
values.Set(linkEventTypeKey, eventRef.GetEventType().String())
|
|
return values.Encode()
|
|
}
|
|
|
|
func convertURLQueryToLinkWorkflowEventEventReference(queryValues url.Values) (*commonpb.Link_WorkflowEvent_EventReference, error) {
|
|
var err error
|
|
eventRef := &commonpb.Link_WorkflowEvent_EventReference{}
|
|
eventIDValue := queryValues.Get(linkEventIDKey)
|
|
if eventIDValue != "" {
|
|
eventRef.EventId, err = strconv.ParseInt(queryValues.Get(linkEventIDKey), 10, 64)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
eventRef.EventType, err = enumspb.EventTypeFromString(queryValues.Get(linkEventTypeKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return eventRef, nil
|
|
}
|
|
|
|
func convertLinkWorkflowEventRequestIdReferenceToURLQuery(requestIDRef *commonpb.Link_WorkflowEvent_RequestIdReference) string {
|
|
values := url.Values{}
|
|
values.Set(linkWorkflowEventReferenceTypeKey, requestIDReferenceType)
|
|
values.Set(linkRequestIDKey, requestIDRef.GetRequestId())
|
|
values.Set(linkEventTypeKey, requestIDRef.GetEventType().String())
|
|
return values.Encode()
|
|
}
|
|
|
|
func convertURLQueryToLinkWorkflowEventRequestIdReference(queryValues url.Values) (*commonpb.Link_WorkflowEvent_RequestIdReference, error) {
|
|
var err error
|
|
requestIDRef := &commonpb.Link_WorkflowEvent_RequestIdReference{
|
|
RequestId: queryValues.Get(linkRequestIDKey),
|
|
}
|
|
requestIDRef.EventType, err = enumspb.EventTypeFromString(queryValues.Get(linkEventTypeKey))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return requestIDRef, nil
|
|
}
|