github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/kube/activity_functions.go (about)

     1  package kube
     2  
     3  import (
     4  	"time"
     5  
     6  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
     7  	"github.com/pkg/errors"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  )
    10  
    11  func StartPromote(p *v1.PromoteActivityStep) error {
    12  	if p.StartedTimestamp == nil {
    13  		p.StartedTimestamp = &metav1.Time{
    14  			Time: time.Now(),
    15  		}
    16  	}
    17  	if p.Status == v1.ActivityStatusTypeNone {
    18  		p.Status = v1.ActivityStatusTypeRunning
    19  	}
    20  	return nil
    21  }
    22  
    23  func CompletePromote(p *v1.PromoteActivityStep) error {
    24  	err := StartPromote(p)
    25  	if err != nil {
    26  		return err
    27  	}
    28  	if p.CompletedTimestamp == nil {
    29  		p.CompletedTimestamp = &metav1.Time{
    30  			Time: time.Now(),
    31  		}
    32  	}
    33  	p.Status = v1.ActivityStatusTypeSucceeded
    34  	return nil
    35  }
    36  
    37  func FailedPromote(p *v1.PromoteActivityStep) error {
    38  	err := StartPromote(p)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	if p.CompletedTimestamp == nil {
    43  		p.CompletedTimestamp = &metav1.Time{
    44  			Time: time.Now(),
    45  		}
    46  	}
    47  	p.Status = v1.ActivityStatusTypeFailed
    48  	return nil
    49  }
    50  
    51  func StartPromotionPullRequest(a *v1.PipelineActivity, s *v1.PipelineActivityStep, ps *v1.PromoteActivityStep, p *v1.PromotePullRequestStep) error {
    52  	err := StartPromote(ps)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	if p.StartedTimestamp == nil {
    57  		p.StartedTimestamp = &metav1.Time{
    58  			Time: time.Now(),
    59  		}
    60  	}
    61  	if a.Spec.WorkflowStatus != v1.ActivityStatusTypeRunning {
    62  		a.Spec.WorkflowStatus = v1.ActivityStatusTypeRunning
    63  	}
    64  	if a.Spec.Status != v1.ActivityStatusTypeRunning {
    65  		a.Spec.Status = v1.ActivityStatusTypeRunning
    66  	}
    67  	if p.Status != v1.ActivityStatusTypeRunning {
    68  		p.Status = v1.ActivityStatusTypeRunning
    69  	}
    70  	return nil
    71  }
    72  
    73  func StartPromotionUpdate(a *v1.PipelineActivity, s *v1.PipelineActivityStep, ps *v1.PromoteActivityStep, p *v1.PromoteUpdateStep) error {
    74  	err := StartPromote(ps)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	pullRequest := ps.PullRequest
    79  	if pullRequest != nil {
    80  		err = CompletePromotionPullRequest(a, s, ps, pullRequest)
    81  		if err != nil {
    82  			return errors.Wrap(err, "unable to complete promotion pull request")
    83  		}
    84  	}
    85  	if p.StartedTimestamp == nil {
    86  		p.StartedTimestamp = &metav1.Time{
    87  			Time: time.Now(),
    88  		}
    89  	}
    90  	if a.Spec.WorkflowStatus != v1.ActivityStatusTypeRunning {
    91  		a.Spec.WorkflowStatus = v1.ActivityStatusTypeRunning
    92  	}
    93  	if a.Spec.Status != v1.ActivityStatusTypeRunning {
    94  		a.Spec.Status = v1.ActivityStatusTypeRunning
    95  	}
    96  	if p.Status != v1.ActivityStatusTypeRunning {
    97  		p.Status = v1.ActivityStatusTypeRunning
    98  	}
    99  	return nil
   100  }
   101  
   102  func CompletePromotionPullRequest(a *v1.PipelineActivity, s *v1.PipelineActivityStep, ps *v1.PromoteActivityStep, p *v1.PromotePullRequestStep) error {
   103  	if p.StartedTimestamp == nil {
   104  		p.StartedTimestamp = &metav1.Time{
   105  			Time: time.Now(),
   106  		}
   107  	}
   108  	if p.CompletedTimestamp == nil {
   109  		p.CompletedTimestamp = &metav1.Time{
   110  			Time: time.Now(),
   111  		}
   112  	}
   113  	p.Status = v1.ActivityStatusTypeSucceeded
   114  	return nil
   115  }
   116  
   117  func FailedPromotionPullRequest(a *v1.PipelineActivity, s *v1.PipelineActivityStep, ps *v1.PromoteActivityStep, p *v1.PromotePullRequestStep) error {
   118  	if p.StartedTimestamp == nil {
   119  		p.StartedTimestamp = &metav1.Time{
   120  			Time: time.Now(),
   121  		}
   122  	}
   123  	if p.CompletedTimestamp == nil {
   124  		p.CompletedTimestamp = &metav1.Time{
   125  			Time: time.Now(),
   126  		}
   127  	}
   128  	p.Status = v1.ActivityStatusTypeFailed
   129  	return nil
   130  }
   131  
   132  func CompletePromotionUpdate(a *v1.PipelineActivity, s *v1.PipelineActivityStep, ps *v1.PromoteActivityStep, p *v1.PromoteUpdateStep) error {
   133  	err := CompletePromote(ps)
   134  	if err != nil {
   135  		return err
   136  	}
   137  	pullRequest := ps.PullRequest
   138  	if pullRequest != nil {
   139  		err = CompletePromotionPullRequest(a, s, ps, pullRequest)
   140  		if err != nil {
   141  			return err
   142  		}
   143  	}
   144  	if p.StartedTimestamp == nil {
   145  		p.StartedTimestamp = &metav1.Time{
   146  			Time: time.Now(),
   147  		}
   148  	}
   149  	if p.CompletedTimestamp == nil {
   150  		p.CompletedTimestamp = &metav1.Time{
   151  			Time: time.Now(),
   152  		}
   153  	}
   154  	p.Status = v1.ActivityStatusTypeSucceeded
   155  	return nil
   156  }
   157  
   158  func FailedPromotionUpdate(a *v1.PipelineActivity, s *v1.PipelineActivityStep, ps *v1.PromoteActivityStep, p *v1.PromoteUpdateStep) error {
   159  	err := FailedPromote(ps)
   160  	if err != nil {
   161  		return err
   162  	}
   163  	pullRequest := ps.PullRequest
   164  	if pullRequest != nil {
   165  		err = CompletePromotionPullRequest(a, s, ps, pullRequest)
   166  		if err != nil {
   167  			return err
   168  		}
   169  	}
   170  	if p.StartedTimestamp == nil {
   171  		p.StartedTimestamp = &metav1.Time{
   172  			Time: time.Now(),
   173  		}
   174  	}
   175  	if p.CompletedTimestamp == nil {
   176  		p.CompletedTimestamp = &metav1.Time{
   177  			Time: time.Now(),
   178  		}
   179  	}
   180  	p.Status = v1.ActivityStatusTypeFailed
   181  	return nil
   182  }