-
Notifications
You must be signed in to change notification settings - Fork 635
Add tags on projects #8334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ArkVex
wants to merge
4
commits into
apache:main
Choose a base branch
from
ArkVex:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add tags on projects #8334
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
"myapp/models" | ||
"myapp/shared" | ||
) | ||
|
||
var db *gorm.DB | ||
|
||
// listProjects returns all projects with optional tag filtering | ||
func listProjects(c *gin.Context) { | ||
// Get tag filter params if any | ||
tagFilter := c.Query("tags") | ||
|
||
var projects []models.Project | ||
query := db.Model(&models.Project{}) | ||
|
||
// Apply tag filtering if provided | ||
if tagFilter != "" { | ||
tags := strings.Split(tagFilter, ",") | ||
query = query.Joins("JOIN _devlake_project_tags pt ON pt.project_id = projects.id"). | ||
Joins("JOIN tags t ON t.id = pt.tag_id"). | ||
Where("t.name IN ?", tags). | ||
Group("projects.id"). | ||
Having("COUNT(DISTINCT t.name) = ?", len(tags)) | ||
} | ||
|
||
// Execute the query | ||
if err := query.Find(&projects).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
// Load the tags for each project | ||
for i := range projects { | ||
db.Model(&projects[i]).Association("Tags").Find(&projects[i].Tags) | ||
} | ||
|
||
c.JSON(200, projects) | ||
} | ||
|
||
// getProject returns a specific project | ||
func getProject(c *gin.Context) { | ||
var project models.Project | ||
if err := db.First(&project, c.Param("id")).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
// Load associated tags | ||
db.Model(&project).Association("Tags").Find(&project.Tags) | ||
|
||
c.JSON(200, project) | ||
} | ||
|
||
func main() { | ||
r := gin.Default() | ||
r.GET("/projects", listProjects) | ||
r.GET("/projects/:id", getProject) | ||
r.Run() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/apache/incubator-devlake/api/shared" | ||
"github.com/apache/incubator-devlake/models" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// TagResponse is the API response for a tag | ||
type TagResponse struct { | ||
Success bool `json:"success"` | ||
Message string `json:"message"` | ||
Tag models.Tag `json:"tag"` | ||
} | ||
|
||
// TagsResponse is the API response for multiple tags | ||
type TagsResponse struct { | ||
Success bool `json:"success"` | ||
Message string `json:"message"` | ||
Tags []models.Tag `json:"tags"` | ||
} | ||
|
||
// TagRequest is the request body for creating/updating tags | ||
type TagRequest struct { | ||
Name string `json:"name" binding:"required"` | ||
Description string `json:"description"` | ||
Color string `json:"color" default:"#3399FF"` | ||
} | ||
|
||
// RegisterTagsRoutes registers the routes for tag management | ||
func RegisterTagsRoutes(router *gin.RouterGroup) { | ||
// Get all tags | ||
router.GET("/tags", listTags) | ||
|
||
// Create a new tag | ||
router.POST("/tags", createTag) | ||
|
||
// Get a specific tag | ||
router.GET("/tags/:id", getTag) | ||
|
||
// Update a tag | ||
router.PATCH("/tags/:id", updateTag) | ||
|
||
// Delete a tag | ||
router.DELETE("/tags/:id", deleteTag) | ||
|
||
// Project tag association endpoints | ||
router.POST("/projects/:projectId/tags/:tagId", addTagToProject) | ||
router.DELETE("/projects/:projectId/tags/:tagId", removeTagFromProject) | ||
router.GET("/projects/:projectId/tags", getProjectTags) | ||
} | ||
|
||
// listTags returns all tags | ||
func listTags(c *gin.Context) { | ||
var tags []models.Tag | ||
if err := db.Find(&tags).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
c.JSON(http.StatusOK, TagsResponse{ | ||
Success: true, | ||
Tags: tags, | ||
}) | ||
} | ||
|
||
// createTag creates a new tag | ||
func createTag(c *gin.Context) { | ||
var req TagRequest | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
tag := models.Tag{ | ||
Name: req.Name, | ||
Description: req.Description, | ||
Color: req.Color, | ||
} | ||
|
||
if err := db.Create(&tag).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, TagResponse{ | ||
Success: true, | ||
Message: "Tag created successfully", | ||
Tag: tag, | ||
}) | ||
} | ||
|
||
// getTag returns a specific tag by ID | ||
func getTag(c *gin.Context) { | ||
id := c.Param("id") | ||
var tag models.Tag | ||
|
||
if err := db.First(&tag, "id = ?", id).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, TagResponse{ | ||
Success: true, | ||
Tag: tag, | ||
}) | ||
} | ||
|
||
// updateTag updates a tag | ||
func updateTag(c *gin.Context) { | ||
id := c.Param("id") | ||
var req TagRequest | ||
|
||
if err := c.ShouldBindJSON(&req); err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
var tag models.Tag | ||
if err := db.First(&tag, "id = ?", id).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
tag.Name = req.Name | ||
tag.Description = req.Description | ||
tag.Color = req.Color | ||
|
||
if err := db.Save(&tag).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, TagResponse{ | ||
Success: true, | ||
Message: "Tag updated successfully", | ||
Tag: tag, | ||
}) | ||
} | ||
|
||
// deleteTag deletes a tag | ||
func deleteTag(c *gin.Context) { | ||
id := c.Param("id") | ||
|
||
// Delete tag associations first | ||
if err := db.Delete(&models.ProjectTag{}, "tag_id = ?", id).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
// Delete the tag | ||
if err := db.Delete(&models.Tag{}, "id = ?", id).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"success": true, | ||
"message": "Tag deleted successfully", | ||
}) | ||
} | ||
|
||
// addTagToProject associates a tag with a project | ||
func addTagToProject(c *gin.Context) { | ||
projectId := c.Param("projectId") | ||
tagId := c.Param("tagId") | ||
|
||
// Check if project exists | ||
var project models.Project | ||
if err := db.First(&project, "id = ?", projectId).Error; err != nil { | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"}) | ||
return | ||
} | ||
|
||
// Check if tag exists | ||
var tag models.Tag | ||
if err := db.First(&tag, "id = ?", tagId).Error; err != nil { | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Tag not found"}) | ||
return | ||
} | ||
|
||
// Create association | ||
projectTag := models.ProjectTag{ | ||
ProjectId: projectId, | ||
TagId: tagId, | ||
} | ||
|
||
// Check if association already exists | ||
var count int64 | ||
db.Model(&models.ProjectTag{}).Where("project_id = ? AND tag_id = ?", projectId, tagId).Count(&count) | ||
if count > 0 { | ||
c.JSON(http.StatusConflict, gin.H{"error": "Project already has this tag"}) | ||
return | ||
} | ||
|
||
if err := db.Create(&projectTag).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"success": true, | ||
"message": "Tag added to project successfully", | ||
}) | ||
} | ||
|
||
// removeTagFromProject removes a tag from a project | ||
func removeTagFromProject(c *gin.Context) { | ||
projectId := c.Param("projectId") | ||
tagId := c.Param("tagId") | ||
|
||
if err := db.Delete(&models.ProjectTag{}, "project_id = ? AND tag_id = ?", projectId, tagId).Error; err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"success": true, | ||
"message": "Tag removed from project successfully", | ||
}) | ||
} | ||
|
||
// getProjectTags gets all tags for a project | ||
func getProjectTags(c *gin.Context) { | ||
projectId := c.Param("projectId") | ||
|
||
// Check if project exists | ||
var project models.Project | ||
if err := db.First(&project, "id = ?", projectId).Error; err != nil { | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"}) | ||
return | ||
} | ||
|
||
var tags []models.Tag | ||
if err := db.Model(&project).Association("Tags").Find(&tags); err != nil { | ||
shared.ApiErrorHandler(c, err) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, TagsResponse{ | ||
Success: true, | ||
Tags: tags, | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package migrationscripts | ||
|
||
import ( | ||
"github.com/apache/incubator-devlake/core/plugin" | ||
"github.com/apache/incubator-devlake/core/errors" | ||
"github.com/apache/incubator-devlake/helpers/migrationhelper" | ||
) | ||
|
||
type addTags struct{} | ||
|
||
func (*addTags) Name() string { | ||
return "Add tag tables for project tagging" | ||
} | ||
|
||
func (*addTags) Up(baseRes context.BasicRes) errors.Error { | ||
db := baseRes.GetDal() | ||
|
||
err := db.AutoMigrate(&Tag{}, &ProjectTag{}) | ||
if err != nil { | ||
return errors.Convert(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Tag model for migration | ||
type Tag struct { | ||
ID string `gorm:"primaryKey;type:varchar(255)"` | ||
Name string `gorm:"type:varchar(255);uniqueIndex"` | ||
Description string `gorm:"type:varchar(255)"` | ||
Color string `gorm:"type:varchar(50)"` | ||
CreatedAt time.Time | ||
UpdatedAt time.Time | ||
} | ||
|
||
func (Tag) TableName() string { | ||
return "_devlake_tags" | ||
} | ||
|
||
// ProjectTag model for migration | ||
type ProjectTag struct { | ||
ProjectId string `gorm:"primaryKey;type:varchar(255)"` | ||
TagId string `gorm:"primaryKey;type:varchar(255)"` | ||
} | ||
|
||
func (ProjectTag) TableName() string { | ||
return "_devlake_project_tags" | ||
} | ||
|
||
func init() { | ||
migrationhelper.RegisterMigration(&addTags{}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not also be http.StatusOK instead of the explicit 200? Makes for easier reading.