#!/bin/sh
#
# Find the latest version at:
#   https://evilham.com/en/blog/2020-phabricator/batch-audit/phabaudit.sh
# With the accompanying post:
#   https://evilham.com/en/blog/2020-phabricator/batch-audit/
# 
# Copyright 2020 Evilham <code@evilham.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

#
# Process environment variables.
#
# User may provide CONDUIT_ARGS or PHABRICATOR_INSTANCE
# If PHABRICATOR_INSTANCE is passed, CONDUIT_ARGS will be adapted so that
# arc call-conduit always uses: --conduit-uri ${PHABRICATOR_INSTANCE}
#
# If CONDUIT_ARGS is passed, it will be used verbatim (modulo appending
# --conduit-uri as explained before).
if [ -n "${PHABRICATOR_INSTANCE}" ]; then
	CONDUIT_ARGS="${CONDUIT_ARGS} --conduit-uri '${PHABRICATOR_INSTANCE}'"
fi

# Temp dir for the audit diffs
TMP_DIR=$(mktemp -d -t audits)

get_audits()
{
	echo '{"queryKey": "active", "order": "oldest"}' | \
		arc call-conduit ${CONDUIT_ARGS} diffusion.commit.search
}

filter_commit_data()
{
	jq -c '.response.data[] | 
		{
			"phid": .phid,
			"commit": .fields.identifier,
			"repository": .fields.repositoryPHID
		}'
}

get_diff_phid()
{
	jq -c '{"commit": .commit, "repository": .repository}' | \
		arc call-conduit ${CONDUIT_ARGS} diffusion.rawdiffquery | \
		jq -r .response.filePHID
}

save_diff()
{
	diff_phid="${1}"
	# Download the diff to disk
	echo "{\"phid\": \"${diff_phid}\"}" | \
		arc call-conduit ${CONDUIT_ARGS} \
			file.download | \
		jq -r .response | \
		b64decode -r > "${TMP_DIR}/${diff_phid}.diff"
	# Return path to diff in stdout
	echo "${TMP_DIR}/${diff_phid}.diff"
}

get_phid_url()
{
	jq -c '{"names": [.phid]}' | \
		arc call-conduit ${CONDUIT_ARGS} \
			phid.lookup | \
		jq -r '.response | to_entries | .[] | .value.uri'
}

accept_changes()
{
	jq -c '{
		"objectIdentifier": .phid,
		"transactions": [{"type": "accept", "value": true}]
	}' | \
		arc call-conduit ${CONDUIT_ARGS} \
			diffusion.commit.edit
}

for audit in $(get_audits | filter_commit_data); do
	audit_url="$(echo "${audit}" | get_phid_url)"
	printf "\n\nAudit URL: ${audit_url}\n"
	diff_phid="$(echo "${audit}" | get_diff_phid)"
	diff_file="$(save_diff "${diff_phid}")"
	# Open in editor for easy review
	${EDITOR} "${diff_file}"
	# Ask for feedback as necessary
	echo -n "Do you accept this change? [Y/n] "
	read accept
	if [ -z "${accept}" ] || [ "${accept}" != "${accept#[Yy]}" ]; then
		# Close audit 
		echo "${audit}" | accept_changes
	fi
done
