summaryrefslogtreecommitdiff
path: root/github.sh
blob: 64f7741bd3ab87927bae9887b0cbae6494877f20 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env bash

##############################################################################
##
##  GitHub Upload+Update Script (V2, combined) for DevPlat Samples
##
##############################################################################

update=true
upload=true
deleteTemp=true
useAllSamples=true
allSamples=()
token=

## Generates a random 32 character alphaneumeric string to use as a post script 
##   for the temporary code folder (folder will be deleted at end)
folderPS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)

#utility function to print to stderr
echoerr() { echo "$@" 1>&2; }

display_usage() {
echo -e "\e[90mUsage:

      -t | --token [github_auth_token]
         Input an auth token to access the googlesamples GitHub org
         (if this is not present, you will be prompted for one later)

      -s | --samples [sample1 sample2 sample3 ... sampleN]
         If you don't want to check the entire samples folder,
         you can specify which samples to use with this option.

      --upload-only
          Only uploads new samples - samples with existing
          repos will be ignored

      --update-only
          Only updates samples with existing repos - new
          samples will be ignored

      --keep-temp-files
          Will not delete the temporary directory used to pull/push
          to Github. (normally deleted upon exit) Preserves logs.

  This script can be run with no options - it will check the entire
  ./prebuilts/gradle folder and prompt for an auth token when needed.\e[0m\n"
}

##############################################################################
##  Make sure we delete the temporary folder (if it gets created) before exiting
finish() {
  if $deleteTemp; then
    if [ -d "../github-temp$folderPS" ]; then
     cd ..
     rm -rf ./github-temp$folderPS
    elif [ -d "github-temp$folderPS" ]; then
     rm -rf ./github-temp$folderPS
    fi
  fi
}
# this ensures finish() will always be called no matter how the script ends
trap finish EXIT


##############################################################################
##  Process input parameters. (see above for usage)

## How this works:
##  $# is the number of parameters passed in
##  $1 is the first parameter, $2 the second, and so on. (space delimited)
##  shift basically left shifts the params array - $1 goes away, $2 becomes $1, etc
##  Thus, this while loop iterates through all command line parameters
while [[ $# > 0 ]]; do
case "$1" in

  -t|--token)
    if [[ $2 != -* ]] && [[ $2 ]]; then
      token="$2"; shift
    else
      echoerr -e "Option $1 requires an argument. Cancelling script.\nUse --help to display usage."
      exit 1
    fi;;

  --update-only) upload=false;;

  --upload-only) update=false;;

  --keep-temp-files) deleteTemp=false;;

  -s|--samples)
    useAllSamples=false
    while [[ $2 != -* ]] && [[ $2 ]]; do
      #if true; then ##for testing
      if [ -d "./prebuilts/gradle/$2" ]; then
        allSamples+=("$2")
        shift
      else
        echoerr -e "Sample \"$2\" does not exist in ./prebuilts/gradle. Cancelling script.\n"
        exit 1
      fi
    done;;

  -h|--help)
      display_usage
      exit 1;;

  *)
    echoerr -e "Unknown Option: $1\nUse --help to display usage."
    exit 1;;

esac
shift
done #ends options while loop

if ! $upload && ! $update; then
  echoerr -e "Do not use both --update-only and --upload-only, no samples will be processed.
  If you want to do both updates and uploads, no flags are needed.
  Use --help to display usage."
  exit 1
fi

##############################################################################
##  Get all folders in prebuilts and stick 'em in an array

if $useAllSamples; then
  allSamples=($(ls ./prebuilts/gradle))
fi

