-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcopy-repository-variables.sh
executable file
·53 lines (43 loc) · 1.45 KB
/
copy-repository-variables.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
if [ $# -lt 3 ]
then
echo "usage: $0 <source org> <source repo> <target org> [target repo]"
echo "If target repo is skipped the name will be same as the source repo"
exit 1
fi
if [ -z "$SOURCE_TOKEN" ]
then
echo "SOURCE_TOKEN must be set"
exit 1
fi
if [ -z "$TARGET_TOKEN" ]
then
echo "TARGET_TOKEN must be set"
exit 1
fi
source_owner=$1
source_repo=$2
target_owner=$3
target_repo=${4:-$source_repo}
function createOrUpdateRepoVariable() {
local name=$1
local value=$2
local repo=$3
if ! gh api "repos/$repo/actions/variables/$name" > /dev/null 2>&1; then
echo -e " creating variable $name"
gh api --method POST -H "X-GitHub-Api-Version: 2022-11-28" \
"repos/$repo/actions/variables" \
-f name="$name" -f value="$value" > /dev/null
else
echo -e " updating variable $name"
gh api --method PATCH -H "X-GitHub-Api-Version: 2022-11-28" \
"repos/$repo/actions/variables/$name" \
-f name="$name" -f value="$value" > /dev/null
fi
}
echo "Copying repo variables from $source_owner/$source_repo to $target_owner/$target_repo"
GH_TOKEN=$SOURCE_TOKEN gh api "repos/$source_owner/$source_repo/actions/variables" | jq -c '.variables[] | {name,value}' | while read -r json_item; do
name=$(echo "$json_item" | jq -r '.name')
value=$(echo "$json_item" | jq -r '.value')
GH_TOKEN=$TARGET_TOKEN createOrUpdateRepoVariable "$name" "$value" "$target_owner/$target_repo"
done