-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(api): allow template_name when creating project tasks via API #4128
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
Merged
fiftin
merged 10 commits into
semaphoreui:develop
from
befika:sem-207-allow-template_name-when-creating-project-tasks-via-api
Sep 9, 2026
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f80014
feat(api): allow template_name when creating project tasks via API
befika 7cf3520
Merge remote-tracking branch 'upstream/develop' into sem-207-allow-te…
befika 7418a48
feat(api): update validation errors to use common_errors package
befika ecac740
Potential fix for pull request finding
fiftin 07f95a8
feat(api): ensure template_name is cleared when resolving task template
befika 7e3ef4f
Merge branch 'sem-207-allow-template_name-when-creating-project-tasks…
befika af9c5fa
feat(api): enforce unique template names within projects and handle d…
befika 9e4d55e
feat(api): handle unique template names during project task creation …
befika 339f157
feat(api): improve template name handling during migration to ensure …
befika 2f2c28f
Merge branch 'develop' into sem-207-allow-template_name-when-creating…
befika 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
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
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
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
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
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
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
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,77 @@ | ||
| package sql | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "github.com/go-gorp/gorp/v3" | ||
| ) | ||
|
|
||
| type migration_2_20_2 struct { | ||
| db *SqlDb | ||
| } | ||
|
|
||
| // PreApply renames templates which share a name inside a project, so that | ||
| // v2.20.2.sql can put a unique index on (project_id, name). Template names were | ||
| // never unique before, so any installation may hold duplicates and the index | ||
| // would otherwise fail the upgrade. | ||
| // | ||
| // The rename is done here rather than in SQL because a generated name can clash | ||
| // with a name which is already taken ("Build" twice next to a real "Build (2)"), | ||
| // which needs a retry that portable SQL cannot express. | ||
| func (m migration_2_20_2) PreApply(tx *gorp.Transaction) error { | ||
| type templateName struct { | ||
| ID int `db:"id"` | ||
| ProjectID int `db:"project_id"` | ||
| Name string `db:"name"` | ||
| } | ||
|
|
||
| var templates []templateName | ||
|
|
||
| // Ordered by id so the oldest template of each name keeps it, and so the | ||
| // result does not depend on the order rows come back in. | ||
| _, err := tx.Select(&templates, | ||
| m.db.PrepareQuery("select `id`, `project_id`, `name` from `project__template` order by `id`")) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| taken := make(map[string]bool, len(templates)) | ||
| key := func(projectID int, name string) string { | ||
| return strconv.Itoa(projectID) + "\x00" + name | ||
| } | ||
|
|
||
| // Every name in use is reserved before anything is renamed, so that a | ||
| // generated name cannot take the name of a template which already has it: | ||
| // "Build" twice next to a real "Build (2)" must not turn the latter into | ||
| // "Build (2) (2)". | ||
| var duplicates []templateName | ||
|
|
||
| for _, template := range templates { | ||
| if taken[key(template.ProjectID, template.Name)] { | ||
| duplicates = append(duplicates, template) | ||
| continue | ||
| } | ||
|
|
||
| taken[key(template.ProjectID, template.Name)] = true | ||
| } | ||
|
|
||
| for _, template := range duplicates { | ||
| name := template.Name | ||
| for i := 2; taken[key(template.ProjectID, name)]; i++ { | ||
| name = template.Name + " (" + strconv.Itoa(i) + ")" | ||
| } | ||
|
|
||
| _, err = tx.Exec( | ||
| m.db.PrepareQuery("update `project__template` set `name`=? where `id`=?"), | ||
| name, template.ID) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| taken[key(template.ProjectID, name)] = true | ||
|
befika marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| return nil | ||
| } | ||
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,77 @@ | ||
| package sql | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestMigration_2_20_2_EnforcesUniqueTemplateName checks the index exists after | ||
| // the migrations the test store runs, so that a name identifies one template. | ||
| func TestMigration_2_20_2_EnforcesUniqueTemplateName(t *testing.T) { | ||
| store := InitConfigCreateTestStore() | ||
| projectID, repositoryID := newTemplateTestProject(t, store) | ||
|
|
||
| insert := func(name string) error { | ||
| _, err := store.exec( | ||
| "insert into project__template (project_id, repository_id, name, playbook, arguments, allow_override_args_in_task, app) values (?, ?, ?, 'site.yml', '[]', false, 'ansible')", | ||
| projectID, repositoryID, name) | ||
| return err | ||
| } | ||
|
|
||
| require.NoError(t, insert("Build website")) | ||
|
|
||
| // The store also rejects this, but here the insert goes straight to the | ||
| // database, so only the index can stop it. | ||
| assert.Error(t, insert("Build website")) | ||
|
|
||
| assert.NoError(t, insert("Deploy website")) | ||
| } | ||
|
|
||
| // TestMigration_2_20_2_RenamesExistingDuplicates covers PreApply: templates which | ||
| // already share a name must be renamed rather than failing the upgrade. | ||
| func TestMigration_2_20_2_RenamesExistingDuplicates(t *testing.T) { | ||
| store := InitConfigCreateTestStore() | ||
| projectID, repositoryID := newTemplateTestProject(t, store) | ||
|
|
||
| // Recreate the pre-migration state: drop the index, then write the duplicates | ||
| // an old installation would be holding. | ||
| _, err := store.exec("drop index project__template__project_id_name") | ||
| require.NoError(t, err) | ||
|
|
||
| names := []string{"Build", "Build", "Build", "Build (2)", "Deploy"} | ||
| for _, name := range names { | ||
| _, err = store.exec( | ||
| "insert into project__template (project_id, repository_id, name, playbook, arguments, allow_override_args_in_task, app) values (?, ?, ?, 'site.yml', '[]', false, 'ansible')", | ||
| projectID, repositoryID, name) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| tx, err := store.Sql().Begin() | ||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, migration_2_20_2{db: store}.PreApply(tx)) | ||
| require.NoError(t, tx.Commit()) | ||
|
|
||
| var renamed []struct { | ||
| ID int `db:"id"` | ||
| Name string `db:"name"` | ||
| } | ||
| _, err = store.Sql().Select(&renamed, | ||
| "select id, name from project__template order by id") | ||
| require.NoError(t, err) | ||
|
|
||
| // The oldest template of each name keeps it; the rest get a suffix, skipping | ||
| // "Build (2)" because that name is already taken. | ||
| actual := make([]string, 0, len(renamed)) | ||
| for _, template := range renamed { | ||
| actual = append(actual, template.Name) | ||
| } | ||
| assert.Equal(t, []string{"Build", "Build (3)", "Build (4)", "Build (2)", "Deploy"}, actual) | ||
|
|
||
| // The renames must leave the table indexable. | ||
| _, err = store.exec( | ||
| "create unique index project__template__project_id_name on project__template (project_id, name)") | ||
| assert.NoError(t, err) | ||
| } |
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 @@ | ||
| create unique index `project__template__project_id_name` on `project__template` (`project_id`, `name`); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.