macaron "gopkg.in/macaron.v1" | macaron "gopkg.in/macaron.v1" | ||||
) | ) | ||||
// APIContext is a specific macaron context for API service | |||||
type APIContext struct { | type APIContext struct { | ||||
*Context | *Context | ||||
Org *APIOrganization | Org *APIOrganization | ||||
} | } | ||||
} | } | ||||
// APIContexter returns apicontext as macaron middleware | |||||
func APIContexter() macaron.Handler { | func APIContexter() macaron.Handler { | ||||
return func(c *Context) { | return func(c *Context) { | ||||
ctx := &APIContext{ | ctx := &APIContext{ |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
) | ) | ||||
// APIOrganization contains organization and team | |||||
type APIOrganization struct { | type APIOrganization struct { | ||||
Organization *models.User | Organization *models.User | ||||
Team *models.Team | Team *models.Team |
macaron "gopkg.in/macaron.v1" | macaron "gopkg.in/macaron.v1" | ||||
) | ) | ||||
// ToggleOptions contains required or check options | |||||
type ToggleOptions struct { | type ToggleOptions struct { | ||||
SignInRequired bool | SignInRequired bool | ||||
SignOutRequired bool | SignOutRequired bool | ||||
DisableCSRF bool | DisableCSRF bool | ||||
} | } | ||||
// Toggle returns toggle options as middleware | |||||
func Toggle(options *ToggleOptions) macaron.Handler { | func Toggle(options *ToggleOptions) macaron.Handler { | ||||
return func(ctx *Context) { | return func(ctx *Context) { | ||||
// Cannot view any page before installation. | // Cannot view any page before installation. |
Org *Organization | Org *Organization | ||||
} | } | ||||
// HasError returns true if error occurs in form validation. | |||||
func (ctx *Context) HasApiError() bool { | |||||
// HasAPIError returns true if error occurs in form validation. | |||||
func (ctx *Context) HasAPIError() bool { | |||||
hasErr, ok := ctx.Data["HasError"] | hasErr, ok := ctx.Data["HasError"] | ||||
if !ok { | if !ok { | ||||
return false | return false | ||||
return hasErr.(bool) | return hasErr.(bool) | ||||
} | } | ||||
// GetErrMsg returns error message | |||||
func (ctx *Context) GetErrMsg() string { | func (ctx *Context) GetErrMsg() string { | ||||
return ctx.Data["ErrorMsg"].(string) | return ctx.Data["ErrorMsg"].(string) | ||||
} | } | ||||
ctx.Handle(500, title, err) | ctx.Handle(500, title, err) | ||||
} | } | ||||
// HandleText handles HTTP status code | |||||
func (ctx *Context) HandleText(status int, title string) { | func (ctx *Context) HandleText(status int, title string) { | ||||
if (status/100 == 4) || (status/100 == 5) { | if (status/100 == 4) || (status/100 == 5) { | ||||
log.Error(4, "%s", title) | log.Error(4, "%s", title) | ||||
ctx.PlainText(status, []byte(title)) | ctx.PlainText(status, []byte(title)) | ||||
} | } | ||||
// ServeContent serves content to http request | |||||
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) { | func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) { | ||||
modtime := time.Now() | modtime := time.Now() | ||||
for _, p := range params { | for _, p := range params { |
macaron "gopkg.in/macaron.v1" | macaron "gopkg.in/macaron.v1" | ||||
) | ) | ||||
// Organization contains organization context | |||||
type Organization struct { | type Organization struct { | ||||
IsOwner bool | IsOwner bool | ||||
IsMember bool | IsMember bool | ||||
Team *models.Team | Team *models.Team | ||||
} | } | ||||
// HandleOrgAssignment handles organization assignment | |||||
func HandleOrgAssignment(ctx *Context, args ...bool) { | func HandleOrgAssignment(ctx *Context, args ...bool) { | ||||
var ( | var ( | ||||
requireMember bool | requireMember bool | ||||
} | } | ||||
} | } | ||||
// OrgAssignment returns a macaron middleware to handle organization assignment | |||||
func OrgAssignment(args ...bool) macaron.Handler { | func OrgAssignment(args ...bool) macaron.Handler { | ||||
return func(ctx *Context) { | return func(ctx *Context) { | ||||
HandleOrgAssignment(ctx, args...) | HandleOrgAssignment(ctx, args...) |
macaron "gopkg.in/macaron.v1" | macaron "gopkg.in/macaron.v1" | ||||
) | ) | ||||
// PullRequest contains informations to make a pull request | |||||
type PullRequest struct { | type PullRequest struct { | ||||
BaseRepo *models.Repository | BaseRepo *models.Repository | ||||
Allowed bool | Allowed bool | ||||
HeadInfo string // [<user>:]<branch> | HeadInfo string // [<user>:]<branch> | ||||
} | } | ||||
// Repository contains informations to operate a repository | |||||
type Repository struct { | type Repository struct { | ||||
AccessMode models.AccessMode | AccessMode models.AccessMode | ||||
IsWatching bool | IsWatching bool | ||||
return editorconfig.ParseBytes(data) | return editorconfig.ParseBytes(data) | ||||
} | } | ||||
// RetrieveBaseRepo retrieves base repository | |||||
func RetrieveBaseRepo(ctx *Context, repo *models.Repository) { | func RetrieveBaseRepo(ctx *Context, repo *models.Repository) { | ||||
// Non-fork repository will not return error in this method. | // Non-fork repository will not return error in this method. | ||||
if err := repo.GetBaseRepo(); err != nil { | if err := repo.GetBaseRepo(); err != nil { | ||||
}))) | }))) | ||||
} | } | ||||
// RepoAssignment returns a macaron to handle repository assignment | |||||
func RepoAssignment(args ...bool) macaron.Handler { | func RepoAssignment(args ...bool) macaron.Handler { | ||||
return func(ctx *Context) { | return func(ctx *Context) { | ||||
var ( | var ( | ||||
} | } | ||||
} | } | ||||
// RequireRepoAdmin returns a macaron middleware for requiring repository admin permission | |||||
func RequireRepoAdmin() macaron.Handler { | func RequireRepoAdmin() macaron.Handler { | ||||
return func(ctx *Context) { | return func(ctx *Context) { | ||||
if !ctx.IsSigned || (!ctx.Repo.IsAdmin() && !ctx.User.IsAdmin) { | if !ctx.IsSigned || (!ctx.Repo.IsAdmin() && !ctx.User.IsAdmin) { | ||||
} | } | ||||
} | } | ||||
// RequireRepoWriter returns a macaron middleware for requiring repository write permission | |||||
func RequireRepoWriter() macaron.Handler { | func RequireRepoWriter() macaron.Handler { | ||||
return func(ctx *Context) { | return func(ctx *Context) { | ||||
if !ctx.IsSigned || (!ctx.Repo.IsWriter() && !ctx.User.IsAdmin) { | if !ctx.IsSigned || (!ctx.Repo.IsWriter() && !ctx.User.IsAdmin) { |
// Markdown render markdown document to HTML | // Markdown render markdown document to HTML | ||||
// see https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-an-arbitrary-markdown-document | // see https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-an-arbitrary-markdown-document | ||||
func Markdown(ctx *context.APIContext, form api.MarkdownOption) { | func Markdown(ctx *context.APIContext, form api.MarkdownOption) { | ||||
if ctx.HasApiError() { | |||||
if ctx.HasAPIError() { | |||||
ctx.Error(422, "", ctx.GetErrMsg()) | ctx.Error(422, "", ctx.GetErrMsg()) | ||||
return | return | ||||
} | } |