Skip to content

Commit

Permalink
Improve the PR builds: post a github status
Browse files Browse the repository at this point in the history
  • Loading branch information
elibarzilay committed Jun 12, 2017
1 parent 306ca38 commit ef01ba3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
27 changes: 13 additions & 14 deletions tools/build-pr/checkout
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,43 @@ 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)"
echo ""
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")"
20 changes: 8 additions & 12 deletions tools/build-pr/report
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
31 changes: 30 additions & 1 deletion tools/build-pr/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ef01ba3

Please sign in to comment.