-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.sh
128 lines (102 loc) · 2.71 KB
/
utils.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
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
# Set up the mutex repo
# args:
# $1: repo_url
set_up_repo() {
__repo_url=$1
git init --quiet
git config --local user.name "github-bot" --quiet
git config --local user.email "github-bot@users.noreply.github.com" --quiet
git remote remove origin 2>/dev/null || true
git remote add origin "$__repo_url"
}
# Update the branch to the latest from the remote. Or checkout to an orphan branch
# args:
# $1: branch
update_branch() {
__branch=$1
git switch --orphan gh-action-mutex/temp-branch-$(date +%s) --quiet
git branch -D $__branch --quiet 2>/dev/null || true
git fetch origin $__branch --quiet 2>/dev/null || true
git checkout $__branch --quiet || git switch --orphan $__branch --quiet
}
# Add to the queue
# args:
# $1: branch
# $2: queue_file
# $3: ticket_id
enqueue() {
__branch=$1
__queue_file=$2
__ticket_id=$3
__has_error=0
echo "[$__ticket_id] Enqueuing to branch $__branch, file $__queue_file"
update_branch $__branch
touch $__queue_file
# if we are not in the queue, add ourself to the queue
if [ -z "$(cat $__queue_file | grep -F $__ticket_id)" ]; then
echo "$__ticket_id" >> "$__queue_file"
git add $__queue_file
git commit -m "[$__ticket_id] Enqueue " --quiet
set +e # allow errors
git push --set-upstream origin $__branch --quiet
__has_error=$((__has_error + $?))
set -e
fi
if [ ! $__has_error -eq 0 ]; then
sleep 1
enqueue $@
fi
}
# Wait for the lock to become available
# args:
# $1: branch
# $2: queue_file
# $3: ticket_id
wait_for_lock() {
__branch=$1
__queue_file=$2
__ticket_id=$3
echo "[$__ticket_id] Waiting for lock"
update_branch $__branch
# if we are not the first in line, spin
if [ "$(cat $__queue_file | awk NF | head -n 1)" != "$__ticket_id" ]; then
sleep 5
wait_for_lock $@
fi
}
# Remove from the queue, when locked by it or just enqueued
# args:
# $1: branch
# $2: queue_file
# $3: ticket_id
dequeue() {
__branch=$1
__queue_file=$2
__ticket_id=$3
__has_error=0
update_branch $__branch
if [ "$(cat $__queue_file | awk NF | head -n 1)" == "$__ticket_id" ]; then
echo "[$__ticket_id] Unlocking"
__message="[$__ticket_id] Unlock"
else
echo "[$__ticket_id] Dequeueing. We don't have the lock!"
__message="[$__ticket_id] Dequeue"
fi
if [ $(awk "/$__ticket_id/" $__queue_file | wc -l) == "0" ]; then
1>&2 echo "[$__ticket_id] Not in queue! Mutex file:"
cat $__queue_file
exit 1
fi
awk "!/$__ticket_id/" $__queue_file | awk NF > ${__queue_file}.new
mv ${__queue_file}.new $__queue_file
git add $__queue_file
git commit -m "$__message" --quiet
set +e # allow errors
git push --set-upstream origin $__branch --quiet
__has_error=$((__has_error + $?))
set -e
if [ ! $__has_error -eq 0 ]; then
sleep 1
dequeue $@
fi
}