reconquest / atlassian-external-hooks

External Hooks plugin for Atlassian Bitbucket
https://external-hooks.reconquest.io
Other
44 stars 37 forks source link

List files if "$to_ref" == "$z40" first init repo #112

Closed blablabla-bla2009 closed 1 year ago

blablabla-bla2009 commented 4 years ago

how can I get all list of files during the first repository initialization (when there are no commits yet)?

kovetskiy commented 4 years ago

Check out the examples section in our docs, it has a few examples of your case to_ref == z40

https://external-hooks.reconquest.io/docs/example.bitbucket-add_hook_javascript_linter/ https://external-hooks.reconquest.io/docs/example.bitbucket-require-commits-to-be-associated-with-a-jira-issue/ https://external-hooks.reconquest.io/docs/example.merge-check-disallow-wip-commits/

so basically you just need to retrieve list of commits:

  1. boundaries = git rev-list --simplify-by-decoration -2 to_ref get starting and ending positions of your changeset
  2. git rev-list $boundaries[0]..$to_ref get list of commits
  3. and then for each commit you can retrieve list of files: git show --pretty="" --name-only $commit
blablabla-bla2009 commented 4 years ago

My bash script:

#!/bin/bash
failed=false
export PATH="$PATH:/usr/local/bin"
EXT_FILES='\.kt$|\.kts$'
FIRST_INIT="0000000000000000000000000000000000000000"
LINT_PARAMS='--relative --editorconfig=./.editorconfig'

echo "Checked files: ${EXT_FILES}"

while read from_ref to_ref ref_name; do

    if [[ $from_ref = $FIRST_INIT ]]
        then
            echo " FIRST INIT for repo/branch"
            TEMPDIR=$(mktemp -d)
            echo $TEMPDIR

            git cat-file -p "$to_ref:.editorconfig" > "$TEMPDIR/.editorconfig"
            echo " Old value: $from_ref"
            echo " New value: $to_ref"
            echo " Ref name:  $ref_name"

            boundaries=($(git rev-list --simplify-by-decoration -2 "$to_ref"))
            echo "boundaries=${boundaries}"
            commits=($(git rev-list "${boundaries[0]}".."$to_ref"))
            echo "commits=${commits[@]}"

            for commit in "${commits[@]}"; do
                files=($(git show --name-only  ${commit} | grep -E ${EXT_FILES}))
                for file in "${files[@]}"; do
                    mkdir -p "$TEMPDIR/$(dirname "$file")"
                    git cat-file -p "$to_ref:${file}" > "$TEMPDIR/$file"
                    echo "Checked files = ${file}"
                done
            done

            echo "#files=${#files}"
            if [[ ${#files} -eq 0 ]]; then
                echo "no ${EXT_FILES} files found, skipping"
            #rm -rf "$TEMPDIR"
                continue
            fi
            cd "$TEMPDIR"
            echo -e "\n    FIRST_INIT: KtLint running "

            ktlint "${LINT_PARAMS}" "${files[@]}"
            if [[ $? -ne 0 ]]
                then
                echo " KtLint: changeset $from_ref → $to_ref has not passed checks"
                failed=true
                else
                    echo "    FIRST_INIT: KtLint succesfull"
            fi

           #rm -rf "$TEMPDIR"
            if $failed; then
                    exit 1
                else
                    exit 0
            fi
        else
            echo "No FIRST_INIT: repositories/branch"
    fi
done

Command execute:

git init
git add --all
git commit -m "Initial Commit"
git remote add origin http://192.168.3.47:7990/scm/project-test1/photoideas.git
git push -u origin master

In out:

remote: Resolving deltas: 100% (184/184), done.
remote: fatal: your current branch 'master' does not have any commits yet
remote: fatal: Invalid revision range 0000000000000000000000000000000000000000..17fd7f8eaa33d317a8e3a2f02a69d3a0178269bf
remote: Checked files: \.kt$|\.kts$
remote:  FIRST INIT for repo/branch
remote: /var/atlassian/application-data/bitbucket/tmp/tmp.0RE80PRQPX
remote:  Old value: 0000000000000000000000000000000000000000
remote:  New value: 17fd7f8eaa33d317a8e3a2f02a69d3a0178269bf
remote:  Ref name:  refs/heads/master
remote: boundaries=17fd7f8eaa33d317a8e3a2f02a69d3a0178269bf
remote: commits=
remote: #files=0
remote: no \.kt$|\.kts$ files found, skipping
seletskiy commented 4 years ago

Which version of Bitbucket and External Hooks add-on are you using? The version of git installed on the server is also worth checking.

kovetskiy commented 1 year ago

Feel free to re-open the issue if you still have any questions.