Git/GitLab

From Ever changing code
< Git
Jump to navigation Jump to search

Download all repositories

Gitlab server

Note: Pending update 2020, to distinguish all repos vs private to view as it's a difference when hosting on GitLab.com and your own server where 'public' repos are still private to you. This is not a case for GitLab.com You can get token when log in to your account > settings and generate access token. Assign all permissions. Also so you know v3 APIs may be deployed on older version Gitlab.

#!/bin/bash 

# Get pages count of all repos you can access
# $ curl --head "http://<host.com>/api/v4/projects?private_token=<token>=100&page=1" | grep Total
#   X-Total: 509
#   X-Total-Pages: 6

#Create JSON file with repo name and path
#for PAGE in {1..6}; do curl "<host.com>/api/v4/projects?private_token=CMQdz-Qik8XanGYJTbnH&per_page=100&page=$PAGE" | jq --raw-output --compact-output ".[] | { "path": .path, "git": .ssh_url_to_repo }" >> repos.json; done

[ -d repos ] || mkdir repos
cd repos
while read repo; do
    THEPATH=$(echo "$repo" | jq -r ".path")
    echo "path: $THEPATH"
    GIT=$(echo "$repo" | jq -r ".git")
    echo "git: $GIT"

    if [ ! -d "$THEPATH" ]; then
        echo "Cloning $THEPATH ( $GIT )"
        git clone "$GIT"
    else
        echo "Pulling $THEPATH"
        (cd "$THEPATH" && git pull)
    fi
done < "../$FILENAME"
cd ..


Example repos.json

{"path":"project-x","git":"git@<host.com>:Spikes/project-x.git"}
{"path":"project-y","git":"git@<host.com>:Spikes/AWS/project-y.git"}

Gitlab.com (SaaS)

Download all private repositories from a given group. Tested on Sep-2021.

#!/bin/bash
PAT=11111111111111111111
GROUP_NAME=platform
GROUP_ID=1234567
FILE=ume-gitlab.com-${GROUP_NAME}-$(date +"%Y%m%d-%H%M").json
curl -s --header "PRIVATE-TOKEN: ${PAT}" https://gitlab.com/api/v4/groups/${GROUP_ID} | jq . > $FILE
for repo in $(cat  $FILE | jq ".projects[].ssh_url_to_repo" | tr -d '"'); do
  repo_dir=${repo%.git}; repo_dir=${repo_dir##*/}
  [ ! -d $repo_dir ] && git clone $repo || { cd $repo_dir; git pull; cd -; }
done

Gitlab - glab cli

glab - work seamlessly with Gitlab from the command line inspired by Github gh tool.
$ glab auth login
? What GitLab instance do you want to log into? gitlab.com
- Logging into gitlab.com

Tip: you can generate a Personal Access Token here https://gitlab.com/-/profile/personal_access_tokens
The minimum required scopes are 'api' and 'write_repository'.
? Paste your authentication token: ********************
? Choose default git protocol HTTPS
? Authenticate Git with your GitLab credentials? Yes
- glab config set -h gitlab.com git_protocol https
✓ Configured git protocol
- glab config set -h gitlab.com api_protocol https
✓ Configured API protocol
✓ Logged in as piotr

# cd to repo, so the tool knows which remote project talk to
glab mr view   <merge_request_id>
glab mr accept <merge_request_id>
glab mr merge  <merge_request_id>

# Create MR using git only. The title is the last commit message eg. JIRA-1234: Update README
git push --set-upstream origin mybranch -o merge_request.create

Reset a user password

This could be also root user.

ubuntu@gitlab:~$  sudo gitlab-rails console
-------------------------------------------------------------------------------------
 GitLab:       11.4.0 (6ebbd70)
 GitLab Shell: 8.3.3
 postgresql:   9.6.8
-------------------------------------------------------------------------------------
Loading production environment (Rails 4.2.10)
irb(main):001:0> user = User.find_by(username: "bob")
                 user = User.find_by(id: 1)    //by id
=> #<User id:5 @bob>
irb(main):002:0> user.password = 'test1234'    //at least 8 characters long
=> "test1"
irb(main):003:0> user.password_confirmation = 'test1234'
=> "test1"
irb(main):007:0> user.unlock_access!
=> true
irb(main):008:0> user.save!
=> false

Deployment token

In course to grant access to a single repository for reading, writting or both we can use feature called Deploy Tokens. Go to repository > repository settings > Deploy Tokens to create one.

ClipCapIt-191107-092625.PNG


Access repository

# Syntax
# git clone http://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git

# Example
git clone https://darkmatter:PLjeFh56F15yJeq8xrZv@gitlab.com/hackerrank1/apache-nginx-ssl.git
Cloning into 'apache-nginx-ssl'...
remote: Enumerating objects: 110, done.
remote: Counting objects: 100% (110/110), done.
...
References

GitLab CI

References