Skip to content

Commit 0fa3966

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Return `responseText` instead of string in some functions (go-gitea#28836) Fix display latest sync time for pull mirrors on the repo page (go-gitea#28841) Add testing for CalcCommitStatus (go-gitea#28823) Remove duplicated checkinit on git module (go-gitea#28824) Add missing migration (go-gitea#28827) Fix uploaded artifacts should be overwritten (go-gitea#28726)
2 parents e3ade4e + b60a7c3 commit 0fa3966

File tree

16 files changed

+247
-45
lines changed

16 files changed

+247
-45
lines changed

cmd/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ func runGenerateActionsRunnerToken(c *cli.Context) error {
5050
if extra.HasError() {
5151
return handleCliResponseExtra(extra)
5252
}
53-
_, _ = fmt.Printf("%s\n", respText)
53+
_, _ = fmt.Printf("%s\n", respText.Text)
5454
return nil
5555
}

cmd/keys.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ func runKeys(c *cli.Context) error {
7878
if extra.Error != nil {
7979
return extra.Error
8080
}
81-
_, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString))
81+
_, _ = fmt.Fprintln(c.App.Writer, strings.TrimSpace(authorizedString.Text))
8282
return nil
8383
}

cmd/mailer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ func runSendMail(c *cli.Context) error {
4545
if extra.HasError() {
4646
return handleCliResponseExtra(extra)
4747
}
48-
_, _ = fmt.Printf("Sent %s email(s) to all users\n", respText)
48+
_, _ = fmt.Printf("Sent %s email(s) to all users\n", respText.Text)
4949
return nil
5050
}

models/git/commit_status_test.go

+115
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,118 @@ func TestGetCommitStatuses(t *testing.T) {
6060
assert.Equal(t, int(maxResults), 5)
6161
assert.Empty(t, statuses)
6262
}
63+
64+
func Test_CalcCommitStatus(t *testing.T) {
65+
kases := []struct {
66+
statuses []*git_model.CommitStatus
67+
expected *git_model.CommitStatus
68+
}{
69+
{
70+
statuses: []*git_model.CommitStatus{
71+
{
72+
State: structs.CommitStatusPending,
73+
},
74+
},
75+
expected: &git_model.CommitStatus{
76+
State: structs.CommitStatusPending,
77+
},
78+
},
79+
{
80+
statuses: []*git_model.CommitStatus{
81+
{
82+
State: structs.CommitStatusSuccess,
83+
},
84+
{
85+
State: structs.CommitStatusPending,
86+
},
87+
},
88+
expected: &git_model.CommitStatus{
89+
State: structs.CommitStatusPending,
90+
},
91+
},
92+
{
93+
statuses: []*git_model.CommitStatus{
94+
{
95+
State: structs.CommitStatusSuccess,
96+
},
97+
{
98+
State: structs.CommitStatusPending,
99+
},
100+
{
101+
State: structs.CommitStatusSuccess,
102+
},
103+
},
104+
expected: &git_model.CommitStatus{
105+
State: structs.CommitStatusPending,
106+
},
107+
},
108+
{
109+
statuses: []*git_model.CommitStatus{
110+
{
111+
State: structs.CommitStatusError,
112+
},
113+
{
114+
State: structs.CommitStatusPending,
115+
},
116+
{
117+
State: structs.CommitStatusSuccess,
118+
},
119+
},
120+
expected: &git_model.CommitStatus{
121+
State: structs.CommitStatusError,
122+
},
123+
},
124+
{
125+
statuses: []*git_model.CommitStatus{
126+
{
127+
State: structs.CommitStatusWarning,
128+
},
129+
{
130+
State: structs.CommitStatusPending,
131+
},
132+
{
133+
State: structs.CommitStatusSuccess,
134+
},
135+
},
136+
expected: &git_model.CommitStatus{
137+
State: structs.CommitStatusWarning,
138+
},
139+
},
140+
{
141+
statuses: []*git_model.CommitStatus{
142+
{
143+
State: structs.CommitStatusSuccess,
144+
},
145+
{
146+
State: structs.CommitStatusSuccess,
147+
},
148+
{
149+
State: structs.CommitStatusSuccess,
150+
},
151+
},
152+
expected: &git_model.CommitStatus{
153+
State: structs.CommitStatusSuccess,
154+
},
155+
},
156+
{
157+
statuses: []*git_model.CommitStatus{
158+
{
159+
State: structs.CommitStatusFailure,
160+
},
161+
{
162+
State: structs.CommitStatusError,
163+
},
164+
{
165+
State: structs.CommitStatusWarning,
166+
},
167+
},
168+
expected: &git_model.CommitStatus{
169+
State: structs.CommitStatusError,
170+
},
171+
},
172+
}
173+
174+
for _, kase := range kases {
175+
assert.Equal(t, kase.expected, git_model.CalcCommitStatus(kase.statuses))
176+
}
177+
}

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ var migrations = []Migration{
552552
NewMigration("Add Index to pull_auto_merge.doer_id", v1_22.AddIndexToPullAutoMergeDoerID),
553553
// v283 -> v284
554554
NewMigration("Add combined Index to issue_user.uid and issue_id", v1_22.AddCombinedIndexToIssueUser),
555+
// v284 -> v285
556+
NewMigration("Add ignore stale approval column on branch table", v1_22.AddIgnoreStaleApprovalsColumnToProtectedBranchTable),
555557
}
556558

