~emersion/hut

100899797e075fe810e2d9e10edb4f8cb1f231fb — Thorben G√ľnther 6 months ago 998f75a
builds secret share: Implement user completion

The new CoMaintainer completion gets introduced here, which gets a list
of those from the ACL entries of a repository. This can be salvaged by
other services than git, e.g. for proposing people to assign tickets to.
4 files changed, 48 insertions(+), 1 deletions(-)

M builds.go
M git.go
M srht/gitsrht/gql.go
M srht/gitsrht/operations.graphql
M builds.go => builds.go +1 -1
@@ 717,7 717,7 @@ func newBuildsSecretShareCommand() *cobra.Command {
	}
	cmd.Flags().StringVarP(&userName, "user", "u", "", "username")
	cmd.MarkFlagRequired("user")
	cmd.RegisterFlagCompletionFunc("user", cobra.NoFileCompletions)
	cmd.RegisterFlagCompletionFunc("user", completeCoMaintainers)
	return cmd
}


M git.go => git.go +23 -0
@@ 1033,3 1033,26 @@ func completeBranches(cmd *cobra.Command, args []string, toComplete string) ([]s

	return user.Repository.References.Heads(), cobra.ShellCompDirectiveNoFileComp
}

func completeCoMaintainers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
	ctx := cmd.Context()
	// Since completeCoMaintainers is intended to be called from other services
	// than git, we cannot use getRepoName which requires the "repo" flag to be set.
	repoName, _, instace, err := guessGitRepoName(ctx, cmd)
	if err != nil {
		return nil, cobra.ShellCompDirectiveNoFileComp
	}
	c := createClientWithInstance("git", cmd, instace)

	var userList []string
	user, err := gitsrht.CompleteCoMaintainers(c.Client, ctx, repoName)
	if err != nil || user.Repositories == nil {
		return nil, cobra.ShellCompDirectiveNoFileComp
	}

	for _, acl := range user.Repository.Acls.Results {
		userList = append(userList, acl.Entity.CanonicalName)
	}

	return userList, cobra.ShellCompDirectiveNoFileComp
}

M srht/gitsrht/gql.go => srht/gitsrht/gql.go +10 -0
@@ 747,6 747,16 @@ func UserWebhooks(client *gqlclient.Client, ctx context.Context, cursor *Cursor)
	return respData.UserWebhooks, err
}

func CompleteCoMaintainers(client *gqlclient.Client, ctx context.Context, name string) (me *User, err error) {
	op := gqlclient.NewOperation("query completeCoMaintainers ($name: String!) {\n\tme {\n\t\trepository(name: $name) {\n\t\t\tacls {\n\t\t\t\tresults {\n\t\t\t\t\tentity {\n\t\t\t\t\t\tcanonicalName\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n")
	op.Var("name", name)
	var respData struct {
		Me *User
	}
	err = client.Execute(ctx, op, &respData)
	return respData.Me, err
}

func UploadArtifact(client *gqlclient.Client, ctx context.Context, repoId int32, revspec string, file gqlclient.Upload) (uploadArtifact *Artifact, err error) {
	op := gqlclient.NewOperation("mutation uploadArtifact ($repoId: Int!, $revspec: String!, $file: Upload!) {\n\tuploadArtifact(repoId: $repoId, revspec: $revspec, file: $file) {\n\t\tfilename\n\t}\n}\n")
	op.Var("repoId", repoId)

M srht/gitsrht/operations.graphql => srht/gitsrht/operations.graphql +14 -0
@@ 186,6 186,20 @@ query userWebhooks($cursor: Cursor) {
    }
}

query completeCoMaintainers($name: String!) {
    me {
        repository(name: $name) {
            acls {
                results {
                    entity {
                        canonicalName
                    }
                }
            }
        }
    }
}

mutation uploadArtifact($repoId: Int!, $revspec: String!, $file: Upload!) {
    uploadArtifact(repoId: $repoId, revspec: $revspec, file: $file) {
        filename