Semaphore/api/projects/projects.go

192 lines
3.6 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
log "github.com/Sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
2023-07-24 16:04:03 +02:00
"github.com/ansible-semaphore/semaphore/util"
"net/http"
"github.com/gorilla/context"
2016-04-02 14:40:07 +02:00
)
// GetProjects returns all projects in this users context
func GetProjects(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "user").(*db.User)
2016-04-02 14:40:07 +02:00
projects, err := helpers.Store(r).GetProjects(user.ID)
2016-04-02 14:40:07 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
helpers.WriteJSON(w, http.StatusOK, projects)
2016-04-02 14:40:07 +02:00
}
2023-09-17 14:57:57 +02:00
func createDemoProject(projectID int, store db.Store) (err error) {
var noneKey db.AccessKey
var demoRepo db.Repository
var emptyEnv db.Environment
var buildInv db.Inventory
var devInv db.Inventory
var prodInv db.Inventory
noneKey, err = store.CreateAccessKey(db.AccessKey{
Name: "None",
})
if err != nil {
return
}
demoRepo, err = store.CreateRepository(db.Repository{
Name: "Demo Project",
ProjectID: projectID,
GitURL: "https://github.com/semaphoreui/demo-project.git",
GitBranch: "main",
SSHKeyID: noneKey.ID,
})
if err != nil {
return
}
emptyEnv, err = store.CreateEnvironment(db.Environment{
Name: "Empty",
ProjectID: projectID,
JSON: "{}",
})
if err != nil {
return
}
buildInv, err = store.CreateInventory(db.Inventory{
Name: "Build",
ProjectID: projectID,
Inventory: "[builder]\nlocalhost",
Type: "static",
})
if err != nil {
return
}
devInv, err = store.CreateInventory(db.Inventory{
ProjectID: projectID,
})
if err != nil {
return
}
prodInv, err = store.CreateInventory(db.Inventory{
ProjectID: projectID,
})
if err != nil {
return
}
_, err = store.CreateTemplate(db.Template{
Name: "Build",
Playbook: "build.yml",
ProjectID: projectID,
InventoryID: buildInv.ID,
EnvironmentID: &emptyEnv.ID,
RepositoryID: demoRepo.ID,
})
if err != nil {
return
}
_, err = store.CreateTemplate(db.Template{
Name: "Deploy to Dev",
Playbook: "deploy.yml",
ProjectID: projectID,
InventoryID: devInv.ID,
EnvironmentID: &emptyEnv.ID,
RepositoryID: demoRepo.ID,
})
if err != nil {
return
}
_, err = store.CreateTemplate(db.Template{
Name: "Deploy to Production",
Playbook: "deploy.yml",
ProjectID: projectID,
InventoryID: prodInv.ID,
EnvironmentID: &emptyEnv.ID,
RepositoryID: demoRepo.ID,
})
return
}
// AddProject adds a new project to the database
func AddProject(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "user").(*db.User)
2016-04-02 14:40:07 +02:00
2023-07-24 16:04:03 +02:00
if !user.Admin && !util.Config.NonAdminCanCreateProject {
log.Warn(user.Username + " is not permitted to edit users")
w.WriteHeader(http.StatusUnauthorized)
return
}
2023-09-17 14:57:57 +02:00
var bodyWithDemo struct {
db.Project
Demo bool `json:"demo"`
}
if !helpers.Bind(w, r, &bodyWithDemo) {
return
}
2016-04-02 14:40:07 +02:00
2023-09-17 14:57:57 +02:00
body := bodyWithDemo.Project
store := helpers.Store(r)
body, err := store.CreateProject(body)
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
2023-09-17 14:57:57 +02:00
_, err = store.CreateProjectUser(db.ProjectUser{ProjectID: body.ID, UserID: user.ID, Role: db.ProjectOwner})
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
2023-09-17 14:57:57 +02:00
if bodyWithDemo.Demo {
err = createDemoProject(body.ID, store)
if err != nil {
helpers.WriteError(w, err)
return
}
}
desc := "Project Created"
oType := db.EventProject
2023-09-17 14:57:57 +02:00
_, err = store.CreateEvent(db.Event{
2021-08-20 08:28:50 +02:00
UserID: &user.ID,
ProjectID: &body.ID,
Description: &desc,
ObjectType: &oType,
ObjectID: &body.ID,
})
if err != nil {
log.Error(err)
}
2016-04-17 02:20:23 +02:00
helpers.WriteJSON(w, http.StatusCreated, body)
2016-04-02 14:40:07 +02:00
}