Skip to content

Commit

Permalink
Problem: using issue-tracking from the command line
Browse files Browse the repository at this point in the history
It's hard to use `sit` CLI to operate issue tracking
as it would get pretty involved.

Solution: use sit modules + CLI functionality to expose
`sit issue` and other commands.
  • Loading branch information
yrashk committed Apr 12, 2018
1 parent 50633da commit d429957
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cli/sit-issue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

dir=$(dirname $0)
cli=$(cat "${dir}/sit-issue.yaml")

output=$(echo "${cli}" | ${SIT} args -- "sit issue" $*)
command=$(echo "${output}" | head -1)
matches=$(echo "${output}" | tail -n +2)

case ${command} in
list)
query=$(${SIT} config -q "'issue-tracking'.cli.oneliner || 'join(\' | \', [id, summary])'" repository)
exec ${SIT} items -q "${query}"
;;
show)
while true; do
next_line=$(echo ${matches} | head -1)
matches=$(echo ${matches} | tail -n +2)
read -r arg occurrences indices values < <(echo ${next_line})
if [ "${arg}" == "ID" ]; then
echo -n "Summary: "
eval ${SIT} reduce -q "summary" ${values}
echo "Details:"
echo
eval ${SIT} reduce -q "details" ${values}
echo
echo -n "Created at "
eval ${SIT} reduce -q "timestamp" ${values}
echo "by "
eval ${SIT} reduce -q "authors" ${values}
exit 0
fi
if [ "${matches}" == '' ]; then
exit 0
fi
done
;;
*)
echo "${cli}" | ${SIT} args --help;
exit 0
;;

esac
3 changes: 3 additions & 0 deletions cli/sit-issue.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
set script=%~dp0%\sit-issue.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%script%' %*";
32 changes: 32 additions & 0 deletions cli/sit-issue.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$cli = Get-Content -Raw -Path "$scriptPath\sit-issue.yaml"
$output = ($cli | & $Env:SIT args -- "sit issue" $args) -split '\n'
$command = $output[0]
$matched = $output[1..($output.Length - 1)]
switch -Regex ($command) {
'^list'
{
$query = & $Env:SIT config -q "'issue-tracking'.cli.oneliner || 'join(\' | \', [id, summary])'" repository
& $Env:SIT items -q $query
}
'^show' {
switch -Regex ($matched) {
'^ID\s+\d+\s+\d+\s(.+)' {
$ID = Invoke-Expression $Matches[1]
Write-Host -NoNewline "Summary: "
& $Env:SIT reduce -q summary $ID
Write-Host Details:
Write-Host
& $Env:SIT reduce -q details $ID
Write-Host
Write-Host Created at
& $Env:SIT reduce -q timestamp $ID
Write-Host by
& $Env:SIT reduce -q authors $ID
}
}
}
default {
$cli | & $Env:SIT args --help
}
}
14 changes: 14 additions & 0 deletions cli/sit-issue.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: SIT Issue Tracking
version: "0.3.0"
settings: [ColoredHelp, ColorAuto, DisableHelpSubcommand]
subcommands:
- list:
- show:
about: Show issue
args:
- ID:
required: true
takes_value: true
index: 1
- help:

86 changes: 86 additions & 0 deletions cli/sit-mr
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env bash

branch=$1
item=$2
attach=${item}
tmpdir="$(mktemp -d)"
curdir=$(pwd)
editor=${EDITOR:-nano}
range=$(git merge-base --fork-point master "${branch}").."${branch}"
curbranch=$(git rev-parse --abbrev-ref HEAD)
sit=${SIT}

exit

