Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
API
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Psentee
API
Commits
3eac2494
Commit
3eac2494
authored
1 year ago
by
q3k
Browse files
Options
Downloads
Patches
Plain Diff
api: add stars to apps
parent
288f4323
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
server_apps.go
+40
-0
40 additions, 0 deletions
server_apps.go
with
40 additions
and
0 deletions
server_apps.go
+
40
−
0
View file @
3eac2494
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"io"
"io"
"log"
"log"
"net/http"
"net/http"
"net/url"
"path"
"path"
"regexp"
"regexp"
"strings"
"strings"
...
@@ -33,6 +34,11 @@ type appInfo struct {
...
@@ -33,6 +34,11 @@ type appInfo struct {
description
string
description
string
version
int
version
int
commit
string
commit
string
stars
int
}
type
GLProject
struct
{
StarCount
int
`json:"star_count"`
}
}
var
(
var
(
...
@@ -40,6 +46,32 @@ var (
...
@@ -40,6 +46,32 @@ var (
reAppRepo
=
regexp
.
MustCompile
(
`^([a-zA-Z\-_\.0-9]+)/([a-zA-Z\-_0-9]+)$`
)
reAppRepo
=
regexp
.
MustCompile
(
`^([a-zA-Z\-_\.0-9]+)/([a-zA-Z\-_0-9]+)$`
)
)
)
func
(
s
*
server
)
getStars
(
ctx
context
.
Context
,
repo
string
)
(
int
,
error
)
{
path
:=
fmt
.
Sprintf
(
"https://%s/api/v4/projects/%s"
,
flagGitlabHost
,
url
.
PathEscape
(
repo
))
req
,
err
:=
http
.
NewRequestWithContext
(
ctx
,
"GET"
,
path
,
nil
)
if
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"when building request: %w"
,
err
)
}
res
,
err
:=
http
.
DefaultTransport
.
RoundTrip
(
req
)
if
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"when performing request: %w"
,
err
)
}
defer
res
.
Body
.
Close
()
if
res
.
StatusCode
!=
200
{
return
0
,
fmt
.
Errorf
(
"invalid response code %d"
,
res
.
StatusCode
)
}
var
project
GLProject
j
:=
json
.
NewDecoder
(
res
.
Body
)
err
=
j
.
Decode
(
&
project
)
if
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"when performing request: %w"
,
err
)
}
return
project
.
StarCount
,
nil
}
func
(
s
*
server
)
parseAppToml
(
ctx
context
.
Context
,
pathInRepo
string
,
obj
*
object
.
Commit
)
(
*
appInfo
,
error
)
{
func
(
s
*
server
)
parseAppToml
(
ctx
context
.
Context
,
pathInRepo
string
,
obj
*
object
.
Commit
)
(
*
appInfo
,
error
)
{
p
:=
path
.
Join
(
pathInRepo
,
"flow3r.toml"
)
p
:=
path
.
Join
(
pathInRepo
,
"flow3r.toml"
)
f
,
err
:=
obj
.
File
(
p
)
f
,
err
:=
obj
.
File
(
p
)
...
@@ -92,6 +124,7 @@ func (s *server) parseAppToml(ctx context.Context, pathInRepo string, obj *objec
...
@@ -92,6 +124,7 @@ func (s *server) parseAppToml(ctx context.Context, pathInRepo string, obj *objec
if
len
(
data
.
Metadata
.
Description
)
>
140
{
if
len
(
data
.
Metadata
.
Description
)
>
140
{
return
nil
,
fmt
.
Errorf
(
"metadata description too long"
)
return
nil
,
fmt
.
Errorf
(
"metadata description too long"
)
}
}
return
&
appInfo
{
return
&
appInfo
{
name
:
data
.
App
.
Name
,
name
:
data
.
App
.
Name
,
author
:
data
.
Metadata
.
Author
,
author
:
data
.
Metadata
.
Author
,
...
@@ -167,6 +200,11 @@ func (s *server) getAppInfo(ctx context.Context, pathInRepo, repo string) (*appI
...
@@ -167,6 +200,11 @@ func (s *server) getAppInfo(ctx context.Context, pathInRepo, repo string) (*appI
if
highsetVerNil
{
if
highsetVerNil
{
return
nil
,
fmt
.
Errorf
(
"no version"
)
return
nil
,
fmt
.
Errorf
(
"no version"
)
}
}
stars
,
err
:=
s
.
getStars
(
ctx
,
repo
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"getting stars failed: %w"
,
err
)
}
firstTime
[
highestVer
]
.
stars
=
stars
return
firstTime
[
highestVer
],
nil
return
firstTime
[
highestVer
],
nil
}
}
...
@@ -281,6 +319,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) {
...
@@ -281,6 +319,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) {
Author
string
`json:"author"`
Author
string
`json:"author"`
Description
string
`json:"description"`
Description
string
`json:"description"`
Version
int
`json:"version"`
Version
int
`json:"version"`
Stars
int
`json:"stars"`
}
}
type
res
struct
{
type
res
struct
{
...
@@ -308,6 +347,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) {
...
@@ -308,6 +347,7 @@ func (s *server) handleApps(w http.ResponseWriter, r *http.Request) {
Author
:
a
.
appInfo
.
author
,
Author
:
a
.
appInfo
.
author
,
Description
:
a
.
appInfo
.
description
,
Description
:
a
.
appInfo
.
description
,
Version
:
a
.
appInfo
.
version
,
Version
:
a
.
appInfo
.
version
,
Stars
:
a
.
appInfo
.
stars
,
})
})
}
}
w
.
Header
()
.
Add
(
"Access-Control-Allow-Origin"
,
"*"
)
w
.
Header
()
.
Add
(
"Access-Control-Allow-Origin"
,
"*"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment