-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.nu
167 lines (129 loc) · 4.34 KB
/
script.nu
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
let SYSML_URL = 'https://sysml-v2.ft-ssy-stonks.intra.dlr.de'
module sysml {
def project_ids [ ] {
projects | each { |it| { value: $it.'@id', description: $it.name } }
}
# Get all projects, or one specific project
export def projects [
# id of the project to get
project_id?: string@project_ids
] {
if ($project_id == null) {
http get $"($SYSML_URL)/projects"
} else {
http get $"($SYSML_URL)/projects/($project_id)"
}
}
def branch_ids [context: string] {
let project_id = ($context | str trim | split row ' ' | last)
branches $project_id
| each { |it| { value: $it.'@id', description: $it.name } }
}
# Get all branches of a project, or a specific branch
export def branches [
# id of the project whose branches to get
project_id: string@project_ids
# id of the branch to get
branch_id?: string@branch_ids
] {
if ($branch_id == null) {
http get $"($SYSML_URL)/projects/($project_id)/branches"
} else {
http get $"($SYSML_URL)/projects/($project_id)/branches/($branch_id)"
}
}
def tag_ids [context: string] {
let project_id = ($context | str trim | split row ' ' | last)
tags $project_id
| each { |it| { value: $it.'@id', description: $it.name } }
}
# Get all tags of a project, or a specific tag
export def tags [
# id of the project whose tags to get
project_id: string@project_ids
# id of the branch to get
tag_id?: string@tag_ids
] {
if ($tag_id == null) {
http get $"($SYSML_URL)/projects/($project_id)/tags"
} else {
http get $"($SYSML_URL)/projects/($project_id)/tags/($tag_id)"
}
}
def commit_ids [context: string] {
let project_id = ($context | str trim | split row ' ' | last)
commits $project_id
| update created {|it| $it.created | into datetime}
| sort-by created --reverse
| each { |it| { value: $it.'@id', description: $"($it.created): ($it.description)" } }
}
# Get all commits of a project, or a specific commit
export def commits [
# id of the project whose commits to get
project_id: string@project_ids
# id of the branch to get
commit_id?: string@commit_ids
] {
if ($commit_id == null) {
http get $"($SYSML_URL)/projects/($project_id)/commits"
} else {
http get $"($SYSML_URL)/projects/($project_id)/commits/($commit_id)"
}
}
def element_ids [context: string] {
let ids = ($context | str trim | split row ' ' | last 2)
elements $ids.0 $ids.1
| sort-by 'qualifiedName'
| each { |it| { value: $it.'@id', description: $"($it.qualifiedName) [($it.'@type')]" } }
}
# Get all commits of a project, or a specific commit
export def elements [
# id of the project whose commits to get
project_id: string@project_ids
# id of the branch to get
commit_id: string@commit_ids
# id of the branch to get
element_id?: string@element_ids
# number of elements to fetch
--page-size: int = 100
] {
if ($element_id == null) {
http get $"($SYSML_URL)/projects/($project_id)/commits/($commit_id)/elements?page%5Bsize%5D=($page_size)"
} else {
http get $"($SYSML_URL)/projects/($project_id)/commits/($commit_id)/elements/($element_id)"
}
}
# fetch an element, and its direct sub-elements
export def fetch-subelements [
# id of the project whose commits to get
project_id: string@project_ids
# id of the branch to get
commit_id: string@commit_ids
# id of the branch to get
element_id: string@element_ids
] {
mut element = (elements $project_id $commit_id $element_id)
for $field_name in ($element | columns) {
let $field = ($element | get $field_name)
let $field_type = ($field | describe)
# if element is a list of subelements
if ($field_type == "table<@id: string>") {
$element = ($element | update $field_name {|_|
$field | each {|row|
let $new_element_id = ($row | get '@id')
elements $project_id $commit_id $new_element_id
}
})
}
# if element is just a record
if ($field_type == "record<@id: string>") {
let new_element_id = ($field | get '@id')
$element = ($element | update $field_name {|_|
elements $project_id $commit_id $new_element_id
})
}
}
$element
}
}
use sysml *