diff --git a/tools/build-pr/checkout b/tools/build-pr/checkout index e14b314f5a..f6e95043bc 100755 --- a/tools/build-pr/checkout +++ b/tools/build-pr/checkout @@ -15,26 +15,25 @@ if [[ "$BUILDPR" = "" ]]; then exit 0; fi echo "##[section] PR Build for #$BUILDPR" +get_pr_info api "pulls/$BUILDPR" - '.head.sha // error("no such PR")' > /dev/null -sha1="$(api "pulls/$BUILDPR" - '.head.sha')" -repo="$(api "pulls/$BUILDPR" - '.head.repo.full_name')" -ref="$(api "pulls/$BUILDPR" - '.head.ref')" -repourl="https://github.com/$repo" +repourl="https://github.com/$REPO" printf 'PR BUILD for #%s\n repo: %s\n ref: %s\n sha1: %s\n' \ - "$BUILDPR" "$repo" "$ref" "$sha1" + "$BUILDPR" "$REPO" "$REF" "$SHA1" git checkout "master" > /dev/null 2>&1 oldbr="$(git for-each-ref --format="%(refname:short)" "refs/heads/pr-*")" if [[ "x$oldbr" != "x" ]]; then git branch -D $oldbr; fi -text="$(jsonq "[A build has started.]($VURL)")" -api "issues/$BUILDPR/comments" -d '{"body":'"$text"'}' - '.id' > "$PRDIR/comment-id" +text="A build has started." +post_status "pending" "$text" +post_comment "$text" _get_T -git fetch "https://$T@github.com/$repo" "$ref:refs/heads/pr-$BUILDPR" +git fetch "https://$T@github.com/$REPO" "$REF:refs/heads/pr-$BUILDPR" git checkout "pr-$BUILDPR" -git reset --hard "$sha1" +git reset --hard "$SHA1" # useful info in build { echo "# This is a build for [github PR #$BUILDPR]($GURL)" @@ -42,17 +41,17 @@ git reset --hard "$sha1" echo "Associated Changes (actual ones)" echo ""; echo "---"; echo "" git log --format="* [%h]($repourl/commit/%H) [%aN](mailto:%aE) %s" \ - "origin/master..$sha1" + "origin/master..$SHA1" } > "$PRDIR/PR-Build.md" echo "##vso[task.uploadsummary]$PRDIR/PR-Build.md" # variable overrides prvar() { printf '%s=%q\n' "$1" "$2" >> "$TOOLSDIR/local-config.sh"; } -prvar BUILD_SOURCEVERSION "$sha1" -prvar BUILD_REPOSITORY_NAME "$repo" +prvar BUILD_SOURCEVERSION "$SHA1" +prvar BUILD_REPOSITORY_NAME "$REPO" prvar BUILD_REPOSITORY_ID "$repourl" prvar BUILD_REPOSITORY_URI "$repourl" -prvar BUILD_SOURCEBRANCH "refs/heads/$ref" -prvar BUILD_SOURCEBRANCHNAME "$(basename "$ref")" +prvar BUILD_SOURCEBRANCH "refs/heads/$REF" +prvar BUILD_SOURCEBRANCHNAME "$(basename "$REF")" prvar BUILD_SOURCEVERSIONAUTHOR "$(git log -1 --format="%aN <%aE>")" prvar BUILD_SOURCEVERSIONMESSAGE "$(git log -1 --format="%s")" diff --git a/tools/build-pr/report b/tools/build-pr/report index 0173e6573a..b5705e79e4 100755 --- a/tools/build-pr/report +++ b/tools/build-pr/report @@ -16,10 +16,10 @@ fi ICONS_URL="https://$MAIN_CONTAINER.blob.core.windows.net/icons" icon="$ICONS_URL/Robot" case "${AGENT_JOBSTATUS,,}" in - ( succeeded ) icon+="2.png"; box="![PASS]($icon) Pass" ;; - ( canceled ) icon+="1.png"; box="![CANCEL]($icon) Canceled" ;; - ( failed ) icon+="0.png"; box="![FAIL]($icon) Fail" ;; - ( * ) icon+="1.png"; box="![$AGENT_JOBSTATUS]($icon) Unknown" ;; + ( succeeded ) icon+="2.png"; box="![PASS]($icon) Pass"; state="success" ;; + ( canceled ) icon+="1.png"; box="![CANCEL]($icon) Canceled"; state="error" ;; + ( failed ) icon+="0.png"; box="![FAIL]($icon) Fail"; state="failure" ;; + ( * ) icon+="1.png"; box="![$AGENT_JOBSTATUS]($icon) Unknown"; state="error" ;; esac if [[ "$BUILDPR" = "" ]]; then @@ -29,11 +29,7 @@ if [[ "$BUILDPR" = "" ]]; then exit fi -if [[ ! -r "$PRDIR/comment-id" ]]; then exit; fi - -cid="$(< "$PRDIR/comment-id")" - -api "issues/comments/$cid" -X DELETE - -text="$(jsonq "[$box! The build has ${AGENT_JOBSTATUS,,}.]($VURL)")" -api "issues/$BUILDPR/comments" -d '{"body":'"$text"'}' - '.id' > "$PRDIR/comment-id" +text="The build has ${AGENT_JOBSTATUS,,}." +post_status "$state" "$text" +delete_comment +post_comment "$box! — $text" diff --git a/tools/build-pr/shared.sh b/tools/build-pr/shared.sh index 09875b3147..f15d5fd834 100644 --- a/tools/build-pr/shared.sh +++ b/tools/build-pr/shared.sh @@ -44,4 +44,33 @@ jsonq() { # text...; quotes the text as a json string VURL="${SYSTEM_TASKDEFINITIONSURI%/}/$SYSTEM_TEAMPROJECT" VURL+="/_build/index?buildId=$BUILD_BUILDID&_a=summary" -GURL="$(api "pulls/$BUILDPR" - '.html_url')" +GURL="" SHA1="" REPO="" REF="" + +get_pr_info() { + if [[ "$SHA1" != "" ]]; then return; fi + SHA1="$(api "pulls/$BUILDPR" - '.head.sha')" + REPO="$(api "pulls/$BUILDPR" - '.head.repo.full_name')" + REF="$(api "pulls/$BUILDPR" - '.head.ref')" + GURL="$(api "pulls/$BUILDPR" - '.html_url')" +} + +# post a status, only if we're running all tests +post_status() { + if [[ "$TESTS" != "all" ]]; then return; fi + get_pr_info + local status='"context":"build-pr","state":"'"$1"'","target_url":"'"$VURL"'"' + api "statuses/$SHA1" -d '{'"$status"',"description":"'"$2"'"}' > /dev/null +} + +# post a comment with the given text and link to the build; remember its id +post_comment() { + local text="$(jsonq "[$1]($VURL)")" + api "issues/$BUILDPR/comments" -d '{"body":'"$text"'}' - '.id' \ + > "$PRDIR/comment-id" +} + +# delete the last posted comment +delete_comment() { + if [[ ! -r "$PRDIR/comment-id" ]]; then return; fi + api "issues/comments/$(< "$PRDIR/comment-id")" -X DELETE +}