Reorganize common files and create fetch commmits script

Signed-off-by: Miguel A. Cabrera Minagorri <mcabrera@vmware.com>
This commit is contained in:
Miguel A. Cabrera Minagorri
2022-04-11 16:59:09 +02:00
parent 6e782bfad8
commit 77f92f1122
1772 changed files with 141 additions and 45265 deletions

118
scripts/fetch-commits.sh Normal file
View File

@@ -0,0 +1,118 @@
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
set -o xtrace # Uncomment this line for debugging purpose
TARGET_DIR="."
COMMIT_SHIFT="${1:-0}" # Used when you push commits manually
function queryRepos() {
local page=0
local repos="[]" # Initial empty JSON array
local -r repos_per_page="100"
while [[ "$page" -gt -1 ]]; do
# Query only the public repos since we won't add private containers to bitnami/containers
page_repos="$(curl -H 'Content-Type: application/json' -H 'Accept: application/json' "https://api.github.com/orgs/bitnami/repos?type=public&per_page=${repos_per_page}&page=${page}")"
repos="$(jq -s 'reduce .[] as $x ([]; . + $x)' <(echo "$repos") <(echo "$page_repos"))"
n_repos="$(jq length <<< "$page_repos")"
if [[ "$n_repos" -lt "$repos_per_page" ]]; then
page="-1"
else
page="$((page + 1))"
fi
done
echo "$repos"
}
function getContainerRepos() {
local -r repos="$(queryRepos)"
local -r container_repos="$(jq -r '[ .[] | select(.name | test("bitnami-docker-.")) ]' <<< "$repos")"
echo "$container_repos" > /tmp/repos
echo "$container_repos"
}
# Commits a directory
function commitChanges() {
git config user.name "Bitnami Containers"
git config user.email "containers@bitnami.com"
# git diff --exit-code --quiet <commit-A> <commit-b>
git diff --exit-code || (git add -A && git commit -m "Sync ${name} files")
}
function pushChanges() {
git config user.name "Bitnami Containers"
git config user.email "containers@bitnami.com"
git push
}
function findCommitsToSync() {
local origin_name="${1:?Missing origin name}"
# Get all commits IDs on the origin
local -r commits=($(git rev-list "${origin_name}/master" -- .))
# Find the commit that doesn't have changes respect the container folder
# Get the last commit message in the container folder
local shift=$((COMMIT_SHIFT + 1))
local -r last_commit_message="$(git log -n "$shift" --pretty=tformat:"%s" containers/"$origin_name" | tail -n 1)"
# Search on the container origin repo the ID for the latest commit we have locally in the container folder
local -r last_synced_commit_id="$(git rev-list "$origin_name"/master --grep="${last_commit_message}")"
local commits_to_sync=""
local max=20 # If we need to sync more than 20 commits there must be something wrong since we run the job on a daily basis
for commit in "${commits[@]}"; do
if [[ "$commit" != "$last_synced_commit_id" ]] && [[ "$max" -gt "0" ]]; then
commits_to_sync="${commit} ${commits_to_sync}"
else
# We reached the last commit synced
break
fi
max=$((max - 1))
done
[[ "$max" -eq "0" ]] && echo "Last commit not found into the original repo history" && exit 1
printf "$commits_to_sync"
}
syncCommit() {
local -r commit_id="${1:?Missing commit id}"
local -r app="${2:?Missing app name}"
local -r patch_file="$(git format-patch -1 "$commit_id")"
# Apply patch
git am --directory "containers/${app}" "$patch_file"
rm -f "$patch_file"
}
function syncRepos() {
local -r repos="$(getContainerRepos)"
mkdir -p "$TARGET_DIR"
# Build array of app names since we need to exclude them when moving files
local apps=("mock")
local -r urls=($(echo "$repos" | jq -r '.[].html_url'))
for repo_url in "${urls[@]}"; do
name="${repo_url:42}" # 42 is the length of https://github.com/bitnami/bitnami-docker-
apps=("${apps[@]}" "$name")
done
echo "$repos" | jq -r '.[].html_url' | while read -r repo_url; do
name="${repo_url:42}" # 42 is the length of https://github.com/bitnami/bitnami-docker-
(
cd "$TARGET_DIR"
# Fetch the old repo master
git remote add --fetch "$name" "$repo_url"
read -r -a commits_to_sync <<< "$(findCommitsToSync "$name")"
for commit in "${commits_to_sync[@]}"; do
syncCommit "$commit" "$name"
done
git remote remove "$name"
)
done
pushChanges
}
syncRepos

View File

@@ -7,29 +7,6 @@ set -o xtrace # Uncomment this line for debugging purpose
TARGET_DIR="."
# App and repo that will be used as base
BASE_REPO="https://github.com/bitnami/bitnami-docker-wordpress"
function joinStrings() {
local a=("${@:3}")
printf "%s" "$2${a[@]/#/$1}"
}
function moveFilesExcluding() {
local -r dest="${1:?Missing destination directory}"
shift
local -r excludeFiles=("$@")
echo "Files to exclude: ${excludeFiles[@]}"
mkdir -p "$dest"
local -r f_exclude_flags=($(joinStrings ' -and ! -name ' "${excludeFiles[@]}"))
echo "${f_exclude_flags[@]}"
find . -depth 1 ! -name "${f_exclude_flags[@]}" | while read -r file; do
cp -a "$file" "${dest}/"
rm -Rf "$file"
done
}
function queryRepos() {
local page=0
local repos="[]" # Initial empty JSON array
@@ -57,18 +34,10 @@ function getContainerRepos() {
echo "$container_repos"
}
# Commits a directory
function commitChanges() {
git config user.name "Bitnami Containers"
git config user.email "containers@bitnami.com"
git diff --exit-code || (git add -A && git commit -m "Sync ${name} files")
}
function pushChanges() {
git config user.name "Bitnami Containers"
git config user.email "containers@bitnami.com"
git push
git push origin main
}
function mergeRepos() {
@@ -92,8 +61,7 @@ function mergeRepos() {
(
cd "$TARGET_DIR"
mkdir -p "containers/${name}" # Create directory for the app
# Back up the README.md to avoid getting merge conflicts
#mv README.md README.md.back
# clone the repositoy outside of this one
git clone "$repo_url" ../temporal/"$name"
(
@@ -103,16 +71,9 @@ function mergeRepos() {
)
# Fetch the old repo and merge maintaining history
git remote add --fetch "$name" "../temporal/${name}"
git merge "${name}/master" --allow-unrelated-histories --no-log --no-ff -Xtheirs #--no-commit
#git checkout main "${special_files[@]}"
git merge "${name}/master" --allow-unrelated-histories --no-log --no-ff -Xtheirs
git remote remove "$name"
rm -Rf ../temporal/"$name"
# Move the content to the app directory
# moveFilesExcluding "containers/${name}" "${static_files[@]}" "README.md.back" "${special_files[@]}" "${apps[@]}"
# Recover README.md
#mv README.md.back README.md
#commitChanges
exit 0
)
done