-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.bit.dependency-update
More file actions
158 lines (137 loc) · 4.78 KB
/
Copy pathgithub.bit.dependency-update
File metadata and controls
158 lines (137 loc) · 4.78 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# Check if $BIT_CONFIG_ACCESS_TOKEN is set
if [[ -z "$BIT_CONFIG_USER_TOKEN" && -z "$BIT_CONFIG_ACCESS_TOKEN" ]]; then
echo "Error: BIT_CONFIG_ACCESS_TOKEN environment variable is not set. It is required!"
exit 1
fi
# Assign BIT_CONFIG_ACCESS_TOKEN to BIT_CONFIG_USER_TOKEN
if [[ -z "$BIT_CONFIG_USER_TOKEN" ]]; then
BIT_CONFIG_USER_TOKEN="$BIT_CONFIG_ACCESS_TOKEN"
export BIT_CONFIG_USER_TOKEN
fi
# Disable git terminal prompt to prevent the script from getting stuck waiting for input
export GIT_TERMINAL_PROMPT=0
# Helper function to check if a value exists in an array.
contains() {
local e
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
# Ensure required environment variables are set
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Error: GITHUB_TOKEN environment variable is not set"
exit 1
fi
# Check if both variables are set
if [[ -z "$GIT_USER_NAME" || -z "$GIT_USER_EMAIL" ]]; then
echo "Error: Both GIT_USER_NAME and GIT_USER_EMAIL must be set in environment variables."
exit 1
fi
allow="all"
branch="main"
branchName="bit-dependency-update"
commitMessage="Update Bit envs, outdated (direct) external dependencies, and workspace components according to the defined CI task parameter --allow"
prTitle="Update bit dependencies"
prBody="This PR updates the bit dependencies."
componentPatterns=""
envPatterns=""
packagePatterns=""
versionUpdatePolicy=""
# Extract the parameters
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--allow)
allow="$2"
shift # past argument
shift # past value
;;
--branch)
branch="$2"
shift # past argument
shift # past value
;;
--component-patterns)
componentPatterns="$2"
shift # past argument
shift # past value
;;
--env-patterns)
envPatterns="$2"
shift # past argument
shift # past value
;;
--package-patterns)
packagePatterns="$2"
shift # past argument
shift # past value
;;
--version-update-policy)
versionUpdatePolicy="$2"
shift # past argument
shift # past value
;;
*)
# Unknown option
shift # past argument
;;
esac
done
echo "Allow: $allow"
echo "Branch: $branch"
echo "Component Patterns: ${componentPatterns:-"not provided"}"
echo "Env Patterns: ${envPatterns:-"not provided"}"
echo "Package Patterns: ${packagePatterns:-"not provided"}"
echo "Version Update Policy: ${versionUpdatePolicy:-"not provided"}"
# Convert the comma-separated string into an array.
IFS=',' read -ra allowArray <<< "$allow"
if contains "all" "${allowArray[@]}" || contains "workspace-components" "${allowArray[@]}"; then
bit checkout head --all "${componentPatterns}"
echo "Updating workspace-components"
fi
if contains "all" "${allowArray[@]}" || contains "envs" "${allowArray[@]}"; then
bit envs update "${envPatterns}"
echo "Updating env-components"
fi
if contains "all" "${allowArray[@]}" || contains "external-dependencies" "${allowArray[@]}"; then
semverOption=""
if [[ -n "$versionUpdatePolicy" ]]; then
semverOption="--${versionUpdatePolicy}"
fi
bit update -y ${semverOption} "${packagePatterns}"
echo "Updating external dependencies"
fi
# Check git status
statusOutput=$(git status --porcelain)
# If there's any change
if [ -n "$statusOutput" ]; then
echo "Found modified components or dependencies"
# Setting git configuration
git config --global user.name "${GIT_USER_NAME}"
git config --global user.email "${GIT_USER_EMAIL}"
git checkout -b "${branchName}"
git add .
git commit -m "${commitMessage}"
REPO_URL="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git push "${REPO_URL}" "${branchName}" --force
# Create a pull request using GitHub API with curl
PR_RESPONSE=$(curl --request POST \
--header "Authorization: token ${GITHUB_TOKEN}" \
--header "Content-Type: application/json" \
--data '{
"head": "'"$branchName"'",
"base": "'"$branch"'",
"title": "'"$prTitle"'",
"body": "'"$prBody"'"
}' \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls")
PR_HTML_URL=$(echo "$PR_RESPONSE" | jq -r '.html_url')
# Check if PR was created successfully
if [ "$PR_HTML_URL" != "null" ]; then
echo "Pull request created successfully: $PR_HTML_URL"
else
ERROR_MESSAGE=$(echo "$PR_RESPONSE" | jq -r '.message')
echo "Error creating pull request: $ERROR_MESSAGE"
exit 1
fi
fi