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"
2016-04-04 15:44:34 +02:00
"github.com/ansible-semaphore/semaphore/database"
"github.com/ansible-semaphore/semaphore/models"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gin-gonic/gin"
"github.com/masterminds/squirrel"
)
2016-04-02 14:40:07 +02:00
2016-04-04 15:44:34 +02:00
func RepositoryMiddleware ( c * gin . Context ) {
2016-04-09 21:09:57 +02:00
project := c . MustGet ( "project" ) . ( models . Project )
repositoryID , err := util . GetIntParam ( "repository_id" , c )
if err != nil {
return
}
var repository models . Repository
if err := database . Mysql . SelectOne ( & repository , "select * from project__repository where project_id=? and id=?" , project . ID , repositoryID ) ; err != nil {
if err == sql . ErrNoRows {
c . AbortWithStatus ( 404 )
return
}
panic ( err )
}
c . Set ( "repository" , repository )
2016-04-04 15:44:34 +02:00
c . Next ( )
2016-04-02 14:40:07 +02:00
}
2016-04-04 15:44:34 +02:00
func GetRepositories ( c * gin . Context ) {
project := c . MustGet ( "project" ) . ( models . Project )
var repos [ ] models . Repository
query , args , _ := squirrel . Select ( "*" ) .
From ( "project__repository" ) .
Where ( "project_id=?" , project . ID ) .
ToSql ( )
if _ , err := database . Mysql . Select ( & repos , query , args ... ) ; err != nil {
panic ( err )
}
c . JSON ( 200 , repos )
}
func AddRepository ( c * gin . Context ) {
project := c . MustGet ( "project" ) . ( models . Project )
var repository models . Repository
if err := c . Bind ( & repository ) ; err != nil {
return
}
if _ , err := database . Mysql . Exec ( "insert into project__repository set project_id=?, git_url=?, ssh_key_id=?" , project . ID , repository . GitUrl , repository . SshKeyID ) ; err != nil {
panic ( err )
}
c . AbortWithStatus ( 204 )
}
func UpdateRepository ( c * gin . Context ) {
2016-04-09 21:09:57 +02:00
project := c . MustGet ( "project" ) . ( models . Project )
var repository models . Repository
if err := c . Bind ( & repository ) ; err != nil {
return
}
if _ , err := database . Mysql . Exec ( "update project__repository set git_url=?, ssh_key_id=? where id=?" , repository . GitUrl , repository . SshKeyID , c . MustGet ( "repository" ) . ( models . Repository ) . ID ) ; err != nil {
panic ( err )
}
c . AbortWithStatus ( 204 )
2016-04-02 14:40:07 +02:00
}
2016-04-04 15:44:34 +02:00
func RemoveRepository ( c * gin . Context ) {
2016-04-09 21:09:57 +02:00
repository := c . MustGet ( "repository" ) . ( models . Repository )
2016-04-04 15:44:34 +02:00
2016-04-09 21:38:14 +02:00
if _ , err := database . Mysql . Exec ( "delete from project__repository where project_id=? and id=?" , project . ID , repository . ID ) ; err != nil {
2016-04-04 15:44:34 +02:00
panic ( err )
}
c . AbortWithStatus ( 204 )
2016-04-02 14:40:07 +02:00
}