# [@] returns all items in an array, ${#...} counts them
numSamples=${#allSamples[@]}
echo "Running script for $numSamples samples"

##############################################################################
##  Iterate through all the samples and see if there's
##  a repo for them on GitHub already - save results so we only do it once

toUpdate=()
toUpload=()
problemSamples=()
curSample=0

echo -ne "Checking for existence of repos... ($curSample/$numSamples)\r"
for i in ${allSamples[@]};
do
 #echo "$i"
 URL=https://github.com/googlesamples/android-$i
 result=$(curl -o /dev/null --silent --head --write-out '%{http_code}' "$URL")
 #echo "$result $URL"
 if [ "$result" -eq "404" ]; then
   toUpload+=("$i")
 elif [ "$result" -eq "200" ]; then
   toUpdate+=("$i")
 else
   problemSamples+=("$i")
 fi
 curSample=$(($curSample+1))
 echo -ne "Checking for existence of repos... ($curSample/$numSamples)\r"
done #close for loop for existence check
echo ""


##############################################################################
##  For every sample that has a repo already, clone it and diff it against
##  the sample code in our git to see if it needs updating.

if $update; then

needsUpdate=()
curSample=0
numUpdates=${#toUpdate[@]}

##make temporary dir to pull code into - will be deleted upon exit.
mkdir github-temp$folderPS
cd github-temp$folderPS

echo -ne "Checking for out-of-date repos... ($curSample/$numUpdates)\r"
for i in ${toUpdate[@]};
do
 URL=https://github.com/googlesamples/android-$i
 git clone $URL.git &> /dev/null
 if [ -d "android-$i" ]; then
   diffResult=$(diff -r --exclude '*.git' ../prebuilts/gradle/$i/ ./android-$i/)
   #for testing (will show diff in every repo)
   #diffResult=$(diff -r ../prebuilts/gradle/$i/ ./android-$i/)`
   #echo $diffResult
   if [ -n "$diffResult" ]; then
     needsUpdate+=("$i")
   fi
 else
   echoerr "Something went wrong when cloning $i - result directory does not exist.
   Leaving temp files in place for further examination."
   deleteTemp=false;
 fi
 curSample=$(($curSample+1))
 echo -ne "Checking for out-of-date repos... ($curSample/$numUpdates)\r"
done #end of for loop when checking which repos actually need updating
echo ""
fi

echo ""

##############################################################################
##  Display the detected changes to be made and get user confirmation

if $upload; then
  if [ ${#toUpload[@]} -ne 0 ]; then
    echo -e "\n\e[1mNew samples that will be uploaded:\e[0m"
    for i in ${toUpload[@]}; do
     echo -e "\e[32m$i\e[0m"
    done
  else
    upload=false
    echo "Nothing new to upload."
  fi
else
  echo "No uploads - check skipped on user request"
fi

if $update; then
  if [ ${#needsUpdate[@]} -ne 0 ]; then
    echo -e "\n\e[1mSamples that will be updated:\e[0m"
    for i in ${needsUpdate[@]}; do
      echo -e "\e[34m$i\e[0m"
    done
  else
    update=false
    echo "Nothing to update."
  fi
else
  echo "No updates - check skipped on user request"
fi

if [ ${#problemSamples[@]} -ne 0 ]; then
 echoerr "
These repos returned something other than a 404 or 200 result code:"
 for i in ${problemSamples[@]};
 do
  echoerr "$i"
 done
fi

if ! $upload && ! $update; then
 echo -e "\e[1mLooks like everything's up-to-date.\e[0m\n"
else

read -p "
Do you want to continue? [y/n]: " -n 1 -r
echo
# if they type anything but an upper or lower case y, don't proceed.
if [[ $REPLY =~ ^[Yy]$ ]]
then
   #echo "Commencing Github updates"

##############################################################################
##  If the user hasn't supplied a token via parameter, ask now

if ! [ -n "$token" ]
then
 read -p "
Input a valid googlesamples GitHub access token to continue: " -r
 token=$REPLY
fi

##############################################################################
##  Test that token

tokenTest=$(curl -o /dev/null --silent \
  -H "Authorization: token $token" \
  --write-out '%{http_code}' "https://api.github.com/orgs/googlesamples/repos")

if [ "$tokenTest" -eq "200" ]; then


##############################################################################
##  If there's something to update, do the updates
if [ ${#needsUpdate[@]} -ne 0 ] && $update; then
 for i in ${needsUpdate[@]}; do
   echo -e "\nUpdating $i"
   if [ -d "android-$i" ]; then
     rsync -az --delete --exclude '*.git' ../prebuilts/gradle/$i/ ./android-$i/

     cd ./android-$i/

     git config user.name "google-automerger"
     git config user.email automerger@google.com

     git add .
     git status
     git commit -m "Auto-update"

     git remote set-url origin "https://$token@github.com/googlesamples/android-$i.git"
     git push origin master

     #overwrite remote url to not contain auth token
     git remote set-url origin "http://github.com/googlesamples/android-$i.git"

     cd ..
   else
     echoerr "Something went wrong when cloning $i - result directory does not exist.
Leaving temp files in place for further examination."
   deleteTemp=false;
  fi
 done
fi

#moves out of the temp folder, if we're in it.
if [ -d "../github-temp$folderPS" ]; then
   cd ..
fi

##############################################################################
##  If there's something new to upload, do the uploads
if [ ${#toUpload[@]} -ne 0 ] && $upload; then
 for i in ${toUpload[@]}; do
   echo -e "\nUploading $i"

   repoName="googlesamples/android-$i"

   CREATE="curl -H 'Authorization: token '$TOKEN \
     -d '{\"name\":\"android-'$i'\", \"team_id\":889859}' \
     https://api.github.com/orgs/googlesamples/repos"
   eval $CREATE

   #add secondary team permissions (robots)
   ADDTEAM="curl -X PUT \
     -H 'Authorization: token '$TOKEN \
     -H 'Content-Length: 0' \
     https://api.github.com/teams/889856/repos/$repoName"

   eval $ADDTEAM

   URL="https://$token@github.com/$repoName"

   cd $i
   git init
    #overrides .gitconfig just for this project - does not alter your global settings.
   git config user.name "google-automerger"
   git config user.email automerger@google.com
   git add .
   git commit -m "Initial Commit"
   git remote add origin $URL
   git push origin master
   cd ..


 done
fi

else
  echoerr "That token doesn't work. A test returned the code: $tokenTest"
fi

else
   echo "User cancelled Github update."
fi

fi #end of "is there something to do?" if statement