This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
executable file
·145 lines (138 loc) · 6.73 KB
/
update.sh
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
set -e
set -u
set -o pipefail
echo "::group::Parsing inputs"
owner="$1"
echo "owner=${owner}"
project_name="$2"
echo "project_name=${project_name}"
timestamp="$(date -u +"%Y-%m-%d %H:%M")"
echo "timestamp=${timestamp}"
date="$(date -u +"%d/%m/%Y")"
echo "date=${date}"
echo "::endgroup::"
echo "::group::Checking the type of the owner"
type="$(gh api "users/${owner}" --jq '.type' | tr '[:upper:]' '[:lower:]')"
echo "type=${type}"
echo "::endgroup::"
echo "::group::Retrieving project"
project="$(gh api graphql -f query='query($login: String!, $project_name: String!, $endCursor: String) {
'"$type"'(login: $login) {
projectsNext(first: 1, query: $project_name) {
nodes {
id
fields(first: 100) {
nodes {
id
name
settings
}
}
items(first: 100, after: $endCursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
title
fieldValues(first: 100) {
nodes {
value
projectField {
id
}
}
}
}
}
}
}
}
}' -f login="${owner}" -f project_name="${project_name}" --jq '.data.'"$type"'.projectsNext.nodes[0]' --paginate | jq -nc '[inputs] | .[0] + {"items":{"nodes":(map(.items.nodes) | add)}}')"
echo "project=$(jq '.id' <<< "${project}")"
echo "::endgroup::"
echo "::group::Retrieving information about project"
project_id="$(jq -r '.id' <<< "${project}")"
echo "project_id=${project_id}"
project_status_field="$(jq -c '.fields.nodes | map(select(.name == "Status")) | .[0]' <<< "${project}")"
echo "project_status_field=$(jq -r '.id' <<< "${project_status_field}")"
project_status_field_id="$(jq -r '.id' <<< "${project_status_field}")"
echo "project_status_field_id=${project_status_field_id}"
project_status_field_settings="$(jq -r '.settings' <<< "${project_status_field}")"
echo "project_status_field_settings=${project_status_field_settings}"
project_status_history_field="$(jq -c '.fields.nodes | map(select(.name == "Status History")) | .[0]' <<< "${project}")"
echo "project_status_history_field=$(jq -r '.id' <<< "${project_status_history_field}")"
project_status_history_field_id="$(jq -r '.id' <<< "${project_status_history_field}")"
echo "project_status_history_field_id=${project_status_history_field_id}"
project_status_timestamp_field="$(jq -c '.fields.nodes | map(select(.name == "Status Timestamp")) | .[0]' <<< "${project}")"
echo "project_status_timestamp_field=$(jq -r '.id' <<< "${project_status_timestamp_field}")"
project_status_timestamp_field_id="$(jq -r '.id' <<< "${project_status_timestamp_field}")"
echo "project_status_timestamp_field_id=${project_status_timestamp_field_id}"
project_status_date_field="$(jq -c '.fields.nodes | map(select(.name == "Status Date")) | .[0]' <<< "${project}")"
echo "project_status_date_field=$(jq -r '.id' <<< "${project_status_date_field}")"
project_status_date_field_id="$(jq -r '.id' <<< "${project_status_date_field}")"
echo "project_status_date_field_id=${project_status_date_field_id}"
echo "::endgroup::"
echo "::group::Processing project items"
while read project_item_id; do
if [[ -z "$project_item_id" ]]; then
continue
fi
echo "project_item_id=${project_item_id}"
project_item="$(jq -c '.items.nodes | map(select(.id == $id)) | .[0]' --arg id "${project_item_id}" <<< "${project}")"
echo "project_item=$(jq -r '.id' <<< "$project_item")"
if [[ "$(jq -r '.title' <<< "$project_item")" == "You can't see this item" ]]; then
echo "::warning title=Inaccessible project item::The provided GitHub token cannot update the item with ID ${project_item_id}"
continue
fi
project_item_status_history_field_value="$(jq -r '.fieldValues.nodes | map(select(.projectField.id == $id)) | .[0].value // "[]"' --arg id "${project_status_history_field_id}" <<< "${project_item}")"
echo "project_item_status_history_field_value=${project_item_status_history_field_value}"
project_item_status_field_value="$(jq -r '.fieldValues.nodes | map(select(.projectField.id == $id)) | .[0].value // ""' --arg id "${project_status_field_id}" <<< "${project_item}")"
echo "project_item_status_field_value=${project_item_status_field_value}"
project_item_status_field_value_name="$(jq -r '.options | map(select(.id == $id)) | .[0].name // ""' --arg id "${project_item_status_field_value}" <<< "${project_status_field_settings}")"
echo "project_item_status_field_value_name=${project_item_status_field_value_name}"
if [[ "$(jq -r '.[0] // null' <<< "${project_item_status_history_field_value}")" == "${project_item_status_field_value_name}" ]]; then
continue
fi
project_item_status_history_field_value="$(jq -c '[$status] + .[:1]' --arg status "${project_item_status_field_value_name}" <<< "${project_item_status_history_field_value}")"
echo "project_item_status_history_field_value=${project_item_status_history_field_value}"
> /dev/null gh api graphql -f query='mutation($project_id: ID!, $project_item_id: ID!, $project_status_history_field_id: ID!, $project_item_status_history_field_value: String!) {
updateProjectNextItemField(input: {
projectId: $project_id,
itemId: $project_item_id,
fieldId: $project_status_history_field_id,
value: $project_item_status_history_field_value,
}) {
projectNextItem {
id
}
}
}' -f project_id="${project_id}" -f project_item_id="${project_item_id}" -f project_status_history_field_id="${project_status_history_field_id}" -f project_item_status_history_field_value="${project_item_status_history_field_value}"
> /dev/null gh api graphql -f query='mutation($project_id: ID!, $project_item_id: ID!, $project_status_timestamp_field_id: ID!, $timestamp: String!) {
updateProjectNextItemField(input: {
projectId: $project_id,
itemId: $project_item_id,
fieldId: $project_status_timestamp_field_id,
value: $timestamp,
}) {
projectNextItem {
id
}
}
}' -f project_id="${project_id}" -f project_item_id="${project_item_id}" -f project_status_timestamp_field_id="${project_status_timestamp_field_id}" -f timestamp="${timestamp}"
> /dev/null gh api graphql -f query='mutation($project_id: ID!, $project_item_id: ID!, $project_status_date_field_id: ID!, $date: String!) {
updateProjectNextItemField(input: {
projectId: $project_id,
itemId: $project_item_id,
fieldId: $project_status_date_field_id,
value: $date,
}) {
projectNextItem {
id
}
}
}' -f project_id="${project_id}" -f project_item_id="${project_item_id}" -f project_status_date_field_id="${project_status_date_field_id}" -f date="${date}"
done <<< "$(jq -r '.items.nodes[].id' <<< "$project")"
echo "::endgroup"