From 566b27867288469367faf5ef905ca38d8c30a0fa Mon Sep 17 00:00:00 2001 From: Hiroyuki Kusu Date: Wed, 20 Dec 2023 18:38:32 +0900 Subject: [PATCH] Refactor --- action.yml | 112 ++++++++++++++++++++++++------------------------- raise_error.sh | 4 ++ 2 files changed, 59 insertions(+), 57 deletions(-) create mode 100644 raise_error.sh diff --git a/action.yml b/action.yml index ec6fc73..c1ccfdc 100644 --- a/action.yml +++ b/action.yml @@ -27,7 +27,7 @@ inputs: description: 'Bash script to run before Gradle dependencies task.' required: false repository: # opned for .github/workflows/ci.yml - description: '' + description: 'Target repository.' required: false default: ${{ github.repository }} sha: # opend for .github/workflows/ci.yml @@ -37,7 +37,7 @@ inputs: outputs: exists-diff: description: 'Whether there are any differences in dependencies.' - value: ${{ steps.report.outputs.exists-diff || 'false' }} + value: ${{ steps.report.outputs.exists-diff }} runs: using: 'composite' steps: @@ -58,18 +58,19 @@ runs: PR_NUMBER: ${{ github.event.pull_request.number }} GH_TOKEN: ${{ github.token }} run: | - function raise_error() { - echo "::error::$1" - return 1 - } + echo "::debug::git version: $(git --version)" + echo "::debug::gh version: $(gh --version | tr '\n' ' ')" + source "$GITHUB_ACTION_PATH/raise_error.sh" + + readonly WORK_DIR="$GITHUB_ACTION_PATH" + + mkdir "$WORK_DIR/tools" + mkdir "$WORK_DIR/deps" if [[ "$EVENT_NAME" != 'pull_request' && "$EVENT_NAME" != 'push' ]]; then raise_error "This action must be triggered by a 'pull_request' or 'push' event."; fi if [[ -z "$MODULES" ]]; then raise_error "Specify 'modules' input."; fi - modules=() - configurations=() - for module in $MODULES ; do configuration="$CONFIGURATION" @@ -84,27 +85,18 @@ runs: configurations+=($configuration) done - cd "$GITHUB_ACTION_PATH" - - readonly WORK_DIR="$(pwd)" - - mkdir tools - mkdir deps - curl -f -L -o "$WORK_DIR/tools/dependency-diff-tldr-r8.jar" 'https://github.com/careem/dependency-diff-tldr/releases/download/v0.0.6/dependency-diff-tldr-r8.jar' - - if [[ 'b76af4e71fe1bc3362207d648542337c21ab91e8' != "$(cat "$WORK_DIR/tools/dependency-diff-tldr-r8.jar" | openssl sha1 | awk '{print $2}')" ]]; then + if [ 'b76af4e71fe1bc3362207d648542337c21ab91e8' != "$(cat "$WORK_DIR/tools/dependency-diff-tldr-r8.jar" | openssl sha1 | awk '{print $2}')" ]; then raise_error "Incorrect checksum for dependency-diff-tldr-r8.jar." fi curl -f -L -o "$WORK_DIR/tools/dependency-tree-diff.jar" 'https://github.com/JakeWharton/dependency-tree-diff/releases/download/1.2.0/dependency-tree-diff.jar' - - if [[ '949394274f37c06ac695b5d49860513e4d16e847' != "$(cat "$WORK_DIR/tools/dependency-tree-diff.jar" | openssl sha1 | awk '{print $2}')" ]]; then + if [ '949394274f37c06ac695b5d49860513e4d16e847' != "$(cat "$WORK_DIR/tools/dependency-tree-diff.jar" | openssl sha1 | awk '{print $2}')" ]; then raise_error "Incorrect checksum for dependency-tree-diff.jar." fi gh repo clone "$REPOSITORY" sources > /dev/null 2>&1 || raise_error "May not have 'contents: read' permission." - cd ./sources + cd "$WORK_DIR/sources" # enable fetch # ref: https://github.com/actions/checkout/blob/72f2cec99f417b1a1c5e2e88945068983b7965f9/src/git-auth-helper.ts#L55-L63 @@ -145,42 +137,48 @@ runs: cd "$WORK_DIR/sources/$PROJECT_DIR" # directory may have changed in the script for i in "${!modules[@]}" ; do - # on windows, files containing CR cause an error in diff tools ./gradlew -q ":${modules[$i]}:dependencies" --configuration "${configurations[$i]}" | tr -d '\r' > "$WORK_DIR/deps/${modules[$i]}-after-deps.txt" done - echo "### $REPORT_TITLE" >> $GITHUB_STEP_SUMMARY - echo '' >> $GITHUB_STEP_SUMMARY - echo "- commit SHA before change: [${before_sha}](https://github.com/${REPOSITORY}/commit/${before_sha})" >> $GITHUB_STEP_SUMMARY - echo "- commit SHA after change: [${after_sha}](https://github.com/${REPOSITORY}/commit/${after_sha})" >> $GITHUB_STEP_SUMMARY - - for i in "${!modules[@]}" ; do - result="$(java -jar "$WORK_DIR/tools/dependency-diff-tldr-r8.jar" "$WORK_DIR/deps/${modules[$i]}-before-deps.txt" "$WORK_DIR/deps/${modules[$i]}-after-deps.txt")" - - echo '' >> $GITHUB_STEP_SUMMARY - - if [[ -n "$result" ]]; then - echo ":orange_square: \`${modules[$i]}\` module ( \`${configurations[$i]}\` configuration )" >> $GITHUB_STEP_SUMMARY - echo '' >> $GITHUB_STEP_SUMMARY - echo '```' >> $GITHUB_STEP_SUMMARY - echo "$result" >> $GITHUB_STEP_SUMMARY - echo '```' >> $GITHUB_STEP_SUMMARY - - result_detail="$(java -jar "$WORK_DIR/tools/dependency-tree-diff.jar" "$WORK_DIR/deps/${modules[$i]}-before-deps.txt" "$WORK_DIR/deps/${modules[$i]}-after-deps.txt")" - - echo '' >> $GITHUB_STEP_SUMMARY - echo '
' >> $GITHUB_STEP_SUMMARY - echo 'detail' >> $GITHUB_STEP_SUMMARY - echo '' >> $GITHUB_STEP_SUMMARY - echo '```diff' >> $GITHUB_STEP_SUMMARY - echo "$result_detail" >> $GITHUB_STEP_SUMMARY - echo '```' >> $GITHUB_STEP_SUMMARY - echo '
' >> $GITHUB_STEP_SUMMARY - - echo 'exists-diff=true' >> $GITHUB_OUTPUT - else - echo ":green_square: \`${modules[$i]}\` module ( \`${configurations[$i]}\` configuration )" >> $GITHUB_STEP_SUMMARY - echo '' >> $GITHUB_STEP_SUMMARY - echo 'No differences in dependencies, when this module is the base point.' >> $GITHUB_STEP_SUMMARY - fi - done + exists=false + + { + echo "### $REPORT_TITLE" + echo '' + echo "- commit SHA before change: [${before_sha}](https://github.com/${REPOSITORY}/commit/${before_sha})" + echo "- commit SHA after change: [${after_sha}](https://github.com/${REPOSITORY}/commit/${after_sha})" + + for i in "${!modules[@]}" ; do + result="$(java -jar "$WORK_DIR/tools/dependency-diff-tldr-r8.jar" "$WORK_DIR/deps/${modules[$i]}-before-deps.txt" "$WORK_DIR/deps/${modules[$i]}-after-deps.txt")" + + echo '' + + if [[ -n "$result" ]]; then + exists=true + + echo ":orange_square: \`${modules[$i]}\` module ( \`${configurations[$i]}\` configuration )" + echo '' + echo '```' + echo "$result" + echo '```' + + result_detail="$(java -jar "$WORK_DIR/tools/dependency-tree-diff.jar" "$WORK_DIR/deps/${modules[$i]}-before-deps.txt" "$WORK_DIR/deps/${modules[$i]}-after-deps.txt")" + + echo '' + echo '
' + echo 'detail' + echo '' + echo '```diff' + echo "$result_detail" + echo '```' + echo '
' + else + echo ":green_square: \`${modules[$i]}\` module ( \`${configurations[$i]}\` configuration )" + echo '' + echo 'No differences in dependencies, when this module is the base point.' + fi + done + } >> "$GITHUB_STEP_SUMMARY" + + echo "exists-diff=$exists" >> "$GITHUB_OUTPUT" + rm -fr "$WORK_DIR/tools"; rm -fr "$WORK_DIR/deps"; rm -fr "$WORK_DIR/sources" diff --git a/raise_error.sh b/raise_error.sh new file mode 100644 index 0000000..dcd83ef --- /dev/null +++ b/raise_error.sh @@ -0,0 +1,4 @@ +function raise_error() { + echo "::error::$1" + return 1 +}