~xenrox/hut

78d5ac52b263a4541cb5e84287f531ed6c3e5744 — Thorben Günther 2 months ago e625b16
git: Add git webhook list
4 files changed, 79 insertions(+), 0 deletions(-)

M doc/hut.1.scd
M git.go
M srht/gitsrht/gql.go
M srht/gitsrht/operations.graphql
M doc/hut.1.scd => doc/hut.1.scd +3 -0
@@ 353,6 353,9 @@ Options are:
	*-u*, *--url* <URL>
		The payload URL which receives the _POST_ request. Required.

*webhook list* [repo]
	List git webhooks.

## hg

Options are:

M git.go => git.go +55 -0
@@ 999,6 999,7 @@ func newGitWebhookCommand() *cobra.Command {
		Short: "Manage git webhooks",
	}
	cmd.AddCommand(newGitWebhookCreateCommand())
	cmd.AddCommand(newGitWebhookListCommand())
	return cmd
}



@@ 1062,6 1063,60 @@ func newGitWebhookCreateCommand() *cobra.Command {
	return cmd
}

func newGitWebhookListCommand() *cobra.Command {
	run := func(cmd *cobra.Command, args []string) {
		ctx := cmd.Context()

		var name, owner, instance string
		if len(args) > 0 {
			name, owner, instance = parseResourceName(args[0])
		} else {
			var err error
			name, owner, instance, err = getGitRepoName(ctx, cmd)
			if err != nil {
				log.Fatal(err)
			}
		}

		c := createClientWithInstance("git", cmd, instance)
		id, err := getGitRepoID(c, ctx, name, owner)
		if err != nil {
			log.Fatal(err)
		}

		var cursor *gitsrht.Cursor
		err = pagerify(func(p pager) error {
			webhooks, err := gitsrht.GitWebhooks(c.Client, ctx, id, cursor)
			if err != nil {
				return err
			}

			for _, webhook := range webhooks.Results {
				fmt.Fprintf(p, "%s %s\n", termfmt.DarkYellow.Sprintf("#%d", webhook.Id), webhook.Url)
			}

			cursor = webhooks.Cursor
			if cursor == nil {
				return pagerDone
			}

			return nil
		})
		if err != nil {
			log.Fatal(err)
		}
	}

	cmd := &cobra.Command{
		Use:               "list [repo]",
		Short:             "List git webhooks",
		Args:              cobra.MaximumNArgs(1),
		ValidArgsFunction: completeGitRepo,
		Run:               run,
	}
	return cmd
}

func getGitRepoName(ctx context.Context, cmd *cobra.Command) (repoName, owner, instance string, err error) {
	repoName, err = cmd.Flags().GetString("repo")
	if err != nil {

M srht/gitsrht/gql.go => srht/gitsrht/gql.go +11 -0
@@ 840,6 840,17 @@ func CompleteCoMaintainers(client *gqlclient.Client, ctx context.Context, name s
	return respData.Me, err
}

func GitWebhooks(client *gqlclient.Client, ctx context.Context, repositoryID int32, cursor *Cursor) (gitWebhooks *WebhookSubscriptionCursor, err error) {
	op := gqlclient.NewOperation("query gitWebhooks ($repositoryID: Int!, $cursor: Cursor) {\n\tgitWebhooks(repositoryID: $repositoryID, cursor: $cursor) {\n\t\tresults {\n\t\t\tid\n\t\t\turl\n\t\t}\n\t\tcursor\n\t}\n}\n")
	op.Var("repositoryID", repositoryID)
	op.Var("cursor", cursor)
	var respData struct {
		GitWebhooks *WebhookSubscriptionCursor
	}
	err = client.Execute(ctx, op, &respData)
	return respData.GitWebhooks, 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 +10 -0
@@ 230,6 230,16 @@ query completeCoMaintainers($name: String!) {
    }
}

query gitWebhooks($repositoryID: Int!, $cursor: Cursor) {
    gitWebhooks(repositoryID: $repositoryID, cursor: $cursor) {
        results {
            id
            url
        }
        cursor
    }
}

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