Skip to content

Commit 6cd6cb0

Browse files
committed
avoid nil access
1 parent 3951ba8 commit 6cd6cb0

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

services/actions/clear_tasks.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ func notifyWorkflowJobStatusUpdate(ctx context.Context, jobs []*actions_model.Ac
4343
for _, job := range jobs {
4444
if err := job.LoadAttributes(ctx); err != nil {
4545
log.Error("Failed to load job attributes: %v", err)
46+
continue
4647
}
4748
CreateCommitStatusForRunJobs(ctx, job.Run, job)
4849
notify_service.WorkflowJobStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job, nil)
4950
}
5051

51-
job := jobs[0]
52-
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
52+
if job := jobs[0]; job.Run != nil && job.Run.Repo != nil {
53+
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
54+
}
5355
}
5456

5557
func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error {
@@ -212,8 +214,8 @@ func CancelAbandonedJobs(ctx context.Context) error {
212214
log.Warn("cancel abandoned job %v: %v", job.ID, err)
213215
// go on
214216
}
215-
if job.Run == nil {
216-
continue // error occurs during loading attributes, the following code that depends on "Run" will fail, so ignore and skip
217+
if job.Run == nil || job.Run.Repo == nil {
218+
continue // error occurs during loading attributes, the following code that depends on "Run.Repo" will fail, so ignore and skip
217219
}
218220
CreateCommitStatusForRunJobs(ctx, job.Run, job)
219221
if updated {

services/actions/commit_status.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func CreateCommitStatusForRunJobs(ctx context.Context, run *actions_model.Action
3131
return
3232
}
3333

34-
event, commitID, err := getCommitStatusEventNameAndSHA(run)
34+
event, commitID, err := getCommitStatusEventNameAndCommitID(run)
3535
if err != nil {
3636
log.Error("GetCommitStatusEventNameAndSHA: %v", err)
3737
} else if event == "" || commitID == "" {
@@ -50,7 +50,7 @@ func CreateCommitStatusForRunJobs(ctx context.Context, run *actions_model.Action
5050
}
5151
}
5252

53-
func getCommitStatusEventNameAndSHA(run *actions_model.ActionRun) (event, sha string, _ error) {
53+
func getCommitStatusEventNameAndCommitID(run *actions_model.ActionRun) (event, commitID string, _ error) {
5454
switch run.Event {
5555
case webhook_module.HookEventPush:
5656
event = "push"
@@ -61,7 +61,7 @@ func getCommitStatusEventNameAndSHA(run *actions_model.ActionRun) (event, sha st
6161
if payload.HeadCommit == nil {
6262
return "", "", errors.New("head commit is missing in event payload")
6363
}
64-
sha = payload.HeadCommit.ID
64+
commitID = payload.HeadCommit.ID
6565
case // pull_request
6666
webhook_module.HookEventPullRequest,
6767
webhook_module.HookEventPullRequestSync,
@@ -83,14 +83,13 @@ func getCommitStatusEventNameAndSHA(run *actions_model.ActionRun) (event, sha st
8383
} else if payload.PullRequest.Head == nil {
8484
return "", "", errors.New("head of pull request is missing in event payload")
8585
}
86-
sha = payload.PullRequest.Head.Sha
86+
commitID = payload.PullRequest.Head.Sha
8787
case webhook_module.HookEventRelease:
8888
event = string(run.Event)
89-
sha = run.CommitSHA
90-
default:
91-
return "", "", nil
89+
commitID = run.CommitSHA
90+
default: // do nothing, return empty
9291
}
93-
return event, sha, nil
92+
return event, commitID, nil
9493
}
9594

9695
func createCommitStatus(ctx context.Context, repo *repo_model.Repository, event, commitID string, run *actions_model.ActionRun, job *actions_model.ActionRunJob) error {

0 commit comments

Comments
 (0)