|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -trap 'exit 1' ERR |
4 | | -bin/build-jq.sh |
| 3 | +contains_setup_file() { |
| 4 | + local files=$1 |
| 5 | + for file in $files; do |
| 6 | + if [[ $file == *.gradle || $file == *.sh || $file == config.json ]]; then |
| 7 | + return 0 |
| 8 | + fi |
| 9 | + done |
| 10 | + return 1 |
| 11 | +} |
| 12 | + |
| 13 | +contains_exercise() { |
| 14 | + local files=$1 |
| 15 | + for file in $files; do |
| 16 | + if [[ $file == exercises* ]]; then |
| 17 | + return 0 |
| 18 | + fi |
| 19 | + done |
| 20 | + return 1 |
| 21 | +} |
5 | 22 |
|
6 | | -pr_files_json=`curl -s https://api.github.com/repos/exercism/java/pulls/${TRAVIS_PULL_REQUEST}/files` |
| 23 | +run_journey_test_with_modified_exercises() { |
| 24 | + local modded_files=$1 |
| 25 | + local last_modded_exercise="" |
| 26 | + local modded_exercises="" |
7 | 27 |
|
8 | | -echo "Pull request number: ${TRAVIS_PULL_REQUEST}" |
9 | | -echo "Changes in pr json: ${pr_files_json}" |
| 28 | + for file in $modded_files; do |
| 29 | + if [[ $file == exercises* ]] && [[ $file != exercises/settings.gradle ]] && [[ $file != exercises/build.gradle ]]; then |
| 30 | + local modded_exercise=${file#exercises/} |
| 31 | + modded_exercise=${modded_exercise%%/*} |
| 32 | + if [[ $last_modded_exercise != $modded_exercise ]]; then |
| 33 | + modded_exercises=$modded_exercises$modded_exercise$'\n' |
| 34 | + fi |
| 35 | + last_modded_exercise=$modded_exercise |
| 36 | + fi |
| 37 | + done |
10 | 38 |
|
11 | | -# if jq fails to get the required data, then that means TRAVIS_PULL_REQUEST was not set (not run in travis-ci), |
12 | | -# or was false (not a pull request). In that case, we should fall back with testing every exercise |
| 39 | + echo "Running journey test with modified exercise(s): ${modded_exercises}" |
| 40 | + bin/journey-test.sh $modded_exercises |
| 41 | +} |
13 | 42 |
|
14 | | -modded_files=`echo $pr_files_json | bin/jq -r '.[].filename'` || bin/journey-test.sh |
| 43 | +run_journey_test_with_all_exercises() { |
| 44 | + echo "Running journey test with all exercises" |
| 45 | + bin/journey-test.sh |
| 46 | +} |
15 | 47 |
|
16 | | -for file in $modded_files |
17 | | - do if [[ $file == exercises* ]] || [[ $file == config.json ]] |
18 | | - then |
19 | | - for file2 in $modded_files |
20 | | - do if [[ $file2 == exercises* ]] && [[ $file2 != exercises/settings.gradle ]] && [[ $file2 != exercises/build.gradle ]] |
21 | | - then modded_exercise=${file2#exercises/} |
22 | | - modded_exercise=${modded_exercise%%/*} |
23 | | - if [[ $last_modded_exercise != $modded_exercise ]] |
24 | | - then modded_exercises=$modded_exercises$modded_exercise$'\n' |
25 | | - fi |
26 | | - last_modded_exercise=$modded_exercise |
27 | | - fi |
28 | | - done |
29 | | - bin/journey-test.sh $modded_exercises |
30 | | - break |
| 48 | +main() { |
| 49 | + bin/build-jq.sh |
| 50 | + |
| 51 | + local pr_files_json=`curl -s https://api.github.com/repos/exercism/java/pulls/${TRAVIS_PULL_REQUEST}/files` |
| 52 | + |
| 53 | + echo "Pull request number: ${TRAVIS_PULL_REQUEST}" |
| 54 | + echo "Changes in pr json: ${pr_files_json}" |
| 55 | + |
| 56 | + # if jq fails to get the required data, then that means TRAVIS_PULL_REQUEST was not set (not run in travis-ci), |
| 57 | + # or was false (not a pull request), or the api limit was reached, or some other error occurred. |
| 58 | + # In that case, we should fall back with testing every exercise |
| 59 | + local pr_files_json_type=`echo $pr_files_json | bin/jq -r 'type'` |
| 60 | + if [[ $pr_files_json_type != "array" ]]; then |
| 61 | + echo "Didn't get pr changes from travis" |
| 62 | + run_journey_test_with_all_exercises |
| 63 | + return |
31 | 64 | fi |
32 | | -done |
| 65 | + |
| 66 | + local modded_files=`echo $pr_files_json | bin/jq -r '.[].filename'` |
| 67 | + |
| 68 | + # If the changed files contain a .sh file or .gradle file or config.json then we should run all the exercises |
| 69 | + if contains_setup_file "${modded_files}"; then |
| 70 | + echo "Pr changes contain setup file(s): ${modded_files}" |
| 71 | + run_journey_test_with_all_exercises |
| 72 | + return |
| 73 | + fi |
| 74 | + |
| 75 | + if contains_exercise "${modded_files}"; then |
| 76 | + echo "Pr changes contain modified exercise file(s)" |
| 77 | + run_journey_test_with_modified_exercises "${modded_files}" |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | +trap 'exit 1' ERR |
| 82 | +main |
0 commit comments