Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ repos:
additional_dependencies: [ '@commitlint/config-conventional' ]
#【可选】提升 Python 代码风格,符合更高版本的 Python 语法
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.0
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py38-plus]
2 changes: 1 addition & 1 deletion app_desc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spec_version: 2
app_version: "1.11.9"
app_version: "1.11.10"
app:
region: default
bk_app_code: &APP_CODE bk_flow_engine
Expand Down
1 change: 1 addition & 0 deletions bkflow/apigw/serializers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class CreateTaskSerializer(CredentialsValidationMixin, serializers.Serializer):
custom_span_attributes = serializers.DictField(
help_text=_("自定义 Span 属性,会添加到所有节点上报的 Span 中"), required=False, default={}
)
label_ids = serializers.ListField(help_text=_("标签ID列表"), child=serializers.IntegerField(), required=False)


class CreateTaskByAppSerializer(serializers.Serializer):
Expand Down
1 change: 1 addition & 0 deletions bkflow/apigw/serializers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CreateTemplateSerializer(serializers.Serializer):
)
extra_info = serializers.JSONField(help_text=_("额外扩展信息"), required=False)
pipeline_tree = serializers.JSONField(help_text=_("任务树"), required=False)
label_ids = serializers.ListField(help_text=_("标签"), child=serializers.IntegerField(), required=False)

def validate(self, attrs):
# 将 bind_app_code 映射到 bk_app_code 字段(models 中的字段名)
Expand Down
19 changes: 19 additions & 0 deletions bkflow/contrib/api/collections/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ def _get_task_url(self, api_name):
def task_list(self, data):
return self._request(method="get", url=self._get_task_url("task/"), data=data)

def update_labels(self, task_id, data):
return self._request(method="post", url=self._get_task_url("task/{}/update_labels/".format(task_id)), data=data)

def get_task_label_ref_count(self, space_id, label_ids):
return self._request(
method="get",
url=self._get_task_url(
"task/get_task_label_ref_count/?space_id={}&label_ids={}".format(space_id, label_ids)
),
data=None,
)

def delete_task_label_relation(self, data):
return self._request(
method="post",
url=self._get_task_url("task/delete_task_label_relation/"),
data=data,
)

def create_task(self, data):
return self._request(method="post", url=self._get_task_url("task/"), data=data)

Expand Down
31 changes: 30 additions & 1 deletion bkflow/interface/task/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
TaskTokenPermission,
)
from bkflow.interface.task.utils import StageConstantHandler, StageJobStateHandler
from bkflow.label.models import Label
from bkflow.permission.models import TASK_PERMISSION_TYPE, Token
from bkflow.space.configs import SuperusersConfig
from bkflow.space.models import SpaceConfig
Expand All @@ -56,7 +57,35 @@ class TaskInterfaceAdminViewSet(GenericViewSet):
@action(methods=["GET"], detail=False, url_path="get_task_list/(?P<space_id>\\d+)")
def get_task_list(self, request, space_id):
client = TaskComponentClient(space_id=space_id)
result = client.task_list(data={**request.query_params, "space_id": space_id})
# 把标签名称转换为id进行搜索
query_params = request.query_params.copy()
labels = request.query_params.get("label", "")
label_ids = Label.get_label_ids_by_names(labels)
if label_ids:
query_params["label"] = ",".join([str(label_id) for label_id in label_ids])
result = client.task_list(data={**query_params, "space_id": space_id})

label_ids = []
for item in result["data"]["results"]:
label_ids.extend(item["labels"])

labels_map = Label.objects.get_labels_map(set(label_ids))

for item in result["data"]["results"]:
item["labels"] = [labels_map.get(label_id) for label_id in item["labels"]]

return Response(result)

@action(methods=["POST"], detail=False, url_path="update_labels/(?P<space_id>\\d+)/(?P<pk>\\d+)")
def update_labels(self, request, space_id, pk=None):
"""
更新特定任务(pk指定)的标签列表。
请求体期望格式:{"label_ids": [1, 2, 5]}
"""
client = TaskComponentClient(space_id=space_id)
result = client.update_labels(pk, data={**request.data, "space_id": space_id})
labels_map = Label.objects.get_labels_map(set(result["data"]))
result["data"] = [labels_map.get(label_id) for label_id in result["data"]]
return Response(result)