run() {
if [ -z "${branch}" ]; then
echo "Usage: sit mr <branch> [<item>]"
exit 1
fi

git format-patch "${range}" -o "${tmpdir}/git" || exit 2


if [ -z "${attach}" ]; then
# Summary
echo " Summary of the merge request (typically, one line)" > "${tmpdir}/summary"
echo " If your patchset contained more than one commit, you will see a few options below. Please choose one." >> "${tmpdir}/summary"
echo " (ALL LINES STARTING WITH '#' WILL BE REMOVED)" >> "${tmpdir}/summary"
git log --reverse --pretty=format:"%s" "${range}" >> "${tmpdir}/summary" || exit 2
sed -i 's/^/#/' "${tmpdir}/summary"
sed -i '4s/^#//' "${tmpdir}/summary"
$editor "${tmpdir}/summary" || exit 2
sed -i '/^#/ d' "${tmpdir}/summary"

# Details
echo "# Details of the merge request" >> "${tmpdir}/details"
echo "# (ALL LINES STARTING WITH '#' WILL BE REMOVED)" >> "${tmpdir}/details"
git log --reverse --pretty=format:"%b%n" "${range}" >> "${tmpdir}/details" || exit 2
$editor "${tmpdir}/details" || exit 2
sed -i '/^#/ d' "${tmpdir}/details"

item=$(${sit} item)
item_branch=${item}
else
# Comment
echo "# Comment to accompany your merge request" >> "${tmpdir}/text"
echo "# (ALL LINES STARTING WITH '#' WILL BE REMOVED)" >> "${tmpdir}/text"
git log --reverse --pretty=format:"%s%n%n%b" "${range}" >> "${tmpdir}/text" || exit 2
$editor "${tmpdir}/text" || exit 2
sed -i '/^#/ d' "${tmpdir}/text"

last_record=$(${sit} records ${item} | tail -1)
item_branch="${item}-${last_record}-$(date +%s)"
fi

git checkout -b "${item_branch}" master || exit 2

pushd "$(pwd)" >/dev/null || exit 2
cd "${tmpdir}" || exit 2

if [ -z "${attach}" ]; then
mv -f summary text
"${sit}" -d "${curdir}" record -t SummaryChanged "${item}" text || exit 2

mv -f details text
"${sit}" -d "${curdir}" record -t DetailsChanged,MergeRequested "${item}" text git/*.patch || exit 2
else
"${sit}" -d "${curdir}" record -t Commented,MergeRequested "${item}" text git/*.patch || exit 2
fi

popd >/dev/null || exit 2

rm -rf "${tmpdir}"

git add .sit/items/"${item}" || exit 2
git commit -m "Added merge request: ${item}" || exit 2
git checkout "${curbranch}" || exit 2

if [ -f ${SIT_DIR}/cli/mr-send-upstream ]; then
source ${SIT_DIR}/cli/mr-send-upstream ${item_branch}
else
echo "${item_branch}"
fi
}

run
66 changes: 66 additions & 0 deletions cli/sit-mr-compare
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash

root=$(git rev-parse --show-toplevel)
currev=$(git rev-parse HEAD)
rev=$1

if [ -z "${rev}" ]; then
echo "Usage: sit mr-compare REV"
exit 1
fi

rename () {
case "$1" in
open*)
echo -n " "
;;
close*)
echo -n ""
;;
none*)
echo -n "-"
;;
esac
}

sit=${SIT}

if [ -z "${sit}" ]; then
echo "sit not found"
exit 1
fi

sit_path=$(${sit} path)
rsit_path=$(realpath "${sit_path}" --relative-to="${root}")

tmpdir=$(mktemp -d)

git clone "${root}" "${tmpdir}" >/dev/null 2>/dev/null

items=$(git -C "${tmpdir}" diff ${currev}..${rev} --name-only | grep -e "^${rsit_path/./\\.}/items" | cut -d/ -f3 | uniq)

git -C "${tmpdir}" checkout -qf "${rev}"

for item in ${items}; do
mr_p="merge_requests != null"
mr=$(${sit} -r "${sit_path}" reduce "${item}" -q "${mr_p}" 2>/dev/null || echo)
mr1=$(${sit} -r "${tmpdir}/${rsit_path}" reduce "${item}" -q "${mr_p}" 2>/dev/null || echo)
mr=${mr:-${mr1}}

if [ "${mr}" == "true" ]; then
ours=$(${sit} -r "${sit_path}" reduce "${item}" -q state 2>/dev/null || echo none)
theirs=$(${sit} -r "${tmpdir}/${rsit_path}" reduce "${item}" -q state 2>/dev/null || echo none)
summary=$(${sit} -r "${sit_path}" reduce "${item}" -q summary)
summary1=$(${sit} -r "${sit_path}" reduce "${item}" -q summary)
summary=${summary:-${summary1}}
if [ "${ours}" == "${theirs}" ]; then
true
else
ours=$(rename ${ours})
theirs=$(rename ${theirs})
echo "${item} || ${summary} || [${ours}] || [${theirs}]"
fi
fi
done

rm -rf "${tmpdir}"
53 changes: 53 additions & 0 deletions cli/sit-mr-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

item=$1
mr=$2
comment=$3
tmpdir="$(mktemp -d)"
curdir=$(pwd)
editor=${EDITOR:-nano}
range=$(git merge-base --fork-point master $branch)..$branch
curbranch=$(git rev-parse --abbrev-ref HEAD)
sit=${SIT}

if [ -z ${item} ]; then
echo "Usage: sit mr-merge <item> <mr> [comment]"
exit 1
fi

if [ -z ${mr} ]; then
echo "Usage: sit mr-merge <item> <mr> [comment]"
exit 1
fi

repo=$(${sit} path)

git checkout -b merge-${item}-${mr} || exit 2

git am $(ls ${repo}/items/${item}/${mr}/git/*.patch) || exit 2


pushd $(pwd) >/dev/null
cd "${tmpdir}"

echo "${mr}" > record
if [ -z "${comment}" ]; then
${sit} -d ${curdir} record -t Merged,Closed ${item} record || exit 2
else
echo "# Leave a comment below. All lines starting with # will be removed" > text
echo "${comment}" >> text
$editor text || exit 2
sed -i '/^#/ d' text
${sit} -d ${curdir} record -t Commented,Merged,Closed ${item} text record || exit 2
fi

popd >/dev/null

rm -rf "${tmpdir}"

git add .sit/items/${item} || exit 2
git commit -m "Closed merge request ${item}/${mr}" || exit 2
git checkout ${curbranch} || exit 2
git merge --no-ff merge-${item}-${mr}

echo You can push to upstream now

0 comments on commit d429957

Please sign in to comment.