Skip to content

Commit

Permalink
Merge pull request #2 from MessageDream/web
Browse files Browse the repository at this point in the history
Web
  • Loading branch information
Jayden Zhao committed Jul 21, 2017
2 parents 8b23b96 + 57105e3 commit 0c56dc7
Show file tree
Hide file tree
Showing 76 changed files with 23,092 additions and 1,448 deletions.
3 changes: 3 additions & 0 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ func runWeb(cliCtx *cli.Context) {

m.Group("/app", func() {
m.Combo("/list").Get(web.AppsGet)
m.Combo("/add").Post(bindIgnErr(forms.AppAddForm{}), web.AppAdd)
m.Get("/detail/:appName/collaborators", web.AppCollaboratorsGet)
m.Get("/detail/:appName/deployments", web.AppDeploymentsGet)
}, reqSignIn)

m.Group("/access_key", func() {
Expand Down
2 changes: 1 addition & 1 deletion core/clientService/client_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func UpdateCheck(cache cache.Cache, deploymentKey, appVersion, label, packageHas
checkResult.PackageSize = pkg.Size

pkgDiff := &model.PackageDiff{
PackageID: pkg.ID,
OriginalPackageHash: pkg.Hash,
DiffAgainstPackageHash: packageHash,
}

Expand Down
34 changes: 2 additions & 32 deletions core/collaboratorService/collaborator_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,18 @@ package collaboratorService
import (
"errors"

. "gopkg.in/ahmetb/go-linq.v3"

. "github.com/MessageDream/goby/core"
"github.com/MessageDream/goby/model"
"github.com/MessageDream/goby/model/dto"
"github.com/MessageDream/goby/module/infrastructure"
)

func List(uid uint64, appName string) (map[string]*dto.Collaborator, error) {
func List(uid uint64, appName string) ([]*model.Collaborator, error) {
col, err := CollaboratorOf(uid, appName)
if err != nil {
return nil, err
}

cols, err := model.FindCollaboratorsByAppIDs([]uint64{col.AppID})
if err != nil {
return nil, err
}

canOwner := false
colsMap := map[string]*dto.Collaborator{}
From(cols).Select(func(col interface{}) interface{} {
co := col.(*model.Collaborator)
isOwner := uid == co.UID
if isOwner && co.Role == 1 {
canOwner = true
}
permission := "Member"
if co.Role == 1 {
permission = "Owner"
}
return KeyValue{
Key: co.Email,
Value: &dto.Collaborator{
IsCurrentAccount: isOwner,
Permission: permission,
},
}

}).ToMap(&colsMap)

return colsMap, nil
return model.FindCollaboratorsByAppIDs([]uint64{col.AppID})

}

Expand Down
11 changes: 11 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ func OwnerCan(uid uint64, name string) (*model.Collaborator, error) {
return col, nil
}

func OwnerOf(name string) (*model.Collaborator, error) {
col, err := model.FindOwnerByAppName(name)
if err != nil || col == nil {
if err != nil {
return nil, err
}
return nil, ErrAppNotExist
}
return col, nil
}

func CollaboratorOf(uid uint64, name string) (*model.Collaborator, error) {
col, err := model.FindCollaboratorByAppNameAndUID(name, uid)

Expand Down
14 changes: 7 additions & 7 deletions core/deploymentService/deployment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"os"
"path"
"strconv"
"strings"

"github.com/go-macaron/cache"
Expand Down Expand Up @@ -274,7 +273,7 @@ func ReleaseDeployment(user *model.User,
manifesURLPath,
zipURLPath,
"Upload",
strconv.FormatUint(user.ID, 10)+"&"+user.Email,
user.Email,
description,
"",
"")
Expand Down Expand Up @@ -507,7 +506,7 @@ func RollbackDeployment(user *model.User, cache cache.Cache, appName, deployment
DeployVersionID: version.ID,
}

pkgs, err := rollbackPkg.FindPackagesByVersionIDAndReleaseMethods([]string{"Upload", "Promote"}, 2, false)
pkgs, err := rollbackPkg.FindPackagesByVersionIDAndReleaseMethods(2, false, "Upload", "Promote")
if err != nil {
return err
}
Expand Down Expand Up @@ -536,15 +535,16 @@ func RollbackDeployment(user *model.User, cache cache.Cache, appName, deployment
rollbackPkg.ManifestBlobURL,
rollbackPkg.BlobURL,
"Rollback",
strconv.FormatUint(user.ID, 10)+"&"+user.Email,
user.Email,
rollbackPkg.Description,
rollbackPkg.Label,
deployment.Name)

if err != nil {
clearCache(cache, deployment.Key, version.AppVersion)
return err
}
return err
clearCache(cache, deployment.Key, version.AppVersion)
return nil
}

func PromoteDeployment(user *model.User, cache cache.Cache, appName, sourceDeploymentName, destDeploymentName string) error {
Expand Down Expand Up @@ -599,7 +599,7 @@ func PromoteDeployment(user *model.User, cache cache.Cache, appName, sourceDeplo
sourcePkg.ManifestBlobURL,
sourcePkg.BlobURL,
"Promote",
strconv.FormatUint(user.ID, 10)+"&"+user.Email,
user.Email,
sourcePkg.Description,
sourcePkg.Label,
sourceDeployment.Name)
Expand Down
10 changes: 6 additions & 4 deletions core/deploymentService/package_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ func createPackage(deployment *model.Deployment,
}

if exist, err := pkgCheck.Exist(); err != nil || exist {
if exist {
if err != nil {
return nil, err
}
if exist && releaseMethod != "Rollback" {
return nil, ErrDeploymentPackageAlreadyExist
}
return nil, err
}

result, err := model.Transaction(func(sess *xorm.Session) (interface{}, error) {
Expand Down Expand Up @@ -316,7 +318,7 @@ func createDiffPackage(originalPackage *model.Package) ([]*model.PackageDiff, er

func createOneDiffPackage(originalPackage, prePackage *model.Package) (*model.PackageDiff, error) {
pkgDiff := &model.PackageDiff{
PackageID: originalPackage.ID,
OriginalPackageHash: originalPackage.Hash,
DiffAgainstPackageHash: prePackage.Hash,
}

Expand Down Expand Up @@ -474,7 +476,7 @@ func createOneDiffPackage(originalPackage, prePackage *model.Package) (*model.Pa
}()

diffPkg := &model.PackageDiff{
PackageID: originalPackage.ID,
OriginalPackageHash: originalPackage.Hash,
DiffAgainstPackageHash: prePackage.Hash,
DiffBlobURL: zipURLPath,
DiffSize: diffSize,
Expand Down
20 changes: 19 additions & 1 deletion model/collaborator.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,31 @@ func FindCollaboratorsByAppIDs(appIds []uint64) ([]*Collaborator, error) {
return cols, x.In("app_id", appIds).Find(&cols)
}

func FindOwnerByAppName(appName string) (*Collaborator, error) {
col := &Collaborator{}

exsit, err := x.Sql(`SELECT b.* FROM app AS a
INNER JOIN collaborator AS b
ON a.id = b.app_id
WHERE a.name = ? AND b.Role = 1 AND a.deleted_at IS NULL AND b.deleted_at IS NULL
LIMIT 1`, appName).Get(col)

if err != nil {
return nil, err
}
if !exsit {
return nil, nil
}
return col, nil
}

func FindCollaboratorByAppNameAndUID(appName string, uid uint64) (*Collaborator, error) {
col := &Collaborator{}

exsit, err := x.Sql(`SELECT b.* FROM app AS a
INNER JOIN collaborator AS b
ON a.id = b.app_id
WHERE a.name = ? and b.uid = ? AND a.deleted_at IS NULL AND b.deleted_at IS NULL
WHERE a.name = ? AND b.uid = ? AND a.deleted_at IS NULL AND b.deleted_at IS NULL
LIMIT 1`, appName, uid).Get(col)

if err != nil {
Expand Down
17 changes: 14 additions & 3 deletions model/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ func (self *DeploymentDetail) Convert() interface{} {
UploadTime: self.Package.CreatedAt.Unix() * 1000,
}
}

var metrics *dto.PackageMetrics
if self.PackageMetrics.ID > 0 {
metrics = &dto.PackageMetrics{
Active: self.PackageMetrics.Active,
Downloaded: self.PackageMetrics.Downloaded,
Failed: self.PackageMetrics.Failed,
Installed: self.PackageMetrics.Installed,
}
}
return &dto.Deployment{
Key: self.Key,
Name: self.Name,
Package: pkg,
Key: self.Key,
Name: self.Name,
Package: pkg,
PackageMetrics: metrics,
}
}

Expand Down
7 changes: 4 additions & 3 deletions model/dto/dto_deployment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dto

type Deployment struct {
Key string `json:"key"`
Name string `json:"name"`
Package *Package `json:"package"`
Key string `json:"key"`
Name string `json:"name"`
Package *Package `json:"package"`
PackageMetrics *PackageMetrics `json:packageMetrics`
}
9 changes: 5 additions & 4 deletions model/dto/dto_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ type Package struct {
}

type PackageMetrics struct {
Active uint64 `json:"active"`
Downloaded uint64 `json:"downloaded"`
Failed uint64 `json:"failed"`
Installed uint64 `json:"installed"`
Active uint64 `json:"active"`
TotalActive uint64 `json:"totalActive"`
Downloaded uint64 `json:"downloaded"`
Failed uint64 `json:"failed"`
Installed uint64 `json:"installed"`
}

type UpdatePackageInfo struct {
Expand Down
8 changes: 4 additions & 4 deletions model/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func (self *Package) FindPrePackages(number int, isAsc bool) ([]*Package, error)

}

func (self *Package) FindPackagesByVersionIDAndReleaseMethods(methods []string, number int, isAsc bool) ([]*Package, error) {
func (self *Package) FindPackagesByVersionIDAndReleaseMethods(number int, isAsc bool, methods ...string) ([]*Package, error) {

packages := make([]*Package, 0, number)
sess := x.Where("deploy_version_id = ?", self.DeployVersionID).In("release_method", &methods)
sess := x.Where("deploy_version_id = ?", self.DeployVersionID).In("release_method", methods)
if isAsc {
sess = sess.Asc("id")
} else {
Expand All @@ -92,7 +92,7 @@ func (self *Package) FindPackagesByVersionIDAndReleaseMethods(methods []string,

type PackageDiff struct {
ID uint64 `xorm:"pk autoincr"`
PackageID uint64 `xorm:"notnull default(0)"`
OriginalPackageHash string
DiffAgainstPackageHash string
DiffBlobURL string
DiffSize int64 `xorm:"notnull default(0)"`
Expand All @@ -106,7 +106,7 @@ func (self *PackageDiff) Update(engine Engine, specFields ...string) error {
}

func (self *PackageDiff) Exist() (bool, error) {
exi, err := x.Where("id!=?", 0).Get(&PackageDiff{PackageID: self.PackageID, DiffAgainstPackageHash: self.DiffAgainstPackageHash})
exi, err := x.Where("id!=?", 0).Get(&PackageDiff{OriginalPackageHash: self.OriginalPackageHash, DiffAgainstPackageHash: self.DiffAgainstPackageHash})
return exi, err
}

Expand Down
2 changes: 1 addition & 1 deletion model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (self *User) GenerateSalt() {

func (self *User) Tokens() ([]*UserToken, error) {
tokens := make([]*UserToken, 0, 10)
err := x.Find(&tokens, &UserToken{UID: self.ID})
err := x.Desc("created_at").Find(&tokens, &UserToken{UID: self.ID})
return tokens, err
}

Expand Down
14 changes: 7 additions & 7 deletions module/context/html_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ func HTMLContexter() macaron.Handler {
Session: sess,
}

ctx.Auth()

link := ctx.Req.RequestURI
i := strings.Index(link, "?")
if i > -1 {
link = link[:i]
}
ctx.Data["Link"] = link

ctx.Data["PageStartTime"] = time.Now()
ctx.Data["ShowRegistrationButton"] = !disableRegistration
csrfToken := x.GetToken()
ctx.Data["CsrfToken"] = csrfToken
ctx.Data["CsrfTokenHtml"] = template.HTML(`<input type="hidden" name="_csrf" value="` + csrfToken + `">`)

ctx.Auth()

ctx.Data["CsrfToken"] = x.GetToken()
ctx.Data["CsrfTokenHtml"] = template.HTML(`<input type="hidden" name="_csrf" value="` + x.GetToken() + `">`)
log.Trace("Session ID: %s", sess.ID())
log.Trace("CSRF Token: %v", ctx.Data["CsrfToken"])
log.Trace("CSRF Token: %v", csrfToken)

ctx.Data["ShowRegistrationButton"] = !disableRegistration
c.Map(ctx)
}
}
15 changes: 15 additions & 0 deletions module/form/app_form.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package form

import (
"github.com/go-macaron/binding"
macaron "gopkg.in/macaron.v1"
)

type AppAddForm struct {
Name string `form:"name" binding:"Required;MaxSize(35)"`
Platform string `form:"platform" binding:"Required"`
}

func (f *AppAddForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
return validate(errs, ctx.Data, f)
}
13 changes: 12 additions & 1 deletion module/infrastructure/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ func timeSince(then time.Time) string {
case diff <= 2:
return fmt.Sprintf("1 秒%s", lbl)
case diff < 1*Minute:
return fmt.Sprintf("tool.seconds", diff, lbl)
se := float64(diff) / 1000
if se < 0.1 {
se = 0.1
}
return fmt.Sprintf("%.1f 秒%s", se, lbl)

case diff < 2*Minute:
return fmt.Sprintf("1 分钟%s", lbl)
Expand Down Expand Up @@ -589,6 +593,13 @@ func DeCompress(zipFile, dest string) error {
}
defer rc.Close()
filename := path.Join(dest, file.Name)
if file.FileInfo().IsDir() {
err = os.MkdirAll(filename, 0755)
if err != nil {
return err
}
continue
}
err = os.MkdirAll(getDir(filename), 0755)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 0c56dc7

Please sign in to comment.