@swagger_auto_schema(methods=["post"], operation_description="任务状态查询", request_body=GetTasksStatesBodySerializer)
Expand Down
18 changes: 18 additions & 0 deletions bkflow/label/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
70 changes: 70 additions & 0 deletions bkflow/label/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
from django.contrib import admin

from .models import Label, TemplateLabelRelation


@admin.register(Label)
class LabelAdmin(admin.ModelAdmin):
"""Admin configuration for Label model."""

list_display = (
"id",
"name",
"space_id",
"parent_label",
"is_default",
"color",
"full_path",
"created_at",
"updated_at",
)
list_filter = ("space_id", "is_default")
search_fields = ("name", "creator", "updated_by", "description")
readonly_fields = ("created_at", "updated_at", "full_path")
ordering = ("space_id", "parent_id", "name")
list_per_page = 50

def parent_label(self, obj):
"""Show parent label name in list_display."""
parent = obj.get_parent_label()
return parent.name if parent else "-"

parent_label.short_description = "Parent label"


@admin.register(TemplateLabelRelation)
class TemplateLabelRelationAdmin(admin.ModelAdmin):
"""Admin for template-label relations."""

list_display = ("id", "template_id", "label_id", "label_name")
list_filter = ("template_id",)
search_fields = ("template_id", "label_id")
list_per_page = 50

def label_name(self, obj):
"""Resolve label name from label_id."""
try:
label = Label.objects.get(id=obj.label_id)
return label.name
except Label.DoesNotExist:
return "-"

label_name.short_description = "Label"
24 changes: 24 additions & 0 deletions bkflow/label/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
from django.apps import AppConfig


class LabelConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "bkflow.label"
84 changes: 84 additions & 0 deletions bkflow/label/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="TemplateLabelRelation",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("template_id", models.IntegerField(db_index=True, verbose_name="模版ID")),
("label_id", models.IntegerField(db_index=True, verbose_name="标签ID")),
],
options={
"verbose_name": "模版标签关系 TemplateLabelRelation",
"verbose_name_plural": "模版标签关系 TemplateLabelRelation",
"unique_together": {("template_id", "label_id")},
},
),
migrations.CreateModel(
name="Label",
fields=[
("id", models.BigAutoField(primary_key=True, serialize=False, verbose_name="标签ID")),
("name", models.CharField(db_index=True, help_text="标签名称", max_length=255, verbose_name="标签名称")),
("creator", models.CharField(help_text="标签创建人", max_length=255, verbose_name="创建者")),
("updated_by", models.CharField(help_text="标签更新人", max_length=255, verbose_name="更新者")),
(
"space_id",
models.IntegerField(default=-1, help_text="标签对应的空间id(默认标签时space_id=-1)", verbose_name="空间ID"),
),
("is_default", models.BooleanField(default=False, help_text="是否是默认标签", verbose_name="默认标签")),
(
"color",
models.CharField(default="#dcffe2", help_text="标签颜色值(如#ffffff)", max_length=7, verbose_name="标签颜色"),
),
(
"description",
models.CharField(blank=True, help_text="标签描述", max_length=255, null=True, verbose_name="标签描述"),
),
(
"label_scope",
models.JSONField(
default="template", help_text="标签范围(支持多选,如['task', 'common'])", verbose_name="标签范围"
),
),
(
"parent_id",
models.IntegerField(
blank=True, default=None, help_text="父标签ID(根标签填null或留空)", null=True, verbose_name="父标签ID"
),
),
("created_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")),
("updated_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")),
],
options={
"verbose_name": "用户标签 Label",
"verbose_name_plural": "用户标签 Label",
"ordering": ["space_id", "parent_id", "name"],
"unique_together": {("space_id", "parent_id", "name")},
},
),
]
18 changes: 18 additions & 0 deletions bkflow/label/migrations/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
TencentBlueKing is pleased to support the open source community by making
蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available.
Copyright (C) 2024 THL A29 Limited,
a Tencent company. All rights reserved.
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the
specific language governing permissions and limitations under the License.

We undertake not to change the open source license (MIT license) applicable

to the current version of the project delivered to anyone in the future.
"""
Loading
Loading