Last active
January 24, 2018 16:56
-
-
Save aslakknutsen/6c345300be4a6e926bb4e4bcfab3a18c to your computer and use it in GitHub Desktop.
WIT API Generator (requires pre setup of iterations, randomly distributes across iterations and states)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wit | |
import ( | |
"context" | |
"fmt" | |
"io/ioutil" | |
"math/rand" | |
"net/http" | |
"net/url" | |
"testing" | |
"github.com/fabric8-services/fabric8-notification/wit/api" | |
goaclient "github.com/goadesign/goa/client" | |
"github.com/goadesign/goa/uuid" | |
"github.com/icrowley/fake" | |
) | |
var URL = "https://api.prod-preview.openshift.io" | |
var Token = "XXXXX" | |
var spaceID = "xxx-uuid-xxx" | |
func TestRun(t *testing.T) { | |
Scenario( | |
Static(20), | |
Experience( | |
Random(3, 7), | |
Bug( | |
Random(1, 2), | |
Task( | |
Random(0, 2), | |
), | |
), | |
Feature( | |
Random(3, 10), | |
Bug( | |
Random(1, 20), | |
Task( | |
Random(1, 3), | |
), | |
), | |
Task( | |
Random(1, 14), | |
), | |
), | |
), | |
) | |
} | |
type Count func() int | |
type Creator func(parent string) | |
type Object func(parent string) string | |
func Static(i int) Count { return func() int { return i } } | |
func Random(min, max int) Count { return func() int { return rand.Intn(max-min) + min } } | |
func Scenario(c Count, creators ...Creator) { | |
create(c(), "", CreateScenario, creators) | |
} | |
func safeUUID(uid string) uuid.UUID { | |
u, _ := uuid.FromString(uid) | |
return u | |
} | |
var client = func() *api.Client { | |
u, _ := url.Parse(URL) | |
c := api.New(goaclient.HTTPClientDoer(http.DefaultClient)) | |
c.Host = u.Host | |
c.Scheme = u.Scheme | |
c.SetJWTSigner( | |
&goaclient.JWTSigner{ | |
TokenSource: &goaclient.StaticTokenSource{ | |
StaticToken: &goaclient.StaticToken{ | |
Type: "Bearer", | |
Value: Token}}}) | |
return c | |
}() | |
var space = safeUUID(spaceID) | |
var states = []string{"new", "open", "in progress", "resolved", "closed"} | |
var iterations = func() []*api.Iteration { | |
resp, err := client.ListSpaceIterations(context.Background(), api.ListSpaceIterationsPath(space), nil, nil) | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
}() | |
if resp.StatusCode != http.StatusOK { | |
jerr, _ := client.DecodeJSONAPIErrors(resp) | |
panic(jerr.Errors[0].Detail) | |
} | |
list, err := client.DecodeIterationList(resp) | |
if err != nil { | |
panic(err) | |
} | |
return list.Data | |
}() | |
func CreateWorkItem(title, wit string) string { | |
fmt.Println("Creating WI " + title) | |
iteration := (*iterations[rand.Intn(len(iterations)-1)].ID).String() | |
iterationType := "iterations" | |
wip := api.CreateWorkitemsPayload{ | |
Data: &api.WorkItem{ | |
Type: "workitems", | |
Attributes: map[string]interface{}{ | |
"system.state": states[rand.Intn(len(states)-1)], | |
"system.title": fmt.Sprintf("%v %v", title, fake.WordsN(Random(5, 12)())), | |
"system.description": fake.ParagraphsN(Random(2, 20)()), | |
}, | |
Relationships: &api.WorkItemRelationships{ | |
BaseType: &api.RelationBaseType{ | |
Data: &api.BaseTypeData{ | |
ID: safeUUID(wit), | |
Type: "workitemtypes", | |
}, | |
}, | |
Iteration: &api.RelationGeneric{ | |
Data: &api.GenericData{ | |
ID: &iteration, | |
Type: &iterationType, | |
}, | |
}, | |
}, | |
}, | |
} | |
resp, err := client.CreateWorkitems(context.Background(), api.CreateWorkitemsPath(space), &wip) | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
}() | |
if resp.StatusCode != http.StatusCreated { | |
jerr, _ := client.DecodeJSONAPIErrors(resp) | |
panic(jerr.Errors[0].Detail) | |
} | |
wi, err := client.DecodeWorkItemSingle(resp) | |
if err != nil { | |
panic(err) | |
} | |
return (*wi.Data.ID).String() | |
} | |
func CreateWorkItemLink(source, target string) { | |
wip := api.CreateWorkItemLinkPayload{ | |
Data: &api.WorkItemLinkData{ | |
Type: "workitemlinks", | |
Relationships: &api.WorkItemLinkRelationships{ | |
LinkType: &api.RelationWorkItemLinkType{ | |
Data: &api.RelationWorkItemLinkTypeData{ | |
Type: "workitemlinktypes", | |
ID: safeUUID("25c326a7-6d03-4f5a-b23b-86a9ee4171e9"), | |
}, | |
}, | |
Source: &api.RelationWorkItem{ | |
Data: &api.RelationWorkItemData{ | |
Type: "workitems", | |
ID: safeUUID(source), | |
}, | |
}, | |
Target: &api.RelationWorkItem{ | |
Data: &api.RelationWorkItemData{ | |
Type: "workitems", | |
ID: safeUUID(target), | |
}, | |
}, | |
}, | |
}, | |
} | |
resp, err := client.CreateWorkItemLink(context.Background(), api.CreateWorkItemLinkPath(), &wip) | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
}() | |
if resp.StatusCode != http.StatusCreated { | |
jerr, _ := client.DecodeJSONAPIErrors(resp) | |
panic(jerr.Errors[0].Detail) | |
} | |
} | |
func CreateScenario(parent string) string { | |
return CreateWorkItem("Scenario", "71171e90-6d35-498f-a6a7-2083b5267c18") | |
} | |
func Experience(c Count, creators ...Creator) Creator { | |
return func(parent string) { | |
create(c(), parent, CreateExperience, creators) | |
} | |
} | |
func CreateExperience(parent string) string { | |
id := CreateWorkItem("Experience", "b9a71831-c803-4f66-8774-4193fffd1311") | |
if parent != "" { | |
CreateWorkItemLink(parent, id) | |
} | |
return id | |
} | |
func Task(c Count, creators ...Creator) Creator { | |
return func(parent string) { | |
create(c(), parent, CreateTask, creators) | |
} | |
} | |
func CreateTask(parent string) string { | |
id := CreateWorkItem("Task", "bbf35418-04b6-426c-a60b-7f80beb0b624") | |
if parent != "" { | |
CreateWorkItemLink(parent, id) | |
} | |
return id | |
} | |
func Feature(c Count, creators ...Creator) Creator { | |
return func(parent string) { | |
create(c(), parent, CreateFeature, creators) | |
} | |
} | |
func CreateFeature(parent string) string { | |
id := CreateWorkItem("Feature", "0a24d3c2-e0a6-4686-8051-ec0ea1915a28") | |
if parent != "" { | |
CreateWorkItemLink(parent, id) | |
} | |
return id | |
} | |
func Bug(c Count, creators ...Creator) Creator { | |
return func(parent string) { | |
create(c(), parent, CreateBug, creators) | |
} | |
} | |
func CreateBug(parent string) string { | |
id := CreateWorkItem("Bug", "26787039-b68f-4e28-8814-c2f93be1ef4e") | |
if parent != "" { | |
CreateWorkItemLink(parent, id) | |
} | |
return id | |
} | |
func create(count int, parent string, obj Object, creators []Creator) { | |
for i := 0; i < count; i++ { | |
p := obj(parent) | |
invoke(p, creators) | |
} | |
} | |
func invoke(parent string, creators []Creator) { | |
for _, creator := range creators { | |
creator(parent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment