Skip to content

Commit

Permalink
Add integration test and clone url functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrie30 committed May 20, 2024
1 parent 33bdf2a commit 4b6b9c2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
44 changes: 39 additions & 5 deletions scm/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,43 @@ func (c Gitlab) GetTopLevelGroups() ([]string, error) {
return allGroups, nil
}

// In this case take the cloneURL from the cloneTartet repo and just inject /snippets/:id before the .git
// cloud example
// http clone target url https://gitlab.com/ghorg-test-group/subgroup-2/foobar.git
// http snippet clone url https://gitlab.com/ghorg-test-group/subgroup-2/foobar/snippets/3711587.git
// ssh clone target url [email protected]:ghorg-test-group/subgroup-2/foobar.git
// ssh snippet clone url [email protected]:ghorg-test-group/subgroup-2/foobar/snippets/3711587.git
func (c Gitlab) createRepoSnippetCloneURL(cloneTargetURL string, snippetID string) string {
// Split the cloneTargetURL into two parts at the ".git"
parts := strings.Split(cloneTargetURL, ".git")

// Insert the "/snippets/:id" before the ".git"
snippetCloneURL := parts[0] + "/snippets/" + snippetID + ".git"

return snippetCloneURL
}

// hosted example
// root snippet ssh clone url [email protected]:snippets/1.git
// root snippet http clone url http://gitlab.example.com/snippets/1.git
func (c Gitlab) createRootLevelSnippetCloneURL(snippetWebURL string) string {

// Web URL example, http://gitlab.example.com/-/snippets/1
// Both http and ssh clone urls do not have the /-/ in them so just remove it first and add the .git extention
cloneURL := strings.Replace(snippetWebURL, "/-/", "/", -1) + ".git"

if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" {
return c.addTokenToCloneURL(cloneURL, os.Getenv("GHORG_GITLAB_TOKEN"))
} else {
// http://gitlab.example.com/snippets/1.git
sshCloneURL := strings.Replace(cloneURL, "http://", "git@", 1)
// [email protected]/snippets/1.git
sshCloneURL = strings.Replace(sshCloneURL, "/", ":", 1)
// [email protected]:snippets/1.git
return sshCloneURL
}
}

func (c Gitlab) GetSnippets(cloneData []Repo) ([]Repo, error) {

if os.Getenv("GHORG_CLONE_SNIPPETS") != "true" {
Expand Down Expand Up @@ -216,7 +253,6 @@ func (c Gitlab) GetSnippets(cloneData []Repo) ([]Repo, error) {

for _, snippet := range allSnippets {
snippetAtRootLevel := c.rootLevelSnippet(snippet.WebURL)
snippetCloneURL := strings.Replace(snippet.WebURL, "/-/", "/", -1) + ".git"
snippetID := strconv.Itoa(snippet.ID)
snippetTitle := strings.ToLower(snippet.Title)
s := Repo{}
Expand All @@ -225,7 +261,7 @@ func (c Gitlab) GetSnippets(cloneData []Repo) ([]Repo, error) {
// If the snippet is not made on any repo its a root level snippet, this works for cloud
if snippetAtRootLevel {
s.IsGitLabRootLevelSnippet = true
s.CloneURL = snippetCloneURL
s.CloneURL = c.createRootLevelSnippetCloneURL(snippet.WebURL, snippetID)

Check failure on line 264 in scm/gitlab.go

View workflow job for this annotation

GitHub Actions / Build and Test Windows

too many arguments in call to c.createRootLevelSnippetCloneURL

Check failure on line 264 in scm/gitlab.go

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

too many arguments in call to c.createRootLevelSnippetCloneURL

Check failure on line 264 in scm/gitlab.go

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

too many arguments in call to c.createRootLevelSnippetCloneURL

Check failure on line 264 in scm/gitlab.go

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

too many arguments in call to c.createRootLevelSnippetCloneURL

Check failure on line 264 in scm/gitlab.go

View workflow job for this annotation

GitHub Actions / Build and Test Windows

too many arguments in call to c.createRootLevelSnippetCloneURL

Check failure on line 264 in scm/gitlab.go

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

too many arguments in call to c.createRootLevelSnippetCloneURL
s.URL = snippet.WebURL
s.Name = snippetTitle
s.GitLabSnippetInfo.ID = snippetID
Expand All @@ -235,11 +271,9 @@ func (c Gitlab) GetSnippets(cloneData []Repo) ([]Repo, error) {
for _, cloneTarget := range cloneData {
// determine if the snippet was created on the repo if it is then create a new Repo{} using the cloneTarget data

// clone target url https://gitlab.com/gabrie30/group/group/test-this
// snippet url https://gitlab.com/gabrie30/group/group/test-this/-/snippets/3711349
snippetRepoURL := strings.Split(snippet.WebURL, "/-/")
if cloneTarget.URL == snippetRepoURL[0]+".git" {
s.CloneURL = snippetCloneURL
s.CloneURL = c.createRepoSnippetCloneURL(cloneTarget.CloneURL, snippetID)
s.URL = snippet.WebURL
s.Name = snippetTitle
s.Path = cloneTarget.Path
Expand Down
19 changes: 19 additions & 0 deletions scripts/gitlab_cloud_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ else
exit 1
fi

# SNIPPETS
ghorg clone $GITLAB_GROUP_2 --token="${GITLAB_TOKEN}" --scm=gitlab --clone-snippets --preserve-dir

if [ -e "${HOME}"/ghorg/"${GITLAB_GROUP_2}"/subgroup-2/foobar.snippets/test-snippet-2-3711655 ]
then
echo "Pass: gitlab group clone snippet 2 with preserve dir"
else
echo "Fail: gitlab group clone snippet 2 with preserve dir"
exit 1
fi

if [ -e "${HOME}"/ghorg/"${GITLAB_GROUP_2}"/subgroup-2/foobar.snippets/test-snippet-1-3711654 ]
then
echo "Pass: gitlab group clone snippet 1 with preserve dir"
else
echo "Fail: gitlab group clone snippet 1 with preserve dir"
exit 1
fi

#
# SUBGROUP TESTS
#
Expand Down

0 comments on commit 4b6b9c2

Please sign in to comment.