aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-04-19 01:02:29 +0200
committerTeddy Wing2018-04-19 01:02:29 +0200
commitd8c5d44baed74adb039e9a66768818653213fe39 (patch)
tree55855a24d85279e2f0cb302b4a8d2a27fcf83322
parent5670759a5623fa4cef6cb0d37602bb41b7c34c5c (diff)
downloadredprine-d8c5d44baed74adb039e9a66768818653213fe39.tar.bz2
update_redmine_statuses(): Take pull request JSON instead of TSV
This function now takes an array of pull requests as JSON instead of as TSV. Doing so allows us to pass a single pull request JSON string to `update_redmine_status()` and have it deal with extracting the issue number and pull request URL. Doing this because I want to be able to incrementally update the cache file on successful Redmine update. The functions that extracted the issue ID and pull request URL have been renamed to singular versions of themselves now that they only treat a single pull request hash instead of an array of them. A new function takes an array of pull request hashes and uses `jq` to output each one on a separate line, enabling us to easily loop through each one in `update_redmine_statuses()`.
-rwxr-xr-xredprine52
1 files changed, 30 insertions, 22 deletions
diff --git a/redprine b/redprine
index 4069e95..a9b89eb 100755
--- a/redprine
+++ b/redprine
@@ -86,42 +86,51 @@ function extract_data_from_pull_request_json () {
)'
}
-# Produces a tab-separated list of branch names and pull request URLs from our
-# extracted JSON.
-function branches_and_pull_request_urls () {
- local pull_requests="$1"
+# Produces a tab-separated branch name and pull request URL from our pull
+# request JSON.
+function branch_and_pull_request_url () {
+ local pull_request="$1"
- printf "%s\n" "$pull_requests" |
- jq --raw-output 'map([.ref, .html_url]) | .[] | @tsv'
+ printf "%s\n" "$pull_request" |
+ jq --raw-output '[.ref, .html_url] | @tsv'
}
-# Turns a tab-separated list of branch names and pull request URLs into a
-# tab-separated list of Redmine issue numbers and pull request URLs. Gets the
-# issue number as a four-digit prefix of the branch name.
-function issue_numbers_and_pull_request_urls () {
- local pull_requests="$1"
+# Turns a tab-separated branch name and pull request URL into a tab-separated
+# Redmine issue number and pull request URL. Gets the issue number as a
+# four-digit prefix of the branch name.
+function issue_number_and_pull_request_url () {
+ local pull_request="$1"
- local branch_and_urls=$(branches_and_pull_request_urls "$pull_requests")
+ local branch_and_url=$(branch_and_pull_request_url "$pull_request")
- echo "$branch_and_urls" |
+ echo "$branch_and_url" |
perl -ne '/^(\d{4})-[^\t]*\t(.*)/ && print "$1\t$2\n"'
}
+# Takes a JSON array of Redprine pull request hashes and outputs one hash per
+# line so the hashes can be iterated over in a shell loop.
+function each_pull_request () {
+ local pull_requests="$1"
+
+ printf "%s\n" "$pull_requests" |
+ jq --compact-output '. | .[]'
+}
+
# Updates fields for each given issue.
function update_redmine_statuses () {
- local issue_ids_and_urls="$1"
+ local pull_requests="$1"
- if [ ! -z "$issue_ids_and_urls" ]; then
- for id_and_url in "$issue_ids_and_urls"; do
- update_redmine_status "$id_and_url"
- done
- fi
+ for pull_request in $(each_pull_request "$pull_requests"); do
+ update_redmine_status "$pull_request"
+ done
}
# Given a tab-separated Redmine issue number and GitHub pull request URL, will
# update fields on the given issue.
function update_redmine_status () {
- local issue_pr="$1"
+ local pull_request="$1"
+
+ local issue_pr=$(issue_number_and_pull_request_url "$pull_request")
local issue_id=$(
echo "$issue_pr" |
@@ -173,9 +182,8 @@ function update_cache_with_new_pulls () {
function main () {
local pr_json=$(new_pull_requests)
- local issue_prs=$(issue_numbers_and_pull_request_urls "$pr_json")
- update_redmine_statuses "$issue_prs"
+ update_redmine_statuses "$pr_json"
update_cache_with_new_pulls "$pr_json"
}