Skip to content

Commit

Permalink
fix: script without iter
Browse files Browse the repository at this point in the history
  • Loading branch information
flukolo4ek committed Oct 14, 2024
1 parent b635ce1 commit 8f72f15
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/.scripts/check_added_labels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ do
fi
done

COMPONENTS_AND_TYPES_LABELS=$(echo $COMPONENTS_AND_TYPES_LABELS | tr ' ' '\n' | sort | tr '\n' ' ')
COMPONENTS_AND_TYPES_LABELS=$(echo $COMPONENTS_AND_TYPES_LABELS | tr ' ' '\n' | sort | tr '\n' ',' | rev | cut -c2- | rev)

echo $COMPONENTS_AND_TYPES_LABELS
30 changes: 30 additions & 0 deletions .github/.scripts/construct_labels.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

CONFIG_DATA=$(curl -s "https://raw.githubusercontent.com/datalens-tech/datalens/main/.github/workflows/scripts/changelog/changelog_config.json")
TYPE_LABELS_PREFIX=$(echo "$CONFIG_DATA" | jq -r '.section_tags.prefix')
COMPONENT_LABELS_PREFIX=$(echo "$CONFIG_DATA" | jq -r '.component_tags.prefix')

PR_TITLE=${PR_TITLE}
GH_TOKEN=${GH_TOKEN}
REPO=${REPO}
PR_NUMBER=${PR_NUMBER}
CURRENT_LABELS=${CURRENT_LABELS}

CURRENT_LABELS=$(echo "${CURRENT_LABELS}" | tr ',' '\n' | grep -E "$TYPE_LABELS_PREFIX|$COMPONENT_LABELS_PREFIX" | sort)

LABELS_SECTION=${PR_TITLE%%:*}
COMMIT_TYPE=${LABELS_SECTION%%\(*}
COMPONENT_SECTION=${LABELS_SECTION#"$COMMIT_TYPE"}

LABELS_TO_ADD="$TYPE_LABELS_PREFIX$COMMIT_TYPE\n"

if [[ -n $COMPONENT_SECTION ]]; then
COMMIT_COMPONENTS=$(echo "$CONFIG_DATA" | jq -r '.component_tags.tags[].id' | awk -v component_sec="$COMPONENT_SECTION" 'component_sec ~ $0' | sed "s#^#$COMPONENT_LABELS_PREFIX#")
LABELS_TO_ADD+="$COMMIT_COMPONENTS"
fi

LABELS_TO_ADD=$(echo -e "$LABELS_TO_ADD" | sort)
LABELS_TO_DELETE=$(comm -13 <(echo "$LABELS_TO_ADD") <(echo "$CURRENT_LABELS") | tr "\n" "," | rev | cut -c2- | rev)

echo "labels_to_add=$(echo "$LABELS_TO_ADD" | tr '\n' ',' | cut -c2- | rev | cut -c2- | rev)" >> $GITHUB_OUTPUT
echo "labels_to_delete=$LABELS_TO_DELETE" >> $GITHUB_OUTPUT
38 changes: 0 additions & 38 deletions .github/.scripts/get_labels.sh

This file was deleted.

45 changes: 21 additions & 24 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,41 @@ outputs:
runs:
using: "composite"
steps:
- name: Get labels from PR title
- name: Get current labels from PR
id: get_labels
shell: bash
run: echo "current_labels=$(gh pr view $PR_NUMBER --repo $REPO --json labels | jq -r '.labels[].name' | tr '\n' ',')" >> $GITHUB_OUTPUT
env:
REPO: ${{ inputs.github_owner }}/${{ inputs.github_repo }}
PR_NUMBER: ${{ inputs.github_pr_number }}
PR_TITLE: ${{ inputs.github_pr_title }}
GH_TOKEN: ${{ inputs.github_token }}

- name: Construct labels to add/delete
id: construct_labels
working-directory: .github/.scripts
shell: bash
run: ./get_labels.sh
run: ./construct_labels.sh
env:
REPO: ${{ inputs.github_owner }}/${{ inputs.github_repo }}
PR_NUMBER: ${{ inputs.github_pr_number }}
PR_TITLE: ${{ inputs.github_pr_title }}
GH_TOKEN: ${{ inputs.github_token }}
CURRENT_LABELS: ${{ steps.get_labels.outputs.current_labels }}

- name: Add labels
id: add_labels
- name: Add and delete labels
shell: bash
run: |
LABELS_TO_ADD="${{ steps.get_labels.outputs.labels_to_add }}"
echo LABELS_TO_ADD=$LABELS_TO_ADD
gh pr edit $PR_NUMBER --repo $REPO --add-label $LABELS_TO_ADD
CURRENT_PR_LABELS=$(gh pr view $PR_NUMBER --repo $REPO --json labels | jq -r '.labels[].name')
LABELS_TO_DELETE=""
echo CURRENT_PR_LABELS=$CURRENT_PR_LABELS
for cur_label in $CURRENT_PR_LABELS
do
if ! [[ $LABELS_TO_ADD == *$cur_label* ]]; then
if [[ $cur_label == *type/* || $cur_label == *component/* ]]; then
LABELS_TO_DELETE+=$cur_label,
fi
fi
done
LABELS_TO_DELETE=$(echo $LABELS_TO_DELETE | rev | cut -c2- | rev)
LABELS_TO_ADD=${{ steps.construct_labels.outputs.labels_to_add }}
LABELS_TO_DELETE=${{ steps.construct_labels.outputs.labels_to_delete }}
echo LABELS_TO_ADD=$LABELS_TO_ADD
echo LABELS_TO_DELETE=$LABELS_TO_DELETE
if [ -n "$LABELS_TO_ADD" ]; then
gh pr edit $PR_NUMBER --repo $REPO --add-label $LABELS_TO_ADD
fi
if [ -n "$LABELS_TO_DELETE" ]; then
gh pr edit $PR_NUMBER --repo $REPO --remove-label $LABELS_TO_DELETE
fi
Expand Down

0 comments on commit 8f72f15

Please sign in to comment.