-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[soil] Implemented 'maybe-merge' task.
This is #745. Right now it's gated on just two tasks: dummy and dev-minimal. Notes: 1. I figured out the Github API to fast forward. 2. I generated an API token from the web interface 3. Put that token into Github Actions
- Loading branch information
Andy C
committed
Jun 21, 2022
1 parent
ae02c9d
commit b024287
Showing
2 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,138 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Fast forward a green branch to master. | ||
# | ||
# Usage: | ||
# ./maybe-merge.sh <function name> | ||
# soil/maybe-merge.sh <function name> | ||
|
||
set -o nounset | ||
set -o pipefail | ||
set -o errexit | ||
|
||
fast-forward() { | ||
# Generate a token in "Settings" -> Developer Settings | ||
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token | ||
# Should be a secret in Github Actions | ||
local github_token=$1 | ||
|
||
local commit_hash=${2:-} | ||
local to_branch=${3:-'master'} | ||
|
||
# local testing | ||
if test -z "$github_token"; then | ||
# set by YAML | ||
github_token=${SOIL_GITHUB_API_TOKEN:-} | ||
|
||
# Local testing | ||
if test -z "$github_token"; then | ||
github_token=$(cat token.txt) | ||
fi | ||
fi | ||
if test -z "$commit_hash"; then | ||
# $GITHUB_SHA is the commit, set by Github Actions | ||
commit_hash=${GITHUB_SHA:-} | ||
|
||
# Local testing | ||
if test -z "$commit_hash"; then | ||
commit_hash='ae02c9d6e8ba8e19399de556292a1d93faa220d3' | ||
fi | ||
fi | ||
|
||
# Adapted from | ||
# https://stackoverflow.com/questions/55800253/how-can-i-do-a-fast-forward-merge-using-the-github-api | ||
|
||
local response=_tmp/soil/gh-fast-forward.json | ||
|
||
curl \ | ||
-o $response \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: token ${github_token}" \ | ||
-X PATCH \ | ||
https://api.github.com/repos/oilshell/oil/git/refs/heads/$to_branch \ | ||
-d '{"sha": "'$commit_hash'", "force": false }' | ||
|
||
local error | ||
error=$(cat $response | jq '.message') | ||
|
||
if test "$error" = 'null'; then | ||
echo "Fast-forwarded branch $to_branch to commit $commit_hash" | ||
else | ||
echo 'ERROR fast forwarding:' | ||
fi | ||
|
||
cat $response | ||
} | ||
|
||
test-fast-forward() { | ||
fast-forward '' '' dev-andy-3 | ||
} | ||
|
||
all-status-zero() { | ||
### Do all files contain status 0? | ||
|
||
for path in "$@"; do | ||
# There may be a newline on the end, which 'read' stops at. | ||
read -r st < $path | ||
|
||
if test "$st" != '0'; then | ||
echo "$path = $st" | ||
return 1 | ||
fi | ||
done | ||
|
||
return 0 | ||
} | ||
|
||
soil-run() { | ||
echo 'Hello from maybe-merge.sh' | ||
local github_token=${1:-} # SOIL_GITHUB_API_TOKEN | ||
local run_id=${2:-} # $GITHUB_RUN_ID | ||
local jobs=${3:-'dummy dev-minimal'} # minimal set of jobs to wait for | ||
local commit_hash=${4:-} # GITHUB_SHA | ||
local to_branch=${5:-} # defaults to master | ||
|
||
if test -z "$run_id"; then | ||
# GITHUB_RUN_ID is set by Github Actions | ||
run_id=${GITHUB_RUN_ID:-} | ||
|
||
# local testing | ||
if test -z "$run_id"; then | ||
run_id='2526880241' | ||
fi | ||
fi | ||
|
||
local branch=$(git rev-parse --abbrev-ref HEAD) | ||
echo "BRANCH = $branch" | ||
echo "Should we auto-merge branch $branch to master?" | ||
|
||
if test "$branch" = 'soil-staging'; then | ||
echo 'TODO: check all state' | ||
if test "$branch" != 'soil-staging'; then | ||
echo 'No, must be soil-staging' | ||
#return | ||
fi | ||
|
||
local dir=_tmp/status-api | ||
rm -f -v $dir/* | ||
mkdir -p $dir | ||
|
||
# These tiny files are written by each Soil task | ||
local url_base="http://travis-ci.oilshell.org/status-api/github/$run_id" | ||
|
||
local -a args=() | ||
for job in $jobs; do # relies on word splitting | ||
|
||
# output each URL in a different file | ||
args=("${args[@]}" -o $dir/$job $url_base/$job ) | ||
done | ||
|
||
curl -v ${args[@]} | ||
|
||
if all-status-zero $dir/*; then | ||
fast-forward "$github_token" "$commit_hash" "$to_branch" | ||
fi | ||
} | ||
|
||
test-soil-run() { | ||
# test with non-master branch | ||
# other params have testing defaults | ||
soil-run '' '' '' '' dev-andy-3 | ||
} | ||
|
||
"$@" |