Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/review made easy #477

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ nbactions.xml
# Local environment
.env

# Direnv
.envrc

#JReleaser
out/

Expand All @@ -47,3 +50,7 @@ out/
/*.out
out_expected.txt
/*-timing.json

# 1BRC test-unique output
/logs
/results
19 changes: 19 additions & 0 deletions create_measurements_unique.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
#
# Copyright 2023 The original authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


java --class-path target/average-1.0.0-SNAPSHOT.jar dev.morling.onebrc.CreateMeasurementsUnique $1
4 changes: 4 additions & 0 deletions reset-sdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk e install
123 changes: 123 additions & 0 deletions scripts/review/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
= Code review made easy
:toc: left
:icons: font
:branch: feature/review-made-easy

If you want to perform a local review of a GitHub pull-request, these scripts will help (checkout) prepare, test and rollback the code.

== Pre-requisites

Make sure the following assumptions are true

. You can access GitHub via SSH (cf. GitHub docs)
. You have the `gh` CLI tool installed (e.g., via `brew install gh` on macOS)
. You have a current version of the original repository cloned locally on your machine (e.g., by `git clone [email protected]:gunnarmorling/1brc`)
. You perform all future steps from this directory (i.e., `cd 1brc` in the beginning)

== Preparation

=== Copy scripts (mandatory for misc. use cases)

As long as the code is not merged, you perhaps need to copy the script files into your local directory (depending on the work mode).
This is necessary since they may vanish when you switch tasks.

[NOTE]
====
Once it is merged, or if you work on a subbranch of the current development branch, you may skip this step.
If you want to use the current branch for these changes as baseline, we recommend to set the environment variable: `export REVIEW_BASE_BRANCH={branch}` (or whatever your local branch is named).
====

Use the latest version from my repository

[source, bash, subs='normal']
----
git remote add ascheman [email protected]:ascheman/1brc
git checkout {branch}
----

[[prepare-link]]
Copy (or better link) the files to the base directory.

[source, bash]
.Copy/Link scripts to local directory
----
ln scripts/review/prepare.sh ./review-prepare.sh
ln scripts/review/rollback.sh ./review-rollback.sh
----

[[sec:environment]]
=== Environment variables

Set the environment variable +`${EDITOR}`+ to your favored editor, e.g., `vim` on Linux.
It enables to review source code later in this application.

[TIP]
.Use IntelliJ (or other IDE)
====
If you use IntelliJ, you can point the editor variable to the IntelliJ executable.
If you run the subsequent tasks from an existing IntelliJ project, it will then open the files in the IDE then.
This should work with other IDEs as well, e.g., Visual Studio Code.

IntelliJ IDEA may install a convenience wrapper script `idea` directly to `/usr/local/bin` (or some other directory at your disposal).
If you have that directory on your +`${PATH}`+, you may just set `export EDITOR=idea`.
====

== Usage

=== Show existing PRs

Check the open PRs

[script, bash]
----
gh pr list
----

=== Check out PR

[CAUTION]
====
The following may only work on open (or draft) PRs.
====

Pick one of the open PRs and call the _prepare_ script.

[source, bash]
----
./review-prepare.sh <pr> # <1>
----
<1> Depending on whether you have executed the <<prepare-link,link prepare>> step (otherwise you my use `./scripts/review/prepare.sh` instead).

The script will perform the following steps

. Fetch repository for the PR
. Prepare merge of PR branch (but do not commit)
. Build jar (and other artifacts using the respective `prepare_...` script if it exists and is executable)
. Execute default `test.sh`
. Execute tests with 100.000 unique measure points to check for hash conflicts
. Show PR and comments
. Optionally open changed files in your editor/IDE (cf. <<sec:environment>>).

[NOTE]
====
As the script prepares (but does not commit) a merge, you will find all changed files in the Git index (just execute `git status` to see the list of changed files).
====

=== Roll back PR

[CAUTION]
====
Make sure you do not change some files (either from the PR or other existing files).
Otherwise, automatic rollback is not so easy.

In this case, we trust your Git knowledge to get rid of the changes.
If all else fails, check the actions performed in the{nbsp}link:rollback.sh[] script.
====

Once you are finished with your work, you can roll back the checked-out files and get back to the state of your original branch.

[source, bash]
----
./review-rollback.sh <pr> # <1>
----
<1> Depending on whether you have executed the <<prepare-link,link prepare>> step (otherwise you may use `./scripts/review/rollback.sh` instead).
48 changes: 48 additions & 0 deletions scripts/review/prepare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

set -euo pipefail

if ! type -p gh >/dev/null; then
echo "Please install the 'gh' tool, e.g., via Homebrew: brew install gh" >&2
exit 1
fi

if [ "$#" -ne 1 ]; then
echo "Usage: $0 <pr>" >&2
exit 1
fi

pr="${1}"

author="$(gh pr view "${pr}" --json author -q .author.login)"
repository="$(gh pr view "${pr}" --json headRepository -q .headRepository.name)"
branch="$(gh pr view "${pr}" --json headRefName -q .headRefName)"

remote="review-${author}"
if ! git remote | grep -s "${remote}"; then
git remote add "${remote}" "[email protected]:${author}/${repository}"
fi

current_branch="$(git rev-parse --abbrev-ref HEAD)"
new_branch="review/${author}"
if [ "${current_branch}" != "${new_branch}" ]; then
git checkout -b "${new_branch}"
fi

git fetch "${remote}"
#gh pr merge "${pr}"

git merge --no-commit "${remote}/${branch}"

./mvnw clean verify -Dlicense.skip

./test.sh "${author}"
test -x test-unique.sh && ./test-unique.sh "${author}"

gh pr view "${pr}" -c

if test "${EDITOR:-}"; then
files="$(gh pr view "${pr}" --json files -q .files[].path)"
# shellcheck disable=SC2086
"${EDITOR}" ${files}
fi
38 changes: 38 additions & 0 deletions scripts/review/rollback.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

: "${REVIEW_BASE_BRANCH:=main}"

set -euo pipefail

git merge --abort || echo "Skipping merge rollback"
git stash save
git checkout "${REVIEW_BASE_BRANCH}"
git stash pop

if ! type -p gh >/dev/null; then
echo "Please install the 'gh' tool, e.g., via Homebrew: brew install gh" >&2
exit 1
fi

if [ "$#" -ne 1 ]; then
echo "Usage: $0 <pr>" >&2
exit 1
fi

pr="${1}"

author="$(gh pr view "${pr}" --json author -q .author.login)"

review_branch="review/${author}"
if git branch -v | grep -E -s "^\s+${review_branch}\s+"; then
git branch -d "${review_branch}"
fi

remote="review-${author}"
if git remote | grep -s "${remote}"; then
git remote remove "${remote}"
fi

./reset-sdk.sh

./mvnw clean verify -Dlicense.skip
Loading
Loading