github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/pod-utils/clone/format.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package clone
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  )
    23  
    24  // FormatRecord describes the record in a human-readable
    25  // manner for inclusion into build logs
    26  func FormatRecord(record Record) string {
    27  	var output bytes.Buffer
    28  	if record.Failed {
    29  		fmt.Fprintln(&output, "# FAILED")
    30  	}
    31  	if record.Refs.Repo == "" {
    32  		fmt.Fprintf(&output, "Environment setup")
    33  	} else {
    34  		fmt.Fprintf(&output, "# Cloning %s/%s at %s", record.Refs.Org, record.Refs.Repo, record.Refs.BaseRef)
    35  	}
    36  	if record.Refs.BaseSHA != "" {
    37  		fmt.Fprintf(&output, "(%s)", record.Refs.BaseSHA)
    38  	}
    39  	output.WriteString("\n")
    40  	if len(record.Refs.Pulls) > 0 {
    41  		output.WriteString("# Checking out pulls:\n")
    42  		for _, pull := range record.Refs.Pulls {
    43  			fmt.Fprintf(&output, "#\t%d", pull.Number)
    44  			if pull.SHA != "" {
    45  				fmt.Fprintf(&output, "(%s)", pull.SHA)
    46  			}
    47  			fmt.Fprint(&output, "\n")
    48  		}
    49  	}
    50  	for _, command := range record.Commands {
    51  		runtime := ""
    52  		if command.Duration != 0 {
    53  			runtime = fmt.Sprintf(" (runtime: %v)", command.Duration)
    54  		}
    55  		fmt.Fprintf(&output, "$ %s%s\n", command.Command, runtime)
    56  		fmt.Fprint(&output, command.Output)
    57  		if command.Error != "" {
    58  			fmt.Fprintf(&output, "# Error: %s\n", command.Error)
    59  		}
    60  	}
    61  	fmt.Fprintf(&output, "# Final SHA: %v\n", record.FinalSHA)
    62  	fmt.Fprintf(&output, "# Total runtime: %v\n", record.Duration)
    63  
    64  	return output.String()
    65  }