Skip to content

Instantly share code, notes, and snippets.

@ruwanka
Created November 11, 2018 06:41
Show Gist options
  • Save ruwanka/60683e3eb87abf1494ad7221b93e51f2 to your computer and use it in GitHub Desktop.
Save ruwanka/60683e3eb87abf1494ad7221b93e51f2 to your computer and use it in GitHub Desktop.
Clone all gitlab projects in a group
package main
import (
"encoding/json"
"flag"
"fmt"
"gopkg.in/src-d/go-git.v4"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
type GitLabProject struct {
ID int `json:"id"`
Description string `json:"description"`
DefaultBranch string `json:"default_branch"`
TagList []interface{} `json:"tag_list"`
Archived bool `json:"archived"`
Visibility string `json:"visibility"`
SSHURLToRepo string `json:"ssh_url_to_repo"`
HTTPURLToRepo string `json:"http_url_to_repo"`
WebURL string `json:"web_url"`
Name string `json:"name"`
NameWithNamespace string `json:"name_with_namespace"`
Path string `json:"path"`
PathWithNamespace string `json:"path_with_namespace"`
IssuesEnabled bool `json:"issues_enabled"`
MergeRequestsEnabled bool `json:"merge_requests_enabled"`
WikiEnabled bool `json:"wiki_enabled"`
JobsEnabled bool `json:"jobs_enabled"`
SnippetsEnabled bool `json:"snippets_enabled"`
CreatedAt time.Time `json:"created_at"`
LastActivityAt time.Time `json:"last_activity_at"`
SharedRunnersEnabled bool `json:"shared_runners_enabled"`
CreatorID int `json:"creator_id"`
Namespace struct {
ID int `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Kind string `json:"kind"`
} `json:"namespace"`
AvatarURL interface{} `json:"avatar_url"`
StarCount int `json:"star_count"`
ForksCount int `json:"forks_count"`
OpenIssuesCount int `json:"open_issues_count"`
PublicJobs bool `json:"public_jobs"`
SharedWithGroups []interface{} `json:"shared_with_groups"`
RequestAccessEnabled bool `json:"request_access_enabled"`
}
// usage
// ./gitlab -gitlabUrl=gitlab.com -apiToken xxxxxxxxxxxxxxxxxxx -group=gitlab-org -dir=tmp/
func main() {
gitlabUrl := flag.String("gitlabUrl", "gitlab.com", "gitlab host")
apiToken := flag.String("apiToken", "", "gitlab access token")
group := flag.String("group", "", "repository group")
dir := flag.String("dir","tmp/", "destination directory")
flag.Parse()
response, err := http.Get("https://" + *gitlabUrl + "/api/v4/groups/" + *group + "/projects?private_token=" + *apiToken)
if err != nil {
fmt.Println("Error occurred fetching gitlab project data! " + err.Error())
} else {
bytes, e := ioutil.ReadAll(response.Body)
if e != nil {
fmt.Println("Error occurred reading response! " + e.Error())
} else {
parseResponse(bytes, dir)
}
}
}
func parseResponse(bytes []byte, dir *string) {
var projects [] GitLabProject
jsonErr := json.Unmarshal(bytes, &projects)
if jsonErr != nil {
fmt.Println("error occurred when unmarshal json!")
} else {
fmt.Printf("found %d projects, cloning one by one...", len(projects))
for _, project := range projects {
clone(project.HTTPURLToRepo, *dir)
}
}
}
func clone(url string, dir string){
if !strings.HasSuffix(dir, "/") {
dir = dir + "/"
}
lastPathIndex := strings.LastIndex(url, "/")
lastDotIndex := strings.LastIndex(url, ".git")
repoName := url[lastPathIndex+1 : lastDotIndex]
fmt.Println("cloning " + repoName + "...")
git.PlainClone(dir+ repoName + "/", false, &git.CloneOptions{
URL: url,
Progress: os.Stdout,
} )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment