Skip to content

Edit file workflow for creating a fork and proposing changes #34240

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

Merged
merged 16 commits into from
Jun 22, 2025

Conversation

brechtvl
Copy link
Contributor

@brechtvl brechtvl commented Apr 18, 2025

When viewing a file that the user can't edit because they can't write to
the branch, the new, upload, patch, edit and delete functionality is no
longer disabled.

If no user fork of the repository exists, there is now a page to create one.
It will automatically create a fork with a single branch matching the one
being viewed, and a unique repository name will be automatically picked.

When a fork exists, but it's archived, a mirror or the user can't write
code to it, there will instead be a message explaining the situation.

If the usable fork exists, a message will appear at the top of the edit page
explaining that the changes will be applied to a branch in the fork. The
base repository branch will be pushed to a new branch to the fork, and
then the edits will be applied on top.

The suggestion to fork happens when accessing /_edit/, so that for
example online documentation can have an "edit this page" link to
the base repository that does the right thing.

Also includes changes to properly report errors when trying to commit
to a new branch that is protected, and when trying to commit to an
existing branch when choosing the new branch option.

Resolves #9017, #20882


fork

not_directly_editable

archived

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Apr 18, 2025
@github-actions github-actions bot added modifies/translation modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files labels Apr 18, 2025
@lunny lunny added this to the 1.24.0 milestone Apr 18, 2025
@lunny
Copy link
Member

lunny commented Apr 18, 2025

1 A middle page may be necessary like GH did? Looks like GH will not display a fork form page and will fork at the backend.

image

2 The branches may be fixed as All branches to prevent to choose a wrong branch

3 Is that really necessary to allow user to fork to an organization when forking for the edit?

4 The edit button on README file of repository home page could be displayed.

5 Too many steps and it will make some beginner confusing.

@wxiaoguang wxiaoguang removed this from the 1.24.0 milestone Apr 21, 2025
@brechtvl brechtvl marked this pull request as draft April 22, 2025 01:15
@brechtvl brechtvl changed the title Redirect to fork or propose to create one when editing files Edit file workflow for creating a fork and proposing changes Apr 22, 2025
@brechtvl
Copy link
Contributor Author

I rewrote the whole thing, which should hopefully address all the comments.

I think the permissions checking is rather confusing now. Previously ctx.Repo.CanWriteToBranch, ctx.Repo.CanEnableEditor and ctx.Repo.MustBeEditable were easy to understand, but now that two repositories are involved it's no so simple anymore. Maybe I should add a ctx.Repo.UserFork into the context, so that these functions can use it? Suggestions welcome.

@brechtvl brechtvl marked this pull request as ready for review April 22, 2025 18:40
@github-actions github-actions bot added the modifies/api This PR adds API routes or modifies them label Apr 23, 2025
@lunny
Copy link
Member

lunny commented Apr 23, 2025

Fork repository was created after clicking the fork the repository button. But it redirects to 404 . And when I re edit the file,
image

@lunny lunny added this to the 1.24.0 milestone Apr 23, 2025
@wxiaoguang wxiaoguang removed this from the 1.24.0 milestone Apr 23, 2025
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Apr 23, 2025

The "fork" logic has too much legacy patched code, it seems that it is not good to introduce more patches, instead maybe it needs refactoring ....

And I think it needs tests to make sure the logic is overall right.

Do we have enough time to take it into 1.24? (TBH I guess no)

@brechtvl
Copy link
Contributor Author

Fork repository was created after clicking the fork the repository button. But it redirects to 404 .

Fixed now, with test.

The "fork" logic has too much legacy patched code, it seems that it is not good to introduce more patches, instead maybe it needs refactoring ....

Ok, I'm not familiar enough with the overall code to see what is legacy here. Some hints or help would be appreciated.

Do we have enough time to take it into 1.24? (TBH I guess no)

I'm not in a rush to put it in 1.24.

@lunny
Copy link
Member

lunny commented Apr 29, 2025

The edit buttons might be disabled for anonymous users. I’m not sure if they should be, but GitHub does it that way. Otherwise LGTM

@GiteaBot GiteaBot removed the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 1, 2025
@wxiaoguang

This comment was marked as resolved.

@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jun 21, 2025
@brechtvl
Copy link
Contributor Author

  • The Add File button is not visible on the base repo, only when you have permission to edit it directly.
  • I'm unsure if Cherry Pick is supposed to work. I didn't implement support for doing that in this work flow. This new code doesn't seem to prohibit it in the router, but also doesn't expose it in the UI.
  • The You can not edit this repository directly. The changes will be written to your fork text is now at the bottom of the page. Was than an intentional change? Personally I think it's useful information to see before you start editing. GitHub also shows it at the top though the overall UI is different.
  • When specifying e.g. main in the new branch name, it succeeds even though that branch already exists.
  • When I disable the "Code" unit on my fork, it lets me edit the file, but when clicking "Propose file changes" I get Network error SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.
  • My version added tests for some features that were not previously tested. It might be useful to salvage some of those even if it's just the non-fork cases.

@brechtvl
Copy link
Contributor Author

I don't really have anything to mention regarding the code. It's much simpler now and the hard work seems to have been done in the refactoring. Many thanks!

@wxiaoguang wxiaoguang marked this pull request as draft June 22, 2025 01:58
@wxiaoguang wxiaoguang removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jun 22, 2025
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jun 22, 2025

The Add File button is not visible on the base repo, only when you have permission to edit it directly.

Fixed and added a test

I'm unsure if Cherry Pick is supposed to work. I didn't implement support for doing that in this work flow. This new code doesn't seem to prohibit it in the router, but also doesn't expose it in the UI.

They share the same backend logic (the same as "Apply Patch"), so I think it's fine to leave it as-is.

We can expose it in the future if anyone really needs it.

The You can not edit this repository directly. The changes will be written to your fork text is now at the bottom of the page. Was than an intentional change? Personally I think it's useful information to see before you start editing. GitHub also shows it at the top though the overall UI is different.

Moved to the top (see the screenshot)

When specifying e.g. main in the new branch name, it succeeds even though that branch already exists.

Fixed and added some tests.

When I disable the "Code" unit on my fork, it lets me edit the file, but when clicking "Propose file changes" I get Network error SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

Improved the frontend error handling (see the screenshot)

My version added tests for some features that were not previously tested. It might be useful to salvage some of those even if it's just the non-fork cases.

I will try to pick some tests later. And could you help to list the tests you think are important?


Screenshots (slightly outdated, the new commits moved the prompt to before the editor header line)

image

image

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jun 22, 2025

My version added tests for some features that were not previously tested. It might be useful to salvage some of those even if it's just the non-fork cases.

I will try to pick some tests later. And could you help to list the tests you think is important?

Made some refactorings to editor_test.go and picked most of your new tests. Are there somethings more we need to take a look at?

@wxiaoguang wxiaoguang marked this pull request as ready for review June 22, 2025 05:11
@brechtvl
Copy link
Contributor Author

Looks great now, no more comments from my side.

@wxiaoguang
Copy link
Contributor

Let's merge and try (and fix regressions 😁 )

@wxiaoguang wxiaoguang enabled auto-merge (squash) June 22, 2025 12:22
@wxiaoguang wxiaoguang added the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Jun 22, 2025
@wxiaoguang wxiaoguang merged commit a46b16f into go-gitea:main Jun 22, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/frontend modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files modifies/translation type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating fork and submit PR to origin automatically when editing file from UI with read-only permission
5 participants