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

Raw file API: deprecate {ref} path parameter + allow shortened commit refs #17185

Closed
wants to merge 6 commits into from

Conversation

noerw
Copy link
Member

@noerw noerw commented Sep 29, 2021

Looking into #17179 I noticed a couple oddities with the raw file API:

All this culminates in the weird failure mode of #17179.

This PR aims to fix #17179, but also to find a way out of this mess:

  • adds support for short commit SHAs on the old {ref} path parameter, to fix above bug
  • documents the old path parameter {ref}
  • deprecates the old path parameter

Backporting the actual bugfix is simpler, as mentioned in df834a7

the ?ref= param was added recently in go-gitea#14602, and can fully replace the
(previously undocumented) ref-prefix of the filepath
we just deprecated this API, but this is a short fix to go-gitea#17179
A more minimal fix would be to check
  if len(parts) > 1
in line context/repo.go#L698
@noerw noerw requested a review from lafriks September 29, 2021 21:22
@noerw noerw added backport/v1.15 modifies/api This PR adds API routes or modifies them type/bug type/docs This PR mainly updates/creates documentation labels Sep 29, 2021
@@ -413,7 +413,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
return
}
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
} else if len(refName) == 40 {
} else if len(refName) >= 7 && len(refName) <= 40 {
Copy link
Member

Choose a reason for hiding this comment

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

I doubt that that will improve the situation.
"Great. Now I cannot even get the README.md".
Maybe it would be better to check whether that file is tracked when the if three lines below is entered before returning the error.

Or do I misunderstand what this is supposed to do?
Regarding the else block below, I am fairly certain that I am misunderstanding something here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think hard-coding the length is a good idea. Maybe git will use SHA256 in future.

https://stackoverflow.com/questions/28159071/why-doesnt-git-use-more-modern-sha

Copy link
Member Author

@noerw noerw Sep 30, 2021

Choose a reason for hiding this comment

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

@delvh no this won't happen, as we check if such a commit exists. If not, getRefName() falls back to treating the entire thing as a TreePath and returns Repo.DefaultBranch as ref, in which case we don't enter this if-clause at all.
@wxiaoguang this is all over the codebase, definitily a different issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can introduce a function like IsPossibleGitCommit and use it from now on. It's easier to understand and maintain.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 29, 2021
@lunny
Copy link
Member

lunny commented Sep 30, 2021

Maybe we should have a break change.{repo_api_url}/raw/{ref}/{tree_path} is the only router and drop {repo_api_url}/raw/{tree_path}.

@lafriks
Copy link
Member

lafriks commented Sep 30, 2021

Answering to your question shortened commit sha was added because of support needed for pkg.go.dev

As for API I did not see need for it as for API you will usually work with full commit sha

As for legacy urls we should drop support for them imho, it's been quite some time already

@noerw
Copy link
Member Author

noerw commented Sep 30, 2021

Maybe we should have a break change. {repo_api_url}/raw/{ref}/{tree_path} is the only router and drop {repo_api_url}/raw/{tree_path}.

@lunny Aesthetically I too prefer the {ref} path parameter, but

  1. this was never documented, so removing (or better deprecating, I'd bet there are users of this) this one and keeping the ?ref= variant is not a breaking change
  2. this is a pain to parse, specifying ref in a separate query param simplifies the code a lot
  3. (using ?ref= is also closer to GH API, though they have changed their raw file handling a couple times over, so compatibility isn't given anyway)

@noerw
Copy link
Member Author

noerw commented Sep 30, 2021

regarding the CI fail in check:

310See errors below:
311- "paths./repos/{owner}/{repo}/raw/{ref}/{filepath}.get.parameters" must validate one and only one schema (oneOf). Found none valid
313- in operation "repoGetRawFileOld",path param "ref" must be declared as required

well.. the ref param isn't required. is there a workaround wrt the linter? (It dawns upon me that this is the reason this param was never documented..)

noerw added a commit to noerw/gitea that referenced this pull request Oct 8, 2021
...when path contains no hash-path-separator ('/')

This is a workaround to go-gitea#17179.

Entering this case when `path` does not contain a '/' does not really
make sense, as that means the tree path is empty, but this case is only
entered for routes that expect a non-empty tree path.

Treepaths like <40-char-dirname>/<filename> will still fail,
but hopefully don't occur that often. A more complete fix that avoids
this case too is outlined in go-gitea#17185, but too big of a change to backport
techknowlogick pushed a commit that referenced this pull request Oct 8, 2021
...when path contains no hash-path-separator ('/')

This is a workaround to #17179.

Entering this case when `path` does not contain a '/' does not really
make sense, as that means the tree path is empty, but this case is only
entered for routes that expect a non-empty tree path.

Treepaths like <40-char-dirname>/<filename> will still fail,
but hopefully don't occur that often. A more complete fix that avoids
this case too is outlined in #17185, but too big of a change to backport
@lunny lunny added the backport/done All backports for this PR have been created label Oct 30, 2021
@lunny lunny added this to the 1.16.0 milestone Oct 30, 2021
@lunny
Copy link
Member

lunny commented Nov 19, 2021

Please resolve the conflicts.

@techknowlogick techknowlogick modified the milestones: 1.16.0, 1.17.0 Nov 23, 2021
@wxiaoguang wxiaoguang removed backport/done All backports for this PR have been created backport/v1.15 labels Apr 3, 2022
@lunny lunny modified the milestones: 1.17.0, 1.18.0 Jun 3, 2022
@lunny lunny removed this from the 1.18.0 milestone Oct 17, 2022
@lunny lunny added this to the 1.19.0 milestone Oct 17, 2022
@lunny lunny removed this from the 1.19.0 milestone Feb 1, 2023
@wxiaoguang
Copy link
Contributor

Is this PR active?

@wxiaoguang wxiaoguang added the issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail label Apr 24, 2023
@wxiaoguang
Copy link
Contributor

It has been stale for a long time and I can't think of a way to handling it other than closing it. Feel free to reopen if there's any new progress and I could also help.

@wxiaoguang wxiaoguang closed this May 1, 2023
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Aug 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/needs-feedback For bugs, we need more details. For features, the feature must be described in more detail lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them type/bug type/docs This PR mainly updates/creates documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API /repos​/{owner}​/{repo}​/raw​/{filepath} fails if filepath is 40-char long
8 participants