Skip to content

Commit

Permalink
repo: allow private repository to have public wiki or issues
Browse files Browse the repository at this point in the history
Relates to #649 and #2157
  • Loading branch information
unknwon committed Mar 24, 2017
1 parent 7a99e56 commit 8196430
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 100 deletions.
34 changes: 23 additions & 11 deletions cmd/web.go
Expand Up @@ -493,18 +493,15 @@ func runWeb(ctx *cli.Context) error {

m.Get("/:username/:reponame/action/:action", reqSignIn, context.RepoAssignment(), repo.Action)
m.Group("/:username/:reponame", func() {
m.Get("/issues", repo.RetrieveLabels, repo.Issues)
m.Get("/issues/:index", repo.ViewIssue)

// FIXME: should use different URLs but mostly same logic for comments of issue and pull reuqest.
// So they can apply their own enable/disable logic on routers.
m.Group("/issues", func() {
m.Combo("/new", repo.MustEnableIssues).Get(context.RepoRef(), repo.NewIssue).
Post(bindIgnErr(form.NewIssue{}), repo.NewIssuePost)

m.Group("/:index", func() {
m.Post("/label", repo.UpdateIssueLabel)
m.Post("/milestone", repo.UpdateIssueMilestone)
m.Post("/assignee", repo.UpdateIssueAssignee)
}, reqRepoWriter)

m.Group("/:index", func() {
m.Post("/title", repo.UpdateIssueTitle)
m.Post("/content", repo.UpdateIssueContent)
Expand All @@ -515,6 +512,24 @@ func runWeb(ctx *cli.Context) error {
m.Post("", repo.UpdateCommentContent)
m.Post("/delete", repo.DeleteComment)
})
}, ignSignIn, context.RepoAssignment(true))
m.Group("/:username/:reponame", func() {
m.Group("/wiki", func() {
m.Get("/?:page", repo.Wiki)
m.Get("/_pages", repo.WikiPages)
}, repo.MustEnableWiki, context.RepoRef())
}, ignSignIn, context.RepoAssignment(false, true))

m.Group("/:username/:reponame", func() {
// FIXME: should use different URLs but mostly same logic for comments of issue and pull reuqest.
// So they can apply their own enable/disable logic on routers.
m.Group("/issues", func() {
m.Group("/:index", func() {
m.Post("/label", repo.UpdateIssueLabel)
m.Post("/milestone", repo.UpdateIssueMilestone)
m.Post("/assignee", repo.UpdateIssueAssignee)
}, reqRepoWriter)
})
m.Group("/labels", func() {
m.Post("/new", bindIgnErr(form.CreateLabel{}), repo.NewLabel)
m.Post("/edit", bindIgnErr(form.CreateLabel{}), repo.UpdateLabel)
Expand Down Expand Up @@ -580,8 +595,8 @@ func runWeb(ctx *cli.Context) error {
m.Group("/:username/:reponame", func() {
m.Group("", func() {
m.Get("/releases", repo.MustBeNotBare, repo.Releases)
m.Get("/^:type(issues|pulls)$", repo.RetrieveLabels, repo.Issues)
m.Get("/^:type(issues|pulls)$/:index", repo.ViewIssue)
m.Get("/pulls", repo.RetrieveLabels, repo.Pulls)
m.Get("/pulls/:index", repo.ViewPull)
m.Get("/labels/", repo.RetrieveLabels, repo.Labels)
m.Get("/milestones", repo.Milestones)
}, context.RepoRef())
Expand All @@ -595,9 +610,6 @@ func runWeb(ctx *cli.Context) error {
})

m.Group("/wiki", func() {
m.Get("/?:page", repo.Wiki)
m.Get("/_pages", repo.WikiPages)

m.Group("", func() {
m.Combo("/_new").Get(repo.NewWiki).
Post(bindIgnErr(form.NewWiki{}), repo.NewWikiPost)
Expand Down
2 changes: 2 additions & 0 deletions conf/locale/locale_en-US.ini
Expand Up @@ -690,11 +690,13 @@ settings.change_reponame_prompt = This change will affect how links relate to th
settings.advanced_settings = Advanced Settings
settings.wiki_desc = Enable wiki system
settings.use_internal_wiki = Use builtin wiki
settings.allow_public_wiki_desc = Allow public access to wiki when repository is private
settings.use_external_wiki = Use external wiki
settings.external_wiki_url = External Wiki URL
settings.external_wiki_url_desc = Visitors will be redirected to URL when they click on the tab.
settings.issues_desc = Enable issue tracker
settings.use_internal_issue_tracker = Use builtin lightweight issue tracker
settings.allow_public_issues_desc = Allow public access to issues when repository is private
settings.use_external_issue_tracker = Use external issue tracker
settings.external_tracker_url = External Issue Tracker URL
settings.external_tracker_url_desc = Visitors will be redirected to URL when they click on the tab.
Expand Down
2 changes: 1 addition & 1 deletion gogs.go
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting"
)

const APP_VER = "0.10.29.0324"
const APP_VER = "0.10.30.0324"

func init() {
setting.AppVer = APP_VER
Expand Down
21 changes: 17 additions & 4 deletions models/repo.go
Expand Up @@ -173,9 +173,11 @@ type Repository struct {

// Advanced settings
EnableWiki bool `xorm:"NOT NULL DEFAULT true"`
AllowPublicWiki bool
EnableExternalWiki bool
ExternalWikiURL string
EnableIssues bool `xorm:"NOT NULL DEFAULT true"`
AllowPublicIssues bool
EnableExternalTracker bool
ExternalTrackerURL string
ExternalTrackerFormat string
Expand Down Expand Up @@ -253,10 +255,21 @@ func (repo *Repository) LoadAttributes() error {
return repo.loadAttributes(x)
}

// MustOwner always returns a valid *User object to avoid
// conceptually impossible error handling.
// It creates a fake object that contains error deftail
// when error occurs.
// IsPartialPublic returns true if repository is public or allow public access to wiki or issues.
func (repo *Repository) IsPartialPublic() bool {
return !repo.IsPrivate || repo.AllowPublicWiki || repo.AllowPublicIssues
}

func (repo *Repository) CanGuestViewWiki() bool {
return repo.IsPartialPublic() && repo.EnableWiki && !repo.EnableExternalWiki && repo.AllowPublicWiki
}

func (repo *Repository) CanGuestViewIssues() bool {
return repo.IsPartialPublic() && repo.EnableIssues && !repo.EnableExternalTracker && repo.AllowPublicIssues
}

// MustOwner always returns a valid *User object to avoid conceptually impossible error handling.
// It creates a fake object that contains error deftail when error occurs.
func (repo *Repository) MustOwner() *User {
return repo.mustOwner(x)
}
Expand Down
4 changes: 2 additions & 2 deletions modules/bindata/bindata.go

Large diffs are not rendered by default.

74 changes: 52 additions & 22 deletions modules/context/repo.go
Expand Up @@ -126,13 +126,24 @@ func earlyResponseForGoGetMeta(ctx *Context) {
})))
}

func RepoAssignment() macaron.Handler {
// [0]: issues, [1]: wiki
func RepoAssignment(pages ...bool) macaron.Handler {
return func(ctx *Context) {
var (
owner *models.User
err error
owner *models.User
err error
isIssuesPage bool
isWikiPage bool
)

if len(pages) > 0 {
isIssuesPage = pages[0]
}
if len(pages) > 1 {
isWikiPage = pages[1]
}
_, _ = isIssuesPage, isWikiPage

ownerName := ctx.Params(":username")
repoName := strings.TrimSuffix(ctx.Params(":reponame"), ".git")
refName := ctx.Params(":branchname")
Expand Down Expand Up @@ -174,37 +185,61 @@ func RepoAssignment() macaron.Handler {
ctx.Handle(500, "GetRepositoryByName", err)
}
return
} else if err = repo.GetOwner(); err != nil {
ctx.Handle(500, "GetOwner", err)
return
}

ctx.Repo.Repository = repo
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
ctx.Repo.RepoLink = repo.Link()
ctx.Data["RepoLink"] = ctx.Repo.RepoLink
ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name

// Admin has super access.
if ctx.IsSigned && ctx.User.IsAdmin {
ctx.Repo.AccessMode = models.ACCESS_MODE_OWNER
} else {
var userID int64
if ctx.IsSigned {
userID = ctx.User.ID
}
mode, err := models.AccessLevel(userID, repo)
mode, err := models.AccessLevel(ctx.UserID(), repo)
if err != nil {
ctx.Handle(500, "AccessLevel", err)
return
}
ctx.Repo.AccessMode = mode
}

// Check access.
// Check access
if ctx.Repo.AccessMode == models.ACCESS_MODE_NONE {
if ctx.Query("go-get") == "1" {
earlyResponseForGoGetMeta(ctx)
return
}
ctx.NotFound()
return

// Redirect to any accessible page if not yet on it
if repo.IsPartialPublic() &&
(!(isIssuesPage || isWikiPage) ||
(isIssuesPage && !repo.CanGuestViewIssues()) ||
(isWikiPage && !repo.CanGuestViewWiki())) {
switch {
case repo.CanGuestViewIssues():
ctx.Redirect(repo.Link() + "/issues")
case repo.CanGuestViewWiki():
ctx.Redirect(repo.Link() + "/wiki")
default:
ctx.NotFound()
}
return
}

// Response 404 if user is on completely private repository or possible accessible page but owner doesn't enabled
if !repo.IsPartialPublic() ||
(isIssuesPage && !repo.CanGuestViewIssues()) ||
(isWikiPage && !repo.CanGuestViewWiki()) {
ctx.NotFound()
return
}

ctx.Repo.Repository.EnableIssues = repo.CanGuestViewIssues()
ctx.Repo.Repository.EnableWiki = repo.CanGuestViewWiki()
}
ctx.Data["HasAccess"] = true

if repo.IsMirror {
ctx.Repo.Mirror, err = models.GetMirrorByRepoID(repo.ID)
Expand All @@ -217,19 +252,12 @@ func RepoAssignment() macaron.Handler {
ctx.Data["Mirror"] = ctx.Repo.Mirror
}

ctx.Repo.Repository = repo
ctx.Data["RepoName"] = ctx.Repo.Repository.Name
ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare

gitRepo, err := git.OpenRepository(models.RepoPath(ownerName, repoName))
if err != nil {
ctx.Handle(500, "RepoAssignment Invalid repo "+models.RepoPath(ownerName, repoName), err)
return
}
ctx.Repo.GitRepo = gitRepo
ctx.Repo.RepoLink = repo.Link()
ctx.Data["RepoLink"] = ctx.Repo.RepoLink
ctx.Data["RepoRelPath"] = ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name

tags, err := ctx.Repo.GitRepo.GetTags()
if err != nil {
Expand Down Expand Up @@ -288,6 +316,8 @@ func RepoAssignment() macaron.Handler {
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
}

ctx.Data["IsGuest"] = !ctx.Repo.HasAccess()
}
}

Expand Down
2 changes: 2 additions & 0 deletions modules/form/repo.go
Expand Up @@ -92,9 +92,11 @@ type RepoSetting struct {

// Advanced settings
EnableWiki bool
AllowPublicWiki bool
EnableExternalWiki bool
ExternalWikiURL string
EnableIssues bool
AllowPublicIssues bool
EnableExternalTracker bool
ExternalTrackerURL string
TrackerURLFormat string
Expand Down
3 changes: 3 additions & 0 deletions public/css/gogs.css
Expand Up @@ -2321,6 +2321,9 @@ footer .ui.language .menu {
.repository.wiki.view > .markdown h6:first-of-type {
margin-top: 0;
}
.repository.settings.options .box.field {
padding-left: 27px;
}
.repository.settings.collaboration .collaborator.list {
padding: 0;
}
Expand Down
9 changes: 3 additions & 6 deletions public/js/gogs.js
Expand Up @@ -265,11 +265,8 @@ function initRepository() {
}
});
$('.enable-system-radio').change(function () {
if (this.value == 'false') {
$($(this).data('target')).addClass('disabled');
} else if (this.value == 'true') {
$($(this).data('target')).removeClass('disabled');
}
$($(this).data('enable')).removeClass('disabled');
$($(this).data('disable')).addClass('disabled');
});
}

Expand Down Expand Up @@ -1289,7 +1286,7 @@ $(document).ready(function () {
$this.data('url', url);
$this.removeClass('disabled');
} else {
$this.remove();
$this.remove();
}
});
});
Expand Down
6 changes: 6 additions & 0 deletions public/less/_repository.less
Expand Up @@ -1325,6 +1325,12 @@
}

&.settings {
&.options {
.box.field {
padding-left: 27px;
}
}

&.collaboration {
.collaborator.list {
padding: 0;
Expand Down

0 comments on commit 8196430

Please sign in to comment.