Skip to content

Commit ce4609b

Browse files
committed
Auto adjust timeout & add logging
1 parent b2dbf87 commit ce4609b

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

cmd/hook.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ Gitea or set your environment appropriately.`, "")
196196
newCommitIDs := make([]string, hookBatchSize)
197197
refFullNames := make([]string, hookBatchSize)
198198
count := 0
199+
total := 0
200+
results := make([]private.HookPostReceiveResult, 0)
199201

200202
scanner := bufio.NewScanner(os.Stdin)
201203
for scanner.Scan() {
@@ -209,34 +211,26 @@ Gitea or set your environment appropriately.`, "")
209211
continue
210212
}
211213

214+
fmt.Fprintf(os.Stderr, ".")
212215
oldCommitIDs[count] = string(fields[0])
213216
newCommitIDs[count] = string(fields[1])
214217
refFullNames[count] = string(fields[2])
215218
count++
219+
total++
220+
os.Stderr.Sync()
216221

217222
if count >= hookBatchSize {
223+
fmt.Fprintf(os.Stderr, " Processing %d references\n", count)
224+
os.Stderr.Sync()
218225
hookOptions.OldCommitIDs = oldCommitIDs
219226
hookOptions.NewCommitIDs = newCommitIDs
220227
hookOptions.RefFullNames = refFullNames
221228
resps, err := private.HookPostReceive(repoUser, repoName, hookOptions)
222229
if resps == nil {
230+
hookPrintResults(results)
223231
fail("Internal Server Error", err)
224232
}
225-
for _, res := range resps {
226-
if !res.Message {
227-
continue
228-
}
229-
230-
fmt.Fprintln(os.Stderr, "")
231-
if res.Create {
232-
fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", res.Branch)
233-
fmt.Fprintf(os.Stderr, " %s\n", res.URL)
234-
} else {
235-
fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
236-
fmt.Fprintf(os.Stderr, " %s\n", res.URL)
237-
}
238-
fmt.Fprintln(os.Stderr, "")
239-
}
233+
results = append(results, resps...)
240234
count = 0
241235
}
242236
}
@@ -248,11 +242,25 @@ Gitea or set your environment appropriately.`, "")
248242
hookOptions.OldCommitIDs = oldCommitIDs[:count]
249243
hookOptions.NewCommitIDs = newCommitIDs[:count]
250244
hookOptions.RefFullNames = refFullNames[:count]
245+
246+
fmt.Fprintf(os.Stderr, " Processing %d references\n", count)
247+
os.Stderr.Sync()
248+
fmt.Fprintf(os.Stderr, "Processed %d references in total\n", total)
249+
os.Stderr.Sync()
250+
251251
resps, err := private.HookPostReceive(repoUser, repoName, hookOptions)
252252
if resps == nil {
253+
hookPrintResults(results)
253254
fail("Internal Server Error", err)
254255
}
255-
for _, res := range resps {
256+
results = append(results, resps...)
257+
hookPrintResults(results)
258+
259+
return nil
260+
}
261+
262+
func hookPrintResults(results []private.HookPostReceiveResult) {
263+
for _, res := range results {
256264
if !res.Message {
257265
continue
258266
}
@@ -266,7 +274,6 @@ Gitea or set your environment appropriately.`, "")
266274
fmt.Fprintf(os.Stderr, " %s\n", res.URL)
267275
}
268276
fmt.Fprintln(os.Stderr, "")
277+
os.Stderr.Sync()
269278
}
270-
271-
return nil
272279
}

modules/private/hook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/http"
1111
"net/url"
12+
"time"
1213

1314
"code.gitea.io/gitea/modules/setting"
1415
)
@@ -53,6 +54,7 @@ func HookPreReceive(ownerName, repoName string, opts HookOptions) (int, string)
5354
req = req.Header("Content-Type", "application/json")
5455
jsonBytes, _ := json.Marshal(opts)
5556
req.Body(jsonBytes)
57+
req.SetTimeout(60*time.Second, time.Duration(60+len(opts.OldCommitIDs))*time.Second)
5658
resp, err := req.Response()
5759
if err != nil {
5860
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
@@ -75,6 +77,7 @@ func HookPostReceive(ownerName, repoName string, opts HookOptions) ([]HookPostRe
7577

7678
req := newInternalRequest(reqURL, "POST")
7779
req = req.Header("Content-Type", "application/json")
80+
req.SetTimeout(60*time.Second, time.Duration(60+len(opts.OldCommitIDs))*time.Second)
7881
jsonBytes, _ := json.Marshal(opts)
7982
req.Body(jsonBytes)
8083
resp, err := req.Response()

0 commit comments

Comments
 (0)