Skip to content
  • Timo Furrer's avatar
    Implement autoflow CLI to run Flow script locally · 9779ea2d
    Timo Furrer authored
    Example Flow script:
    
        def check_status(resp, expected_status_code = 200):
            if resp.status_code != expected_status_code:
                fail("Unexpected HTTP status code", resp.status_code)
    
        def check_content_type(resp, expected_content_type = "application/json"):
            ct = resp.header.get("Content-Type", [])
            if len(ct) == 0 or not ct[0].startswith(expected_content_type):
                fail("Unexpected Content-Type", ct)
    
        def handle_issue_updated(w, ev):
            print("running handle_issue_updated event handler")
            project_id = ev["data"]["project_id"]
            issue_id = ev["data"]["issue_id"]
    
            issues_resource_url = "{}/api/v4/projects/{}/issues/{}".format(w.vars.gitlab_url, project_id, issue_id)
    
            resp = w.http.do(
              method = "GET",
              url = "{}/related_merge_requests".format(issues_resource_url),
            )
            check_status(resp)
            check_content_type(resp)
            body = json.decode(str(resp.body))
    
            open_related_merge_requests = [x for x in body if x["state"] == "opened"]
            if len(open_related_merge_requests) == 0:
              print("no related merge requests found, exiting handler")
              return
    
            resp = w.http.do(
              method = "PUT",
              url = issues_resource_url,
              body = bytes(json.encode({
                "labels": [
                  "workflow::in progress",
                ],
              })),
              header = {
                "Private-Token": w.vars.gitlab_token,
                "Content-Type": "application/json",
              },
            )
            check_status(resp)
    
        on_event(
            type="com.gitlab.events.issue_updated",
            handler=handle_issue_updated,
        )
    
    Example Event:
    
        {
          "id": "test",
          "source": "test",
          "spec_version": "v1",
          "type": "com.gitlab.events.issue_updated",
          "attributes": {
            "datacontenttype": {
              "ce_string": "application/json"
            }
          },
          "text_data": "{\"project_id\": 40819621, \"issue_id\": 3}"
        }
    
    Example Invocation:
    
        cat test-event | autoflow run-local -f flow.star
    9779ea2d