Skip to content

Pipeline token#3044

Merged
iwanghc merged 7 commits intomainfrom
pipeline_token
May 16, 2025
Merged

Pipeline token#3044
iwanghc merged 7 commits intomainfrom
pipeline_token

Conversation

@LordofAvernus
Copy link
Copy Markdown
Collaborator

@LordofAvernus LordofAvernus commented May 15, 2025

User description

关联的 issue

#3045

描述你的变更

流水线节点支持重置token

确认项(pr提交后操作)

Tip

请在指定复审人之前,确认并完成以下事项,完成后✅


  • 我已完成自测
  • 我已记录完整日志方便进行诊断
  • 我已在关联的issue里补充了实现方案
  • 我已在关联的issue里补充了测试影响面
  • 我已确认了变更的兼容性,如果不兼容则在issue里标记 not_compatible
  • 我已确认了是否要更新文档,如果要更新则在issue里标记 need_update_doc


Description

  • 添加刷新流水线节点token接口

  • 扩展Pipeline模型支持节点token刷新

  • 更新接口文档和Swagger配置

  • 增加服务端处理及路由注册


Changes walkthrough 📝

Relevant files
Enhancement
4 files
app.go
为pipeline添加刷新token路由                                                                         
+1/-0     
pipeline.go
添加刷新流水线token处理函数                                                                                 
+27/-1   
pipline.go
添加获取和更新pipeline节点的方法                                                                         
+19/-0   
pipeline.go
新增刷新流水线token服务实现                                                                                 
+17/-2   
Documentation
3 files
docs.go
更新接口文档以包含刷新token接口描述                                                                         
+53/-0   
swagger.json
更新Swagger文档展示刷新token接口                                                                     
+53/-0   
swagger.yaml
更新Swagger配置刷新token接口及字段                                                                   
+35/-0   

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @actiontech-bot actiontech-bot requested a review from iwanghc May 15, 2025 09:28
    @github-actions
    Copy link
    Copy Markdown

    github-actions Bot commented May 15, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 26b6a51)

    🎫 Ticket compliance analysis 🔶

    2602 - Partially compliant

    Compliant requirements:

    • 新增刷新流水线节点token接口
    • 路由和文档更新

    Non-compliant requirements:

    Requires further human verification:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    错误处理

    在 RefreshPipelineToken 方法中对 pipeline_id 和 node_id 的错误处理逻辑重复,建议抽取公共函数以提高代码复用性和可维护性。

    // @Summary 刷新流水线节点token
    // @Description refresh pipeline token
    // @Id refreshPipelineNodeTokenV1
    // @Tags pipeline
    // @Security ApiKeyAuth
    // @Param project_name path string true "project name"
    // @Param pipeline_id path string true "pipeline id"
    // @Param node_id path string true "node id"
    // @Success 200 {object} controller.BaseRes
    // @router /v1/projects/{project_name}/pipelines/{pipeline_id}/token/{node_id}/ [patch]
    func RefreshPipelineToken(c echo.Context) error {
    	pipelineID, err := strconv.Atoi(c.Param("pipeline_id"))
    	if err != nil {
    		return controller.JSONBaseErrorReq(c, err)
    	}
    	nodeID, err := strconv.Atoi(c.Param("node_id"))
    	if err != nil {
    		return controller.JSONBaseErrorReq(c, err)
    	}
    	var pipelineSvc pipeline.PipelineSvc
    	err = pipelineSvc.RefreshPipelineToken(uint(pipelineID), uint(nodeID), controller.GetUserID(c))
    	if err != nil {
    		return controller.JSONBaseErrorReq(c, err)
    	}
    	return c.JSON(http.StatusOK, controller.NewBaseReq(nil))
    }
    文档一致性

    新增的刷新流水线节点token接口在Swagger文档中的描述和字段(如 token_exp 和 version)需与 swagger.json 和 swagger.yaml 保持一致,建议仔细检查。

                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/controller.BaseRes"
                    }
                }
            }
        }
    },
    "/v1/projects/{project_name}/pipelines/{pipeline_id}/token/{node_id}/": {
        "patch": {
            "security": [
                {
                    "ApiKeyAuth": []
                }
            ],
            "description": "refresh pipeline token",
            "tags": [
                "pipeline"
            ],
            "summary": "刷新流水线节点token",
            "operationId": "refreshPipelineNodeTokenV1",
            "parameters": [
                {
                    "type": "string",
                    "description": "project name",
                    "name": "project_name",
                    "in": "path",
                    "required": true
                },
                {
                    "type": "string",
                    "description": "pipeline id",
                    "name": "pipeline_id",
                    "in": "path",
                    "required": true
                },
                {
                    "type": "string",
                    "description": "node id",
                    "name": "node_id",
                    "in": "path",
                    "required": true
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "$ref": "#/definitions/controller.BaseRes"
                    }
                }
            }
        }
    变量命名

    GetPipelineNode 和 UpdatePipelineNode 方法中使用的变量名与文件名中 “pipline” 的拼写需确认是否一致,建议检查拼写确保代码命名的清晰性。

    func (s *Storage) GetPipelineDetail(projectID ProjectUID, pipelineID uint) (*Pipeline, error) {
    	pipeline := &Pipeline{}
    	err := s.db.Model(Pipeline{}).Where("project_uid = ? AND id = ?", projectID, pipelineID).First(pipeline).Error
    	if err != nil {
    		return pipeline, errors.New(errors.ConnectStorageError, err)
    	}
    	return pipeline, nil
    }
    
    func (s *Storage) GetPipelineNode(pipelineID uint, nodeID uint) (*PipelineNode, error) {
    	var node PipelineNode
    	err := s.db.Model(PipelineNode{}).Where("pipeline_id = ? AND id = ?", pipelineID, nodeID).First(&node).Error
    	if err != nil {
    		return nil, errors.New(errors.ConnectStorageError, err)
    	}
    	return &node, nil
    }

    @github-actions
    Copy link
    Copy Markdown

    github-actions Bot commented May 15, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 26b6a51

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    参数有效性检查

    建议在参数转换后增加对 pipelineID 和 nodeID
    的有效性判断(例如判断是否为正整数),以防止无效的ID导致后续逻辑异常或潜在panic。此修改可以提高接口的健壮性,及时捕获参数错误。

    sqle/api/controller/v1/pipeline.go [400-405]

     pipelineID, err := strconv.Atoi(c.Param("pipeline_id"))
    -if err != nil {
    -    return controller.JSONBaseErrorReq(c, err)
    +if err != nil || pipelineID <= 0 {
    +    return controller.JSONBaseErrorReq(c, fmt.Errorf("invalid pipelineID"))
     }
     nodeID, err := strconv.Atoi(c.Param("node_id"))
    -if err != nil {
    -    return controller.JSONBaseErrorReq(c, err)
    +if err != nil || nodeID <= 0 {
    +    return controller.JSONBaseErrorReq(c, fmt.Errorf("invalid nodeID"))
     }
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion enhances error handling by checking that the converted pipelineID and nodeID are positive, improving robustness. However, using fmt.Errorf may require an additional import and the change is a minor defensive tweak, hence a moderate score.

    Medium

    Previous suggestions

    Suggestions up to commit f194b7e
    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    修复 nil 指针风险

    建议改为先声明一个 PipelineNode 的变量,之后传递它的地址给 First(),这样可以避免返回 nil 指针。同时确保返回的是变量的地址。这样可以防止后续对
    nil 指针的操作引发 panic。

    sqle/model/pipline.go [134-141]

     func (s *Storage) GetPipelineNode(pipelineID uint, nodeID uint) (*PipelineNode, error) {
    -	var node *PipelineNode
    +	var node PipelineNode
     	err := s.db.Model(PipelineNode{}).Where("pipeline_id = ? AND id = ?", pipelineID, nodeID).First(&node).Error
     	if err != nil {
    -		return node, errors.New(errors.ConnectStorageError, err)
    +		return nil, errors.New(errors.ConnectStorageError, err)
     	}
    -	return node, nil
    +	return &node, nil
     }
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion addresses a potential nil pointer risk by changing the declaration from a pointer to a local variable and then returning its address, which prevents runtime panics.

    Medium
    修正保存指针错误

    建议去掉对 newNode 使用取地址操作符 &,因为 newNode 已经是一个指针,将它传递给 Save() 即可。这样可以确保传递正确的参数类型给 ORM
    操作,防止潜在的类型错误。

    sqle/model/pipline.go [216-224]

     func (s *Storage) UpdatePipelineNode(newNode *PipelineNode) error {
     	return s.Tx(func(txDB *gorm.DB) error {
     		// 3 更新节点属性
    -		if err := txDB.Save(&newNode).Error; err != nil {
    +		if err := txDB.Save(newNode).Error; err != nil {
     			return fmt.Errorf("failed to update pipeline node: %w", err)
     		}
     		return nil
     	})
     }
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion removes the unnecessary address operator when passing newNode (already a pointer) to txDB.Save, ensuring the correct parameter type is used and avoiding potential errors.

    Medium
    Suggestions up to commit d8df7e2
    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    修复 Nil 指针问题

    建议更改 GetPipelineNode 中变量 node 的声明方式,使用非指针类型并传其地址给 ORM 查询,以避免因未正确初始化导致的 nil pointer
    问题。该修改能够防止潜在的 panic 情况发生。

    sqle/model/pipline.go [134-141]

     func (s *Storage) GetPipelineNode(pipelineID uint, nodeID uint) (*PipelineNode, error) {
    -	var node *PipelineNode
    +	var node PipelineNode
     	err := s.db.Model(PipelineNode{}).Where("pipeline_id = ? AND id = ?", pipelineID, nodeID).First(&node).Error
     	if err != nil {
    -		return node, errors.New(errors.ConnectStorageError, err)
    +		return nil, errors.New(errors.ConnectStorageError, err)
     	}
    -	return node, nil
    +	return &node, nil
     }
    Suggestion importance[1-10]: 9

    __

    Why: The change in GetPipelineNode replaces a pointer declaration with a value type and returns its address, effectively preventing a potential nil pointer dereference. This is a critical bug fix that directly improves stability.

    High
    修复双重指针错误

    建议在 UpdatePipelineNode 中直接传递 newNodeSave() 方法,而不是传递其地址,因为 newNode
    已经是一个指针。这可防止因错误的双重指针引用而导致的运行时错误。

    sqle/model/pipline.go [216-224]

     func (s *Storage) UpdatePipelineNode(newNode *PipelineNode) error {
     	return s.Tx(func(txDB *gorm.DB) error {
     		// 3 更新节点属性
    -		if err := txDB.Save(&newNode).Error; err != nil {
    +		if err := txDB.Save(newNode).Error; err != nil {
     			return fmt.Errorf("failed to update pipeline node: %w", err)
     		}
     		return nil
     	})
     }
    Suggestion importance[1-10]: 9

    __

    Why: The proposed change removes the unnecessary address operator in UpdatePipelineNode, ensuring that the correct pointer is passed to Save(). This directly addresses a runtime error and is an important and accurate fix.

    High

    @github-actions
    Copy link
    Copy Markdown

    Persistent review updated to latest commit f194b7e

    @github-actions
    Copy link
    Copy Markdown

    Persistent review updated to latest commit 26b6a51

    @iwanghc iwanghc merged commit 4133fa4 into main May 16, 2025
    4 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants