1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Fast forward a green branch to master.
|
4 | #
|
5 | # Usage:
|
6 | # soil/maybe-merge.sh <function name>
|
7 |
|
8 | set -o nounset
|
9 | set -o pipefail
|
10 | set -o errexit
|
11 |
|
12 | fast-forward() {
|
13 | # Generate a token in "Settings" -> Developer Settings
|
14 | # https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
|
15 | # Should be a secret in Github Actions
|
16 | local github_token=$1
|
17 |
|
18 | local commit_hash=${2:-}
|
19 | local to_branch=${3:-'master'}
|
20 |
|
21 | # local testing
|
22 | if test -z "$github_token"; then
|
23 | # set by YAML
|
24 | github_token=${SOIL_GITHUB_API_TOKEN:-}
|
25 |
|
26 | # Local testing
|
27 | if test -z "$github_token"; then
|
28 | github_token=$(cat token.txt)
|
29 | fi
|
30 | fi
|
31 | if test -z "$commit_hash"; then
|
32 | # $GITHUB_SHA is the commit, set by Github Actions
|
33 | commit_hash=${GITHUB_SHA:-}
|
34 |
|
35 | # Local testing
|
36 | if test -z "$commit_hash"; then
|
37 | commit_hash='ae02c9d6e8ba8e19399de556292a1d93faa220d3'
|
38 | fi
|
39 | fi
|
40 |
|
41 | # Adapted from
|
42 | # https://stackoverflow.com/questions/55800253/how-can-i-do-a-fast-forward-merge-using-the-github-api
|
43 | #
|
44 | # https://docs.github.com/en/rest/git/refs#update-a-reference
|
45 |
|
46 | local response=_tmp/soil/gh-fast-forward.json
|
47 |
|
48 | echo
|
49 | echo "Trying to fast-forward branch $to_branch to commit $commit_hash"
|
50 | echo
|
51 |
|
52 | curl \
|
53 | -o $response \
|
54 | -X PATCH \
|
55 | -H "Content-Type: application/json" \
|
56 | -H "Accept: application/vnd.github.v3+json" \
|
57 | -H "Authorization: token ${github_token}" \
|
58 | https://api.github.com/repos/oilshell/oil/git/refs/heads/$to_branch \
|
59 | -d '{"sha": "'$commit_hash'", "force": false }'
|
60 |
|
61 | local error
|
62 | error=$(cat $response | jq '.message')
|
63 |
|
64 | local ret
|
65 | if test "$error" = 'null'; then
|
66 | echo "Success:"
|
67 | ret=0
|
68 | else
|
69 | echo 'ERROR fast forwarding:'
|
70 | ret=1
|
71 | fi
|
72 |
|
73 | cat $response
|
74 | return $ret
|
75 | }
|
76 |
|
77 | test-fast-forward() {
|
78 | fast-forward '' '' dev-andy-3
|
79 | }
|
80 |
|
81 | all-status-zero() {
|
82 | ### Do all files contain status 0?
|
83 |
|
84 | for path in "$@"; do
|
85 | # There may be a newline on the end, which 'read' stops at.
|
86 | read -r status unused_job_id < $path
|
87 |
|
88 | if test "$status" != '0'; then
|
89 | echo "$path = $status"
|
90 | return 1
|
91 | fi
|
92 | done
|
93 |
|
94 | return 0
|
95 | }
|
96 |
|
97 | soil-run() {
|
98 | local github_token=${1:-} # SOIL_GITHUB_API_TOKEN
|
99 | local run_id=${2:-} # $GITHUB_RUN_ID
|
100 | local commit_hash=${3:-} # GITHUB_SHA
|
101 | local to_branch=${4:-} # defaults to master
|
102 |
|
103 | if test -z "$run_id"; then
|
104 | # GITHUB_RUN_ID is set by Github Actions
|
105 | run_id=${GITHUB_RUN_ID:-}
|
106 |
|
107 | # local testing
|
108 | if test -z "$run_id"; then
|
109 | run_id='2526880241'
|
110 | fi
|
111 | fi
|
112 |
|
113 | local branch=$(git rev-parse --abbrev-ref HEAD)
|
114 | echo "Should we auto-merge branch $branch to master?"
|
115 |
|
116 | if test "$branch" != 'soil-staging'; then
|
117 | echo 'No, only soil-staging is merged to master'
|
118 | return
|
119 | fi
|
120 |
|
121 | local dir=_tmp/status-api
|
122 | rm -f -v $dir/*
|
123 | mkdir -p $dir
|
124 |
|
125 | # These tiny files are written by each Soil task
|
126 | local url_base="http://travis-ci.oilshell.org/status-api/github/$run_id"
|
127 |
|
128 | #local jobs='dummy pea other-tests' # minimal set of jobs to wait for
|
129 | local jobs=$(soil/worker.sh list-jobs)
|
130 |
|
131 | local -a args=()
|
132 | for job in $jobs; do # relies on word splitting
|
133 |
|
134 | # output each URL in a different file
|
135 | args=( "${args[@]}" -o $dir/$job $url_base/$job )
|
136 | done
|
137 |
|
138 | curl -v ${args[@]}
|
139 |
|
140 | if all-status-zero $dir/*; then
|
141 | fast-forward "$github_token" "$commit_hash" "$to_branch"
|
142 | fi
|
143 | }
|
144 |
|
145 | test-soil-run() {
|
146 | # test with non-master branch
|
147 | # other params have testing defaults
|
148 | soil-run '' '' '' '' dev-andy-3
|
149 | }
|
150 |
|
151 | "$@"
|