migrate_gists.sh
· 3.6 KiB · Bash
Bruto
#!/bin/bash
# Clone gists from GitHub to Opengist
# Copyright (C) 2024 Jarno Rankinen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Requirements: jq, gh-cli (gh), git, sqlite3
#---------------------------------------------------
# Github access details, give token the "gist" scope
#---------------------------------------------------
TOKEN=''
GITHUB_USER=''
PER_PAGE=100
#-----------------------------------------------------------------------------------------
# Opengist access details:
#
# OG_URL is the instance url without scheme, e.g 'opengist.example.com'
# Set a password for the Opengist user and put username and password into OG_USER and OG_PW
# SQLITE_DB is the path to the sqlite db of Opengist, e.g. '/path/to/opengist/opengist.db'
#-----------------------------------------------------------------------------------------
OG_URL=''
OG_USER=''
OG_PW=''
SQLITE_DB=''
### Check requirements
requirements=( jq gh git sqlite3 )
for req in ${requirements[@]}; do
if !command -v $req &> /dev/null; then
echo "This script needs $req installed"
exit 1
fi
done
#--------------------------
# Get gist info from GitHub
#--------------------------
data="$(curl -sL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/users/$GITHUB_USER/gists?per_page=$PER_PAGE)"
tmp=$(mktemp)
echo $data > $tmp
#-------------------------------
# Generate ids and descrs arrays
#-------------------------------
ids=($(jq -r '.[] | .id' $tmp))
IFS=$'\n' read -rd '' -a descrs <<<$(echo $data | jq ".[] | .description" | sed "s/\"/\'/g")
#--------------------------------
# For each gist in GitHub,
# - clone the gist
# - init new gist in Opengist
# - Alter the OG Database and set:
# - Title as the filename
# - Access level like on github
# ( private or public )
# - Description
#---------------------------------
i=0
for id in ${ids[@]}; do
### Number of files
l=$(jq -r ".[] | select(.id==\"$id\") | .files | length" "$tmp")
### Visibility
public=$(jq -r ".[] | select(.id==\"$id\") | .public" "$tmp")
private=2
[[ "$public" == "true" ]] && private=0
### Name of first file
f0=$(jq -r ".[] | select(.id==\"$id\") | .files | keys_unsorted[0]" "$tmp")
### Description
desc=${descrs[$i]}
[[ "${descrs[$i]}" == "''" ]] && desc=\'$f0\'
echo -e "$i: ${id@Q} ${descrs[$i]} files:$l first:${f0@Q} private:${private@Q}"
### Clone gist
gh gist clone $id $id
pushd "$id" &> /dev/null || exit 1
### Push as new gist to Opengist, get the UUID of the new gist
git remote add gist https://$OG_USER:$OG_PW@$OG_URL/init
uuid=$(git push -u gist --all 2>&1 | grep set-url | awk -F "/" '{print $NF}' | sed 's|\s||g')
### Set title, visibility and description
sqlite3 "$SQLITE_DB" "update gists set title = ${f0@Q}, private = ${private@Q}, description = ${desc} where uuid = ${uuid@Q}"
printf "Created gist %s " ${uuid@Q}
popd &> /dev/null || exit 1
rm -rf "$id"
((i++))
done
echo $tmp
| 1 | #!/bin/bash |
| 2 | |
| 3 | # Clone gists from GitHub to Opengist |
| 4 | # Copyright (C) 2024 Jarno Rankinen |
| 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU Affero General Public License as |
| 8 | # published by the Free Software Foundation, either version 3 of the |
| 9 | # License, or (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU Affero General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU Affero General Public License |
| 17 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | |
| 19 | |
| 20 | # Requirements: jq, gh-cli (gh), git, sqlite3 |
| 21 | |
| 22 | #--------------------------------------------------- |
| 23 | # Github access details, give token the "gist" scope |
| 24 | #--------------------------------------------------- |
| 25 | TOKEN='' |
| 26 | GITHUB_USER='' |
| 27 | PER_PAGE=100 |
| 28 | |
| 29 | #----------------------------------------------------------------------------------------- |
| 30 | # Opengist access details: |
| 31 | # |
| 32 | # OG_URL is the instance url without scheme, e.g 'opengist.example.com' |
| 33 | # Set a password for the Opengist user and put username and password into OG_USER and OG_PW |
| 34 | # SQLITE_DB is the path to the sqlite db of Opengist, e.g. '/path/to/opengist/opengist.db' |
| 35 | #----------------------------------------------------------------------------------------- |
| 36 | OG_URL='' |
| 37 | OG_USER='' |
| 38 | OG_PW='' |
| 39 | SQLITE_DB='' |
| 40 | |
| 41 | ### Check requirements |
| 42 | requirements=( jq gh git sqlite3 ) |
| 43 | for req in ${requirements[@]}; do |
| 44 | if !command -v $req &> /dev/null; then |
| 45 | echo "This script needs $req installed" |
| 46 | exit 1 |
| 47 | fi |
| 48 | done |
| 49 | |
| 50 | #-------------------------- |
| 51 | # Get gist info from GitHub |
| 52 | #-------------------------- |
| 53 | data="$(curl -sL \ |
| 54 | -H "Accept: application/vnd.github+json" \ |
| 55 | -H "Authorization: Bearer $TOKEN" \ |
| 56 | -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 57 | https://api.github.com/users/$GITHUB_USER/gists?per_page=$PER_PAGE)" |
| 58 | tmp=$(mktemp) |
| 59 | echo $data > $tmp |
| 60 | |
| 61 | #------------------------------- |
| 62 | # Generate ids and descrs arrays |
| 63 | #------------------------------- |
| 64 | ids=($(jq -r '.[] | .id' $tmp)) |
| 65 | IFS=$'\n' read -rd '' -a descrs <<<$(echo $data | jq ".[] | .description" | sed "s/\"/\'/g") |
| 66 | |
| 67 | #-------------------------------- |
| 68 | # For each gist in GitHub, |
| 69 | # - clone the gist |
| 70 | # - init new gist in Opengist |
| 71 | # - Alter the OG Database and set: |
| 72 | # - Title as the filename |
| 73 | # - Access level like on github |
| 74 | # ( private or public ) |
| 75 | # - Description |
| 76 | #--------------------------------- |
| 77 | i=0 |
| 78 | for id in ${ids[@]}; do |
| 79 | ### Number of files |
| 80 | l=$(jq -r ".[] | select(.id==\"$id\") | .files | length" "$tmp") |
| 81 | ### Visibility |
| 82 | public=$(jq -r ".[] | select(.id==\"$id\") | .public" "$tmp") |
| 83 | private=2 |
| 84 | [[ "$public" == "true" ]] && private=0 |
| 85 | ### Name of first file |
| 86 | f0=$(jq -r ".[] | select(.id==\"$id\") | .files | keys_unsorted[0]" "$tmp") |
| 87 | ### Description |
| 88 | desc=${descrs[$i]} |
| 89 | [[ "${descrs[$i]}" == "''" ]] && desc=\'$f0\' |
| 90 | echo -e "$i: ${id@Q} ${descrs[$i]} files:$l first:${f0@Q} private:${private@Q}" |
| 91 | ### Clone gist |
| 92 | gh gist clone $id $id |
| 93 | pushd "$id" &> /dev/null || exit 1 |
| 94 | ### Push as new gist to Opengist, get the UUID of the new gist |
| 95 | git remote add gist https://$OG_USER:$OG_PW@$OG_URL/init |
| 96 | uuid=$(git push -u gist --all 2>&1 | grep set-url | awk -F "/" '{print $NF}' | sed 's|\s||g') |
| 97 | ### Set title, visibility and description |
| 98 | sqlite3 "$SQLITE_DB" "update gists set title = ${f0@Q}, private = ${private@Q}, description = ${desc} where uuid = ${uuid@Q}" |
| 99 | printf "Created gist %s " ${uuid@Q} |
| 100 | popd &> /dev/null || exit 1 |
| 101 | rm -rf "$id" |
| 102 | ((i++)) |
| 103 | done |
| 104 | |
| 105 | echo $tmp |
| 106 |