-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_action.bash
84 lines (76 loc) · 2.5 KB
/
update_action.bash
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
if [ $# -lt 3 ]; then
echo "Usage: $0 [action: create-branch/push-yml] [branch-name/config-file-path] [action_yml_name]"
exit 1
fi
action=$1
param=$2
action_yml_name=$3
org_name="quic-qrb-ros"
excluded_repos=(
"quic-qrb-ros.github.io"
"rviz"
"ros2cli"
"ros2_tracing"
"meta-ros"
".github"
"ros2_benchmark"
)
repos=$(gh api /orgs/$org_name/repos | jq '.[].name')
create_branches() {
branch_name=$1
for repo_raw in $repos; do
repo=$(echo $repo_raw | tr -d '"')
echo "Checking repo: $repo"
excluded=false
for excluded_repo in "${excluded_repos[@]}"; do
if [[ $repo =~ $excluded_repo ]]; then
excluded=true
break
fi
done
if $excluded; then
echo "Skipping excluded repo: $repo"
continue
fi
gh api /repos/$org_name/$repo/git/refs -X POST -F ref="refs/heads/$branch_name" -F sha="$(gh api /repos/$org_name/$repo/branches/master | jq -r '.commit.sha')"
done
}
push_yml_file() {
yml_file_path=$1
for repo_raw in $repos; do
repo=$(echo $repo_raw | tr -d '"')
echo "Checking repo: $repo"
excluded=false
for excluded_repo in "${excluded_repos[@]}"; do
if [[ $repo =~ $excluded_repo ]]; then
excluded=true
break
fi
done
if $excluded; then
echo "Skipping excluded repo: $repo"
continue
fi
file_response=$(gh api /repos/$org_name/$repo/contents/.github/workflows/$action_yml_name)
file_exists=$(echo $file_response | jq -r '.message' | grep -i 'not found')
if [ -n "$file_exists" ]; then
echo "Creating for $repo"
encoded_content=$(cat $yml_file_path | base64)
gh api /repos/$org_name/$repo/contents/.github/workflows/$action_yml_name -X PUT -F message="Create action" -F content="$encoded_content"
else
echo "Updating $repo"
existing_sha=$(echo $file_response | jq -r '.sha')
encoded_content=$(cat $yml_file_path | base64)
gh api /repos/$org_name/$repo/contents/.github/workflows/$action_yml_name -X PUT -F message="Update action" -F content="$encoded_content" -F sha=$existing_sha
fi
done
}
if [ "$action" == "create-branch" ]; then
create_branches $param
elif [ "$action" == "push-yml" ]; then
push_yml_file $param $action_yml_name
else
echo "Invalid action. Use 'create-branch' or 'push-yml'."
exit 1
fi