Skip to content
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

Fix implementation of repo Home func #2601

Merged
merged 2 commits into from
Oct 1, 2017

Conversation

Morlinest
Copy link
Member

No description provided.

unit, ok := models.Units[tp]
if ok {
unit, ok := models.Units[ctx.Repo.Repository.Units[0].Type]
if ok && unit.URI != models.UnitCode.URI {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it will never be models.UnitCode.URI

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't, but to be sure we don't have dead loop.

@codecov-io
Copy link

codecov-io commented Sep 25, 2017

Codecov Report

Merging #2601 into master will decrease coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2601      +/-   ##
==========================================
- Coverage    27.3%   27.29%   -0.01%     
==========================================
  Files          86       86              
  Lines       17139    17144       +5     
==========================================
  Hits         4680     4680              
- Misses      11781    11786       +5     
  Partials      678      678
Impacted Files Coverage Δ
models/unit.go 0% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bae9cbc...bd98c34. Read the comment docs.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 25, 2017
@Morlinest
Copy link
Member Author

I see 1 problem here... If the first unit in slice is external issue/wiki, we will be redirected to it. This is actual problem my colleague found few days ago.

@daviian
Copy link
Member

daviian commented Sep 25, 2017

I don't see the point in changing this.
If we need sorting anyway Units[0] is always UnitCode, except the user has no permission to UnitCode.
So your for loop either runs 1 time (if UnitCode is existing) or runs through all units and never goes into the if statement.

@Morlinest
Copy link
Member Author

@daviian But you shouldn't care about sorting in this function... You can sort it as u wish, but I know only about having 1 slice of available units, nothing else.

@Morlinest
Copy link
Member Author

Fixed problem with redirecting to external issue/wiki if there is another option.

@daviian
Copy link
Member

daviian commented Sep 25, 2017

As I said: Doesn't ensure that leftmost tab is opened.

@Morlinest
Copy link
Member Author

@daviian You are right. This fix is not about that. Logic for that can be added in another PR.

@Morlinest
Copy link
Member Author

@daviian With your #2603 it should do the magic. Units will be sorted, double checked UniCode existence and also skipping unnecessary redirects to external tracker or wiki.

unit, ok := models.Units[tp]
if ok {
unit, ok := models.Units[firstAvailable]
if ok && unit.URI != models.UnitCode.URI {
Copy link
Member

@ethantkoenig ethantkoenig Sep 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the unit.URI != models.UnitCode.URI condition necessary? From what I can tell, there is no way firstAvailable can be models.UnitTypeCode at this point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid redirecting to the same page (causing an infinite loop). Safer method would be to check requested uri vs computed uri....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Morlinest but that can't happen as only unit with same url is code and if it will be allowed it will render and return and won't get to this point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lafriks It shouldn't, now, but... There can also be new unit with same uri like Issue and ExternalTracker or Wiki and ExternalWiki. You know what's in code, that function doesn't. I don't think making safe functions is wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I've just finished even more secure implementation. It can be used for each unit handler.

@lunny lunny added the type/bug label Sep 28, 2017
@lunny lunny added this to the 1.3.0 milestone Sep 28, 2017
@Morlinest
Copy link
Member Author

@ethantkoenig @lunny @ethantkoenig Removed uri check...

@ethantkoenig
Copy link
Member

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Sep 30, 2017
@Morlinest
Copy link
Member Author

Morlinest commented Sep 30, 2017

I've changed implementation to use Unit.idx to determine first available RepoUnit for redirect if UnitTypeCode is missing. With this change, #2603 will not be necessary and #2621 can be changed to remove RepoUnit.Index completly. If you don't like my use of struct, I have simpler version with map[string]*models.Unit, but for price of losing type safety (as using custom type and constants for key would be probably an overkill). What do you think?

/cc @ethantkoenig @lafriks @daviian

Edit: Add type safety to map version means adding 5 lines of code.

if tp == models.UnitTypeCode {
renderCode(ctx)
return
var firstUnit struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using double pointers and internal structs seems unnecessarily complicated. Can we please just do something like the following:

if ctx.Repo.Repository.UnitEnabled(models.UnitTypeCode) {
	renderCode(ctx)
	return
}
redirectUnitType = ctx.Repo.Repository.Units[0].Type
for _, unit := range ctx.Repo.Repository.Units {
	// LessThan is a helper method to add to unit type, we can 
	// implement it so that it favors non-external units
	if unit.Type.LessThan(redirectUnitType) {
		redirectUnitType = unit.Type
	}
}
redirectUnit := models.Units[redirectUnitType]
ctx.Redirect(fmt.Sprintf("%s/%s%s", setting.AppSubURL,
	ctx.Repo.Repository.FullName(),
	redirectUnit.URI))

Copy link
Member Author

@Morlinest Morlinest Sep 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,I'll use map version instead. The reason for not using only 1 variable is to avoid redirecting to external tracker or wiki if there is another (internal) tab available.

Edit: Read your solution again, it would be better. I've made it too complicated

@Morlinest Morlinest force-pushed the fix/render-repo-home branch 2 times, most recently from a3b7917 to 2dc9a49 Compare September 30, 2017 19:11
@Morlinest
Copy link
Member Author

@ethantkoenig @daviian Done

@daviian
Copy link
Member

daviian commented Oct 1, 2017

@Morlinest Please add my changed fixture file from #2603 for testing.

@Morlinest
Copy link
Member Author

@daviian Done

@lafriks
Copy link
Member

lafriks commented Oct 1, 2017

LGTM

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Oct 1, 2017
@lafriks lafriks merged commit 1ad902d into go-gitea:master Oct 1, 2017
@Morlinest Morlinest deleted the fix/render-repo-home branch October 1, 2017 20:05
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants