@@ -51,48 +51,68 @@ if [[ ! -f "${DEPENDABOT_FILE}" ]]; then
5151 exit 1
5252fi
5353
54+ if ! command -v yq > /dev/null 2>&1 ; then
55+ echo " ERROR: yq v4 is required to update Dependabot coverage safely." >&2
56+ exit 1
57+ fi
58+
5459# --- Functions ---
5560
5661# Check whether a given ecosystem+directory pair already exists in dependabot.yml.
57- # Uses a simple text-based check: looks for the ecosystem line followed by
58- # the directory line within a few lines of each other.
5962entry_exists () {
6063 local ecosystem=" $1 "
6164 local directory=" $2 "
6265
63- # Normalise directory for matching: dependabot uses "/" for root,
64- # "/subdir" for subdirectories.
65- local dir_pattern
66- if [[ " ${directory} " == " /" ]]; then
67- dir_pattern=' directory: "/"'
68- else
69- dir_pattern=" directory: \" ${directory} \" "
70- fi
71-
72- # Search for the ecosystem+directory pair within the file.
73- # We look for both on adjacent lines (within 5 lines of each other).
74- if awk -v eco=" \" ${ecosystem} \" " -v dir=" ${dir_pattern} " '
75- /package-ecosystem:/ && $0 ~ eco { found_eco=NR }
76- found_eco && NR <= found_eco+5 && $0 ~ dir { exit 0 }
77- END { exit 1 }
78- ' " ${DEPENDABOT_FILE} " ; then
79- return 0
80- else
81- return 1
82- fi
66+ ECOSYSTEM=" ${ecosystem} " DIRECTORY=" ${directory} " yq -e '
67+ .updates[]
68+ | select(."package-ecosystem" == strenv(ECOSYSTEM))
69+ | select(
70+ .directory == strenv(DIRECTORY) or
71+ (.directories[]? == strenv(DIRECTORY))
72+ )
73+ ' " ${DEPENDABOT_FILE} " > /dev/null 2>&1
8374}
8475
8576# Append a new ecosystem+directory entry to dependabot.yml.
8677append_entry () {
8778 local ecosystem=" $1 "
8879 local directory=" $2 "
80+ local entry_count
81+
82+ entry_count=" $( ECOSYSTEM=" ${ecosystem} " yq '
83+ [.updates[] | select(."package-ecosystem" == strenv(ECOSYSTEM))] | length
84+ ' " ${DEPENDABOT_FILE} " ) "
85+
86+ if [[ " ${entry_count} " -gt 1 ]]; then
87+ echo " ERROR: ${ecosystem} already has ${entry_count} update blocks." >&2
88+ echo " Consolidate them into one directories: block before adding coverage." >&2
89+ return 1
90+ fi
91+
92+ if [[ " ${entry_count} " -eq 1 ]]; then
93+ ECOSYSTEM=" ${ecosystem} " DIRECTORY=" ${directory} " yq -i '
94+ (.updates[] | select(."package-ecosystem" == strenv(ECOSYSTEM))) |= (
95+ .directories = (((.directories // [.directory]) + [strenv(DIRECTORY)]) | unique)
96+ | del(.directory)
97+ | ."open-pull-requests-limit" =
98+ ([(."open-pull-requests-limit" // 3), 3] | min)
99+ | .groups."coverage-shared"."group-by" = "dependency-name"
100+ )
101+ ' " ${DEPENDABOT_FILE} "
102+ echo " CONSOLIDATED: ${ecosystem} @ ${directory} "
103+ return 0
104+ fi
89105
90106 cat >> " ${DEPENDABOT_FILE} " << EOF
91107
92108 - package-ecosystem: "${ecosystem} "
93109 directory: "${directory} "
94110 schedule:
95- interval: "daily"
111+ interval: "weekly"
112+ open-pull-requests-limit: 3
113+ groups:
114+ dependency-updates:
115+ patterns: ["*"]
96116EOF
97117 echo " ADDED: ${ecosystem} @ ${directory} "
98118}
0 commit comments