<commit_before>#!/bin/zsh

LAST_RELEASE=$1
DO_PR_CHECK=1

set -o pipefail

is_doc_or_tech_debt_pr(){
    if ! (($+commands[jq])); then
        DO_PR_CHECK=0
        echo "jq not found"
        return 1
    fi
    out=$(cat pull.json | python3 -m json.tool \
    | jq '[.labels[].name == "docs" or .labels[].name == "tech-debt" or .labels[].name == "website"] | any')
    grep -q true <<< $out
    return $?
}

if [ -z $LAST_RELEASE ]; then
    echo "you need to give the previous release version. prepare_changelog.sh v<version>"
    exit 1
fi

get_prs(){
    # git log v0.10.2...c3861d167533fb797b0fae0c380806625712e5f7 |
    git log HEAD...${LAST_RELEASE} --first-parent --oneline --grep="Merge pull request #[0-9]\+" --grep="(#[0-9]\+)$" |
    grep -o "#\([0-9]\+\)" | awk -F\# '{print $2}' | while read line
    do
        grep -q "GH-${line}" CHANGELOG.md
        if [ $? -ne 0 ]; then
            echo $line
        fi
    done | while read PR_NUM
    do
        if [[ -z "${GITHUB_TOKEN}" ]] || [[ -z "${GITHUB_USERNAME}" ]] ; then
          out=$(curl -fsS "https://api.github.com/repos/hashicorp/packer/issues/${PR_NUM}" -o pull.json)
        else
          # authenticated call
          out=$(curl -u ${GITHUB_USERNAME}:${GITHUB_TOKEN} -fsS "https://api.github.com/repos/hashicorp/packer/issues/${PR_NUM}" -o pull.json)
        fi
        exy="$?"
        if [ $exy -ne 0 ]; then
            echo "bad response from github: manually check PR ${PR_NUM}"
            continue
        fi

        if (($DO_PR_CHECK)) && is_doc_or_tech_debt_pr; then
            echo "Skipping PR ${PR_NUM}: labeled as tech debt, docs or website. (waiting a second so we don't get rate-limited...)"
            continue
        fi
        echo "$(cat pull.json | python3 -m json.tool | jq -r '.title') - [GH-${PR_NUM}](https://github.com/hashicorp/packer/pull/${PR_NUM})"
    done
}

#is_doc_or_tech_debt_pr 52061111
# is_doc_or_tech_debt_pr 5206 # non-doc pr
#is_doc_or_tech_debt_pr 5434 # doc pr
#echo $?<commit_msg>Update python command for reading in pull.json<commit_after>#!/bin/zsh

LAST_RELEASE=$1
DO_PR_CHECK=1

set -o pipefail

is_doc_or_tech_debt_pr(){
    if ! (($+commands[jq])); then
        DO_PR_CHECK=0
        echo "jq not found"
        return 1
    fi
    out=$(python3 -m json.tool < pull.json  \
    | jq '[.labels[].name == "docs" or .labels[].name == "tech-debt" or .labels[].name == "website"] | any')
    grep -q true <<< $out
    return $?
}

if [ -z $LAST_RELEASE ]; then
    echo "you need to give the previous release version. prepare_changelog.sh v<version>"
    exit 1
fi

get_prs(){
    # git log v0.10.2...c3861d167533fb797b0fae0c380806625712e5f7 |
    git log HEAD...${LAST_RELEASE} --first-parent --oneline --grep="Merge pull request #[0-9]\+" --grep="(#[0-9]\+)$" |
    grep -o "#\([0-9]\+\)" | awk -F\# '{print $2}' | while read line
    do
        grep -q "GH-${line}" CHANGELOG.md
        if [ $? -ne 0 ]; then
            echo $line
        fi
    done | while read PR_NUM
    do
        if [[ -z "${GITHUB_TOKEN}" ]] || [[ -z "${GITHUB_USERNAME}" ]] ; then
          out=$(curl -fsS "https://api.github.com/repos/hashicorp/packer/issues/${PR_NUM}" -o pull.json)
        else
          # authenticated call
          out=$(curl -u ${GITHUB_USERNAME}:${GITHUB_TOKEN} -fsS "https://api.github.com/repos/hashicorp/packer/issues/${PR_NUM}" -o pull.json)
        fi
        exy="$?"
        if [ $exy -ne 0 ]; then
            echo "bad response from github: manually check PR ${PR_NUM}"
            continue
        fi

        if (($DO_PR_CHECK)) && is_doc_or_tech_debt_pr; then
            echo "Skipping PR ${PR_NUM}: labeled as tech debt, docs or website. (waiting a second so we don't get rate-limited...)"
            continue
        fi
        echo "$(python3 -m json.tool < pull.json | jq -r '.title') - [GH-${PR_NUM}](https://github.com/hashicorp/packer/pull/${PR_NUM})"
    done
}

#is_doc_or_tech_debt_pr 52061111
# is_doc_or_tech_debt_pr 5206 # non-doc pr
#is_doc_or_tech_debt_pr 5434 # doc pr
#echo $?
{'fraction_non_alphanumeric': 0.1640277090549233, 'fraction_numerical': 0.032162295893122216, 'mean_word_length': 2.366361365528726, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1527004', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1493, 'n_words': 370}