History

todo: Don't assume that tracker name equals project name

Instead, retrieve all projects that a git repo is linked to.

Thorben Günther committed 3 months ago

Commit cb643b21 parent 283ef2a3 files · +62 −9Browse files

Unified · Split

3 files
  1. srht/hubsrht/gql.go +10 −0
  2. srht/hubsrht/operations.graphql +11 −1
  3. todo.go +41 −8
srht/hubsrht/gql.go+10−0
@@ -349,6 +349,16 @@ func TrackersByUser(client *gqlclient.Client, ctx context.Context, username stri · 348 unmodified lines
349 return respData.User, err349 return respData.User, err
350}350}
351 351
352func ProjectsBySource(client *gqlclient.Client, ctx context.Context, rid string) (source *SourceRepo, err error) {
353 op := gqlclient.NewOperation("query projectsBySource ($rid: ID!) {\n\tsource(rid: $rid) {\n\t\tprojects {\n\t\t\tresults {\n\t\t\t\tname\n\t\t\t}\n\t\t}\n\t}\n}\n")
354 op.Var("rid", rid)
355 var respData struct {
356 Source *SourceRepo
357 }
358 err = client.Execute(ctx, op, &respData)
359 return respData.Source, err
360}
361
352func CreateProject(client *gqlclient.Client, ctx context.Context, name string, visibility Visibility, description *string, tags []string) (createProject *Project, err error) {362func CreateProject(client *gqlclient.Client, ctx context.Context, name string, visibility Visibility, description *string, tags []string) (createProject *Project, err error) {
353 op := gqlclient.NewOperation("mutation createProject ($name: String!, $visibility: Visibility!, $description: String, $tags: [String!]) {\n\tcreateProject(name: $name, visibility: $visibility, description: $description, tags: $tags) {\n\t\trid\n\t\towner {\n\t\t\tcanonicalName\n\t\t}\n\t\tname\n\t}\n}\n")363 op := gqlclient.NewOperation("mutation createProject ($name: String!, $visibility: Visibility!, $description: String, $tags: [String!]) {\n\tcreateProject(name: $name, visibility: $visibility, description: $description, tags: $tags) {\n\t\trid\n\t\towner {\n\t\t\tcanonicalName\n\t\t}\n\t\tname\n\t}\n}\n")
354 op.Var("name", name)364 op.Var("name", name)
srht/hubsrht/operations.graphql+11−1
@@ -11,7 +11,7 @@ query projectsForExport($cursor: Cursor) { · 10 unmodified lines
11 projects(cursor: $cursor) {11 projects(cursor: $cursor) {
12 cursor12 cursor
13 results {13 results {
14 ...projectDetails14 ...projectDetails
15 }15 }
16 }16 }
17 }17 }
@@ -155,6 +155,16 @@ query trackersByUser($username: String!, $name: String!) { · 137 unmodified lines
155 }155 }
156}156}
157 157
158query projectsBySource($rid: ID!) {
159 source(rid: $rid) {
160 projects {
161 results {
162 name
163 }
164 }
165 }
166}
167
158mutation createProject(168mutation createProject(
159 $name: String!169 $name: String!
160 $visibility: Visibility!170 $visibility: Visibility!
todo.go+41−8
@@ -14,6 +14,7 @@ import ( · 13 unmodified lines
14 "strings"14 "strings"
15 15
16 "charm.land/huh/v2"16 "charm.land/huh/v2"
17 "git.sr.ht/~xenrox/hut/srht/gitsrht"
17 "git.sr.ht/~xenrox/hut/srht/hubsrht"18 "git.sr.ht/~xenrox/hut/srht/hubsrht"
18 "git.sr.ht/~xenrox/hut/srht/todosrht"19 "git.sr.ht/~xenrox/hut/srht/todosrht"
19 "git.sr.ht/~xenrox/hut/termfmt"20 "git.sr.ht/~xenrox/hut/termfmt"
@@ -2223,17 +2224,50 @@ func getTrackerName(ctx context.Context, cmd *cobra.Command) (c *Client, r resou · 2203 unmodified lines
2223 return nil, resource{}, err2224 return nil, resource{}, err
2224 }2225 }
2225 2226
2226 c = createClientWithInstance("hub", cmd, r.instance)2227 gitClient := createClientWithInstance("git", cmd, r.instance)
2228 c = gitClient
2227 username := strings.TrimLeft(r.owner, ownerPrefixes)2229 username := strings.TrimLeft(r.owner, ownerPrefixes)
2228 2230
2229 // TODO: Don't assume repo name == project name2231 user, err := gitsrht.RepositoryIDByUser(gitClient.Client, ctx, username, r.name)
2230 user, err := hubsrht.TrackersByUser(c.Client, ctx, username, r.name)2232 if err != nil || user == nil || user.Repository == nil {
2231 if err == nil && user != nil && user.Project != nil {2233 return nil, resource{}, errors.New("failed to retrieve RID for repository")
2232 if len(user.Project.Trackers.Results) == 1 {2234 }
2233 r.name = user.Project.Trackers.Results[0].Name2235
2236 hubClient := gitClient.withService(cmd, "hub", r.instance)
2237 source, err := hubsrht.ProjectsBySource(hubClient.Client, ctx, user.Repository.Rid)
2238 if err != nil {
2239 return nil, resource{}, err
2240 } else if source == nil {
2241 return nil, resource{}, errors.New("this repository has no associated project")
2242 }
2243
2244 var projectName string
2245 if len(source.Projects.Results) == 1 {
2246 projectName = source.Projects.Results[0].Name
2247 } else {
2248 options := make([]huh.Option[string], 0, len(source.Projects.Results))
2249 for _, p := range source.Projects.Results {
2250 options = append(options, huh.NewOption(p.Name, p.Name))
2251 }
2252
2253 err := huh.NewSelect[string]().
2254 Title("Choose a project from which you want to select a tracker").
2255 Options(options...).
2256 Value(&projectName).
2257 Run()
2258
2259 if err != nil {
2260 return nil, resource{}, err
2261 }
2262 }
2263
2264 hubUser, err := hubsrht.TrackersByUser(hubClient.Client, ctx, username, projectName)
2265 if err == nil && hubUser != nil && hubUser.Project != nil {
2266 if len(hubUser.Project.Trackers.Results) == 1 {
2267 r.name = hubUser.Project.Trackers.Results[0].Name
2234 } else {2268 } else {
2235 var trackers []string2269 var trackers []string
2236 for _, t := range user.Project.Trackers.Results {2270 for _, t := range hubUser.Project.Trackers.Results {
2237 trackers = append(trackers, t.Name)2271 trackers = append(trackers, t.Name)
2238 }2272 }
2239 2273
@@ -2267,7 +2301,6 @@ func getTrackerName(ctx context.Context, cmd *cobra.Command) (c *Client, r resou · 27 unmodified lines
2267 }2301 }
2268 2302
2269 // TODO: if no tracker could be determined, ask the user for input2303 // TODO: if no tracker could be determined, ask the user for input
2270 // fallback to git
2271 return c, r, nil2304 return c, r, nil
2272}2305}
2273 2306

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t