-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpending-commits.sh
More file actions
executable file
·93 lines (75 loc) · 2.53 KB
/
Copy pathpending-commits.sh
File metadata and controls
executable file
·93 lines (75 loc) · 2.53 KB
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
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source "scripts/lib/common-functions.sh"
readonly REPO_TAG="Opetushallitus/otuva"
function main {
require_command jq
git fetch --tags --force > /dev/null
print_pending_commits "$@" | less --no-init --quit-if-one-screen --RAW-CONTROL-CHARS
}
function log_cmd {
local fmt="%C(bold blue)%h%C(reset) %C(green)(%cI)%C(reset) %s %C(cyan)<%an>%C(reset)"
git --no-pager log --pretty=format:"$fmt" --color --left-only "$@"
}
function fetch_pipeline_pairs {
aws resourcegroupstaggingapi get-resources \
--tag-filters "Key=Repository,Values=${REPO_TAG}" \
--resource-type-filters codepipeline \
| jq -c '
.ResourceTagMappingList
| map({
from: ((.Tags // []) | map(select(.Key=="FromBranch"))[0].Value // null),
to: ((.Tags // []) | map(select(.Key=="ToBranch"))[0].Value // null)
})
| map(select(.from != null and .to != null))
'
}
function assert_unique_to_branches {
local -r pairs="$1"
local duplicates
duplicates=$(echo "$pairs" | jq -r 'group_by(.to) | map(select(length > 1) | .[0].to) | .[]')
if [[ -n "${duplicates}" ]]; then
fatal "Multiple pipelines share the same ToBranch tag: ${duplicates}"
fi
}
function default_branch {
local head
head=$(git symbolic-ref --short refs/remotes/origin/HEAD)
echo "${head#origin/}"
}
function print_pending_commits {
export_aws_credentials "util"
local pairs
pairs=$(fetch_pipeline_pairs)
if [[ "$(echo "$pairs" | jq 'length')" -eq 0 ]]; then
fatal "No CodePipeline pipelines tagged Repository=${REPO_TAG} found in util account."
fi
assert_unique_to_branches "$pairs"
local heads
heads=$(echo "$pairs" | jq -r '
. as $p
| ($p | map(.from)) as $froms
| $p | map(select(.to as $t | ($froms | index($t)) | not)) | .[].to
')
if [[ -z "${heads}" ]]; then
fatal "Could not determine the leaf(s) of the deploy chain from pipeline tags."
fi
local current from
while IFS= read -r current; do
while :; do
from=$(echo "$pairs" | jq -r --arg t "$current" '.[] | select(.to == $t) | .from')
if [[ -z "${from}" || "${from}" == "null" ]]; then
break
fi
echo "# Commits for ${from} -> ${current}"
log_cmd "origin/${from}...origin/${current}" && echo
current="$from"
done
done <<< "${heads}"
local branch
branch=$(default_branch)
echo "# Commits for ${branch} -> origin/${branch}"
log_cmd "${branch}...origin/${branch}" && echo
}
main "$@"