bndr / gojenkins

Jenkins API Client in Go. Looking for maintainers to move this project forward.
Apache License 2.0
869 stars 446 forks source link

CreateView Function doesn't allow creation of a view inside of a folder #318

Open abrockmeyer-govtact opened 10 months ago

abrockmeyer-govtact commented 10 months ago

The endpoint is always on the main jenkins url. I would hope to see an implementation where a folder name can be passed in as "test_folder" and the endpoint would be modified to be

if folder {
      endpoint := "jobs/" + folder + "/createView"
}

Or even just passing in "jobs/test_folder/jobs/test_folder2" could do nested folders 2 layers deep. Either would help.

func (j *Jenkins) CreateView(ctx context.Context, name string, viewType string) (*View, error) {
    view := &View{Jenkins: j, Raw: new(ViewResponse), Base: "/view/" + name}
    endpoint := "/createView"
    data := map[string]string{
        "name":   name,
        "mode":   viewType,
        "Submit": "OK",
        "json": makeJson(map[string]string{
            "name": name,
            "mode": viewType,
        }),
    }
    r, err := j.Requester.Post(ctx, endpoint, nil, view.Raw, data)

    if err != nil {
        return nil, err
    }

    if r.StatusCode == 200 {
        return j.GetView(ctx, name)
    }
    return nil, errors.New(strconv.Itoa(r.StatusCode))
}
abrockmeyer-govtact commented 10 months ago

It looks like Pull Request #269 takes care of this functionality as well. Albeit it creates a new function CreateViewInFolder as opposed to modifying this section of code.