go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/model/changelogs.go (about)

     1  // Copyright 2022 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 model
    16  
    17  import (
    18  	"fmt"
    19  	"regexp"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  	"google.golang.org/protobuf/types/known/timestamppb"
    24  )
    25  
    26  // ChangeLog represents the changes of a revision
    27  type ChangeLog struct {
    28  	Commit         string          `json:"commit"`
    29  	Tree           string          `json:"tree"`
    30  	Parents        []string        `json:"parents"`
    31  	Author         ChangeLogActor  `json:"author"`
    32  	Committer      ChangeLogActor  `json:"committer"`
    33  	Message        string          `json:"message"`
    34  	ChangeLogDiffs []ChangeLogDiff `json:"tree_diff"`
    35  }
    36  
    37  type ChangeLogActor struct {
    38  	Name  string `json:"name"`
    39  	Email string `json:"email"`
    40  	Time  string `json:"time"`
    41  }
    42  
    43  type ChangeLogDiff struct {
    44  	Type    ChangeType `json:"type"`
    45  	OldID   string     `json:"old_id"`
    46  	OldMode int        `json:"old_mode"`
    47  	OldPath string     `json:"old_path"`
    48  	NewID   string     `json:"new_id"`
    49  	NewMode int        `json:"new_mode"`
    50  	NewPath string     `json:"new_path"`
    51  }
    52  
    53  type ChangeType string
    54  
    55  const (
    56  	ChangeType_ADD    = "add"
    57  	ChangeType_MODIFY = "modify"
    58  	ChangeType_COPY   = "copy"
    59  	ChangeType_RENAME = "rename"
    60  	ChangeType_DELETE = "delete"
    61  )
    62  
    63  // ChangeLogResponse represents the response from gitiles for changelog.
    64  type ChangeLogResponse struct {
    65  	Log  []*ChangeLog `json:"log"`
    66  	Next string       `json:"next"` // From next revision
    67  }
    68  
    69  // GetReviewUrl returns the review URL of the changelog.
    70  func (cl *ChangeLog) GetReviewUrl() (string, error) {
    71  	pattern := regexp.MustCompile("\\nReviewed-on: (https://.+)\\n")
    72  	matches := pattern.FindStringSubmatch(cl.Message)
    73  	if matches == nil {
    74  		return "", fmt.Errorf("Could not find review CL URL. Message: %s", cl.Message)
    75  	}
    76  	return matches[1], nil
    77  }
    78  
    79  // GetReviewTitle returns the review title from the changelog.
    80  func (cl *ChangeLog) GetReviewTitle() (string, error) {
    81  	pattern := regexp.MustCompile("\\A([^\\n]+)\\n")
    82  	matches := pattern.FindStringSubmatch(cl.Message)
    83  	if matches == nil {
    84  		return "", fmt.Errorf("Could not find review title. Message: %s", cl.Message)
    85  	}
    86  	return matches[1], nil
    87  }
    88  
    89  func (cl *ChangeLog) GetCommitTime() (*timestamppb.Timestamp, error) {
    90  	timeStr := cl.Committer.Time
    91  	layout := "Mon Jan 02 15:04:05 2006"
    92  	parsedTime, err := time.ParseInLocation(layout, timeStr, time.UTC)
    93  	if err != nil {
    94  		return nil, errors.Annotate(err, "parse time %s", timeStr).Err()
    95  	}
    96  	return timestamppb.New(parsedTime), nil
    97  }