1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/git/git.go
Fabian Reh 04599e97da
feat(Gitops): Gitops update deployment with helm (#2247)
* makes containerImage not mandatory

* Adds kubectl container

* Adds log statement to debug

* adds general to container image

* removes GENERAL again
Removes condition from Kubectl container

* removes workDir

* marks logs as debug

* adds workingdir again

* Adds author to commits

* Adds commit time now

* remove deprecated and reorder

* adds deprecated again to containerRegistryUrl
Adds GENERAL scope to containerImage

* updates generated file

* Renames containerImageNameTag

* adds else case

* adds debug log

* code cleanup

* adds debug log

* revert

* adds debug logs

* revert

* makes root path not hidden

* revert

* Read container properties

* Removes debug message

* Removes debug message

* Removes general scope again

* Fixes unit test

* Adds helm capabilities to the gitopsUpdateDeployment step

* Adds helm capabilities to gitopsUpdateDeployment step

* Removes condition from input field

* Adds test for invalid deploy tool

* Fixes typo

* Adds tests for git errors and file errors
Simplifies test setup

* Adds test for error on image name extraction

* fixes URL variable name

* adds workind directory to paths

* Refactors too long method

* Reverts refactoring method

* Adds repository name as parameter

* Adds glob method

* Test glob method

* Revert "Test glob method"

This reverts commit ac11b54c14.

* Revert "Adds glob method"

This reverts commit ddf47ddebe.

* Revert "Adds repository name as parameter"

This reverts commit 8fc471c909.

* Removes getWd

* Adds stash deployDescriptor

* removes = from paramters

* Revert "removes = from paramters"

This reverts commit 3ecb3665e2.

* Adds " around parameters

* adds logging of all files

* Updates helm to version 3.3.4

* Clean up debug logs

* Raise error if no branch name provided.
Defaulting should be handled by step configuration.

* clean code

* Updates fields and adds checks for required field for certain deploy tools

* Fixes default commit message

* Update long description

* Removes default parameter

* Update resources/metadata/gitopsUpdateDeployment.yaml

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Updates yaml file

* Add error category and removes too much wrapping

* Update generated file

* Checks all parameters before returning the error

* Introduces constant

* Renames constant

* Fixes unit tests

* unexpose constants

* Makes tests thread safe and resilient to failed deletion

* Remove methods that did not work properly with hash containers rather than tags.

Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-11-03 18:29:46 +01:00

120 lines
3.9 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/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/pkg/errors"
"time"
)
// 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, author string, worktree *git.Worktree) (plumbing.Hash, error) {
return commitSingleFile(filePath, commitMessage, author, worktree)
}
func commitSingleFile(filePath, commitMessage, author 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{
All: true,
Author: &object.Signature{Name: author, When: time.Now()},
})
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 return an error 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 == "" {
return errors.New("no branch name provided")
}
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)
}