Skip to content

Golint fixed modules context #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
macaron "gopkg.in/macaron.v1"
)

// APIContext is a specific macaron context for API service
type APIContext struct {
*Context
Org *APIOrganization
Expand Down Expand Up @@ -63,6 +64,7 @@ func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
}
}

// APIContexter returns apicontext as macaron middleware
func APIContexter() macaron.Handler {
return func(c *Context) {
ctx := &APIContext{
Expand Down
1 change: 1 addition & 0 deletions modules/context/api_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"code.gitea.io/gitea/models"
)

// APIOrganization contains organization and team
type APIOrganization struct {
Organization *models.User
Team *models.Team
Expand Down
2 changes: 2 additions & 0 deletions modules/context/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
macaron "gopkg.in/macaron.v1"
)

// ToggleOptions contains required or check options
type ToggleOptions struct {
SignInRequired bool
SignOutRequired bool
AdminRequired bool
DisableCSRF bool
}

// Toggle returns toggle options as middleware
func Toggle(options *ToggleOptions) macaron.Handler {
return func(ctx *Context) {
// Cannot view any page before installation.
Expand Down
7 changes: 5 additions & 2 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ type Context struct {
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"]
if !ok {
return false
}
return hasErr.(bool)
}

// GetErrMsg returns error message
func (ctx *Context) GetErrMsg() string {
return ctx.Data["ErrorMsg"].(string)
}
Expand Down Expand Up @@ -116,13 +117,15 @@ func (ctx *Context) NotFoundOrServerError(title string, errck func(error) bool,
ctx.Handle(500, title, err)
}

// HandleText handles HTTP status code
func (ctx *Context) HandleText(status int, title string) {
if (status/100 == 4) || (status/100 == 5) {
log.Error(4, "%s", title)
}
ctx.PlainText(status, []byte(title))
}

// ServeContent serves content to http request
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{}) {
modtime := time.Now()
for _, p := range params {
Expand Down
3 changes: 3 additions & 0 deletions modules/context/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
macaron "gopkg.in/macaron.v1"
)

// Organization contains organization context
type Organization struct {
IsOwner bool
IsMember bool
Expand All @@ -23,6 +24,7 @@ type Organization struct {
Team *models.Team
}

// HandleOrgAssignment handles organization assignment
func HandleOrgAssignment(ctx *Context, args ...bool) {
var (
requireMember bool
Expand Down Expand Up @@ -145,6 +147,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
}
}

// OrgAssignment returns a macaron middleware to handle organization assignment
func OrgAssignment(args ...bool) macaron.Handler {
return func(ctx *Context) {
HandleOrgAssignment(ctx, args...)
Expand Down
6 changes: 6 additions & 0 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
macaron "gopkg.in/macaron.v1"
)

// PullRequest contains informations to make a pull request
type PullRequest struct {
BaseRepo *models.Repository
Allowed bool
SameRepo bool
HeadInfo string // [<user>:]<branch>
}

// Repository contains informations to operate a repository
type Repository struct {
AccessMode models.AccessMode
IsWatching bool
Expand Down Expand Up @@ -96,6 +98,7 @@ func (r *Repository) GetEditorconfig() (*editorconfig.Editorconfig, error) {
return editorconfig.ParseBytes(data)
}

// RetrieveBaseRepo retrieves base repository
func RetrieveBaseRepo(ctx *Context, repo *models.Repository) {
// Non-fork repository will not return error in this method.
if err := repo.GetBaseRepo(); err != nil {
Expand Down Expand Up @@ -130,6 +133,7 @@ func earlyResponseForGoGetMeta(ctx *Context) {
})))
}

// RepoAssignment returns a macaron to handle repository assignment
func RepoAssignment(args ...bool) macaron.Handler {
return func(ctx *Context) {
var (
Expand Down Expand Up @@ -446,6 +450,7 @@ func RepoRef() macaron.Handler {
}
}

// RequireRepoAdmin returns a macaron middleware for requiring repository admin permission
func RequireRepoAdmin() macaron.Handler {
return func(ctx *Context) {
if !ctx.IsSigned || (!ctx.Repo.IsAdmin() && !ctx.User.IsAdmin) {
Expand All @@ -455,6 +460,7 @@ func RequireRepoAdmin() macaron.Handler {
}
}

// RequireRepoWriter returns a macaron middleware for requiring repository write permission
func RequireRepoWriter() macaron.Handler {
return func(ctx *Context) {
if !ctx.IsSigned || (!ctx.Repo.IsWriter() && !ctx.User.IsAdmin) {
Expand Down
33 changes: 18 additions & 15 deletions modules/httplib/httplib.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func createDefaultCookie() {
defaultCookieJar, _ = cookiejar.New(nil)
}

// Overwrite default settings
// SetDefaultSetting overwrites default settings
func SetDefaultSetting(setting Settings) {
settingMutex.Lock()
defer settingMutex.Unlock()
Expand All @@ -49,7 +49,7 @@ func SetDefaultSetting(setting Settings) {
}
}

// return *Request with specific method
// newRequest returns *Request with specific method
func newRequest(url, method string) *Request {
var resp http.Response
req := http.Request{
Expand Down Expand Up @@ -87,18 +87,19 @@ func Head(url string) *Request {
return newRequest(url, "HEAD")
}

// Settings is the default settings for http client
type Settings struct {
ShowDebug bool
UserAgent string
ConnectTimeout time.Duration
ReadWriteTimeout time.Duration
TlsClientConfig *tls.Config
TLSClientConfig *tls.Config
Proxy func(*http.Request) (*url.URL, error)
Transport http.RoundTripper
EnableCookie bool
}

// HttpRequest provides more useful methods for requesting one url than http.Request.
// Request provides more useful methods for requesting one url than http.Request.
type Request struct {
url string
req *http.Request
Expand All @@ -109,7 +110,7 @@ type Request struct {
body []byte
}

// Change request settings
// Setting changes request settings
func (r *Request) Setting(setting Settings) *Request {
r.setting = setting
return r
Expand Down Expand Up @@ -148,7 +149,7 @@ func (r *Request) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *Re

// SetTLSClientConfig sets tls connection configurations if visiting https url.
func (r *Request) SetTLSClientConfig(config *tls.Config) *Request {
r.setting.TlsClientConfig = config
r.setting.TLSClientConfig = config
return r
}

Expand All @@ -158,11 +159,12 @@ func (r *Request) Header(key, value string) *Request {
return r
}

// Headers returns headers in request.
func (r *Request) Headers() http.Header {
return r.req.Header
}

// Set the protocol version for incoming requests.
// SetProtocolVersion sets the protocol version for incoming requests.
// Client requests always use HTTP/1.1.
func (r *Request) SetProtocolVersion(vers string) *Request {
if len(vers) == 0 {
Expand All @@ -185,13 +187,13 @@ func (r *Request) SetCookie(cookie *http.Cookie) *Request {
return r
}

// Set transport to
// SetTransport sets transport to
func (r *Request) SetTransport(transport http.RoundTripper) *Request {
r.setting.Transport = transport
return r
}

// Set http proxy
// SetProxy sets http proxy
// example:
//
// func(req *http.Request) (*url.URL, error) {
Expand All @@ -210,6 +212,7 @@ func (r *Request) Param(key, value string) *Request {
return r
}

// PostFile uploads file via http
func (r *Request) PostFile(formname, filename string) *Request {
r.files[formname] = filename
return r
Expand Down Expand Up @@ -301,15 +304,15 @@ func (r *Request) getResponse() (*http.Response, error) {
if trans == nil {
// create default transport
trans = &http.Transport{
TLSClientConfig: r.setting.TlsClientConfig,
TLSClientConfig: r.setting.TLSClientConfig,
Proxy: r.setting.Proxy,
Dial: TimeoutDialer(r.setting.ConnectTimeout, r.setting.ReadWriteTimeout),
}
} else {
// if r.transport is *http.Transport then set the settings.
if t, ok := trans.(*http.Transport); ok {
if t.TLSClientConfig == nil {
t.TLSClientConfig = r.setting.TlsClientConfig
t.TLSClientConfig = r.setting.TLSClientConfig
}
if t.Proxy == nil {
t.Proxy = r.setting.Proxy
Expand Down Expand Up @@ -409,9 +412,9 @@ func (r *Request) ToFile(filename string) error {
return err
}

// ToJson returns the map that marshals from the body bytes as json in response .
// ToJSON returns the map that marshals from the body bytes as json in response .
// it calls Response inner.
func (r *Request) ToJson(v interface{}) error {
func (r *Request) ToJSON(v interface{}) error {
data, err := r.Bytes()
if err != nil {
return err
Expand All @@ -420,9 +423,9 @@ func (r *Request) ToJson(v interface{}) error {
return err
}

// ToXml returns the map that marshals from the body bytes as xml in response .
// ToXML returns the map that marshals from the body bytes as xml in response .
// it calls Response inner.
func (r *Request) ToXml(v interface{}) error {
func (r *Request) ToXML(v interface{}) error {
data, err := r.Bytes()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/misc/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// Markdown render markdown document to HTML
// see https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-an-arbitrary-markdown-document
func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
if ctx.HasApiError() {
if ctx.HasAPIError() {
ctx.Error(422, "", ctx.GetErrMsg())
return
}
Expand Down