2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
import (
|
2020-12-01 20:06:49 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2020-12-03 14:51:15 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
2020-12-04 23:41:26 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2019-07-09 18:14:06 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2022-01-27 13:44:35 +01:00
|
|
|
"net/http"
|
2016-04-04 15:44:34 +02:00
|
|
|
)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// RepositoryMiddleware ensures a repository exists and loads it to the context
|
2019-07-09 18:14:06 +02:00
|
|
|
func RepositoryMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-12-03 14:51:15 +01:00
|
|
|
repositoryID, err := helpers.GetIntParam("repository_id", w, r)
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
2019-07-09 14:56:03 +02:00
|
|
|
}
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
repository, err := helpers.Store(r).GetRepository(project.ID, repositoryID)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
context.Set(r, "repository", repository)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
func GetRepositoryRefs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
repo := context.Get(r, "repository").(db.Repository)
|
|
|
|
refs, err := helpers.Store(r).GetRepositoryRefs(repo.ProjectID, repo.ID)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, refs)
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetRepositories returns all repositories in a project sorted by type
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetRepositories(w http.ResponseWriter, r *http.Request) {
|
2020-11-04 20:30:36 +01:00
|
|
|
if repo := context.Get(r, "repository"); repo != nil {
|
2020-12-04 23:41:26 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, repo.(db.Repository))
|
2020-11-04 20:30:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2017-03-16 16:31:34 +01:00
|
|
|
|
2021-10-13 16:33:07 +02:00
|
|
|
repos, err := helpers.Store(r).GetRepositories(project.ID, helpers.QueryParams(r.URL))
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, repos)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddRepository creates a new repository in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddRepository(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
var repository db.Repository
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &repository) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
if repository.ProjectID != project.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
newRepo, err := helpers.Store(r).CreateRepository(repository)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2020-12-07 20:48:52 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-17 12:41:36 +02:00
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2021-10-13 16:07:22 +02:00
|
|
|
objType := db.EventRepository
|
2019-07-09 18:11:01 +02:00
|
|
|
|
|
|
|
desc := "Repository (" + repository.GitURL + ") created"
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2020-12-07 20:48:52 +01:00
|
|
|
ProjectID: &newRepo.ProjectID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ObjectType: &objType,
|
2020-12-07 20:48:52 +01:00
|
|
|
ObjectID: &newRepo.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
Description: &desc,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateRepository updates the values of a repository in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func UpdateRepository(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
oldRepo := context.Get(r, "repository").(db.Repository)
|
2020-12-07 20:48:52 +01:00
|
|
|
var repository db.Repository
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &repository) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-17 12:41:36 +02:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
if repository.ID != oldRepo.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Repository ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if repository.ProjectID != oldRepo.ProjectID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := helpers.Store(r).UpdateRepository(repository)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-07-01 01:54:09 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
if oldRepo.GitURL != repository.GitURL {
|
2022-01-27 13:44:35 +01:00
|
|
|
util.LogWarning(oldRepo.ClearCache())
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Repository (" + repository.GitURL + ") updated"
|
2021-10-13 16:07:22 +02:00
|
|
|
objType := db.EventRepository
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2022-01-27 13:44:35 +01:00
|
|
|
UserID: &user.ID,
|
2020-12-07 20:48:52 +01:00
|
|
|
ProjectID: &repository.ProjectID,
|
2019-07-09 18:11:01 +02:00
|
|
|
Description: &desc,
|
2020-12-07 20:48:52 +01:00
|
|
|
ObjectID: &repository.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ObjectType: &objType,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// RemoveRepository deletes a repository from a project in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func RemoveRepository(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
repository := context.Get(r, "repository").(db.Repository)
|
2016-06-17 22:16:46 +02:00
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
var err error
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
err = helpers.Store(r).DeleteRepository(repository.ProjectID, repository.ID)
|
|
|
|
if err == db.ErrInvalidOperation {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"error": "Repository is in use by one or more templates",
|
|
|
|
"inUse": true,
|
|
|
|
})
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 20:48:52 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2022-01-27 13:44:35 +01:00
|
|
|
util.LogWarning(repository.ClearCache())
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2016-07-01 01:54:09 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Repository (" + repository.GitURL + ") deleted"
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: &repository.ProjectID,
|
|
|
|
Description: &desc,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-17 12:41:36 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|