Skip to main content

Golang

在本例中,您将创建一个 "包 "蓝图,使用 Port'sAPIwebhook functionality 的组合来引用 Go 模块、版本和依赖关系。然后,您将把该蓝图与 "服务 "蓝图关联起来,以便映射服务所引用的所有包。

为了将软件包引用到 Port,需要使用一个脚本,根据 webhook 配置发送软件包信息。

先决条件

创建以下蓝图定义和 webhook 配置:

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": {}
}

使用 Port 的 API 和 Bash 脚本

下面的示例片段展示了如何使用 Python 和 Bash 将 Port 的 API 和 Webhook 与现有管道集成:

在版本库中创建以下 Python 脚本,以创建或更新 Port 实体,作为管道的一部分:

Go Python script
# Dependencies to install:
# pip install requests
# pip install tldextract

import json
import requests
import os
from urllib.parse import urlparse

output_filename = "output.json"
webhook_url = os.environ.get('WEBHOOK_URL')
SERVICE_ID = os.environ.get('SERVICE_ID')

# Prepare the headers for the requests
headers = {'Content-Type': 'application/json'}

# Initialize the output file
with open(output_filename, 'w') as f:
json.dump([], f)

# Read the go.mod file
with open('go.mod', 'r') as f:
lines = f.readlines()

# Find all require blocks
require_blocks = []
start = -1
for i, line in enumerate(lines):
if line.strip() == 'require (':
start = i
elif line.strip() == ')' and start != -1:
require_blocks.append(lines[start + 1:i])
start = -1

# Process each require block
for requires in require_blocks:
for require in requires:
parts = require.split() # Split the line into parts

package_url = parts[0]
version = parts[1]
indirect = len(parts) > 3 and parts[2] == "//" and parts[3] == "indirect" # Check if the package is indirect

# Parse the package name from the URL
package_name = os.path.basename(urlparse(package_url).path)

# Ensure package_url is in URL format
if not package_url.startswith('http://') and not package_url.startswith('https://'):
package_url = 'https://' + package_url

# Prepare the package dictionary
package_dict = {
"identifier": package_name,
"package_name": package_name,
"package_url": package_url,
"version": version,
"indirect": indirect,
"service" SERVICE_ID
}

# Append to the output file
with open(output_filename, 'r+') as f:
data = json.load(f)
data.append(package_dict)
f.seek(0)
json.dump(data, f, indent=4)

# Send data to the webhook
response = requests.post(webhook_url, headers=headers, data=json.dumps(package_dict))
print(response.status_code)