2016-04-02 14:40:07 +02:00
package projects
2016-04-04 15:44:34 +02:00
import (
2016-04-09 21:09:57 +02:00
"database/sql"
2017-02-23 00:21:49 +01:00
"net/http"
2016-07-01 01:54:09 +02:00
"os"
"strconv"
2016-04-09 21:09:57 +02:00
2017-02-23 06:12:16 +01:00
"github.com/ansible-semaphore/semaphore/db"
2016-04-04 15:44:34 +02:00
"github.com/ansible-semaphore/semaphore/util"
2017-02-23 00:21:49 +01:00
"github.com/castawaylabs/mulekick"
"github.com/gorilla/context"
2016-04-04 15:44:34 +02:00
"github.com/masterminds/squirrel"
)
2016-04-02 14:40:07 +02:00
2017-02-23 06:12:16 +01:00
func clearRepositoryCache ( repository db . Repository ) error {
2016-07-01 01:54:09 +02:00
repoName := "repository_" + strconv . Itoa ( repository . ID )
repoPath := util . Config . TmpPath + "/" + repoName
_ , err := os . Stat ( repoPath )
if err == nil {
return os . RemoveAll ( repoPath )
}
return nil
}
2017-02-22 23:17:36 +01:00
func RepositoryMiddleware ( w http . ResponseWriter , r * http . Request ) {
2017-02-23 06:12:16 +01:00
project := context . Get ( r , "project" ) . ( db . Project )
2017-02-23 00:21:49 +01:00
repositoryID , err := util . GetIntParam ( "repository_id" , w , r )
2016-04-09 21:09:57 +02:00
if err != nil {
return
}
2017-02-23 06:12:16 +01:00
var repository db . Repository
if err := db . Mysql . SelectOne ( & repository , "select * from project__repository where project_id=? and id=?" , project . ID , repositoryID ) ; err != nil {
2016-04-09 21:09:57 +02:00
if err == sql . ErrNoRows {
2017-02-22 23:17:36 +01:00
w . WriteHeader ( http . StatusNotFound )
2016-04-09 21:09:57 +02:00
return
}
panic ( err )
}
2017-02-23 00:21:49 +01:00
context . Set ( r , "repository" , repository )
2016-04-02 14:40:07 +02:00
}
2017-02-22 23:17:36 +01:00
func GetRepositories ( w http . ResponseWriter , r * http . Request ) {
2017-02-23 06:12:16 +01:00
project := context . Get ( r , "project" ) . ( db . Project )
var repos [ ] db . Repository
2016-04-04 15:44:34 +02:00
2017-03-16 16:31:34 +01:00
sort := r . URL . Query ( ) . Get ( "sort" )
order := r . URL . Query ( ) . Get ( "order" )
if order != "asc" && order != "desc" {
order = "asc"
}
q := squirrel . Select ( "pr.id" ,
"pr.name" ,
"pr.project_id" ,
"pr.git_url" ,
"pr.ssh_key_id" ,
"pr.removed" ) .
From ( "project__repository pr" )
switch sort {
case "name" , "git_url" :
q = q . Where ( "pr.project_id=?" , project . ID ) .
OrderBy ( "pr." + sort + " " + order )
case "ssh_key" :
q = q . LeftJoin ( "access_key ak ON (pr.ssh_key_id = ak.id)" ) .
Where ( "pr.project_id=?" , project . ID ) .
OrderBy ( "ak.name " + order )
default :
q = q . Where ( "pr.project_id=?" , project . ID ) .
OrderBy ( "pr.name " + order )
}
query , args , _ := q . ToSql ( )
2016-04-04 15:44:34 +02:00
2017-02-23 06:12:16 +01:00
if _ , err := db . Mysql . Select ( & repos , query , args ... ) ; err != nil {
2016-04-04 15:44:34 +02:00
panic ( err )
}
2017-02-23 00:21:49 +01:00
mulekick . WriteJSON ( w , http . StatusOK , repos )
2016-04-04 15:44:34 +02:00
}
2017-02-22 23:17:36 +01:00
func AddRepository ( w http . ResponseWriter , r * http . Request ) {
2017-02-23 06:12:16 +01:00
project := context . Get ( r , "project" ) . ( db . Project )
2016-04-04 15:44:34 +02:00
2016-04-11 12:02:10 +02:00
var repository struct {
2016-05-21 00:22:09 +02:00
Name string ` json:"name" binding:"required" `
2016-04-11 12:02:10 +02:00
GitUrl string ` json:"git_url" binding:"required" `
SshKeyID int ` json:"ssh_key_id" binding:"required" `
}
2017-02-22 23:21:52 +01:00
if err := mulekick . Bind ( w , r , & repository ) ; err != nil {
2016-04-04 15:44:34 +02:00
return
}
2017-02-23 06:12:16 +01:00
res , err := db . Mysql . Exec ( "insert into project__repository set project_id=?, git_url=?, ssh_key_id=?, name=?" , project . ID , repository . GitUrl , repository . SshKeyID , repository . Name )
2016-04-17 12:41:36 +02:00
if err != nil {
panic ( err )
}
insertID , _ := res . LastInsertId ( )
insertIDInt := int ( insertID )
objType := "repository"
desc := "Repository (" + repository . GitUrl + ") created"
2017-02-23 06:12:16 +01:00
if err := ( db . Event {
2016-04-17 12:41:36 +02:00
ProjectID : & project . ID ,
ObjectType : & objType ,
ObjectID : & insertIDInt ,
Description : & desc ,
} . Insert ( ) ) ; err != nil {
2016-04-04 15:44:34 +02:00
panic ( err )
}
2017-02-22 23:17:36 +01:00
w . WriteHeader ( http . StatusNoContent )
2016-04-04 15:44:34 +02:00
}
2017-02-22 23:17:36 +01:00
func UpdateRepository ( w http . ResponseWriter , r * http . Request ) {
2017-02-23 06:12:16 +01:00
oldRepo := context . Get ( r , "repository" ) . ( db . Repository )
2016-06-14 04:37:40 +02:00
var repository struct {
Name string ` json:"name" binding:"required" `
GitUrl string ` json:"git_url" binding:"required" `
SshKeyID int ` json:"ssh_key_id" binding:"required" `
}
2017-02-22 23:21:52 +01:00
if err := mulekick . Bind ( w , r , & repository ) ; err != nil {
2016-04-09 21:09:57 +02:00
return
}
2017-02-23 06:12:16 +01:00
if _ , err := db . Mysql . Exec ( "update project__repository set name=?, git_url=?, ssh_key_id=? where id=?" , repository . Name , repository . GitUrl , repository . SshKeyID , oldRepo . ID ) ; err != nil {
2016-04-17 12:41:36 +02:00
panic ( err )
}
2016-07-01 01:54:09 +02:00
if oldRepo . GitUrl != repository . GitUrl {
clearRepositoryCache ( oldRepo )
}
2016-04-17 12:41:36 +02:00
desc := "Repository (" + repository . GitUrl + ") updated"
objType := "inventory"
2017-02-23 06:12:16 +01:00
if err := ( db . Event {
2016-04-17 12:41:36 +02:00
ProjectID : & oldRepo . ProjectID ,
Description : & desc ,
ObjectID : & oldRepo . ID ,
ObjectType : & objType ,
} . Insert ( ) ) ; err != nil {
2016-04-09 21:09:57 +02:00
panic ( err )
}
2017-02-22 23:17:36 +01:00
w . WriteHeader ( http . StatusNoContent )
2016-04-02 14:40:07 +02:00
}
2017-02-22 23:17:36 +01:00
func RemoveRepository ( w http . ResponseWriter , r * http . Request ) {
2017-02-23 06:12:16 +01:00
repository := context . Get ( r , "repository" ) . ( db . Repository )
2016-04-04 15:44:34 +02:00
2017-02-23 06:12:16 +01:00
templatesC , err := db . Mysql . SelectInt ( "select count(1) from project__template where project_id=? and repository_id=?" , repository . ProjectID , repository . ID )
2016-06-17 22:16:46 +02:00
if err != nil {
panic ( err )
}
if templatesC > 0 {
2017-02-23 00:21:49 +01:00
if len ( r . URL . Query ( ) . Get ( "setRemoved" ) ) == 0 {
mulekick . WriteJSON ( w , http . StatusBadRequest , map [ string ] interface { } {
2016-06-17 22:16:46 +02:00
"error" : "Repository is in use by one or more templates" ,
"templatesUse" : true ,
} )
return
}
2017-02-23 06:12:16 +01:00
if _ , err := db . Mysql . Exec ( "update project__repository set removed=1 where id=?" , repository . ID ) ; err != nil {
2016-06-17 22:16:46 +02:00
panic ( err )
}
2017-02-22 23:17:36 +01:00
w . WriteHeader ( http . StatusNoContent )
2016-06-17 22:16:46 +02:00
return
}
2017-02-23 06:12:16 +01:00
if _ , err := db . Mysql . Exec ( "delete from project__repository where id=?" , repository . ID ) ; err != nil {
2016-04-04 15:44:34 +02:00
panic ( err )
}
2016-07-01 01:54:09 +02:00
clearRepositoryCache ( repository )
2016-04-17 12:41:36 +02:00
desc := "Repository (" + repository . GitUrl + ") deleted"
2017-02-23 06:12:16 +01:00
if err := ( db . Event {
2016-04-17 12:41:36 +02:00
ProjectID : & repository . ProjectID ,
Description : & desc ,
} . Insert ( ) ) ; err != nil {
panic ( err )
}
2017-02-22 23:17:36 +01:00
w . WriteHeader ( http . StatusNoContent )
2016-04-02 14:40:07 +02:00
}