|
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 |
|
| 29 | +function get_jq () { |
| 30 | + # Get jq so we can parse some JSON, man. |
| 31 | + # Essential if we want to do anything with the GitHub API responses. |
| 32 | + local JQ_EXECUTABLE_URL="http://stedolan.github.io/jq/download/linux64/jq" |
| 33 | + |
| 34 | + echo "Fetching jq from ${JQ_EXECUTABLE_URL}" |
| 35 | + |
| 36 | + curl --silent --output "$FWDIR/dev/jq" "$JQ_EXECUTABLE_URL" |
| 37 | + local curl_status=$? |
| 38 | + |
| 39 | + if [ $curl_status -ne 0 ]; then |
| 40 | + echo "Failed to get jq." >&2 |
| 41 | + return $curl_status |
| 42 | + fi |
| 43 | + |
| 44 | + chmod u+x "$FWDIR/dev/jq" |
| 45 | +} |
| 46 | + |
27 | 47 | COMMENTS_URL="https://api.github.com/repos/apache/spark/issues/$ghprbPullId/comments"
|
| 48 | +PULL_REQUEST_URL="https://github.com/apache/spark/pull/$ghprbPullId" |
| 49 | + |
| 50 | +function post_message () { |
| 51 | + local message=$1 |
| 52 | + local data="{\"body\": \"$message\"}" |
| 53 | + local HTTP_CODE_HEADER="HTTP Response Code: " |
| 54 | + |
| 55 | + echo "Attempting to post to Github..." |
| 56 | + |
| 57 | + local curl_output=$( |
| 58 | + curl `#--dump-header -` \ |
| 59 | + --silent \ |
| 60 | + --user x-oauth-basic:$GITHUB_OAUTH_KEY \ |
| 61 | + --request POST \ |
| 62 | + --data "$data" \ |
| 63 | + --write-out "${HTTP_CODE_HEADER}%{http_code}\n" \ |
| 64 | + --header "Content-Type: application/json" \ |
| 65 | + "$COMMENTS_URL" #> /dev/null #| "$FWDIR/dev/jq" .id #| head -n 8 |
| 66 | + ) |
| 67 | + local curl_status=${PIPESTATUS[0]} |
| 68 | + |
| 69 | + if [ "$curl_status" -ne 0 ]; then |
| 70 | + echo "Failed to post message to GitHub." >&2 |
| 71 | + echo " > curl_status: ${curl_status}" >&2 |
| 72 | + echo " > curl_output: ${curl_output}" >&2 |
| 73 | + echo " > data: ${data}" >&2 |
| 74 | + # exit $curl_status |
| 75 | + fi |
| 76 | + |
| 77 | + local api_response=$( |
| 78 | + echo "${curl_output}" \ |
| 79 | + | grep -v -e "^${HTTP_CODE_HEADER}" |
| 80 | + ) |
| 81 | + |
| 82 | + local http_code=$( |
| 83 | + echo "${curl_output}" \ |
| 84 | + | grep -e "^${HTTP_CODE_HEADER}" \ |
| 85 | + | sed -r -e "s/^${HTTP_CODE_HEADER}//g" |
| 86 | + ) |
| 87 | + |
| 88 | + if [ -n "$http_code" ] && [ "$http_code" -ne "201" ]; then |
| 89 | + echo " > http_code: ${http_code}." >&2 |
| 90 | + echo " > api_response: ${api_response}" >&2 |
| 91 | + echo " > data: ${data}" >&2 |
| 92 | + fi |
| 93 | + |
| 94 | + if [ "$curl_status" -eq 0 ] && [ "$http_code" -eq "201" ]; then |
| 95 | + echo " > Post successful." |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +COMMIT_URL="https://github.com/apache/spark/commit/${ghprbActualCommit}" |
| 100 | +# GitHub doesn't auto-link short hashes when submitted via the API, unfortunately. :( |
| 101 | +short_commit_hash=${ghprbActualCommit:0:7} |
| 102 | + |
| 103 | +# check PR merge-ability and check for new public classes |
| 104 | +{ |
| 105 | + if [ "$sha1" == "$ghprbActualCommit" ]; then |
| 106 | + merge_note=" * This patch **does not** merge cleanly!" |
| 107 | + else |
| 108 | + merge_note=" * This patch merges cleanly." |
| 109 | + |
| 110 | + non_test_files=$(git diff master --name-only | grep -v "\/test" | tr "\n" " ") |
| 111 | + new_public_classes=$( |
| 112 | + git diff master ${non_test_files} `# diff this patch against master and...` \ |
| 113 | + | grep "^\+" `# filter in only added lines` \ |
| 114 | + | sed -r -e "s/^\+//g" `# remove the leading +` \ |
| 115 | + | grep -e "trait " -e "class " `# filter in lines with these key words` \ |
| 116 | + | grep -e "{" -e "(" `# filter in lines with these key words, too` \ |
| 117 | + | grep -v -e "\@\@" -e "private" `# exclude lines with these words` \ |
| 118 | + | grep -v -e "^// " -e "^/\*" -e "^ \* " `# exclude comment lines` \ |
| 119 | + | sed -r -e "s/\{.*//g" `# remove from the { onwards` \ |
| 120 | + | sed -r -e "s/\}//g" `# just in case, remove }; they mess the JSON` \ |
| 121 | + | sed -r -e "s/\"/\\\\\"/g" `# escape double quotes; they mess the JSON` \ |
| 122 | + | sed -r -e "s/^(.*)$/\`\1\`/g" `# surround with backticks for style` \ |
| 123 | + | sed -r -e "s/^/ \* /g" `# prepend ' *' to start of line` \ |
| 124 | + | sed -r -e "s/$/\\\n/g" `# append newline to end of line` \ |
| 125 | + | tr -d "\n" `# remove actual LF characters` |
| 126 | + ) |
28 | 127 |
|
29 |
| -function post_message { |
30 |
| - message=$1 |
31 |
| - data="{\"body\": \"$message\"}" |
32 |
| - echo "Attempting to post to Github:" |
33 |
| - echo "$data" |
| 128 | + if [ "$new_public_classes" == "" ]; then |
| 129 | + public_classes_note=" * This patch adds no public classes." |
| 130 | + else |
| 131 | + public_classes_note=" * This patch adds the following public classes _(experimental)_:" |
| 132 | + public_classes_note="${public_classes_note}\n${new_public_classes}" |
| 133 | + fi |
| 134 | + fi |
| 135 | +} |
34 | 136 |
|
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 |
| 137 | +# post start message |
| 138 | +{ |
| 139 | + start_message="\ |
| 140 | + [QA tests have started](${BUILD_URL}consoleFull) for \ |
| 141 | + PR $ghprbPullId at commit [\`${short_commit_hash}\`](${COMMIT_URL})." |
| 142 | + |
| 143 | + start_message="${start_message}\n${merge_note}" |
| 144 | + # start_message="${start_message}\n${public_classes_note}" |
| 145 | + |
| 146 | + post_message "$start_message" |
38 | 147 | }
|
39 | 148 |
|
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>" |
| 149 | +# run tests |
| 150 | +{ |
| 151 | + ./dev/run-tests |
| 152 | + test_result="$?" |
| 153 | + |
| 154 | + if [ "$test_result" -eq "0" ]; then |
| 155 | + test_result_note=" * This patch **passes** unit tests." |
75 | 156 | else
|
76 |
| - result_message="$result_message- This patch adds the following public classes (experimental):<br>" |
77 |
| - result_message="$result_message$new_public_classes" |
| 157 | + test_result_note=" * This patch **fails** unit tests." |
78 | 158 | fi
|
79 |
| -fi |
80 |
| -result_message="${result_message}<br>For more information see test ouptut:" |
81 |
| -result_message="${result_message}<br>${BUILD_URL}consoleFull" |
| 159 | +} |
82 | 160 |
|
83 |
| -post_message "$result_message" |
| 161 | +# post end message |
| 162 | +{ |
| 163 | + result_message="\ |
| 164 | + [QA tests have finished](${BUILD_URL}consoleFull) for \ |
| 165 | + PR $ghprbPullId at commit [\`${short_commit_hash}\`](${COMMIT_URL})." |
| 166 | + |
| 167 | + result_message="${result_message}\n${test_result_note}" |
| 168 | + result_message="${result_message}\n${merge_note}" |
| 169 | + result_message="${result_message}\n${public_classes_note}" |
| 170 | + |
| 171 | + post_message "$result_message" |
| 172 | +} |
84 | 173 |
|
85 | 174 | exit $test_result
|
0 commit comments