[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Salsa permissions




Hi Sean,

Le mardi 12 mars 2024 à 10:31, Sean Whitton <spwhitton@spwhitton.name> a écrit :

I'd prefer just to unprotect the branches.

Agreed.

Does anyone have a script which mass-unprotects branches?

I just looked it up, I came up with the following scripts. You need to have created an API token with the relevant permissions. And the scripts use the ~jq(1)~ command, installable from the homonymous debian package.

The attached file ~debian_emacsen_ids_and_branches~ contains the project ids and the names of the protected branches of the debian-emacsen group. I generated this list using the scripts ~salsa_fetch_debian_emacsen.sh~ and ~salsa_list_protected_branches.sh~, which I have included for reference.

The script ~salsa_unprotect_branches.sh~ does the actual unprotecting. I could not test it, for I am not the owner (I get 403 errors, which makes sense).

Here are the relevant gitlab api documentations I used to get to this : https://docs.gitlab.com/ee/api/groups.html and https://docs.gitlab.com/ee/api/protected_branches.html.

Do tell me how it worked, I am curious.

Best,

Aymeric


#!/bin/sh

PRIVATE_TOKEN="mustbeset"

while read id branch; do

    curl --request DELETE --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" "https://salsa.debian.org/api/v4/projects/$id/protected_branches/$branch";

done < debian_emacsen_ids_and_branches

Attachment: debian_emacsen_ids_and_branches
Description: Binary data

#!/bin/sh

PRIVATE_TOKEN="mustbeset"

RAW_DIRECTORY=$(mktemp -d)
RAW_FILE=$(mktemp)

amount_pages=$(curl --head --request GET --header "PRIVATE_TOKEN: $PRIVATE_TOKEN" "https://salsa.debian.org/api/v4/groups/emacsen-team/projects?page=1&per_page=50"; 2>/dev/null \
		    | grep "^x-total-pages:" | cut -d':' -f2 | tr -d ' \r')

for i in $(seq 1 1 $amount_pages); do
    curl --request GET --header "PRIVATE_TOKEN: $PRIVATE_TOKEN" "https://salsa.debian.org/api/v4/groups/emacsen-team/projects?page=$i&per_page=50"; > $RAW_DIRECTORY/temp$i 
done

jq -s add $RAW_DIRECTORY/temp* > $RAW_FILE

cat $RAW_FILE | jq '.[] | {id: .id}' \
    | grep -v '^{' | grep -v '^}' \
    | cut -d ':' -f 2 | tr -d ' \r' > debian_emacsen_ids

rm -rf $RAW_DIRECTORY
rm -f $RAW_FILE

#!/bin/sh

PRIVATE_TOKEN="mustbeset"

RAW_FILE=$(mktemp)

while read id ; do

    curl --header "PRIVATE_TOKEN: $PRIVATE_TOKEN" "https://salsa.debian.org/api/v4/projects/$id/protected_branches"; 2>/dev/null | jq 'select(length>0)' | jq ".[] | {name: .name, projectid: $id}" >> $RAW_FILE

done < debian_emacsen_ids

jq '.[]' < $RAW_FILE | tac | sed 'N;s/\n/ /g' | tr -d '"' > debian_emacsen_ids_and_branches


Reply to: