Golang
In this example you are going to create a package blueprint that ingests Go modules, versions and dependencies using a combination of Port's API and webhook functionality. You will then relate this blueprint to a service blueprint, allowing you to map all the packages used by a service.
To ingest the packages to Port, a script that sends information about packages according to the webhook configuration is used.
Prerequisites
Create the following blueprint definition and webhook configuration:
Service blueprint
{
"identifier": "service",
"title": "Service",
"icon": "Service",
"schema": {
"properties": {
"description": {
"title": "Description",
"type": "string"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
}
Package blueprint
{
"identifier": "package",
"description": "This blueprint represents a Go dependency",
"title": "Go Package",
"icon": "Go",
"schema": {
"properties": {
"packageUrl": {
"icon": "DefaultProperty",
"title": "Package URL",
"description": "The URL of the dependency package",
"type": "string",
"format": "url"
},
"version": {
"type": "string",
"title": "Version",
"description": "The version of the dependency"
},
"indirect": {
"type": "boolean",
"title": "Indirect Dependency",
"description": "Whether the dependency is indirect"
},
"packageName": {
"type": "string",
"title": "Package Name",
"description": "The name of the dependency package"
}
},
"required": ["packageName", "packageUrl", "version"]
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {
"service": {
"title": "Service",
"target": "service",
"required": false,
"many": true
}
}
}
Package webhook configuration
{
"identifier": "goDependencyMapper",
"title": "Go Dependency Mapper",
"description": "A webhook configuration to map packages and dependencies from a file",
"icon": "Go",
"mappings": [
{
"blueprint": "package",
"entity": {
"identifier": ".body.package_name",
"title": ".body.package_url",
"properties": {
"packageName": ".body.package_name",
"packageUrl": ".body.package_url",
"version": ".body.version",
"indirect": ".body.indirect"
}
"relations": {
"service": ".body.service"
}
}
}
],
"enabled": true,
"security": {}
}
Working with Port's API and Bash script
Here is an example snippet showing how to integrate Port's API and Webhook with your existing pipelines using Python and Bash:
- Python
- Bash
Create the following Bash script in your repository to create or update Port entities as part of your pipeline: The script utilizes the The script relies on the Go Bash script
mapfile command, which is a built-in command in the Bash shell, to read lines from the go.mod file and store them in an array. Please note that this command may not be available in all shells by default. If you are using a different shell such as Dash or Zsh, you may need to switch to Bash or modify the script to achieve a similar functionality.jq command for manipulating JSON data. It is used to create JSON objects based on the package details extracted from the go.mod file and append these objects to an output JSON file. It is important to note that jq is a powerful JSON processor for the command-line, but it is not typically included in many systems by default. You may need to install it separately to use it.
Create the following Python script in your repository to create or update Port entities as part of your pipeline:Go Python script