Skip to content

Commit 321cbcb

Browse files
authored
Fix bug on downloading job logs (#34041)
Fix #34038
1 parent 894821d commit 321cbcb

File tree

2 files changed

+117
-70
lines changed

2 files changed

+117
-70
lines changed

routers/common/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func DownloadActionsRunJobLogsWithIndex(ctx *context.Base, ctxRepo *repo_model.R
2222
if err = runJobs.LoadRepos(ctx); err != nil {
2323
return fmt.Errorf("LoadRepos: %w", err)
2424
}
25-
if 0 < jobIndex || jobIndex >= int64(len(runJobs)) {
25+
if jobIndex < 0 || jobIndex >= int64(len(runJobs)) {
2626
return util.NewNotExistErrorf("job index is out of range: %d", jobIndex)
2727
}
2828
return DownloadActionsRunJobLogs(ctx, ctxRepo, runJobs[jobIndex])

tests/integration/actions_log_test.go

+116-69
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestDownloadTaskLogs(t *testing.T) {
3131
testCases := []struct {
3232
treePath string
3333
fileContent string
34-
outcome *mockTaskOutcome
34+
outcome []*mockTaskOutcome
3535
zstdEnabled bool
3636
}{
3737
{
@@ -46,21 +46,44 @@ jobs:
4646
runs-on: ubuntu-latest
4747
steps:
4848
- run: echo job1 with zstd enabled
49+
job2:
50+
runs-on: ubuntu-latest
51+
steps:
52+
- run: echo job2 with zstd enabled
4953
`,
50-
outcome: &mockTaskOutcome{
51-
result: runnerv1.Result_RESULT_SUCCESS,
52-
logRows: []*runnerv1.LogRow{
53-
{
54-
Time: timestamppb.New(now.Add(1 * time.Second)),
55-
Content: " \U0001F433 docker create image",
56-
},
57-
{
58-
Time: timestamppb.New(now.Add(2 * time.Second)),
59-
Content: "job1 zstd enabled",
54+
outcome: []*mockTaskOutcome{
55+
{
56+
result: runnerv1.Result_RESULT_SUCCESS,
57+
logRows: []*runnerv1.LogRow{
58+
{
59+
Time: timestamppb.New(now.Add(1 * time.Second)),
60+
Content: " \U0001F433 docker create image",
61+
},
62+
{
63+
Time: timestamppb.New(now.Add(2 * time.Second)),
64+
Content: "job1 zstd enabled",
65+
},
66+
{
67+
Time: timestamppb.New(now.Add(3 * time.Second)),
68+
Content: "\U0001F3C1 Job succeeded",
69+
},
6070
},
61-
{
62-
Time: timestamppb.New(now.Add(3 * time.Second)),
63-
Content: "\U0001F3C1 Job succeeded",
71+
},
72+
{
73+
result: runnerv1.Result_RESULT_SUCCESS,
74+
logRows: []*runnerv1.LogRow{
75+
{
76+
Time: timestamppb.New(now.Add(1 * time.Second)),
77+
Content: " \U0001F433 docker create image",
78+
},
79+
{
80+
Time: timestamppb.New(now.Add(2 * time.Second)),
81+
Content: "job2 zstd enabled",
82+
},
83+
{
84+
Time: timestamppb.New(now.Add(3 * time.Second)),
85+
Content: "\U0001F3C1 Job succeeded",
86+
},
6487
},
6588
},
6689
},
@@ -78,21 +101,44 @@ jobs:
78101
runs-on: ubuntu-latest
79102
steps:
80103
- run: echo job1 with zstd disabled
104+
job2:
105+
runs-on: ubuntu-latest
106+
steps:
107+
- run: echo job2 with zstd disabled
81108
`,
82-
outcome: &mockTaskOutcome{
83-
result: runnerv1.Result_RESULT_SUCCESS,
84-
logRows: []*runnerv1.LogRow{
85-
{
86-
Time: timestamppb.New(now.Add(4 * time.Second)),
87-
Content: " \U0001F433 docker create image",
109+
outcome: []*mockTaskOutcome{
110+
{
111+
result: runnerv1.Result_RESULT_SUCCESS,
112+
logRows: []*runnerv1.LogRow{
113+
{
114+
Time: timestamppb.New(now.Add(4 * time.Second)),
115+
Content: " \U0001F433 docker create image",
116+
},
117+
{
118+
Time: timestamppb.New(now.Add(5 * time.Second)),
119+
Content: "job1 zstd disabled",
120+
},
121+
{
122+
Time: timestamppb.New(now.Add(6 * time.Second)),
123+
Content: "\U0001F3C1 Job succeeded",
124+
},
88125
},
89-
{
90-
Time: timestamppb.New(now.Add(5 * time.Second)),
91-
Content: "job1 zstd disabled",
92-
},
93-
{
94-
Time: timestamppb.New(now.Add(6 * time.Second)),
95-
Content: "\U0001F3C1 Job succeeded",
126+
},
127+
{
128+
result: runnerv1.Result_RESULT_SUCCESS,
129+
logRows: []*runnerv1.LogRow{
130+
{
131+
Time: timestamppb.New(now.Add(4 * time.Second)),
132+
Content: " \U0001F433 docker create image",
133+
},
134+
{
135+
Time: timestamppb.New(now.Add(5 * time.Second)),
136+
Content: "job2 zstd disabled",
137+
},
138+
{
139+
Time: timestamppb.New(now.Add(6 * time.Second)),
140+
Content: "\U0001F3C1 Job succeeded",
141+
},
96142
},
97143
},
98144
},
@@ -124,54 +170,55 @@ jobs:
124170
opts := getWorkflowCreateFileOptions(user2, repo.DefaultBranch, fmt.Sprintf("create %s", tc.treePath), tc.fileContent)
125171
createWorkflowFile(t, token, user2.Name, repo.Name, tc.treePath, opts)
126172

127-
// fetch and execute task
128-
task := runner.fetchTask(t)
129-
runner.execTask(t, task, tc.outcome)
173+
// fetch and execute tasks
174+
for jobIndex, outcome := range tc.outcome {
175+
task := runner.fetchTask(t)
176+
runner.execTask(t, task, outcome)
130177

131-
// check whether the log file exists
132-
logFileName := fmt.Sprintf("%s/%02x/%d.log", repo.FullName(), task.Id%256, task.Id)
133-
if setting.Actions.LogCompression.IsZstd() {
134-
logFileName += ".zst"
135-
}
136-
_, err := storage.Actions.Stat(logFileName)
137-
assert.NoError(t, err)
178+
// check whether the log file exists
179+
logFileName := fmt.Sprintf("%s/%02x/%d.log", repo.FullName(), task.Id%256, task.Id)
180+
if setting.Actions.LogCompression.IsZstd() {
181+
logFileName += ".zst"
182+
}
183+
_, err := storage.Actions.Stat(logFileName)
184+
assert.NoError(t, err)
138185

139-
// download task logs and check content
140-
runIndex := task.Context.GetFields()["run_number"].GetStringValue()
141-
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/0/logs", user2.Name, repo.Name, runIndex)).
142-
AddTokenAuth(token)
143-
resp := MakeRequest(t, req, http.StatusOK)
144-
logTextLines := strings.Split(strings.TrimSpace(resp.Body.String()), "\n")
145-
assert.Len(t, logTextLines, len(tc.outcome.logRows))
146-
for idx, lr := range tc.outcome.logRows {
147-
assert.Equal(
148-
t,
149-
fmt.Sprintf("%s %s", lr.Time.AsTime().Format("2006-01-02T15:04:05.0000000Z07:00"), lr.Content),
150-
logTextLines[idx],
151-
)
152-
}
186+
// download task logs and check content
187+
runIndex := task.Context.GetFields()["run_number"].GetStringValue()
188+
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/actions/runs/%s/jobs/%d/logs", user2.Name, repo.Name, runIndex, jobIndex)).
189+
AddTokenAuth(token)
190+
resp := MakeRequest(t, req, http.StatusOK)
191+
logTextLines := strings.Split(strings.TrimSpace(resp.Body.String()), "\n")
192+
assert.Len(t, logTextLines, len(outcome.logRows))
193+
for idx, lr := range outcome.logRows {
194+
assert.Equal(
195+
t,
196+
fmt.Sprintf("%s %s", lr.Time.AsTime().Format("2006-01-02T15:04:05.0000000Z07:00"), lr.Content),
197+
logTextLines[idx],
198+
)
199+
}
153200

154-
runID, _ := strconv.ParseInt(task.Context.GetFields()["run_id"].GetStringValue(), 10, 64)
201+
runID, _ := strconv.ParseInt(task.Context.GetFields()["run_id"].GetStringValue(), 10, 64)
155202

156-
jobs, err := actions_model.GetRunJobsByRunID(t.Context(), runID)
157-
assert.NoError(t, err)
158-
assert.Len(t, jobs, 1)
159-
jobID := jobs[0].ID
203+
jobs, err := actions_model.GetRunJobsByRunID(t.Context(), runID)
204+
assert.NoError(t, err)
205+
assert.Len(t, jobs, len(tc.outcome))
206+
jobID := jobs[jobIndex].ID
160207

161-
// download task logs from API and check content
162-
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/actions/jobs/%d/logs", user2.Name, repo.Name, jobID)).
163-
AddTokenAuth(token)
164-
resp = MakeRequest(t, req, http.StatusOK)
165-
logTextLines = strings.Split(strings.TrimSpace(resp.Body.String()), "\n")
166-
assert.Len(t, logTextLines, len(tc.outcome.logRows))
167-
for idx, lr := range tc.outcome.logRows {
168-
assert.Equal(
169-
t,
170-
fmt.Sprintf("%s %s", lr.Time.AsTime().Format("2006-01-02T15:04:05.0000000Z07:00"), lr.Content),
171-
logTextLines[idx],
172-
)
208+
// download task logs from API and check content
209+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/actions/jobs/%d/logs", user2.Name, repo.Name, jobID)).
210+
AddTokenAuth(token)
211+
resp = MakeRequest(t, req, http.StatusOK)
212+
logTextLines = strings.Split(strings.TrimSpace(resp.Body.String()), "\n")
213+
assert.Len(t, logTextLines, len(outcome.logRows))
214+
for idx, lr := range outcome.logRows {
215+
assert.Equal(
216+
t,
217+
fmt.Sprintf("%s %s", lr.Time.AsTime().Format("2006-01-02T15:04:05.0000000Z07:00"), lr.Content),
218+
logTextLines[idx],
219+
)
220+
}
173221
}
174-
175222
resetFunc()
176223
})
177224
}

0 commit comments

Comments
 (0)