-
Notifications
You must be signed in to change notification settings - Fork 631
UN-2190 [FEAT] Auto-capture execution ID in the API deployment Postman collection #2031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,24 @@ class HeaderItem: | |
| value: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class ScriptItem: | ||
| exec: list[str] | ||
| type: str = "text/javascript" | ||
|
|
||
|
|
||
| @dataclass | ||
| class EventItem: | ||
| listen: str | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2 (type safety) — constrain the closed enums. |
||
| script: ScriptItem | ||
|
|
||
|
|
||
| @dataclass | ||
| class VariableItem: | ||
| key: str | ||
| value: str | ||
|
|
||
|
|
||
| @dataclass | ||
| class FormDataItem: | ||
| key: str | ||
|
|
@@ -55,6 +73,7 @@ class RequestItem: | |
| class PostmanItem: | ||
| name: str | ||
| request: RequestItem | ||
| event: list[EventItem] | None = None | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2 — event: list[EventItem] = field(default_factory=list)lets you drop the strip loop and reduce |
||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -69,6 +88,12 @@ class APIBase(ABC): | |
| def get_form_data_items(self) -> list[FormDataItem]: | ||
| pass | ||
|
|
||
| def get_collection_variables(self) -> list["VariableItem"]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3 (nit) — unnecessary string forward-reference. |
||
| """Collection-level variables; only needed when a request | ||
| references them. | ||
| """ | ||
| return [] | ||
|
|
||
| @abstractmethod | ||
| def get_api_endpoint(self) -> str: | ||
| pass | ||
|
|
@@ -137,20 +162,52 @@ def get_api_endpoint(self) -> str: | |
| def _get_status_api_request(self) -> RequestItem: | ||
| header_list = [HeaderItem(key="Authorization", value=f"Bearer {self.api_key}")] | ||
| status_query_param = { | ||
| "execution_id": CollectionKey.STATUS_EXEC_ID_DEFAULT, | ||
| "execution_id": CollectionKey.STATUS_EXEC_ID_VARIABLE, | ||
| ApiExecution.INCLUDE_METADATA: "False", | ||
| ApiExecution.INCLUDE_METRICS: "False", | ||
| } | ||
| status_query_str = urlencode(status_query_param) | ||
| # Keep {{...}} unescaped so Postman resolves the collection variable | ||
| status_query_str = urlencode(status_query_param, safe="{}") | ||
| abs_api_endpoint = urljoin(settings.WEB_APP_ORIGIN_URL, self.api_endpoint) | ||
| status_url = urljoin(abs_api_endpoint, "?" + status_query_str) | ||
| return RequestItem(method=HTTPMethod.GET, header=header_list, url=status_url) | ||
|
|
||
| def get_collection_variables(self) -> list[VariableItem]: | ||
| return [ | ||
| VariableItem( | ||
| key=CollectionKey.EXEC_ID_VARIABLE_NAME, | ||
| value=CollectionKey.STATUS_EXEC_ID_DEFAULT, | ||
| ) | ||
| ] | ||
|
|
||
| def _get_execute_capture_event(self) -> EventItem: | ||
| """Post-response script that stores the execution_id from the execute | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3 (comment) — docstring omits the load-bearing response-shape coupling. The script silently no-ops unless the response is |
||
| response into a collection variable, so the status request can use it | ||
| without manual copy-pasting. | ||
| """ | ||
| return EventItem( | ||
| listen="test", | ||
| script=ScriptItem( | ||
| exec=[ | ||
| "let response = null;", | ||
| "try {", | ||
| " response = pm.response.json();", | ||
| "} catch (error) {", | ||
| " // Non-JSON response (e.g. gateway error); nothing to capture", | ||
| "}", | ||
| "if (response && response.message && response.message.execution_id) {", # noqa: E501 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1 — silent reuse of a stale Suggest making the miss explicit and preventing cross-run staleness: if (response && response.message && response.message.execution_id) {
pm.collectionVariables.set("execution_id", response.message.execution_id);
} else {
pm.collectionVariables.set("execution_id", "REPLACE_WITH_EXECUTION_ID");
console.warn("No execution_id in execute response; not reusing a stale value. Status:", pm.response.code);
}Optionally gate capture on |
||
| f' pm.collectionVariables.set("{CollectionKey.EXEC_ID_VARIABLE_NAME}", response.message.execution_id);', # noqa: E501 | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| "}", | ||
| ] | ||
| ), | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| def get_postman_items(self) -> list[PostmanItem]: | ||
| postman_item_list = [ | ||
| PostmanItem( | ||
| name=CollectionKey.EXECUTE_API_KEY, | ||
| request=self.get_create_api_request(), | ||
| event=[self._get_execute_capture_event()], | ||
| ), | ||
| PostmanItem( | ||
| name=CollectionKey.STATUS_API_KEY, | ||
|
|
@@ -192,6 +249,7 @@ def get_postman_items(self) -> list[PostmanItem]: | |
| class PostmanCollection: | ||
| info: PostmanInfo | ||
| item: list[PostmanItem] = field(default_factory=list) | ||
| variable: list[VariableItem] = field(default_factory=list) | ||
|
|
||
| @classmethod | ||
| def create( | ||
|
|
@@ -228,7 +286,11 @@ def create( | |
| ) | ||
| postman_info: PostmanInfo = data_object.get_postman_info() | ||
| postman_item_list = data_object.get_postman_items() | ||
| return cls(info=postman_info, item=postman_item_list) | ||
| return cls( | ||
| info=postman_info, | ||
| item=postman_item_list, | ||
| variable=data_object.get_collection_variables(), | ||
| ) | ||
|
|
||
| def to_dict(self) -> dict[str, Any]: | ||
| """Convert PostmanCollection instance to a dict. | ||
|
|
@@ -237,4 +299,8 @@ def to_dict(self) -> dict[str, Any]: | |
| dict[str, Any]: PostmanCollection as a dict | ||
| """ | ||
| collection_dict = asdict(self) | ||
| # Drop null event blocks; Postman expects "event" to be a list | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2 (test coverage) — |
||
| for item in collection_dict.get("item", []): | ||
| if item.get("event") is None: | ||
| item.pop("event", None) | ||
| return collection_dict | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3 — unenforced coupling between the variable name and its
{{...}}reference.EXEC_ID_VARIABLE_NAME = "execution_id"andSTATUS_EXEC_ID_VARIABLE = "{{execution_id}}"must agree (the status URL references{{execution_id}}, the capture JS sets"execution_id", and the collection variable keys on it). If one is renamed and the other isn't, the collection breaks silently — the status request would query a never-set variable. Derive one from the other so they can't drift: