← All code samples

DevOps → approve request

Fetch a ticket (or pick an awaiting_approval row), approve it, and wait until the run finishes. Optionally post the default triage comment on the upstream ticket.

Step What the SDK does
1. Fetch or list fetch_and_wait or list_requests(status=awaiting_approval)
2. Approve and wait approve_and_wait
3. Optional post_source_comment

Minimal

from stackgen import StackgenClient, StackgenConfig

client = StackgenClient(
    StackgenConfig(
        base_url="https://app.stackgen.com",
        api_token="stackgen_…",
        workspace_id="<workspace-uuid>",
    )
)

req = client.devops.fetch_and_wait("CORE-981")
finished = client.devops.approve_and_wait(req["id"])
print(finished.get("execution_status"), finished.get("execution_result"))

How to run

python examples/devops/approve-request.py --dry-run
python examples/devops/approve-request.py --identifier CORE-981 --comment

Complete script

#!/usr/bin/env python3
"""DevOps: approve an Ops Inbox request and wait for the run.

Documented at:
  https://appcd-dev.github.io/stackgen-sdk/code-samples/devops-approve-request/

Usage:
  python examples/devops/approve-request.py --dry-run
  python examples/devops/approve-request.py --identifier CORE-981 --comment
"""

from __future__ import annotations

import argparse
import json
import os
import sys

from stackgen import StackgenClient, StackgenConfig
from stackgen.errors import StackgenError


def _config(args: argparse.Namespace) -> StackgenConfig:
    base = (args.base_url or os.environ.get("STACKGEN_URL", "")).strip()
    token = (args.api_token or os.environ.get("STACKGEN_TOKEN", "")).strip()
    org = (
        (args.workspace_id or "").strip()
        or os.environ.get("STACKGEN_WORKSPACE_ID", "").strip()
        or os.environ.get("STACKGEN_ORG_ID", "").strip()
        or os.environ.get("STACKGEN_PROJECT", "").strip()
        or os.environ.get("STACKGEN_PROJECT_ID", "").strip()
    )
    if not base or not token:
        raise SystemExit("set STACKGEN_URL / STACKGEN_TOKEN")
    return StackgenConfig(
        base_url=base,
        api_token=token,
        workspace_id=org,
        timeout_seconds=args.timeout_seconds,
        poll_interval_seconds=args.poll_interval_seconds,
    )


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--base-url", default="")
    parser.add_argument("--api-token", default="")
    parser.add_argument("--workspace-id",
        "--org-id", default="")
    parser.add_argument("--identifier", default="")
    parser.add_argument("--request-id", default="")
    parser.add_argument("--workflow-name", default="")
    parser.add_argument("--comment", action="store_true")
    parser.add_argument("--dry-run", action="store_true")
    parser.add_argument("--timeout-seconds", type=int, default=1800)
    parser.add_argument("--poll-interval-seconds", type=int, default=5)
    args = parser.parse_args()

    client = StackgenClient(_config(args))
    request_id = args.request_id.strip()

    if args.identifier.strip():
        req = client.devops.fetch_and_wait(args.identifier.strip())
        print("fetched:", json.dumps(req, indent=2, default=str))
        request_id = str(req.get("id") or "")
    elif not request_id:
        listed = client.devops.list_requests(status="awaiting_approval", page_size=5)
        print("candidates:", json.dumps(listed, indent=2, default=str))
        rows = []
        if isinstance(listed, dict):
            rows = [r for r in (listed.get("requests") or []) if isinstance(r, dict)]
        if not rows:
            print("no awaiting_approval requests", file=sys.stderr)
            return 0 if args.dry_run else 1
        request_id = str(rows[0].get("id") or "")

    if args.dry_run:
        print(f"request_id={request_id}", file=sys.stderr)
        return 0

    finished = client.devops.approve_and_wait(
        request_id,
        workflow_name=args.workflow_name or None,
    )
    print("result:", json.dumps(finished, indent=2, default=str))
    print(f"execution_result={finished.get('execution_result')}", file=sys.stderr)

    if args.comment:
        comment = client.devops.post_source_comment(request_id)
        print("comment:", json.dumps(comment, indent=2, default=str))
    return 0


if __name__ == "__main__":
    try:
        raise SystemExit(main())
    except StackgenError as exc:
        print(f"error: {exc}", file=sys.stderr)
        raise SystemExit(1)

Related: Sync inbox