github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/gerrit/fakegerrit/fakegerrit.go (about)

     1  /*
     2  Copyright 2022 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 fakegerrit
    18  
    19  import (
    20  	"fmt"
    21  	"sync"
    22  
    23  	gerrit "github.com/andygrunwald/go-gerrit"
    24  )
    25  
    26  type Project struct {
    27  	Branches  map[string]*gerrit.BranchInfo
    28  	ChangeIDs []string
    29  }
    30  
    31  type Change struct {
    32  	ChangeInfo *gerrit.ChangeInfo
    33  	Comments   map[string][]*gerrit.CommentInfo
    34  }
    35  
    36  type FakeGerrit struct {
    37  	Changes  map[string]Change
    38  	Accounts map[string]*gerrit.AccountInfo
    39  	Projects map[string]*Project
    40  	// lock to be thread safe
    41  	lock sync.Mutex
    42  }
    43  
    44  func (fg *FakeGerrit) Reset() {
    45  	fg.lock.Lock()
    46  	defer fg.lock.Unlock()
    47  
    48  	fg.Changes = make(map[string]Change)
    49  	fg.Accounts = make(map[string]*gerrit.AccountInfo)
    50  	fg.Projects = make(map[string]*Project)
    51  }
    52  
    53  // Returns changes from project with name `projectName“. Skips the first `start` number of ChangeIDs. `desiredTotal` caps the total to a number smaller or equal to the actual total number of ChangeIDs.
    54  func (fg *FakeGerrit) GetChangesForProject(projectName string, start, desiredTotal int) []*gerrit.ChangeInfo {
    55  	res := []*gerrit.ChangeInfo{}
    56  	if project, ok := fg.Projects[projectName]; !ok {
    57  		return res
    58  	} else {
    59  		for _, id := range project.ChangeIDs {
    60  			if start > 0 {
    61  				start--
    62  			} else {
    63  				res = append(res, fg.GetChange(id))
    64  				if len(res) == desiredTotal {
    65  					return res
    66  				}
    67  			}
    68  		}
    69  	}
    70  	return res
    71  }
    72  
    73  func (fg *FakeGerrit) GetComments(id string) map[string][]*gerrit.CommentInfo {
    74  	fg.lock.Lock()
    75  	defer fg.lock.Unlock()
    76  
    77  	if res, ok := fg.Changes[id]; ok {
    78  		return res.Comments
    79  	}
    80  	return nil
    81  }
    82  
    83  // Add a change to Fake gerrit and keep track that the change belongs to the given project
    84  func (fg *FakeGerrit) AddChange(projectName string, change *gerrit.ChangeInfo) {
    85  	fg.lock.Lock()
    86  	defer fg.lock.Unlock()
    87  
    88  	if project, ok := fg.Projects[projectName]; !ok {
    89  		project = &Project{ChangeIDs: []string{change.ChangeID}}
    90  		fg.Projects[projectName] = project
    91  	} else {
    92  		project.ChangeIDs = append(project.ChangeIDs, change.ChangeID)
    93  	}
    94  
    95  	fg.Changes[change.ChangeID] = Change{ChangeInfo: change, Comments: make(map[string][]*gerrit.CommentInfo)}
    96  }
    97  
    98  func (fg *FakeGerrit) AddBranch(projectName, branchName string, branch *gerrit.BranchInfo) {
    99  	fg.lock.Lock()
   100  	defer fg.lock.Unlock()
   101  
   102  	if project, ok := fg.Projects[projectName]; !ok {
   103  		project = &Project{ChangeIDs: []string{}, Branches: map[string]*gerrit.BranchInfo{}}
   104  		project.Branches[branchName] = branch
   105  		fg.Projects[projectName] = project
   106  	} else {
   107  		project.Branches[branchName] = branch
   108  	}
   109  }
   110  
   111  func (fg *FakeGerrit) GetBranch(projectName, branchID string) *gerrit.BranchInfo {
   112  	fg.lock.Lock()
   113  	defer fg.lock.Unlock()
   114  	var project *Project
   115  	var res *gerrit.BranchInfo
   116  	var ok bool
   117  
   118  	if project, ok = fg.Projects[projectName]; !ok {
   119  		return nil
   120  	}
   121  	if res, ok = project.Branches[branchID]; !ok {
   122  		return nil
   123  	}
   124  	return res
   125  }
   126  
   127  func (fg *FakeGerrit) GetChange(id string) *gerrit.ChangeInfo {
   128  	fg.lock.Lock()
   129  	defer fg.lock.Unlock()
   130  
   131  	if res, ok := fg.Changes[id]; ok {
   132  		return res.ChangeInfo
   133  	}
   134  	return nil
   135  }
   136  
   137  func (fg *FakeGerrit) GetAccount(id string) *gerrit.AccountInfo {
   138  	fg.lock.Lock()
   139  	defer fg.lock.Unlock()
   140  
   141  	if res, ok := fg.Accounts[id]; ok {
   142  		return res
   143  	}
   144  	return nil
   145  }
   146  
   147  func (fg *FakeGerrit) AddAccount(account *gerrit.AccountInfo) {
   148  	fg.lock.Lock()
   149  	defer fg.lock.Unlock()
   150  
   151  	fg.Accounts[fmt.Sprintf("%d", account.AccountID)] = account
   152  }
   153  
   154  func (fg *FakeGerrit) SetSelf(id string) error {
   155  	fg.lock.Lock()
   156  	defer fg.lock.Unlock()
   157  
   158  	if account, ok := fg.Accounts[id]; ok {
   159  		fg.Accounts["self"] = account
   160  		return nil
   161  	}
   162  
   163  	return fmt.Errorf("id: %s does not exist in accounts", id)
   164  }
   165  
   166  func NewFakeGerritClient() *FakeGerrit {
   167  	return &FakeGerrit{
   168  		Changes:  make(map[string]Change),
   169  		Accounts: make(map[string]*gerrit.AccountInfo),
   170  		Projects: make(map[string]*Project),
   171  	}
   172  }