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

Remove JQ command requirement #12

Merged
Merged
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
3 changes: 3 additions & 0 deletions .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: Test PR
on:
pull_request:

env:
GH_TOKEN: ${{ github.token }}

jobs:
test:
strategy:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
assert.sh
7 changes: 3 additions & 4 deletions backport
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ check_command() {
}

check_command "gh" "https://cli.github.com/"
check_command "jq" "https://jqlang.github.io/jq/download/"

if [[ $# -lt 2 || $# -gt 5 ]]; then
usage
Expand Down Expand Up @@ -155,14 +154,14 @@ PR_BODY=$(\cat .github/pull_request_template.md 2>/dev/null || true)
if [ -n "$ORIGINAL_PR_NUMBER" ]; then
if [ "$NON_INTERACTIVE" = "true" ]; then
# --reviewer flag doesn't work with --web
REVIEWERS=$(gh api repos/"$REPO_UPSTREAM"/pulls/"$ORIGINAL_PR_NUMBER"/reviews | jq '.[] | select(.state == "APPROVED") | .user.login' | jq -c -r -s '. | unique | join(",")')
REVIEWERS=$(get_pr_reviewers "${REPO_UPSTREAM}" "${ORIGINAL_PR_NUMBER}")
if [ -n "$REVIEWERS" ]; then
REVIEWERS_ARG="--reviewer $REVIEWERS"
fi
fi
PR_BODY=$(gh api repos/$REPO_UPSTREAM/pulls/$ORIGINAL_PR_NUMBER | jq -r '"\n\n" + .body')
PR_BODY=$(gh api repos/$REPO_UPSTREAM/pulls/$ORIGINAL_PR_NUMBER --jq '"\n\n" + .body')

LABELS=$(gh api repos/$REPO_UPSTREAM/pulls/$ORIGINAL_PR_NUMBER | jq '.labels[].name' | jq -c -r -s '. | join(",")')
LABELS=$(get_pr_labels "${REPO_UPSTREAM}" "${ORIGINAL_PR_NUMBER}")
if [ -n "$LABELS" ]; then
LABELS_ARG=(--label "$LABELS")
fi
Expand Down
16 changes: 15 additions & 1 deletion backport.functions
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
set -e

function get_pr_number() {

local commit_msg=$1

echo "$commit_msg" | grep --extended-regexp --only-matching '#[0-9]+' | tail -n1 | cut -c2-
}

function get_pr_reviewers() {
local repo_upstream=$1
local pr_number=$2

gh api repos/"${repo_upstream}"/pulls/"${pr_number}"/reviews --jq '[.[] | select(.state == "APPROVED") | .user.login] | unique | join(",")'
}

function get_pr_labels() {
local repo_upstream=$1
local pr_number=$2

gh api repos/"${repo_upstream}"/pulls/"${pr_number}" --jq '[.labels[].name] | join(",")'
}
24 changes: 24 additions & 0 deletions backport.functions_tests
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,32 @@ function test_get_pr_number {
assert_eq "$expected_pr_number" "$actual_pr_number" "$MSG" && log_success "$MSG" || TESTS_RESULT=$?
}

function test_get_pr_reviewers {
local repo_upstream=$1
local pr_number=$2
local expected_reviewers=$3
local actual_reviewers=$(get_pr_reviewers "$repo_upstream" "$pr_number")
local MSG="Expected reviewers extracted from \"$repo_upstream/pull/$pr_number\" should be equal to \"$expected_reviewers\""
assert_eq "$expected_reviewers" "$actual_reviewers" "$MSG" && log_success "$MSG" || TESTS_RESULT=$?
}

function test_get_pr_labels {
local repo_upstream=$1
local pr_number=$2
local expected_labels=$3
local actual_labels=$(get_pr_labels "$repo_upstream" "$pr_number")
local MSG="Expected labels extracted from \"$repo_upstream/pull/$pr_number\" should be equal to \"$actual_labels\""
assert_eq "$expected_labels" "$actual_labels" "$MSG" && log_success "$MSG" || TESTS_RESULT=$?
}

log_header "Tests for get_pr_number"
test_get_pr_number 'Fix private test repository access [DI-236] (#221)' '221'
test_get_pr_number 'Fix private test repository access [DI-236] (#221) (#222)' '222'

log_header "Tests for get_pr_reviewers"
test_get_pr_reviewers 'hazelcast/backport' '1' 'ldziedziul'

log_header "Tests for get_pr_labels"
test_get_pr_labels 'hazelcast/backport' '1' 'enhancement'

assert_eq 0 "$TESTS_RESULT" "All tests should pass"