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
349349 return respData.User, err
350350 }
351351
352+func 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+
352362 func CreateProject(client *gqlclient.Client, ctx context.Context, name string, visibility Visibility, description *string, tags []string) (createProject *Project, err error) {
353363 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")
354364 op.Var("name", name)
srht/hubsrht/operations.graphql+11−1
@@ -11,7 +11,7 @@ query projectsForExport($cursor: Cursor) { · 10 unmodified lines
1111 projects(cursor: $cursor) {
1212 cursor
1313 results {
14− ...projectDetails
14+ ...projectDetails
1515 }
1616 }
1717 }
@@ -155,6 +155,16 @@ query trackersByUser($username: String!, $name: String!) { · 137 unmodified lines
155155 }
156156 }
157157
158+query projectsBySource($rid: ID!) {
159+ source(rid: $rid) {
160+ projects {
161+ results {
162+ name
163+ }
164+ }
165+ }
166+}
167+
158168 mutation createProject(
159169 $name: String!
160170 $visibility: Visibility!
todo.go+41−8
@@ -14,6 +14,7 @@ import ( · 13 unmodified lines
1414 "strings"
1515
1616 "charm.land/huh/v2"
17+ "git.sr.ht/~xenrox/hut/srht/gitsrht"
1718 "git.sr.ht/~xenrox/hut/srht/hubsrht"
1819 "git.sr.ht/~xenrox/hut/srht/todosrht"
1920 "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
22232224 return nil, resource{}, err
22242225 }
22252226
2226− c = createClientWithInstance("hub", cmd, r.instance)
2227+ gitClient := createClientWithInstance("git", cmd, r.instance)
2228+ c = gitClient
22272229 username := strings.TrimLeft(r.owner, ownerPrefixes)
22282230
2229− // TODO: Don't assume repo name == project name
2230− user, err := hubsrht.TrackersByUser(c.Client, ctx, username, r.name)
2231− if err == nil && user != nil && user.Project != nil {
2232− if len(user.Project.Trackers.Results) == 1 {
2233− r.name = user.Project.Trackers.Results[0].Name
2231+ user, err := gitsrht.RepositoryIDByUser(gitClient.Client, ctx, username, r.name)
2232+ if err != nil || user == nil || user.Repository == nil {
2233+ return nil, resource{}, errors.New("failed to retrieve RID for repository")
2234+ }
2235+
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
22342268 } else {
22352269 var trackers []string
2236− for _, t := range user.Project.Trackers.Results {
2270+ for _, t := range hubUser.Project.Trackers.Results {
22372271 trackers = append(trackers, t.Name)
22382272 }
22392273
@@ -2267,7 +2301,6 @@ func getTrackerName(ctx context.Context, cmd *cobra.Command) (c *Client, r resou · 27 unmodified lines
22672301 }
22682302
22692303 // TODO: if no tracker could be determined, ask the user for input
2270− // fallback to git
22712304 return c, r, nil
22722305 }
22732306

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