557559
// GetCurrentDBVersion returns the current db version

modules/git/git.go

-4
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ func InitSimple(ctx context.Context) error {
166166
// InitFull initializes git module with version check and change global variables, sync gitconfig.
167167
// It should only be called once at the beginning of the program initialization (TestMain/GlobalInitInstalled) as this code makes unsynchronized changes to variables.
168168
func InitFull(ctx context.Context) (err error) {
169-
if err = checkInit(); err != nil {
170-
return err
171-
}
172-
173169
if err = InitSimple(ctx); err != nil {
174170
return err
175171
}

modules/private/actions.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ type GenerateTokenRequest struct {
1414
}
1515

1616
// GenerateActionsRunnerToken calls the internal GenerateActionsRunnerToken function
17-
func GenerateActionsRunnerToken(ctx context.Context, scope string) (string, ResponseExtra) {
17+
func GenerateActionsRunnerToken(ctx context.Context, scope string) (*ResponseText, ResponseExtra) {
1818
reqURL := setting.LocalURL + "api/internal/actions/generate_actions_runner_token"
1919

2020
req := newInternalRequest(ctx, reqURL, "POST", GenerateTokenRequest{
2121
Scope: scope,
2222
})
2323

24-
resp, extra := requestJSONResp(req, &responseText{})
25-
if extra.HasError() {
26-
return "", extra
27-
}
28-
return resp.Text, extra
24+
return requestJSONResp(req, &ResponseText{})
2925
}

modules/private/hook.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func HookPreReceive(ctx context.Context, ownerName, repoName string, opts HookOp
101101
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", url.PathEscape(ownerName), url.PathEscape(repoName))
102102
req := newInternalRequest(ctx, reqURL, "POST", opts)
103103
req.SetReadWriteTimeout(time.Duration(60+len(opts.OldCommitIDs)) * time.Second)
104-
_, extra := requestJSONResp(req, &responseText{})
104+
_, extra := requestJSONResp(req, &ResponseText{})
105105
return extra
106106
}
107107

@@ -130,14 +130,14 @@ func SetDefaultBranch(ctx context.Context, ownerName, repoName, branch string) R
130130
url.PathEscape(branch),
131131
)
132132
req := newInternalRequest(ctx, reqURL, "POST")
133-
_, extra := requestJSONResp(req, &responseText{})
133+
_, extra := requestJSONResp(req, &ResponseText{})
134134
return extra
135135
}
136136

137137
// SSHLog sends ssh error log response
138138
func SSHLog(ctx context.Context, isErr bool, msg string) error {
139139
reqURL := setting.LocalURL + "api/internal/ssh/log"
140140
req := newInternalRequest(ctx, reqURL, "POST", &SSHLogOption{IsError: isErr, Message: msg})
141-
_, extra := requestJSONResp(req, &responseText{})
141+
_, extra := requestJSONResp(req, &ResponseText{})
142142
return extra.Error
143143
}

modules/private/key.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,16 @@ func UpdatePublicKeyInRepo(ctx context.Context, keyID, repoID int64) error {
1515
// Ask for running deliver hook and test pull request tasks.
1616
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/ssh/%d/update/%d", keyID, repoID)
1717
req := newInternalRequest(ctx, reqURL, "POST")
18-
_, extra := requestJSONResp(req, &responseText{})
18+
_, extra := requestJSONResp(req, &ResponseText{})
1919
return extra.Error
2020
}
2121

2222
// AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
2323
// and returns public key found.
24-
func AuthorizedPublicKeyByContent(ctx context.Context, content string) (string, ResponseExtra) {
24+
func AuthorizedPublicKeyByContent(ctx context.Context, content string) (*ResponseText, ResponseExtra) {
2525
// Ask for running deliver hook and test pull request tasks.
2626
reqURL := setting.LocalURL + "api/internal/ssh/authorized_keys"
2727
req := newInternalRequest(ctx, reqURL, "POST")
2828
req.Param("content", content)
29-
resp, extra := requestJSONResp(req, &responseText{})
30-
if extra.HasError() {
31-
return "", extra
32-
}
33-
return resp.Text, extra
29+
return requestJSONResp(req, &ResponseText{})
3430
}

modules/private/mail.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Email struct {
2020
// It accepts a list of usernames.
2121
// If DB contains these users it will send the email to them.
2222
// If to list == nil, it's supposed to send emails to every user present in DB
23-
func SendEmail(ctx context.Context, subject, message string, to []string) (string, ResponseExtra) {
23+
func SendEmail(ctx context.Context, subject, message string, to []string) (*ResponseText, ResponseExtra) {
2424
reqURL := setting.LocalURL + "api/internal/mail/send"
2525

2626
req := newInternalRequest(ctx, reqURL, "POST", Email{
@@ -29,9 +29,5 @@ func SendEmail(ctx context.Context, subject, message string, to []string) (strin
2929
To: to,
3030
})
3131

32-
resp, extra := requestJSONResp(req, &responseText{})
33-
if extra.HasError() {
34-
return "", extra
35-
}
36-
return resp.Text, extra
32+
return requestJSONResp(req, &ResponseText{})
3733
}

modules/private/request.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"code.gitea.io/gitea/modules/json"
1313
)
1414

15-
// responseText is used to get the response as text, instead of parsing it as JSON.
16-
type responseText struct {
15+
// ResponseText is used to get the response as text, instead of parsing it as JSON.
16+
type ResponseText struct {
1717
Text string
1818
}
1919

@@ -50,7 +50,7 @@ func (re responseError) Error() string {
5050
// Caller should check the ResponseExtra.HasError() first to see whether the request fails.
5151
//
5252
// * If the "res" is a struct pointer, the response will be parsed as JSON
53-
// * If the "res" is responseText pointer, the response will be stored as text in it
53+
// * If the "res" is ResponseText pointer, the response will be stored as text in it
5454
// * If the "res" is responseCallback pointer, the callback function should set the ResponseExtra fields accordingly
5555
func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra ResponseExtra) {
5656
resp, err := req.Response()
@@ -81,7 +81,7 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
8181

8282
// now, the StatusCode must be 2xx
8383
var v any = res
84-
if respText, ok := v.(*responseText); ok {
84+
if respText, ok := v.(*ResponseText); ok {
8585
// get the whole response as a text string
8686
bs, err := io.ReadAll(resp.Body)
8787
if err != nil {
@@ -119,7 +119,7 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
119119
// requestJSONClientMsg sends a request to the gitea server, server only responds text message status=200 with "success" body
120120
// If the request succeeds (200), the argument clientSuccessMsg will be used as ResponseExtra.UserMsg.
121121
func requestJSONClientMsg(req *httplib.Request, clientSuccessMsg string) ResponseExtra {
122-
_, extra := requestJSONResp(req, &responseText{})
122+
_, extra := requestJSONResp(req, &ResponseText{})
123123
if extra.HasError() {
124124
return extra
125125
}

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ mirror_prune = Prune
987987
mirror_prune_desc = Remove obsolete remote-tracking references
988988
mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable periodic sync. (Minimum interval: %s)
989989
mirror_interval_invalid = The mirror interval is not valid.
990+
mirror_sync = synced
990991
mirror_sync_on_commit = Sync when commits are pushed
991992
mirror_address = Clone From URL
992993
mirror_address_desc = Put any required credentials in the Authorization section.

routers/api/actions/artifacts.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,11 @@ func (ar artifactRoutes) uploadArtifact(ctx *ArtifactContext) {
257257
return
258258
}
259259

260-
// update artifact size if zero
261-
if artifact.FileSize == 0 || artifact.FileCompressedSize == 0 {
260+
// update artifact size if zero or not match, over write artifact size
261+
if artifact.FileSize == 0 ||
262+
artifact.FileCompressedSize == 0 ||
263+
artifact.FileSize != fileRealTotalSize ||
264+
artifact.FileCompressedSize != chunksTotalSize {
262265
artifact.FileSize = fileRealTotalSize
263266
artifact.FileCompressedSize = chunksTotalSize
264267
artifact.ContentEncoding = ctx.Req.Header.Get("Content-Encoding")
@@ -267,6 +270,8 @@ func (ar artifactRoutes) uploadArtifact(ctx *ArtifactContext) {
267270
ctx.Error(http.StatusInternalServerError, "Error update artifact")
268271
return
269272
}
273+
log.Debug("[artifact] update artifact size, artifact_id: %d, size: %d, compressed size: %d",
274+
artifact.ID, artifact.FileSize, artifact.FileCompressedSize)
270275
}
271276

272277
ctx.JSON(http.StatusOK, map[string]string{

routers/api/actions/artifacts_chunks.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,14 @@ func mergeChunksForArtifact(ctx *ArtifactContext, chunks []*chunkFileItem, st st
186186
}()
187187

188188
// save storage path to artifact
189-
log.Debug("[artifact] merge chunks to artifact: %d, %s", artifact.ID, storagePath)
189+
log.Debug("[artifact] merge chunks to artifact: %d, %s, old:%s", artifact.ID, storagePath, artifact.StoragePath)
190+
// if artifact is already uploaded, delete the old file
191+
if artifact.StoragePath != "" {
192+
if err := st.Delete(artifact.StoragePath); err != nil {
193+
log.Warn("Error deleting old artifact: %s, %v", artifact.StoragePath, err)
194+
}
195+
}
196+
190197
artifact.StoragePath = storagePath
191198
artifact.Status = int64(actions.ArtifactStatusUploadConfirmed)
192199
if err := actions.UpdateArtifactByID(ctx, artifact.ID, artifact); err != nil {

templates/repo/header.tmpl

+7-8
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@
2828
<div class="repo-icon" data-tooltip-content="{{ctx.Locale.Tr "repo.desc.template"}}">{{svg "octicon-repo-template" 18}}</div>
2929
{{end}}
3030
</div>
31-
{{if $.PullMirror}}
32-
<div class="fork-flag">
33-
{{ctx.Locale.Tr "repo.mirror_from"}}
34-
<a target="_blank" rel="noopener noreferrer" href="{{$.PullMirror.RemoteAddress}}">{{$.PullMirror.RemoteAddress}}</a>
35-
{{if $.PullMirror.UpdatedUnix}}{{ctx.Locale.Tr "repo.mirror_sync"}} {{TimeSinceUnix $.PullMirror.UpdatedUnix ctx.Locale}}{{end}}
36-
</div>
37-
{{end}}
3831
</div>
3932
{{if not (or .IsBeingCreated .IsBroken)}}
4033
<div class="repo-buttons">
@@ -147,7 +140,13 @@
147140
</div>
148141
{{end}}
149142
</div>
150-
{{if $.PullMirror}}<div class="fork-flag">{{ctx.Locale.Tr "repo.mirror_from"}} <a target="_blank" rel="noopener noreferrer" href="{{$.PullMirror.RemoteAddress}}">{{$.PullMirror.RemoteAddress}}</a></div>{{end}}
143+
{{if $.PullMirror}}
144+
<div class="fork-flag">
145+
{{ctx.Locale.Tr "repo.mirror_from"}}
146+
<a target="_blank" rel="noopener noreferrer" href="{{$.PullMirror.RemoteAddress}}">{{$.PullMirror.RemoteAddress}}</a>
147+
{{if $.PullMirror.UpdatedUnix}}{{ctx.Locale.Tr "repo.mirror_sync"}} {{TimeSinceUnix $.PullMirror.UpdatedUnix ctx.Locale}}{{end}}
148+
</div>
149+
{{end}}
151150
{{if .IsFork}}<div class="fork-flag">{{ctx.Locale.Tr "repo.forked_from"}} <a href="{{.BaseRepo.Link}}">{{.BaseRepo.FullName}}</a></div>{{end}}
152151
{{if .IsGenerated}}<div class="fork-flag">{{ctx.Locale.Tr "repo.generated_from"}} <a href="{{(.TemplateRepo ctx).Link}}">{{(.TemplateRepo ctx).FullName}}</a></div>{{end}}
153152
</div>

0 commit comments

Comments
 (0)