-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.sh
executable file
·135 lines (123 loc) · 6.21 KB
/
migrate.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
#!/bin/bash
# Copyright Broadcom, Inc. All Rights Reserved.
# SPDX-License-Identifier: APACHE-2.0
# shellcheck disable=SC2016
set -o errexit
set -o nounset
set -o pipefail
echo "::group::Parsing inputs"
organization="$1"
echo "organization=${organization}"
legacy_project_board_name="$2"
echo "legacy_project_board_name=${legacy_project_board_name}"
new_project_number="$3"
echo "new_project_number=${new_project_number}"
repo="$4"
echo "repo=${repo}"
echo "::endgroup::"
echo "::group::Retrieving legacy project board"
legacy_project_board="$(
gh api --paginate "repos/${organization}/${repo}/projects" --jq "map(select(.name == \"${legacy_project_board_name}\"))" |
jq -n '[inputs] | add | .[0]'
)"
echo "legacy_project_board=$(jq '.id' <<< "${legacy_project_board}")"
echo "::endgroup::"
echo "::group::Retrieving new project"
new_project="$(
gh api graphql -f query='query($org: String!, $new_project_number: Int!) {
organization(login: $org) {
projectV2(number: $new_project_number) {
id
field(name: "Status"){
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}' -f org="${organization}" -F new_project_number="${new_project_number}" --jq '.data.organization.projectV2'
)"
echo "new_project=$(jq '.id' <<< "${new_project}")"
echo "::endgroup::"
echo "::group::Retrieving information about legacy project board and new project"
new_project_id="$(jq -r '.id' <<< "${new_project}")"
echo "new_project_id=${new_project_id}"
legacy_project_board_id="$(jq '.id' <<< "${legacy_project_board}")"
echo "legacy_project_board_id=${legacy_project_board_id}"
legacy_project_board_columns="$(gh api --paginate "projects/${legacy_project_board_id}/columns" | jq -n '[inputs] | add')"
echo "legacy_project_board_columns=$(jq 'map(.id)' <<< "${legacy_project_board_columns}")"
new_project_status_field="$(jq -r '.field' <<< "${new_project}")"
echo "new_project_status_field=${new_project_status_field}"
new_project_status_field_id="$(jq -r '.id' <<< "${new_project_status_field}")"
echo "new_project_status_field_id=${new_project_status_field_id}"
new_project_status_field_options="$(jq -r '.options' <<< "${new_project_status_field}")"
echo "new_project_status_field_options=${new_project_status_field_options}"
echo "::endgroup::"
echo "::group::Synchronising cards"
while read -r legacy_project_board_column_id; do
if [[ -z "$legacy_project_board_column_id" ]]; then
continue
fi
echo "legacy_project_board_column_id=${legacy_project_board_column_id}"
legacy_project_board_column_name="$(jq -r 'map(select(.id == $legacy_project_board_column_id)) | .[0].name' --argjson legacy_project_board_column_id "${legacy_project_board_column_id}" <<< "$legacy_project_board_columns")"
echo "legacy_project_board_column_name=${legacy_project_board_column_name}"
legacy_project_board_cards="$(gh api --paginate "projects/columns/${legacy_project_board_column_id}/cards" | jq -n '[inputs] | add')"
echo "legacy_project_board_cards=$(jq 'map(.id)' <<< "${legacy_project_board_cards}")"
new_project_status_field_option_id="$(jq -r 'map(select(.name == $legacy_project_board_column_name)) | .[0].id // ""' --arg legacy_project_board_column_name "${legacy_project_board_column_name}" <<< "${new_project_status_field_options}")"
echo "new_project_status_field_option_id=${new_project_status_field_option_id}"
while read -r legacy_project_board_card_id; do
if [[ -z "$legacy_project_board_card_id" ]]; then
continue
fi
echo "legacy_project_board_card_id=${legacy_project_board_card_id}"
legacy_project_board_card="$(jq -r 'map(select(.id == $legacy_project_board_card_id)) | .[0]' --argjson legacy_project_board_card_id "${legacy_project_board_card_id}" <<< "$legacy_project_board_cards")"
legacy_project_board_card_content_url="$(jq -r '.content_url // ""' <<< "${legacy_project_board_card}")"
echo "legacy_project_board_card_content_url=${legacy_project_board_card_content_url}"
if [[ -z "${legacy_project_board_card_content_url}" ]]; then
if [[ ! -z "$repo" ]]; then
legacy_project_board_card_note="$(jq -r '.note // ""' <<< "${legacy_project_board_card}")"
echo "legacy_project_board_card_note=${legacy_project_board_card_note}"
legacy_project_board_card_node_id="$(gh api "repos/${organization}/${repo}/issues" -f title="${legacy_project_board_card_note:0:60}" -f body="${legacy_project_board_card_note}" --jq '.node_id')"
else
continue
fi
else
legacy_project_board_card_node_id="$(gh api "$legacy_project_board_card_content_url" --jq '.node_id')"
fi
echo "legacy_project_board_card_node_id=${legacy_project_board_card_node_id}"
new_project_item_id="$(
gh api graphql -f query='mutation($new_project_id: ID!, $legacy_project_board_card_node_id: ID!) {
addProjectV2ItemById(input: {projectId: $new_project_id, contentId: $legacy_project_board_card_node_id}) {
item {
id
}
}
}' -f new_project_id="${new_project_id}" -f legacy_project_board_card_node_id="${legacy_project_board_card_node_id}" --jq '.data.addProjectV2ItemById.item.id'
)"
echo "new_project_item_id=${new_project_item_id}"
if [[ ! -z "$new_project_status_field_option_id" ]]; then
> /dev/null gh api graphql -f query='mutation($new_project_id: ID!, $new_project_item_id: ID!, $new_project_status_field_id: ID!, $new_project_status_field_option_id: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $new_project_id
itemId: $new_project_item_id
fieldId: $new_project_status_field_id,
value: {
singleSelectOptionId: $new_project_status_field_option_id
}
}
) {
projectV2Item {
id
}
}
}' -f new_project_id="${new_project_id}" -f new_project_item_id="${new_project_item_id}" -f new_project_status_field_id="${new_project_status_field_id}" -f new_project_status_field_option_id="${new_project_status_field_option_id}"
fi
done <<< "$(jq '.[].id' <<< "$legacy_project_board_cards")"
done <<< "$(jq '.[].id' <<< "$legacy_project_board_columns")"
echo "::endgroup::"