daemon: Gitea v1 API adapter (git.westphal.fr is Gitea, not GitLab); fix golang Dockerfile to 1.26; sudo docker in deploy
This commit is contained in:
+26
-25
@@ -38,16 +38,17 @@ type GitLabRepo struct {
|
||||
DefaultBranch string `json:"defaultBranch"`
|
||||
}
|
||||
|
||||
// gitlabProject is the subset of the upstream projects API we map from.
|
||||
type gitlabProject struct {
|
||||
PathWithNamespace string `json:"path_with_namespace"`
|
||||
Name string `json:"name"`
|
||||
Namespace struct {
|
||||
Path string `json:"path"`
|
||||
} `json:"namespace"`
|
||||
LastActivityAt string `json:"last_activity_at"`
|
||||
WebURL string `json:"web_url"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
// giteaRepo is the subset of the upstream Gitea /api/v1/user/repos item we
|
||||
// map from.
|
||||
type giteaRepo struct {
|
||||
FullName string `json:"full_name"`
|
||||
Name string `json:"name"`
|
||||
Owner struct {
|
||||
Login string `json:"login"`
|
||||
} `json:"owner"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
DefaultBranch string `json:"default_branch"`
|
||||
}
|
||||
|
||||
// GitLab stores/validates the PAT in the settings table and lists projects.
|
||||
@@ -92,24 +93,24 @@ func (g *GitLab) Status() map[string]any {
|
||||
|
||||
const settingGitLabUsername string = "gitlab_username"
|
||||
|
||||
// Connect validates the PAT against /api/v4/user and stores it.
|
||||
// Connect validates the token against /api/v1/user and stores it.
|
||||
func (g *GitLab) Connect(ctx context.Context, token string) (string, error) {
|
||||
var user struct {
|
||||
Username string `json:"username"`
|
||||
Login string `json:"login"`
|
||||
}
|
||||
if err := g.do(ctx, "/api/v4/user", token, &user); err != nil {
|
||||
if err := g.do(ctx, "/api/v1/user", token, &user); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if user.Username == "" {
|
||||
return "", &GitLabError{msg: "gitlab returned no username for token"}
|
||||
if user.Login == "" {
|
||||
return "", &GitLabError{msg: "gitea returned no login for token"}
|
||||
}
|
||||
if err := g.store.SetSetting(settingGitLabToken, token); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := g.store.SetSetting(settingGitLabUsername, user.Username); err != nil {
|
||||
if err := g.store.SetSetting(settingGitLabUsername, user.Login); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return user.Username, nil
|
||||
return user.Login, nil
|
||||
}
|
||||
|
||||
// Disconnect drops the stored PAT.
|
||||
@@ -138,22 +139,22 @@ func (g *GitLab) Repos(ctx context.Context) ([]GitLabRepo, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var projects []gitlabProject
|
||||
path := fmt.Sprintf("/api/v4/projects?membership=true&order_by=last_activity_at&per_page=%d", projectsPerPage)
|
||||
var projects []giteaRepo
|
||||
path := fmt.Sprintf("/api/v1/user/repos?limit=%d", projectsPerPage)
|
||||
if err := g.do(ctx, path, token, &projects); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
repos := make([]GitLabRepo, 0, len(projects))
|
||||
for _, p := range projects {
|
||||
if p.PathWithNamespace == "" {
|
||||
if p.FullName == "" {
|
||||
continue
|
||||
}
|
||||
repos = append(repos, GitLabRepo{
|
||||
Path: p.PathWithNamespace,
|
||||
Path: p.FullName,
|
||||
Name: p.Name,
|
||||
Namespace: p.Namespace.Path,
|
||||
LastActivityAt: p.LastActivityAt,
|
||||
WebURL: p.WebURL,
|
||||
Namespace: p.Owner.Login,
|
||||
LastActivityAt: p.UpdatedAt,
|
||||
WebURL: p.HTMLURL,
|
||||
DefaultBranch: p.DefaultBranch,
|
||||
})
|
||||
}
|
||||
@@ -168,7 +169,7 @@ func (g *GitLab) do(ctx context.Context, path, token string, out any) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Private-Token", token)
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
resp, err := g.httpClient().Do(req)
|
||||
if err != nil {
|
||||
return &GitLabError{msg: fmt.Sprintf("gitlab request failed: %v", err)}
|
||||
|
||||
Reference in New Issue
Block a user