code.gitea.io/gitea@v1.19.3/modules/actions/task_state.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package actions
     5  
     6  import (
     7  	actions_model "code.gitea.io/gitea/models/actions"
     8  )
     9  
    10  const (
    11  	preStepName  = "Set up job"
    12  	postStepName = "Complete job"
    13  )
    14  
    15  // FullSteps returns steps with "Set up job" and "Complete job"
    16  func FullSteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
    17  	if len(task.Steps) == 0 {
    18  		return fullStepsOfEmptySteps(task)
    19  	}
    20  
    21  	firstStep := task.Steps[0]
    22  	var logIndex int64
    23  
    24  	preStep := &actions_model.ActionTaskStep{
    25  		Name:      preStepName,
    26  		LogLength: task.LogLength,
    27  		Started:   task.Started,
    28  		Status:    actions_model.StatusRunning,
    29  	}
    30  
    31  	if firstStep.Status.HasRun() || firstStep.Status.IsRunning() {
    32  		preStep.LogLength = firstStep.LogIndex
    33  		preStep.Stopped = firstStep.Started
    34  		preStep.Status = actions_model.StatusSuccess
    35  	} else if task.Status.IsDone() {
    36  		preStep.Stopped = task.Stopped
    37  		preStep.Status = actions_model.StatusFailure
    38  	}
    39  	logIndex += preStep.LogLength
    40  
    41  	var lastHasRunStep *actions_model.ActionTaskStep
    42  	for _, step := range task.Steps {
    43  		if step.Status.HasRun() {
    44  			lastHasRunStep = step
    45  		}
    46  		logIndex += step.LogLength
    47  	}
    48  	if lastHasRunStep == nil {
    49  		lastHasRunStep = preStep
    50  	}
    51  
    52  	postStep := &actions_model.ActionTaskStep{
    53  		Name:   postStepName,
    54  		Status: actions_model.StatusWaiting,
    55  	}
    56  	if task.Status.IsDone() {
    57  		postStep.LogIndex = logIndex
    58  		postStep.LogLength = task.LogLength - postStep.LogIndex
    59  		postStep.Status = task.Status
    60  		postStep.Started = lastHasRunStep.Stopped
    61  		postStep.Stopped = task.Stopped
    62  	}
    63  	ret := make([]*actions_model.ActionTaskStep, 0, len(task.Steps)+2)
    64  	ret = append(ret, preStep)
    65  	ret = append(ret, task.Steps...)
    66  	ret = append(ret, postStep)
    67  
    68  	return ret
    69  }
    70  
    71  func fullStepsOfEmptySteps(task *actions_model.ActionTask) []*actions_model.ActionTaskStep {
    72  	preStep := &actions_model.ActionTaskStep{
    73  		Name:      preStepName,
    74  		LogLength: task.LogLength,
    75  		Started:   task.Started,
    76  		Stopped:   task.Stopped,
    77  		Status:    actions_model.StatusRunning,
    78  	}
    79  
    80  	postStep := &actions_model.ActionTaskStep{
    81  		Name:     postStepName,
    82  		LogIndex: task.LogLength,
    83  		Started:  task.Stopped,
    84  		Stopped:  task.Stopped,
    85  		Status:   actions_model.StatusWaiting,
    86  	}
    87  
    88  	if task.Status.IsDone() {
    89  		preStep.Status = task.Status
    90  		if preStep.Status.IsSuccess() {
    91  			postStep.Status = actions_model.StatusSuccess
    92  		} else {
    93  			postStep.Status = actions_model.StatusCancelled
    94  		}
    95  	}
    96  
    97  	return []*actions_model.ActionTaskStep{
    98  		preStep,
    99  		postStep,
   100  	}
   101  }