|
19 | 19 |
|
20 | 20 | # Wrapper script that runs the Spark tests then reports QA results
|
21 | 21 | # to github via its API.
|
| 22 | +# Environment variables are populated by the code here: |
| 23 | +#+ https://github.com/jenkinsci/ghprb-plugin/blob/master/src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java#L139 |
22 | 24 |
|
23 | 25 | # Go to the Spark project root directory
|
24 | 26 | FWDIR="$(cd `dirname $0`/..; pwd)"
|
25 | 27 | cd "$FWDIR"
|
26 | 28 |
|
27 | 29 | COMMENTS_URL="https://api.github.com/repos/apache/spark/issues/$ghprbPullId/comments"
|
| 30 | +PULL_REQUEST_URL="https://github.com/apache/spark/pull/$ghprbPullId" |
28 | 31 |
|
29 |
| -function post_message { |
30 |
| - message=$1 |
31 |
| - data="{\"body\": \"$message\"}" |
32 |
| - echo "Attempting to post to Github:" |
33 |
| - echo "$data" |
| 32 | +COMMIT_URL="https://github.com/apache/spark/commit/${ghprbActualCommit}" |
| 33 | +# GitHub doesn't auto-link short hashes when submitted via the API, unfortunately. :( |
| 34 | +SHORT_COMMIT_HASH="${ghprbActualCommit:0:7}" |
34 | 35 |
|
35 |
| - curl -D- -u x-oauth-basic:$GITHUB_OAUTH_KEY -X POST --data "$data" -H \ |
36 |
| - "Content-Type: application/json" \ |
37 |
| - $COMMENTS_URL | head -n 8 |
| 36 | +TESTS_TIMEOUT="120m" # format: http://linux.die.net/man/1/timeout |
| 37 | + |
| 38 | +function post_message () { |
| 39 | + local message=$1 |
| 40 | + local data="{\"body\": \"$message\"}" |
| 41 | + local HTTP_CODE_HEADER="HTTP Response Code: " |
| 42 | + |
| 43 | + echo "Attempting to post to Github..." |
| 44 | + |
| 45 | + local curl_output=$( |
| 46 | + curl `#--dump-header -` \ |
| 47 | + --silent \ |
| 48 | + --user x-oauth-basic:$GITHUB_OAUTH_KEY \ |
| 49 | + --request POST \ |
| 50 | + --data "$data" \ |
| 51 | + --write-out "${HTTP_CODE_HEADER}%{http_code}\n" \ |
| 52 | + --header "Content-Type: application/json" \ |
| 53 | + "$COMMENTS_URL" #> /dev/null #| "$FWDIR/dev/jq" .id #| head -n 8 |
| 54 | + ) |
| 55 | + local curl_status=${PIPESTATUS[0]} |
| 56 | + |
| 57 | + if [ "$curl_status" -ne 0 ]; then |
| 58 | + echo "Failed to post message to GitHub." >&2 |
| 59 | + echo " > curl_status: ${curl_status}" >&2 |
| 60 | + echo " > curl_output: ${curl_output}" >&2 |
| 61 | + echo " > data: ${data}" >&2 |
| 62 | + # exit $curl_status |
| 63 | + fi |
| 64 | + |
| 65 | + local api_response=$( |
| 66 | + echo "${curl_output}" \ |
| 67 | + | grep -v -e "^${HTTP_CODE_HEADER}" |
| 68 | + ) |
| 69 | + |
| 70 | + local http_code=$( |
| 71 | + echo "${curl_output}" \ |
| 72 | + | grep -e "^${HTTP_CODE_HEADER}" \ |
| 73 | + | sed -r -e "s/^${HTTP_CODE_HEADER}//g" |
| 74 | + ) |
| 75 | + |
| 76 | + if [ -n "$http_code" ] && [ "$http_code" -ne "201" ]; then |
| 77 | + echo " > http_code: ${http_code}." >&2 |
| 78 | + echo " > api_response: ${api_response}" >&2 |
| 79 | + echo " > data: ${data}" >&2 |
| 80 | + fi |
| 81 | + |
| 82 | + if [ "$curl_status" -eq 0 ] && [ "$http_code" -eq "201" ]; then |
| 83 | + echo " > Post successful." |
| 84 | + fi |
38 | 85 | }
|
39 | 86 |
|
40 |
| -start_message="QA tests have started for PR $ghprbPullId." |
41 |
| -if [ "$sha1" == "$ghprbActualCommit" ]; then |
42 |
| - start_message="$start_message This patch DID NOT merge cleanly! " |
43 |
| -else |
44 |
| - start_message="$start_message This patch merges cleanly. " |
45 |
| -fi |
46 |
| -start_message="$start_message<br>View progress: " |
47 |
| -start_message="$start_message${BUILD_URL}consoleFull" |
48 |
| - |
49 |
| -post_message "$start_message" |
50 |
| - |
51 |
| -./dev/run-tests |
52 |
| -test_result="$?" |
53 |
| - |
54 |
| -result_message="QA results for PR $ghprbPullId:<br>" |
55 |
| - |
56 |
| -if [ "$test_result" -eq "0" ]; then |
57 |
| - result_message="$result_message- This patch PASSES unit tests.<br>" |
58 |
| -else |
59 |
| - result_message="$result_message- This patch FAILED unit tests.<br>" |
60 |
| -fi |
61 |
| - |
62 |
| -if [ "$sha1" != "$ghprbActualCommit" ]; then |
63 |
| - result_message="$result_message- This patch merges cleanly<br>" |
64 |
| - non_test_files=$(git diff master --name-only | grep -v "\/test" | tr "\n" " ") |
65 |
| - new_public_classes=$(git diff master $non_test_files \ |
66 |
| - | grep -e "trait " -e "class " \ |
67 |
| - | grep -e "{" -e "(" \ |
68 |
| - | grep -v -e \@\@ -e private \ |
69 |
| - | grep \+ \ |
70 |
| - | sed "s/\+ *//" \ |
71 |
| - | tr "\n" "~" \ |
72 |
| - | sed "s/~/<br>/g") |
73 |
| - if [ "$new_public_classes" == "" ]; then |
74 |
| - result_message="$result_message- This patch adds no public classes<br>" |
| 87 | +# check PR merge-ability and check for new public classes |
| 88 | +{ |
| 89 | + if [ "$sha1" == "$ghprbActualCommit" ]; then |
| 90 | + merge_note=" * This patch **does not** merge cleanly!" |
75 | 91 | else
|
76 |
| - result_message="$result_message- This patch adds the following public classes (experimental):<br>" |
77 |
| - result_message="$result_message$new_public_classes" |
| 92 | + merge_note=" * This patch merges cleanly." |
| 93 | + |
| 94 | + source_files=$( |
| 95 | + git diff master --name-only \ |
| 96 | + | grep -v -e "\/test" `# ignore files in test directories` \ |
| 97 | + | grep -e "\.py$" -e "\.java$" -e "\.scala$" `# include only code files` \ |
| 98 | + | tr "\n" " " |
| 99 | + ) |
| 100 | + new_public_classes=$( |
| 101 | + git diff master ${source_files} `# diff this patch against master and...` \ |
| 102 | + | grep "^\+" `# filter in only added lines` \ |
| 103 | + | sed -r -e "s/^\+//g" `# remove the leading +` \ |
| 104 | + | grep -e "trait " -e "class " `# filter in lines with these key words` \ |
| 105 | + | grep -e "{" -e "(" `# filter in lines with these key words, too` \ |
| 106 | + | grep -v -e "\@\@" -e "private" `# exclude lines with these words` \ |
| 107 | + | grep -v -e "^// " -e "^/\*" -e "^ \* " `# exclude comment lines` \ |
| 108 | + | sed -r -e "s/\{.*//g" `# remove from the { onwards` \ |
| 109 | + | sed -r -e "s/\}//g" `# just in case, remove }; they mess the JSON` \ |
| 110 | + | sed -r -e "s/\"/\\\\\"/g" `# escape double quotes; they mess the JSON` \ |
| 111 | + | sed -r -e "s/^(.*)$/\`\1\`/g" `# surround with backticks for style` \ |
| 112 | + | sed -r -e "s/^/ \* /g" `# prepend ' *' to start of line` \ |
| 113 | + | sed -r -e "s/$/\\\n/g" `# append newline to end of line` \ |
| 114 | + | tr -d "\n" `# remove actual LF characters` |
| 115 | + ) |
| 116 | + |
| 117 | + if [ "$new_public_classes" == "" ]; then |
| 118 | + public_classes_note=" * This patch adds no public classes." |
| 119 | + else |
| 120 | + public_classes_note=" * This patch adds the following public classes _(experimental)_:" |
| 121 | + public_classes_note="${public_classes_note}\n${new_public_classes}" |
| 122 | + fi |
78 | 123 | fi
|
79 |
| -fi |
80 |
| -result_message="${result_message}<br>For more information see test ouptut:" |
81 |
| -result_message="${result_message}<br>${BUILD_URL}consoleFull" |
| 124 | +} |
82 | 125 |
|
83 |
| -post_message "$result_message" |
| 126 | +# post start message |
| 127 | +{ |
| 128 | + start_message="\ |
| 129 | + [QA tests have started](${BUILD_URL}consoleFull) for \ |
| 130 | + PR $ghprbPullId at commit [\`${SHORT_COMMIT_HASH}\`](${COMMIT_URL})." |
| 131 | + |
| 132 | + start_message="${start_message}\n${merge_note}" |
| 133 | + # start_message="${start_message}\n${public_classes_note}" |
| 134 | + |
| 135 | + post_message "$start_message" |
| 136 | +} |
| 137 | + |
| 138 | +# run tests |
| 139 | +{ |
| 140 | + timeout "${TESTS_TIMEOUT}" ./dev/run-tests |
| 141 | + test_result="$?" |
| 142 | + |
| 143 | + if [ "$test_result" -eq "124" ]; then |
| 144 | + fail_message="**[Tests timed out](${BUILD_URL}consoleFull)** after \ |
| 145 | + a configured wait of \`${TESTS_TIMEOUT}\`." |
| 146 | + post_message "$fail_message" |
| 147 | + exit $test_result |
| 148 | + else |
| 149 | + if [ "$test_result" -eq "0" ]; then |
| 150 | + test_result_note=" * This patch **passes** unit tests." |
| 151 | + else |
| 152 | + test_result_note=" * This patch **fails** unit tests." |
| 153 | + fi |
| 154 | + fi |
| 155 | +} |
| 156 | + |
| 157 | +# post end message |
| 158 | +{ |
| 159 | + result_message="\ |
| 160 | + [QA tests have finished](${BUILD_URL}consoleFull) for \ |
| 161 | + PR $ghprbPullId at commit [\`${SHORT_COMMIT_HASH}\`](${COMMIT_URL})." |
| 162 | + |
| 163 | + result_message="${result_message}\n${test_result_note}" |
| 164 | + result_message="${result_message}\n${merge_note}" |
| 165 | + result_message="${result_message}\n${public_classes_note}" |
| 166 | + |
| 167 | + post_message "$result_message" |
| 168 | +} |
84 | 169 |
|
85 | 170 | exit $test_result
|
0 commit comments