# Components

## Create component

`POST /rest/api/{2-3}/component`

Creates a component. Use components to provide containers for issues within a project.

{% code fullWidth="true" %}

```go
package main

import (
	"context"
	_ "github.com/ctreminiom/go-atlassian/v2/jira/v3"
	"github.com/ctreminiom/go-atlassian/v2/jira/v2"
	"github.com/ctreminiom/go-atlassian/pkg/infra/models"
	"log"
	"os"
)

func main() {

	/*
		----------- Set an environment variable in git bash -----------
		export HOST="https://ctreminiom.atlassian.net/"
		export MAIL="MAIL_ADDRESS"
		export TOKEN="TOKEN_API"

		Docs: https://stackoverflow.com/questions/34169721/set-an-environment-variable-in-git-bash
	*/

	var (
		host  = os.Getenv("HOST")
		mail  = os.Getenv("MAIL")
		token = os.Getenv("TOKEN")
	)

	atlassian, err := v2.New(nil, host)
	if err != nil {
		log.Fatal(err)
	}

	atlassian.Auth.SetBasicAuth(mail, token)

	payload := &models.ComponentPayloadScheme{
		IsAssigneeTypeValid: false,
		Name:                "Component 2",
		Description:         "This is a Jira component",
		Project:             "KP",
		AssigneeType:        "PROJECT_LEAD",
		LeadAccountID:       "5b86be50b8e3cb5895860d6d",
	}

	newComponent, response, err := atlassian.Project.Component.Create(context.Background(), payload)
	if err != nil {
		log.Fatal(err)
	}

	log.Println("HTTP Endpoint Used", response.Endpoint)
	log.Printf("The new component has been created with the ID %v", newComponent.ID)
}
```

{% endcode %}

## Get component

`GET /rest/api/{2-3}/component/{id}`

Returns a component.

{% code fullWidth="true" %}

```go
package main

import (
	"context"
	_ "github.com/ctreminiom/go-atlassian/v2/jira/v3"
	"github.com/ctreminiom/go-atlassian/v2/jira/v2"
	"log"
	"os"
)

func main() {

	/*
		----------- Set an environment variable in git bash -----------
		export HOST="https://ctreminiom.atlassian.net/"
		export MAIL="MAIL_ADDRESS"
		export TOKEN="TOKEN_API"

		Docs: https://stackoverflow.com/questions/34169721/set-an-environment-variable-in-git-bash
	*/

	var (
		host  = os.Getenv("HOST")
		mail  = os.Getenv("MAIL")
		token = os.Getenv("TOKEN")
	)

	atlassian, err := v2.New(nil, host)
	if err != nil {
		log.Fatal(err)
	}

	atlassian.Auth.SetBasicAuth(mail, token)

	component, response, err := atlassian.Project.Component.Get(context.Background(), "10006")
	if err != nil {
		log.Fatal(err)
	}

	log.Println(component)
	log.Println(response.Endpoint)
}
```

{% endcode %}

## Update component

`PUT /rest/api/{2-3}/component/{id}`

Updates a component. Any fields included in the request are overwritten. If `leadAccountId` is an empty string ("") the component lead is removed.

{% code fullWidth="true" %}

```go
package main

import (
	"context"
	_ "github.com/ctreminiom/go-atlassian/v2/jira/v3"
	"github.com/ctreminiom/go-atlassian/v2/jira/v2"
	"github.com/ctreminiom/go-atlassian/pkg/infra/models"
	"log"
	"os"
)

func main() {

	/*
		----------- Set an environment variable in git bash -----------
		export HOST="https://ctreminiom.atlassian.net/"
		export MAIL="MAIL_ADDRESS"
		export TOKEN="TOKEN_API"

		Docs: https://stackoverflow.com/questions/34169721/set-an-environment-variable-in-git-bash
	*/

	var (
		host  = os.Getenv("HOST")
		mail  = os.Getenv("MAIL")
		token = os.Getenv("TOKEN")
	)

	atlassian, err := v2.New(nil, host)
	if err != nil {
		log.Fatal(err)
	}

	atlassian.Auth.SetBasicAuth(mail, token)

	payload := &models.ComponentPayloadScheme{
		IsAssigneeTypeValid: false,
		Name:                "Component 1 - UPDATED",
		Description:         "This is a Jira component - UPDATED",
	}

	componentUpdated, response, err := atlassian.Project.Component.Update(context.Background(), "10006", payload)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(response.Endpoint)
	log.Println(componentUpdated)
}
```

{% endcode %}

## Delete component

`DELETE /rest/api/{2-3}/component/{id}`

Deletes a component.

{% code fullWidth="true" %}

```go
package main

import (
	"context"
	_ "github.com/ctreminiom/go-atlassian/v2/jira/v3"
	"github.com/ctreminiom/go-atlassian/v2/jira/v2"
	"log"
	"os"
)

func main() {

	/*
		----------- Set an environment variable in git bash -----------
		export HOST="https://ctreminiom.atlassian.net/"
		export MAIL="MAIL_ADDRESS"
		export TOKEN="TOKEN_API"

		Docs: https://stackoverflow.com/questions/34169721/set-an-environment-variable-in-git-bash
	*/

	var (
		host  = os.Getenv("HOST")
		mail  = os.Getenv("MAIL")
		token = os.Getenv("TOKEN")
	)

	atlassian, err := v2.New(nil, host)
	if err != nil {
		log.Fatal(err)
	}

	atlassian.Auth.SetBasicAuth(mail, token)

	response, err := atlassian.Project.Component.Delete(context.Background(), "10006")
	if err != nil {
		log.Fatal(err)
	}

	log.Println("HTTP Endpoint Used", response.Endpoint)
}
```

{% endcode %}

## Get component issues count

`GET /rest/api/{2-3}/component/{id}/relatedIssueCounts`

Returns the counts of issues assigned to the component.

{% code fullWidth="true" %}

```go
package main

import (
	"context"
	_ "github.com/ctreminiom/go-atlassian/v2/jira/v3"
	"github.com/ctreminiom/go-atlassian/v2/jira/v2"
	"log"
	"os"
)

func main() {

	/*
		----------- Set an environment variable in git bash -----------
		export HOST="https://ctreminiom.atlassian.net/"
		export MAIL="MAIL_ADDRESS"
		export TOKEN="TOKEN_API"

		Docs: https://stackoverflow.com/questions/34169721/set-an-environment-variable-in-git-bash
	*/

	var (
		host  = os.Getenv("HOST")
		mail  = os.Getenv("MAIL")
		token = os.Getenv("TOKEN")
	)

	atlassian, err := v2.New(nil, host)
	if err != nil {
		log.Fatal(err)
	}

	atlassian.Auth.SetBasicAuth(mail, token)

	count, response, err := atlassian.Project.Component.Count(context.Background(), "10005")
	if err != nil {
		log.Fatal(err)
	}

	log.Println(count)
	log.Println(response.Endpoint)
}
```

{% endcode %}

## Get project components <a href="#get-project-components" id="get-project-components"></a>

`GET /rest/api/{2-3}/project/{projectIdOrKey}/components`

Returns all components in a project.

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```go
package main

import (
	"context"
	_ "github.com/ctreminiom/go-atlassian/v2/jira/v3"
	"github.com/ctreminiom/go-atlassian/v2/jira/v2"
	"log"
	"os"
)

func main() {

	/*
		----------- Set an environment variable in git bash -----------
		export HOST="https://ctreminiom.atlassian.net/"
		export MAIL="MAIL_ADDRESS"
		export TOKEN="TOKEN_API"

		Docs: https://stackoverflow.com/questions/34169721/set-an-environment-variable-in-git-bash
	*/

	var (
		host  = os.Getenv("HOST")
		mail  = os.Getenv("MAIL")
		token = os.Getenv("TOKEN")
	)

	atlassian, err := v2.New(nil, host)
	if err != nil {
		log.Fatal(err)
	}

	atlassian.Auth.SetBasicAuth(mail, token)

	var projectKey = "KP"
	components, response, err := atlassian.Project.Component.Gets(context.Background(), projectKey)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(response.Endpoint)
	for _, component := range components {
		log.Println(component)
	}
}
```

{% endcode %}
