I'm building a cli application using GitHub API V4. I need to remove all labels a repository has.
URL: https://api.github.com/graphql
HEADERS:
- Accept: application/vnd.github.bane-preview+json
- Authorization: bearer
<GITHUB_TOKEN>
I need to get the result of the GetLabelID
query and put it into id
at DeleteLabels
mutation.
query GetLabelID {
repository(owner: "erdaltsksn", name: "playground") {
labels(first:100) {
nodes {
id
}
}
}
}
mutation DeleteLabels {
deleteLabel(input: {
id: "<ID_SHOULD_BE_HERE>"
}) {
clientMutationId
}
}
OUTPUT:
{
"data": {
"repository": {
"labels": {
"nodes": [
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDgy"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDg0"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDg2"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDg4"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDkw"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDkz"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDk1"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDk3"
},
{
"id": "MDU6TGFiZWwyMDg5MzQ0MDk5"
}
]
}
}
}
}
The Output of the DeleteLabels
mutation when I type the ID
manually.
{
"data": {
"deleteLabel": {
"clientMutationId": null
}
}
}
On the client-side, I'm using Golang with machinebox/graphql. One possible solution can be that solving this by sending two different requests on the client-side and getting results. I'm not sure of is that the best solution or not.
My GO Code:
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/gookit/color"
"github.com/machinebox/graphql"
)
func main() {
RemoveLabels("erdaltsksn/playground")
}
func RemoveLabels(repository string) {
repo := strings.Split(repository, "/")
data := graphqlQuery(fmt.Sprintf(`
query {
repository(owner: "%s", name: "%s") {
labels(first:100) {
totalCount
nodes {
id
}
}
}
}
`, repo[0], repo[1]))
fmt.Println(data)
}
type graphqlQueryResponse struct {
Repository struct {
ID string `json:"id,omitempty"`
Labels struct {
TotalCount int `json:"totalCount,omitempty"`
Nodes []struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Color string `json:"color,omitempty"`
Description string `json:"description,omitempty"`
} `json:"nodes,omitempty"`
} `json:"labels,omitempty"`
} `json:"repository,omitempty"`
}
var graphqlClient = graphql.NewClient("https://api.github.com/graphql")
func graphqlQuery(q string) graphqlQueryResponse {
graphqlRequest := graphql.NewRequest(q)
graphqlRequest.Header.Set("Authorization", "bearer MY_GITHUB_API")
graphqlRequest.Header.Set("Accept", "application/vnd.github.bane-preview+json")
var data graphqlQueryResponse
if err := graphqlClient.Run(context.Background(), graphqlRequest, &data); err != nil {
color.Danger.Println("There is a problem while querying GitHub API v4")
color.Warn.Prompt(err.Error())
os.Exit(1)
}
return data
}
GO's output:
{
{
{
8
[
{
MDU6TGFiZWwyMDg5MzQ0MDgy
}
{
MDU6TGFiZWwyMDg5MzQ0MDg0
}
{
MDU6TGFiZWwyMDg5MzQ0MDg2
}
{
MDU6TGFiZWwyMDg5MzQ0MDg4
}
{
MDU6TGFiZWwyMDg5MzQ0MDkw
}
{
MDU6TGFiZWwyMDg5MzQ0MDkz
}
{
MDU6TGFiZWwyMDg5MzQ0MDk1
}
{
MDU6TGFiZWwyMDg5MzQ0MDk3
}
]
}
}
}
I might create to two different request and using it to delete each tag one by one. But I think it is not a good idea. I'm looking for alternative or the better way to do this.
Thank you for any help in advance.
Refs: