forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.nu
39 lines (37 loc) · 1.26 KB
/
gitlab.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
#!/usr/bin/env nu
let base_url = ""
let page_size = 100
let projects = $"($base_url)/api/v4/projects/"
def call-gitlab [
...args: string
--query: string
] {
http get -H [Authorization $"Bearer ($env.GITLAB_TOKEN)"] $"($projects)($args|str join)?($query)"
}
# Search files on your GitLab server
def main [
--base_url: string # base url of your GitLab instance
--file: string # file (or path to file if in a subfolder) you want to scan
--phrase: string # phrase you want to search for
--branch: string # branch to scan
] {
# /projects endpoint can return up to $page_size items which is why we need multiple calls to retrieve full list
let num_of_pages = ((call-gitlab --query 'page=1&per_page=1&order_by=id&simple=true'|get id.0|into int) / $page_size|math round)
seq 1 $num_of_pages|par-each {|page|
call-gitlab --query $"page=($page)&per_page=($page_size)"|select name id
}
|flatten
|par-each {|repo|
let payload = (call-gitlab $repo.id '/repository/files/' $file --query $"ref=($branch)")
if ($payload|columns|find message|is-empty) {
$payload
|get content
|decode base64
|lines
|find $phrase
|if ($in|length) > 0 {
echo $"($file) in ($repo.name) repo contains ($phrase) phrase"
}
}
}
}