|
package kiln
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// Represents the error JSON returned by Kiln API requests
type KilnError struct {
// Guaranteed-unique code for Kiln errors
Code string `json:"codeError"`
// Human-readable description
Description string `json:"sError"`
}
// Represents the project JSON returned from the Kiln API
type KilnProject struct {
Id int64 `json:"ixProject"`
Slug string `json:"sSlug"`
Name string `json:"sName"`
Description string `json:"sDescription"`
DefaultPermission string `json:"permissionDefault"`
RepoGroups []KilnRepoGroup
}
type KilnRepoGroup struct {
Id int64 `json:"ixRepoGroup"`
ProjectId int64 `json:"ixProject"`
Slug string `json:"sSlug"`
Name string `json:"sName"`
Repos []KilnRepo `json:"repos"`
}
type KilnRepo struct {
Id int64 `json:"ixRepo"`
RepoGroupId int64 `json:"ixRepoGroup"`
ParentId int64 `json:"ixParent"`
IsCentral bool `json:"fCentral"`
Slug string `json:"sSlug"`
GroupSlug string `json:"sGroupSlug"`
ProjectSlug string `json:"sProjectSlug"`
GitUrl string `json:"sGitUrl"`
GitSshUrl string `json:"sGitSshUrl"`
Name string `json:"sName"`
Description string `json:"sDescription"`
Status string `json:"sStatus"`
Size int64 `json:"bytesSize"`
Vcs int `json:"vcs"`
Creator KilnPerson `json:"personCreator"`
DefaultPermission string `json:"permissionDefault"`
Branches []KilnRepo `json:"repoBranches"`
}
type KilnPerson struct {
Id int64 `json:"ixPerson"`
Name string `json:"sName"`
Email string `json:"sEmail"`
}
func (k *KilnClient) CreateBranch(repoPath, branchName string) (newRepo *KilnRepo, err error) {
r, err := k.RepoForPath(repoPath)
if err != nil {
return
}
params := apiParams{
"sName": branchName,
"ixRepoGroup": strconv.FormatInt(r.RepoGroupId, 10),
"ixParent": strconv.FormatInt(r.Id, 10),
"fCentral": "false",
}
var resp []byte
if resp, err = k.apiPost("Repo/Create", params); err == nil {
var errors map[string][]KilnError
if err = json.Unmarshal(resp, &errors); err == nil {
if kilnErr, _ := errors["errors"]; len(kilnErr) > 0 {
err = fmt.Errorf("failed: %v\n", kilnErr[0].Description)
return
}
}
newRepo = new(KilnRepo)
err = json.Unmarshal(resp, newRepo)
return
}
return
}
func (k *KilnClient) Projects() (projects []KilnProject, err error) {
if resp, err := k.apiGet("Project", apiParams{}); err == nil {
err = json.Unmarshal(resp, &projects)
}
return
}
func (k *KilnClient) RepoForPath(repoPath string) (*KilnRepo, error) {
parts := strings.Split(repoPath, "/")
if len(parts) != 3 {
return nil, fmt.Errorf("unknown repository target: %v", repoPath)
}
if projects, err := k.Projects(); err == nil {
for _, project := range projects {
for _, repoGroup := range project.RepoGroups {
for _, repo := range repoGroup.Repos {
if strings.EqualFold(repo.ProjectSlug, parts[0]) &&
strings.EqualFold(repo.GroupSlug, parts[1]) &&
strings.EqualFold(repo.Slug, parts[2]) {
return &repo, nil
}
}
}
}
}
return nil, fmt.Errorf("repository not found")
}
func (k *KilnClient) IdForRepo(repoPath string) (int64, error) {
repo, err := k.RepoForPath(repoPath)
if err != nil {
return -1, err
}
return repo.Id, nil
}
func (k *KilnClient) RelatedRepos(repoPath string) (repos []KilnRepo, err error) {
repoId, err := k.IdForRepo(repoPath)
if err == nil {
resp, err := k.apiGet(fmt.Sprintf("Repo/%v/Related", repoId), apiParams{})
if err == nil {
err = json.Unmarshal(resp, &repos)
}
}
return
}
|
Loading...