loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Avoid losing token when updating mirror settings (#30429)

Fix #30416.

Before (it shows as "Unset" while there's a token):

<img width="980" alt="image"
src="https://github.com/go-gitea/gitea/assets/9418365/d7148e3e-62c9-4d2e-942d-3d795b79515a">

After:

<img width="977" alt="image"
src="https://github.com/go-gitea/gitea/assets/9418365/24aaa1db-5baa-4204-9081-470b15ea72b5">

The username shows as "oauth2" because of
https://github.com/go-gitea/gitea/blob/f9fdac9809335729b2ac3227b2a5f71a62fc64ad/services/migrations/dump.go#L99

I have checked that all usage of `MirrorRemoteAddress` has been updated.

<img width="1806" alt="image"
src="https://github.com/go-gitea/gitea/assets/9418365/2f042501-2824-4511-9203-c84a6731a02d">

However, it needs to be checked again when backporting.

---------

Co-authored-by: Giteabot <teabot@gitea.io>
(cherry picked from commit fd59cd9450cbd511ad4a0790bf51f8d5d2c18aa3)

authored by

Jason Song
Giteabot
and committed by
Gergely Nagy
982e9ce5 27977851

+32 -20
+21 -17
modules/templates/util_misc.go
··· 142 142 Password string 143 143 } 144 144 145 - func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteName string, ignoreOriginalURL bool) remoteAddress { 146 - a := remoteAddress{} 147 - 148 - remoteURL := m.OriginalURL 149 - if ignoreOriginalURL || remoteURL == "" { 150 - var err error 151 - remoteURL, err = git.GetRemoteAddress(ctx, m.RepoPath(), remoteName) 152 - if err != nil { 153 - log.Error("GetRemoteURL %v", err) 154 - return a 155 - } 145 + func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteName string) remoteAddress { 146 + ret := remoteAddress{} 147 + remoteURL, err := git.GetRemoteAddress(ctx, m.RepoPath(), remoteName) 148 + if err != nil { 149 + log.Error("GetRemoteURL %v", err) 150 + return ret 156 151 } 157 152 158 153 u, err := giturl.Parse(remoteURL) 159 154 if err != nil { 160 155 log.Error("giturl.Parse %v", err) 161 - return a 156 + return ret 162 157 } 163 158 164 159 if u.Scheme != "ssh" && u.Scheme != "file" { 165 160 if u.User != nil { 166 - a.Username = u.User.Username() 167 - a.Password, _ = u.User.Password() 161 + ret.Username = u.User.Username() 162 + ret.Password, _ = u.User.Password() 168 163 } 169 - u.User = nil 170 164 } 171 - a.Address = u.String() 165 + 166 + // The URL stored in the git repo could contain authentication, 167 + // erase it, or it will be shown in the UI. 168 + u.User = nil 169 + ret.Address = u.String() 170 + // Why not use m.OriginalURL to set ret.Address? 171 + // It should be OK to use it, since m.OriginalURL should be the same as the authentication-erased URL from the Git repository. 172 + // However, the old code has already stored authentication in m.OriginalURL when updating mirror settings. 173 + // That means we need to use "giturl.Parse" for m.OriginalURL again to ensure authentication is erased. 174 + // Instead of doing this, why not directly use the authentication-erased URL from the Git repository? 175 + // It should be the same as long as there are no bugs. 172 176 173 - return a 177 + return ret 174 178 } 175 179 176 180 func FilenameIsImage(filename string) bool {
+10 -2
services/mirror/mirror_pull.go
··· 13 13 system_model "code.gitea.io/gitea/models/system" 14 14 "code.gitea.io/gitea/modules/cache" 15 15 "code.gitea.io/gitea/modules/git" 16 + giturl "code.gitea.io/gitea/modules/git/url" 16 17 "code.gitea.io/gitea/modules/gitrepo" 17 18 "code.gitea.io/gitea/modules/lfs" 18 19 "code.gitea.io/gitea/modules/log" ··· 30 31 31 32 // UpdateAddress writes new address to Git repository and database 32 33 func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error { 34 + u, err := giturl.Parse(addr) 35 + if err != nil { 36 + return fmt.Errorf("invalid addr: %v", err) 37 + } 38 + 33 39 remoteName := m.GetRemoteName() 34 40 repoPath := m.GetRepository(ctx).RepoPath() 35 41 // Remove old remote 36 - _, _, err := git.NewCommand(ctx, "remote", "rm").AddDynamicArguments(remoteName).RunStdString(&git.RunOpts{Dir: repoPath}) 42 + _, _, err = git.NewCommand(ctx, "remote", "rm").AddDynamicArguments(remoteName).RunStdString(&git.RunOpts{Dir: repoPath}) 37 43 if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") { 38 44 return err 39 45 } ··· 70 76 } 71 77 } 72 78 73 - m.Repo.OriginalURL = addr 79 + // erase authentication before storing in database 80 + u.User = nil 81 + m.Repo.OriginalURL = u.String() 74 82 return repo_model.UpdateRepositoryCols(ctx, m.Repo, "original_url") 75 83 } 76 84
+1 -1
templates/repo/settings/options.tmpl
··· 156 156 <label for="interval">{{ctx.Locale.Tr "repo.mirror_interval" .MinimumMirrorInterval}}</label> 157 157 <input id="interval" name="interval" value="{{.PullMirror.Interval}}"> 158 158 </div> 159 - {{$address := MirrorRemoteAddress $.Context .Repository .PullMirror.GetRemoteName false}} 159 + {{$address := MirrorRemoteAddress $.Context .Repository .PullMirror.GetRemoteName}} 160 160 <div class="field {{if .Err_MirrorAddress}}error{{end}}"> 161 161 <label for="mirror_address">{{ctx.Locale.Tr "repo.mirror_address"}}</label> 162 162 <input id="mirror_address" name="mirror_address" value="{{$address.Address}}" required>