-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (71 loc) · 4.39 KB
/
Copy pathcodeberg.yml
File metadata and controls
91 lines (71 loc) · 4.39 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
88
89
90
91
name: Sync All Repositories to Codeberg
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 }}
CODEBERG_TOKEN: ${{ secrets.CODEBERG_TOKEN }}
CODEBERG_USER: ${{ secrets.CODEBERG_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')
IS_PRIVATE="false"
if [ "$REPO_VISIBILITY" = "private" ] || [ "$IS_FORK" = "true" ]; then
IS_PRIVATE="true"
fi
TOPICS_JSON=$(echo "$repo" | jq -c '[.repositoryTopics[]?.name]')
API_BASE="https://codeberg.org/api/v1"
PROJECT_URL="$API_BASE/repos/$CODEBERG_USER/$REPO_NAME"
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: token $CODEBERG_TOKEN" "$PROJECT_URL")
if [ "$HTTP_STATUS" -eq 404 ]; then
CREATE_PAYLOAD=$(jq -n --arg name "$REPO_NAME" --arg desc "$REPO_DESC" --argjson priv "$IS_PRIVATE" '{name: $name, description: $desc, private: $priv}')
curl -s -X POST -H "Authorization: token $CODEBERG_TOKEN" \
-H "Content-Type: application/json" \
-d "$CREATE_PAYLOAD" \
"$API_BASE/user/repos" > /dev/null
fi
curl -s -X DELETE -H "Authorization: token $CODEBERG_TOKEN" "$PROJECT_URL/archive" > /dev/null
PATCH_PAYLOAD=$(jq -n --arg desc "$REPO_DESC" --argjson priv "$IS_PRIVATE" --arg branch "$DEFAULT_BRANCH" --arg web "$HOMEPAGE_URL" '{description: $desc, private: $priv, default_branch: $branch, website: $web}')
curl -s -X PATCH -H "Authorization: token $CODEBERG_TOKEN" \
-H "Content-Type: application/json" \
-d "$PATCH_PAYLOAD" \
"$PROJECT_URL" > /dev/null
TOPICS_PAYLOAD=$(jq -n --argjson topics "$TOPICS_JSON" '{topics: $topics}')
curl -s -X PUT -H "Authorization: token $CODEBERG_TOKEN" \
-H "Content-Type: application/json" \
-d "$TOPICS_PAYLOAD" \
"$PROJECT_URL/topics" > /dev/null
curl -s -X DELETE -H "Authorization: token $CODEBERG_TOKEN" "$PROJECT_URL/branch_protections/$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://$CODEBERG_USER:$CODEBERG_TOKEN@codeberg.org/$CODEBERG_USER/$REPO_NAME.git || echo "Failed to push $REPO_NAME"
cd ..
rm -rf $REPO_NAME.git
if [ "$REPO_ARCHIVED" = "true" ]; then
curl -s -X POST -H "Authorization: token $CODEBERG_TOKEN" "$PROJECT_URL/archive" > /dev/null
fi
done
CB_REPOS_JSON=$(curl -s -H "Authorization: token $CODEBERG_TOKEN" "https://codeberg.org/api/v1/users/$CODEBERG_USER/repos?limit=1000")
if echo "$CB_REPOS_JSON" | jq -e 'type == "array"' > /dev/null; then
CB_REPO_NAMES=$(echo "$CB_REPOS_JSON" | jq -r '.[].name')
for CB_REPO in $CB_REPO_NAMES; do
if ! echo "$GH_REPO_NAMES" | grep -qx "$CB_REPO"; then
curl -s -X DELETE -H "Authorization: token $CODEBERG_TOKEN" \
"https://codeberg.org/api/v1/repos/$CODEBERG_USER/$CB_REPO"
fi
done
fi