From 78c42f60fa754e559f478fa82202cfdf35c99112 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sun, 25 Apr 2021 13:16:45 +0900 Subject: [PATCH] watchbot: Assign a buildid when a new build job is enqueued When a repository change is detected, watchbot only places the path and branch name in the queue. To track a build through the entire build system, a build id should be assigned as well. This commit modifies watchbot to also place a build id in the queue. --- watchbot.sh | 99 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/watchbot.sh b/watchbot.sh index fb76c7b..9027ad9 100755 --- a/watchbot.sh +++ b/watchbot.sh @@ -1,5 +1,85 @@ #!/bin/bash +userinput_to_repo() { + local watch="$1" + + if [[ "$watch" == *"#"* ]]; then + echo "${watch%#*}" + else + echo "$watch" + fi + + return 0 +} + +userinput_to_branch() { + local watch="$1" + + if [[ "$watch" == *"#"* ]]; then + echo "${watch##*#}" + else + echo "master" + fi + + return 0 +} + +watch_to_repo() { + local watch="$1" + + local repobase + + repobase="${watch%/refs/heads/*}" + repobase="${repobase%/.git}" + + echo "$repobase" + return 0 +} + +watch_to_branch() { + local watch="$1" + + echo "${watch##*/}" + return 0 +} + +make_buildid() { + local timestamp + local suffix + + timestamp=$(date +"%Y%m%d-%H%M") + suffix=$(printf "%04d" "$((RANDOM % 10000))") + + echo "$timestamp-$suffix" +} + +enqueue_watch() { + local queue="$1" + local watch="$2" + + local repository + local branch + local buildid + + buildid=$(make_buildid) + repository=$(watch_to_repo "$watch") + branch=$(watch_to_branch "$watch") + + cat < $cur_head" if [[ "$cur_head" != "${heads[$i]}" ]]; then - if ! queue_put "$queue" "${watches[$i]}"; then + log_error "Change detected: ${watches[$i]}" + + if ! enqueue_watch "$queue" "${watches[$i]}"; then log_error "Could not place item in queue" continue fi @@ -55,20 +137,15 @@ watchlist_add() { return 0 } -watch_to_head() { +userinput_to_head() { local watch="$1" local repo local branch local head - if [[ "$watch" == *"#"* ]]; then - repo="${watch%#*}" - branch="${watch##*#}" - else - repo="$watch" - branch="master" - fi + repo=$(userinput_to_repo "$watch") + branch=$(userinput_to_branch "$watch") if [ -d "$repo/.git" ]; then head="$repo/.git/refs/heads/$branch" @@ -100,13 +177,13 @@ main() { return 1 fi - queue=$(opt_get "input") + queue=$(opt_get "output") watches=() for watch in "${watchlist[@]}"; do local head - if ! head=$(watch_to_head "$watch"); then + if ! head=$(userinput_to_head "$watch"); then log_error "Cannot resolve $watch" return 1 fi -- 2.47.3