go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luciexe/build/state_view.go (about)

     1  // Copyright 2020 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package build
    16  
    17  import (
    18  	bbpb "go.chromium.org/luci/buildbucket/proto"
    19  	"google.golang.org/protobuf/proto"
    20  )
    21  
    22  // View is a window into the build State.
    23  //
    24  // You can obtain/manipulate this with the State.Modify method.
    25  type View struct {
    26  	SummaryMarkdown string
    27  	Critical        bbpb.Trinary
    28  	GitilesCommit   *bbpb.GitilesCommit
    29  }
    30  
    31  // Modify allows you to atomically manipulate the View on the build State.
    32  //
    33  // Blocking in Modify will block other callers of Modify and Set*, as well as
    34  // the ability for the build State to be sent (with the function set by
    35  // OptSend).
    36  //
    37  // The Set* methods should be preferred unless you need to read/modify/write
    38  // View items.
    39  func (s *State) Modify(cb func(*View)) {
    40  	s.mutate(func() bool {
    41  		v := View{
    42  			s.buildPb.SummaryMarkdown,
    43  			s.buildPb.Critical,
    44  			nil,
    45  		}
    46  		if s.buildPb.Output.GitilesCommit != nil {
    47  			v.GitilesCommit = proto.Clone(s.buildPb.Output.GitilesCommit).(*bbpb.GitilesCommit)
    48  		}
    49  
    50  		cb(&v)
    51  		modified := false
    52  		if v.SummaryMarkdown != s.buildPb.SummaryMarkdown {
    53  			modified = true
    54  			//summary := v.SummaryMarkdown
    55  			if s.buildPb.Output == nil {
    56  				s.buildPb.Output = &bbpb.Build_Output{}
    57  			}
    58  			s.buildPb.Output.SummaryMarkdown = v.SummaryMarkdown
    59  			s.buildPb.SummaryMarkdown = v.SummaryMarkdown
    60  		}
    61  		if v.Critical != s.buildPb.Critical {
    62  			modified = true
    63  			s.buildPb.Critical = v.Critical
    64  		}
    65  		if !proto.Equal(v.GitilesCommit, s.buildPb.Output.GitilesCommit) {
    66  			modified = true
    67  			s.buildPb.Output.GitilesCommit = proto.Clone(v.GitilesCommit).(*bbpb.GitilesCommit)
    68  		}
    69  		return modified
    70  	})
    71  }
    72  
    73  // SetSummaryMarkdown atomically sets the build's SummaryMarkdown field.
    74  func (s *State) SetSummaryMarkdown(summaryMarkdown string) {
    75  	s.Modify(func(v *View) {
    76  		v.SummaryMarkdown = summaryMarkdown
    77  	})
    78  }
    79  
    80  // SetCritical atomically sets the build's Critical field.
    81  func (s *State) SetCritical(critical bbpb.Trinary) {
    82  	s.Modify(func(v *View) {
    83  		v.Critical = critical
    84  	})
    85  }
    86  
    87  // SetGitilesCommit atomically sets the GitilesCommit.
    88  //
    89  // This will make a copy of the GitilesCommit object to store in the build
    90  // State.
    91  func (s *State) SetGitilesCommit(gc *bbpb.GitilesCommit) {
    92  	s.Modify(func(v *View) {
    93  		v.GitilesCommit = gc
    94  	})
    95  }