mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
586044192c
* kanikoExecute: improve user experience * ensure proper tags * update permissions in case a container runs with a different user we need to make sure that the orchestrator user can work on the file * update permissions * ensure availablility of directories on Jenkins * (fix) clean up tmp dir in test * add resilience for incorrect step yaml * incorporate PR feedback * Adds piper step to update deployment configuration in external git repository. https://github.wdf.sap.corp/ContinuousDelivery/piper-ita/issues/21 * Adds handling of branchName as an optional parameter * Update resources/metadata/gitopsUpdateDeployment.yaml Feedback about description Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> * Adapt to interface guide * Refactors to GitopsExecRunner * Refactors to GitopsExecRunner in test * Removes unnecessary mocked methods * Adds tests for git utils * Adds new step to CommonStepsTest.groovy * Updates description from yaml * Restricts visibility of methods and interfaces Adds comments where necessary * Updates comments * Fixes URL name * updates description * updates generated file * Fixes compile issue in CommonStepsTest.groovy * Updates long description * Updates test to run green on all kind of OS * Removes global variables from tests * Default branch: master Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> * Typo in Hierarchy Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> * Refactors test to allow parallel execution * Renames utility variable in gitopsUpdateDeployment.go * Renames error variables in gitopsUpdateDeployment.go * simplified parameters for kubectl * Refactors util classes to use parameters rather than global variables * makes username and password mandatory * remove unnecessary mandatory flag * remove new methods from mock that are not necessary * replaces with EqualError * replaces with NoError * update generated file * refactor tests * refactor tests * make tests parallel executable * parallel execution of tests * Refactors interfaces to stop exposing interfaces * Feedback from PR * Simplifies failing mocks * Renames variables and interfaces * Fixes error messages * shorten variable names * Renames unused parameters in tests * Cleanup nil parameters * Typo * Wrap errors and remove unnecessary logs * Remove containername and filePath from GENERAL scope * correct generated file * corrects expected error messages Co-authored-by: OliverNocon <oliver.nocon@sap.com> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
115 lines
3.8 KiB
Go
115 lines
3.8 KiB
Go
package git
|
|
|
|
import (
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// utilsWorkTree interface abstraction of git.Worktree to enable tests
|
|
type utilsWorkTree interface {
|
|
Add(path string) (plumbing.Hash, error)
|
|
Commit(msg string, opts *git.CommitOptions) (plumbing.Hash, error)
|
|
Checkout(opts *git.CheckoutOptions) error
|
|
}
|
|
|
|
// utilsRepository interface abstraction of git.Repository to enable tests
|
|
type utilsRepository interface {
|
|
Worktree() (*git.Worktree, error)
|
|
Push(o *git.PushOptions) error
|
|
}
|
|
|
|
// utilsGit interface abstraction of git to enable tests
|
|
type utilsGit interface {
|
|
plainClone(path string, isBare bool, o *git.CloneOptions) (*git.Repository, error)
|
|
}
|
|
|
|
// CommitSingleFile Commits the file located in the relative file path with the commitMessage to the given worktree.
|
|
// In case of errors, the error is returned. In the successful case the commit is provided.
|
|
func CommitSingleFile(filePath, commitMessage string, worktree *git.Worktree) (plumbing.Hash, error) {
|
|
return commitSingleFile(filePath, commitMessage, worktree)
|
|
}
|
|
|
|
func commitSingleFile(filePath, commitMessage string, worktree utilsWorkTree) (plumbing.Hash, error) {
|
|
_, err := worktree.Add(filePath)
|
|
if err != nil {
|
|
return [20]byte{}, errors.Wrap(err, "failed to add file to git")
|
|
}
|
|
|
|
commit, err := worktree.Commit(commitMessage, &git.CommitOptions{})
|
|
if err != nil {
|
|
return [20]byte{}, errors.Wrap(err, "failed to commit file")
|
|
}
|
|
|
|
return commit, nil
|
|
}
|
|
|
|
// PushChangesToRepository Pushes all committed changes in the repository to the remote repository
|
|
func PushChangesToRepository(username, password string, repository *git.Repository) error {
|
|
return pushChangesToRepository(username, password, repository)
|
|
}
|
|
|
|
func pushChangesToRepository(username, password string, repository utilsRepository) error {
|
|
pushOptions := &git.PushOptions{
|
|
Auth: &http.BasicAuth{Username: username, Password: password},
|
|
}
|
|
err := repository.Push(pushOptions)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to push commit")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PlainClone Clones a non-bare repository to the provided directory
|
|
func PlainClone(username, password, serverURL, directory string) (*git.Repository, error) {
|
|
abstractedGit := &abstractionGit{}
|
|
return plainClone(username, password, serverURL, directory, abstractedGit)
|
|
}
|
|
|
|
func plainClone(username, password, serverURL, directory string, abstractionGit utilsGit) (*git.Repository, error) {
|
|
gitCloneOptions := git.CloneOptions{
|
|
Auth: &http.BasicAuth{Username: username, Password: password},
|
|
URL: serverURL,
|
|
}
|
|
repository, err := abstractionGit.plainClone(directory, false, &gitCloneOptions)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to clone git")
|
|
}
|
|
return repository, nil
|
|
}
|
|
|
|
// ChangeBranch checkout the provided branch.
|
|
// It will create a new branch if the branch does not exist yet.
|
|
// It will checkout "master" if no branch name if provided
|
|
func ChangeBranch(branchName string, worktree *git.Worktree) error {
|
|
return changeBranch(branchName, worktree)
|
|
}
|
|
|
|
func changeBranch(branchName string, worktree utilsWorkTree) error {
|
|
if branchName == "" {
|
|
branchName = "master"
|
|
}
|
|
|
|
var checkoutOptions = &git.CheckoutOptions{}
|
|
checkoutOptions.Branch = plumbing.NewBranchReferenceName(branchName)
|
|
checkoutOptions.Create = false
|
|
err := worktree.Checkout(checkoutOptions)
|
|
if err != nil {
|
|
// branch might not exist, try to create branch
|
|
checkoutOptions.Create = true
|
|
err = worktree.Checkout(checkoutOptions)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to checkout branch")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type abstractionGit struct{}
|
|
|
|
func (abstractionGit) plainClone(path string, isBare bool, o *git.CloneOptions) (*git.Repository, error) {
|
|
return git.PlainClone(path, isBare, o)
|
|
}
|