forked from jessfraz/jenkins-dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-mirrors.sh
executable file
·137 lines (107 loc) · 3.12 KB
/
generate-mirrors.sh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# This script generates DSLs for mirroring repositories. It used to do a
# real `git {clone,push} --mirror`, but since you need creds for it and the Git
# Plugin no longer saves the credentials in the workspace we can't call out to
# a shell to run --mirror. Also the git plugin doesn't have mirror options :(
set -e
set -o pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ -z "$GITHUB_USER" ]]; then
echo "Set the GITHUB_USER env variable."
exit 1
fi
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi
URI=https://api.github.com
API_VERSION=v3
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
DEFAULT_PER_PAGE=100
generate_dsl(){
local orig=$1
local user=${orig%/*}
local name=${orig#*/}
rname=${name//-/_}
file="${DIR}/projects/mirrors/${rname//./_}.groovy"
if [[ "$GITHUB_USER" == "$user" ]]; then
dest="$repo"
else
dest="$orig"
fi
echo "${file} | ${dest}"
cat <<-EOF > $file
freeStyleJob('mirror_${rname//./_}') {
displayName('mirror-${name}')
description('Mirror github.com/${orig} to g.j3ss.co/${dest}.')
checkoutRetryCount(3)
properties {
githubProjectUrl('https://github.com/${orig}')
sidebarLinks {
link('https://git.j3ss.co/${dest}', 'git.j3ss.co/${dest}', 'notepad.png')
}
}
logRotator {
numToKeep(2)
daysToKeep(2)
}
triggers {
cron('H H * * *')
}
wrappers { colorizeOutput() }
steps {
shell('git clone --mirror https://github.com/${orig}.git repo')
shell('cd repo && git push --mirror ssh://[email protected]:2200/~/${dest}.git')
}
publishers {
extendedEmail {
recipientList('\$DEFAULT_RECIPIENTS')
contentType('text/plain')
triggers {
stillFailing {
attachBuildLog(true)
}
}
}
wsCleanup()
}
}
EOF
}
main(){
# send the request
local response
response=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/users/${GITHUB_USER}/repos?per_page=${DEFAULT_PER_PAGE}&type=public")
local repos
repos=$(echo "$response" | jq --raw-output '.[] | {fullname:.full_name,repo:.name,fork:.fork,description:.description} | @base64')
mkdir -p $DIR/projects/mirrors
echo "FILE | REPO"
for r in $repos; do
raw="$(echo "$r" | base64 -d)"
local fullname
fullname=$(echo "$raw" | jq --raw-output '.fullname')
local repo
repo=$(echo "$raw" | jq --raw-output '.repo')
local description
description=$(echo "$raw" | jq --raw-output '.description')
local fork
fork=$(echo "$raw" | jq --raw-output '.fork')
response=$(curl -sSL -H "${AUTH_HEADER}" -H "${API_HEADER}" "${URI}/repos/${fullname}")
local user
user=$(echo "$response" | jq --raw-output '.parent.owner.login')
if [[ "$fullname" == "jessfraz/linux" ]]; then
continue
fi
if [[ "$fork" == "true" ]]; then
if [[ -z "$INCLUDE_FORKS" ]]; then
continue
else
generate_dsl "${user}/${repo}"
fi
else
generate_dsl "${fullname}"
fi
done
}
main