Repository navigation
87 lines (68 loc) · 4.05 KB
/
Copy pathgitlab.yml
File metadata and controls
87 lines (68 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
name: Sync All Repositories to GitLab
on:
schedule:
- cron: '0 * * * *'
workflow_dispatch:
jobs:
sync-all:
runs-on: ubuntu-latest
steps:
- name: Sync Repos, Metadata, Archiving, and Handle Deletions
env:
GH_TOKEN: ${{ secrets.GH_PAT }}
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
GITLAB_USER: ${{ secrets.GITLAB_USERNAME }}
run: |
GH_REPOS_JSON=$(gh repo list --limit 500 --json name,description,visibility,isArchived,defaultBranchRef,repositoryTopics,homepageUrl,isFork)
GH_REPO_NAMES=$(echo "$GH_REPOS_JSON" | jq -r '.[].name')
echo "$GH_REPOS_JSON" | jq -c '.[]' | while read -r repo; do
REPO_NAME=$(echo "$repo" | jq -r '.name')
REPO_DESC=$(echo "$repo" | jq -r '.description // ""')
REPO_VISIBILITY=$(echo "$repo" | jq -r '.visibility' | tr '[:upper:]' '[:lower:]')
REPO_ARCHIVED=$(echo "$repo" | jq -r '.isArchived')
DEFAULT_BRANCH=$(echo "$repo" | jq -r '.defaultBranchRef?.name // "main"')
HOMEPAGE_URL=$(echo "$repo" | jq -r '.homepageUrl // ""')
IS_FORK=$(echo "$repo" | jq -r '.isFork')
if [ "$IS_FORK" = "true" ]; then
REPO_VISIBILITY="private"
fi
TOPICS=$(echo "$repo" | jq -r '[.repositoryTopics[]?.name] | join(",")')
PROJECT_URL="https://gitlab.com/api/v4/projects/$GITLAB_USER%2F$REPO_NAME"
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$PROJECT_URL")
if [ "$HTTP_STATUS" -eq 404 ]; then
curl -s --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
--data "name=$REPO_NAME&path=$REPO_NAME&visibility=$REPO_VISIBILITY&description=$REPO_DESC" \
"https://gitlab.com/api/v4/projects" > /dev/null
fi
curl -s --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$PROJECT_URL/unarchive" > /dev/null
curl -s --request PUT --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
--data-urlencode "visibility=$REPO_VISIBILITY" \
--data-urlencode "description=$REPO_DESC" \
--data-urlencode "default_branch=$DEFAULT_BRANCH" \
--data-urlencode "tag_list=$TOPICS" \
"$PROJECT_URL" > /dev/null
if [ -n "$HOMEPAGE_URL" ]; then
curl -s --request PUT --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
--data-urlencode "description=$REPO_DESC - $HOMEPAGE_URL" \
"$PROJECT_URL" > /dev/null
fi
curl -s --request DELETE --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$PROJECT_URL/protected_branches/$DEFAULT_BRANCH" > /dev/null
git clone --mirror https://${{ github.actor }}:${GH_TOKEN}@github.com/${{ github.actor }}/$REPO_NAME.git
cd $REPO_NAME.git
git push --mirror https://oauth2:${GITLAB_TOKEN}@gitlab.com/$GITLAB_USER/$REPO_NAME.git || echo "Failed to push $REPO_NAME"
cd ..
rm -rf $REPO_NAME.git
if [ "$REPO_ARCHIVED" = "true" ]; then
curl -s --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$PROJECT_URL/archive" > /dev/null
fi
done
GL_REPOS_JSON=$(curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v4/users/$GITLAB_USER/projects?per_page=100")
if echo "$GL_REPOS_JSON" | jq -e 'type == "array"' > /dev/null; then
GL_REPO_NAMES=$(echo "$GL_REPOS_JSON" | jq -r '.[].path')
for GL_REPO in $GL_REPO_NAMES; do
if ! echo "$GH_REPO_NAMES" | grep -qx "$GL_REPO"; then
curl -s --request DELETE --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
"https://gitlab.com/api/v4/projects/$GITLAB_USER%2F$GL_REPO"
fi
done
fi