Browse Source

cleaner labeler code (#4933)

Paweł Krupa 6 years ago
parent
commit
31f14e5855
1 changed files with 21 additions and 11 deletions
  1. 21 11
      .travis/labeler.sh

+ 21 - 11
.travis/labeler.sh

@@ -3,6 +3,19 @@
 # This is a simple script which should apply labels to unlabelled issues from last 3 days.
 # It will soon be deprecated by GitHub Actions so no futher development on it is planned.
 
+add_labels() {
+	ISSUE="$1"
+	URL="https://api.github.com/repos/netdata/netdata/issues/$ISSUE/labels"
+	# deduplicate array and add quotes
+	SET=( $(for i in "${@:2}"; do echo "\"$i\""; done | sort -u) )
+	# implode array to string
+	LABELS="${SET[*]}"
+	# add commas between quotes (replace spaces)
+	LABELS="${LABELS//\" \"/\",\"}"
+	echo "-------- Assigning labels to #${ISSUE}: ${LABELS} --------"
+	curl -H "Authorization: token $GITHUB_TOKEN" -d "{\"labels\":[${LABELS}]}" -X POST "${URL}"
+}
+
 if [ "$GITHUB_TOKEN" == "" ]; then
 	echo "GITHUB_TOKEN is needed"
 	exit 1
@@ -24,13 +37,12 @@ echo "===== Categorizing issues ====="
 for STATE in "open" "closed"; do
 	for ISSUE in $(hub issue -f "%I %l%n" -s "$STATE" -d "$(date +%F -d '1 day ago')" | grep -v -f $LABELS_FILE); do
 		echo "-------- Processing $STATE issue no. $ISSUE --------"
-		URL="https://api.github.com/repos/netdata/netdata/issues/$ISSUE"
-		BODY="$(curl "${URL}" 2>/dev/null | jq .body)"
+		BODY="$(curl "https://api.github.com/repos/netdata/netdata/issues/$ISSUE" 2>/dev/null | jq .body)"
 		case "${BODY}" in
-		*"# Question summary"*) curl -H "Authorization: token $GITHUB_TOKEN" -d '{"labels":["question", "no changelog"]}' -X POST "${URL}/labels" ;;
-		*"# Bug report summary"*) curl -H "Authorization: token $GITHUB_TOKEN" -d '{"labels":["needs triage","bug"]}' -X POST "${URL}/labels" ;;
-		*"# Feature idea summary"*) curl -H "Authorization: token $GITHUB_TOKEN" -d '{"labels":["needs triage","feature request"]}' -X POST "${URL}/labels" ;;
-		*) curl -H "Authorization: token $GITHUB_TOKEN" -d '{"labels":["needs triage", "no changelog"]}' -X POST "${URL}/labels" ;;
+		*"# Question summary"*) add_labels "$ISSUE" "question" "no changelog" ;;
+		*"# Bug report summary"*) add_labels "$ISSUE" "needs triage" "bug" ;;
+		*"# Feature idea summary"*) add_labels "$ISSUE" "needs triage" "feature request" ;;
+		*) add_labels "$ISSUE" "needs triage" "no changelog" ;;
 		esac
 	done
 done
@@ -41,7 +53,6 @@ for PR in $(hub pr list -s all -f "%I%n" -L 10); do
 	echo "----- Processing PR #$PR -----"
 	echo "" >$NEW_LABELS
 	NEW_SET=""
-	URL="https://api.github.com/repos/netdata/netdata/issues/$PR"
 	DIFF_URL="https://github.com/netdata/netdata/pull/$PR.diff"
 	for FILE in $(curl -L "${DIFF_URL}" 2>/dev/null | grep "diff --git a/" | cut -d' ' -f3 | sort | uniq); do
 		LABEL=""
@@ -63,7 +74,7 @@ for PR in $(hub pr list -s all -f "%I%n" -L 10); do
 		*) AREA=$(echo "$FILE" | cut -d'/' -f2) ;;
 		esac
 		LABEL="area/$AREA"
-		echo "--------- Selecting $LABEL due to $FILE --------"
+		echo "Selecting $LABEL due to $FILE"
 		if grep "$LABEL" "$LABELS_FILE"; then
 			echo "$LABEL" >>$NEW_LABELS
 			if [[ $LABEL =~ "external" ]]; then
@@ -73,9 +84,8 @@ for PR in $(hub pr list -s all -f "%I%n" -L 10); do
 			echo "-------- Label '$LABEL' not available --------"
 		fi
 	done
-	NEW_SET=$(sort $NEW_LABELS | uniq | grep -v "^$" | sed -e 's/^/"/g;s/$/",/g' | tr -d '\n' | sed 's/.\{1\}$//')
+	NEW_SET=$(sort $NEW_LABELS | uniq)
 	if [ ! -z "$NEW_SET" ]; then
-		echo "-------- Assigning labels: ${NEW_SET} --------"
-		curl -H "Authorization: token $GITHUB_TOKEN" -d "{\"labels\":[${NEW_SET}]}" -X POST "${URL}/labels" &>/dev/null
+		add_labels "$PR" ${NEW_SET}
 	fi
 done