{"id":28069822,"url":"https://github.com/keptn/go-utils","last_synced_at":"2025-05-12T19:15:31.035Z","repository":{"id":37412754,"uuid":"190221792","full_name":"keptn/go-utils","owner":"keptn","description":"Keptn util library for golang","archived":true,"fork":false,"pushed_at":"2023-12-21T11:51:44.000Z","size":1577,"stargazers_count":6,"open_issues_count":0,"forks_count":12,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-12T19:15:22.652Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"https://keptn.sh","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/keptn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-06-04T14:49:25.000Z","updated_at":"2023-12-21T14:02:02.000Z","dependencies_parsed_at":"2023-10-16T13:54:25.907Z","dependency_job_id":"d4c39b3c-5ff7-4e5b-acae-2463208f92d3","html_url":"https://github.com/keptn/go-utils","commit_stats":{"total_commits":674,"total_committers":32,"mean_commits":21.0625,"dds":0.7091988130563798,"last_synced_commit":"b61f2499102300c3a42c81b6ac079d0ed31f84ff"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keptn%2Fgo-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keptn%2Fgo-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keptn%2Fgo-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keptn%2Fgo-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/keptn","download_url":"https://codeload.github.com/keptn/go-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253805847,"owners_count":21967054,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest"],"created_at":"2025-05-12T19:15:30.416Z","updated_at":"2025-05-12T19:15:31.025Z","avatar_url":"https://github.com/keptn.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keptn go-utils\n\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/keptn/go-utils)\n![tests](https://github.com/keptn/go-utils/workflows/tests/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/keptn/go-utils)](https://goreportcard.com/report/github.com/keptn/go-utils)\n\n\u003cimg src=\"./gopher.png\" alt=\"go-utils-gopher\" width=\"210\"/\u003e\n\n## Keptn V1 has reached end of life on December 22nd, 2023 and has been [replaced](https://github.com/keptn/lifecycle-toolkit).\n\nThis repository contains packages for common functionality around the [Keptn Project](https://github.com/keptn).\nPlease post any issues to [keptn/keptn repository](https://github.com/keptn/keptn/issues) and label them\nwith `area:go-utils`.\n\n## Installation\n\nGet the latest version using\n\n```console\ngo get github.com/keptn/go-utils\n```\n\nAlso consider browsing our [Releases Page](https://github.com/keptn/go-utils/releases) to find out about all releases.\n\n## Contributing\n\nIf you want to contribute, just create a PR on the **master** branch.\nPlease also see [CONTRIBUTING.md](CONTRIBUTING.md) instructions on how to contribute.\n\n## Creating a Keptn service using `cp-connector`\n\nOne way to create a Keptn integration (a.k.a. Keptn service) is to use the `cp-connector` library which abstracts away\nthe\ndetails of how to interact with the Keptn API to register your implementation as an integration to the control\nplane.\n\n[Example](./examples/cp-connector)\n\n## Creating a Keptn service using the `go-sdk` (experimental)\n\nIf you want to use more features besides what the `cp-connector` provides, you can use the Keptn `go-sdk` which\nbasically wraps around `cp-connector` and provides features like automatic sending of `.started/.finished` or error\nevents.\n\n[Example](./examples/go-sdk)\n\n## Accessing the Keptn API\n\nThe recommended way of accessing the Keptn API using `GO` is to use the `APISet`:\nYou can use it by importing the following package:\n\n```golang\nimport api \"github.com/keptn/go-utils/pkg/api/utils/v2\"\n```\n\nThen you need to create an `APISet` and provide it the information about\nthe Keptn API endpoint URL and the auth token:\n\n```go\nkeptnAPI, err := api.New(\"https://url-to-my-keptn-cluster/api\", api.WithAuthToken(mySecretToken))\nif err != nil {\nlog.Fatal(err)\n}\n```\n\nOnce you have a handle to the `APISet` you can start using it:\n\n```go\nresources, err := keptnAPI.Resources().GetAllServiceResources(context.TODO(), \"my-project\", \"my-stage\", \"my-service\", api.ResourcesGetAllServiceResourcesOptions{})\nif err != nil {\nlog.Fatal(err)\n}\n```\n\n### Accessing the Keptn API from within the control plane\n\nIf you are developing a Keptn integration that is supposed to run *inside/as part of* the Keptn control plane,\nthere is no need to talk to the Keptn API via the API gateway. In this case one option is to use the `InternalAPISet`.\n\nImport the following package:\n\n```go\nimport api \"github.com/keptn/go-utils/pkg/api/utils/v2\"\n```\n\nThen create an `InternalAPISet`. Note, that this does *not* require you to pass the URL to the keptn API or an API token\n\n```go\nkeptnAPI, err := api.NewInternal(nil)\nif err != nil {\nlog.Fatal(err)\n}\n```\n\n## Creating and inspecting Keptn events:\n\n```go\n// Creating a new event\nnewEvent, _ := lib.KeptnEvent(lib.GetStartedEventType(\"echo-task\"), \"my-service\", lib.EventData{\nProject: \"my-project\",\nStage:   \"my-stage\",\nService: \"my-service\",\n}).Build()\n\n// Converting KeptnContextExtendedCE to cloudevents SDK event\ncloudEvent := lib.ToCloudEvent(newEvent)\nfmt.Println(cloudEvent.ID())\n\n// Converting cloudevents SDK event to KeptnContextExtendedCE\nnewEvent, _ = lib.ToKeptnEvent(cloudEvent)\n\n// Marshalling a keptn event to JSON\nnewEventAsJSON, _ := newEvent.ToJSON()\nfmt.Println(string(newEventAsJSON))\n\n// Creating a started event for a triggered event\nstartedEventType, _ := lib.ReplaceEventTypeKind(*triggeredEvent.Type, \"started\")\nstartedEvent := lib.KeptnEvent(startedEventType, \"my-service\", triggeredEvent.Data)\n\nstartedEventAsJSON, _ := startedEvent.ToJSON()\nfmt.Println(string(startedEventAsJSON))\n\n// Checking for event types\nfmt.Println(lib.IsSequenceEventType(\"sh.keptn.event.echo.triggered\")) // false\nfmt.Println(lib.IsSequenceEventType(\"sh.keptn.event.stage.echo.triggered\")) // true\nfmt.Println(lib.IsTaskEventType(\"sh.keptn.event.echo.triggered\")) // true\nfmt.Println(lib.IsTaskEventType(\"sh.keptn.event.stage.echo.triggered\")) // false\nfmt.Println(lib.IsTriggeredEventType(\"sh.keptn.event.echo.triggered\")) // true\n```\n\n## Querying Keptn events\nEvents can be retrieved from Keptn by using the event API of the `APISet`.\n\n*Note, that when providing an event filter, either `Project` or `KeptnContext` **must** be set.*\n\n```go\n// Creating the APISet\napiSet, _ := api.New(\"http://\u003ckeptn-url\u003e/api\", api.WithAuthToken(\"\u003capi-token\u003e\"))\n\n// Getting all events for a specific project\nevents, _ := apiSet.Events().GetEvents(context.TODO(), \u0026api.EventFilter{Project: \"echo-project\"}, api.EventsGetEventsOptions{})\n\n// Getting all event matching a specific keptn context\nevents, _ = apiSet.Events().GetEvents(context.TODO(), \u0026api.EventFilter{KeptnContext: \"7d4ca79a-6f38-4b88-9139-433342e350bf\"}, api.EventsGetEventsOptions{})\n\n// Getting all .triggered events that are \"not yet processed\" by a keptn integration for a specific project\nevents, _ = apiSet.ShipyardControl().GetOpenTriggeredEvents(context.TODO(), api.EventFilter{Project: \"echo-project\"}, api.ShipyardControlGetOpenTriggeredEventsOptions{})\n```\n\n## Ingesting Keptn events\n\nWhen using `cp-connector` or `go-sdk` there is a way predefined way to get a handle to the event sender for being able\nto *send back* events to Keptn (see [examples](./examples/cp-connector)).\nBelow is an example of how to use the `APISet` directly to send an event to Keptn:\n\n```go\n// Creating the APISet\napiSet, _ := api.New(\"http://\u003ckeptn-url\u003e/api\", api.WithAuthToken(\"\u003capi-token\u003e\"))\n\n// Create event you want to send\neventToSend, _ := lib.KeptnEvent(lib.GetStartedEventType(\"echo-task\"), \"my-service\",\n\tlib.EventData{\n\t    Project: \"my-project\", \n\t\tStage: \"my-stage\", \n\t\tService: \"my-service\",\n\t}).Build()\n\n// Sending the event to Keptn\neventContext, _ := apiSet.API().SendEvent(context.TODO(), eventToSend, api.APISendEventOptions{})\nfmt.Println(*eventContext.KeptnContext)\n```\n\n## Keptn GO models / structs\n\nThe GO models for the Keptn API can be used by importing: `github.com/keptn/go-utils/pkg/api/models`.\n\nFurther, the GO models for e.g. the shipyard `yaml` etc. can be used by\nimporting: `github.com/keptn/go-utils/pkg/lib/v0_2_0`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeptn%2Fgo-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeptn%2Fgo-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeptn%2Fgo-utils/lists"}