mirror of
https://github.com/semaphoreui/semaphore.git
synced 2024-11-23 12:30:41 +01:00
192 lines
3.6 KiB
Go
192 lines
3.6 KiB
Go
package projects
|
|
|
|
import (
|
|
log "github.com/Sirupsen/logrus"
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/context"
|
|
)
|
|
|
|
// GetProjects returns all projects in this users context
|
|
func GetProjects(w http.ResponseWriter, r *http.Request) {
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
projects, err := helpers.Store(r).GetProjects(user.ID)
|
|
|
|
if err != nil {
|
|
helpers.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, projects)
|
|
}
|
|
|
|
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)
|
|
|
|
if !user.Admin && !util.Config.NonAdminCanCreateProject {
|
|
log.Warn(user.Username + " is not permitted to edit users")
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
var bodyWithDemo struct {
|
|
db.Project
|
|
Demo bool `json:"demo"`
|
|
}
|
|
|
|
if !helpers.Bind(w, r, &bodyWithDemo) {
|
|
return
|
|
}
|
|
|
|
body := bodyWithDemo.Project
|
|
|
|
store := helpers.Store(r)
|
|
|
|
body, err := store.CreateProject(body)
|
|
if err != nil {
|
|
helpers.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
_, err = store.CreateProjectUser(db.ProjectUser{ProjectID: body.ID, UserID: user.ID, Role: db.ProjectOwner})
|
|
if err != nil {
|
|
helpers.WriteError(w, err)
|
|
return
|
|
}
|
|
|
|
if bodyWithDemo.Demo {
|
|
err = createDemoProject(body.ID, store)
|
|
|
|
if err != nil {
|
|
helpers.WriteError(w, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
desc := "Project Created"
|
|
oType := db.EventProject
|
|
_, err = store.CreateEvent(db.Event{
|
|
UserID: &user.ID,
|
|
ProjectID: &body.ID,
|
|
Description: &desc,
|
|
ObjectType: &oType,
|
|
ObjectID: &body.ID,
|
|
})
|
|
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
|
|
helpers.WriteJSON(w, http.StatusCreated, body)
|
|
}
|