-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
112 lines (80 loc) · 2.33 KB
/
client.go
File metadata and controls
112 lines (80 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"fmt"
"log"
"os"
"p4planAPIExamples/requests"
"p4planAPIExamples/requests/mutations"
"p4planAPIExamples/requests/queries"
"p4planAPIExamples/util"
"github.com/joho/godotenv"
"github.com/machinebox/graphql"
)
type ResponseStruct struct {
AppInfo struct {
APIVersion string `json:"apiVersion"`
}
}
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
p4PlanAPIHost := os.Getenv("P4PLAN_API_HOST")
url := fmt.Sprintf("%s/graphql", p4PlanAPIHost)
client := graphql.NewClient(url)
req := graphql.NewRequest(`
query appInfo {
appInfo {
apiVersion
}
}
`)
req.Header.Set("Cache-Control", "no-cache")
ctx := context.Background()
var respData ResponseStruct
if err := client.Run(ctx, req, &respData); err != nil {
log.Fatal(err)
}
log.Printf("Go client connected successfully to P4 Plan API: %+v\n", respData.AppInfo.APIVersion)
accessToken, err := requests.Login(client)
if err != nil {
log.Fatal(err)
}
log.Printf("Access token: %s", accessToken)
// Get all the projects available to the user
projects, err := queries.GetProjects(client, accessToken)
if err != nil {
log.Fatal(err)
}
log.Printf("Projects received: %v", len(projects))
// Get all the items of the first project's planning tab, qa tab and backlog tab
items, err := queries.GetItems(client, accessToken, projects[0].ID)
if err != nil {
log.Fatal(err)
}
log.Printf("Planning tab items for %s received: %v", projects[0].Name, len(items))
newProject, err := mutations.CreateProject(client, accessToken)
if err != nil {
log.Fatal(err)
}
log.Printf("New project created: %s", newProject.Name)
authenticatedUserId, err := util.ExtractSubFromToken(accessToken)
if err != nil {
log.Fatal(err)
}
err = mutations.AddUserToProject(client, accessToken, newProject.ID, authenticatedUserId)
if err != nil {
log.Fatal(err)
}
err = mutations.MakeUserMainManager(client, accessToken, newProject.ID, authenticatedUserId)
if err != nil {
log.Fatal(err)
}
log.Printf("Authenticated user added to new project and is now a main manager")
err = mutations.CreateBacklogTasks(client, accessToken, newProject.Backlog.ID)
if err != nil {
log.Fatal(err)
}
log.Printf("10 new backlog tasks have been created in the newly created project")
}