2015-09-03 12:25:03 +02:00
# go-jira
2021-10-10 14:07:36 +02:00
[![GoDoc ](https://godoc.org/github.com/bluevare/go-jira?status.svg )](https://godoc.org/github.com/bluevare/go-jira)
[![Build Status ](https://github.com/bluevare/go-jira/actions/workflows/testing.yml/badge.svg )](https://github.com/bluevare/go-jira/actions/workflows/testing.yml)
[![Go Report Card ](https://goreportcard.com/badge/github.com/bluevare/go-jira )](https://goreportcard.com/report/github.com/bluevare/go-jira)
2016-03-26 22:43:30 +02:00
2020-05-14 17:18:31 +02:00
[Go ](https://golang.org/ ) client library for [Atlassian Jira ](https://www.atlassian.com/software/jira ).
2015-09-03 12:25:03 +02:00
2020-05-14 17:18:31 +02:00
![Go client library for Atlassian Jira ](./img/logo_small.png "Go client library for Atlassian Jira." )
2015-09-03 12:25:03 +02:00
## Features
2015-09-03 12:58:01 +02:00
* Authentication (HTTP Basic, OAuth, Session Cookie)
2017-10-27 02:54:57 +02:00
* Create and retrieve issues
2016-06-20 17:14:55 +02:00
* Create and retrieve issue transitions (status updates)
2020-05-14 17:18:31 +02:00
* Call every API endpoint of the Jira, even if it is not directly implemented in this library
2016-03-27 13:02:25 +02:00
2020-05-14 17:18:31 +02:00
This package is not Jira API complete (yet), but you can call every API endpoint you want. See [Call a not implemented API endpoint ](#call-a-not-implemented-api-endpoint ) how to do this. For all possible API endpoints of Jira have a look at [latest Jira REST API documentation ](https://docs.atlassian.com/jira/REST/latest/ ).
2015-09-03 12:58:01 +02:00
2019-05-05 02:22:09 +02:00
## Requirements
2016-03-27 13:02:25 +02:00
2020-11-30 02:59:29 +02:00
* Go >= 1.14
2020-05-14 17:18:31 +02:00
* Jira v6.3.4 & v7.1.2.
2015-09-03 12:25:03 +02:00
2020-11-30 02:59:29 +02:00
Note that we also run our tests against 1.13, though only the last two versions
of Go are officially supported.
2015-09-03 12:25:03 +02:00
## Installation
It is go gettable
2019-05-05 02:22:09 +02:00
```bash
2021-10-10 14:07:36 +02:00
go get github.com/bluevare/go-jira
2019-05-05 02:22:09 +02:00
```
2015-09-03 12:25:03 +02:00
2017-08-31 13:59:27 +02:00
For stable versions you can use one of our tags with [gopkg.in ](http://labix.org/gopkg.in ). E.g.
```go
package main
import (
2021-10-10 14:07:36 +02:00
jira "gopkg.in/bluevare/go-jira.v1"
2017-08-31 13:59:27 +02:00
)
...
```
2015-09-03 12:25:03 +02:00
(optional) to run unit / example tests:
2019-05-05 02:22:09 +02:00
```bash
2021-10-10 14:07:36 +02:00
cd $GOPATH/src/github.com/bluevare/go-jira
2019-05-05 02:22:09 +02:00
go test -v ./...
```
2015-09-03 12:25:03 +02:00
## API
2021-10-10 14:07:36 +02:00
Please have a look at the [GoDoc documentation ](https://godoc.org/github.com/bluevare/go-jira ) for a detailed API description.
2015-09-03 12:25:03 +02:00
2020-05-14 17:18:31 +02:00
The [latest Jira REST API documentation ](https://docs.atlassian.com/jira/REST/latest/ ) was the base document for this package.
2015-09-03 12:48:16 +02:00
2015-09-03 12:25:03 +02:00
## Examples
Further a few examples how the API can be used.
2021-10-10 14:07:36 +02:00
A few more examples are available in the [GoDoc examples section ](https://godoc.org/github.com/bluevare/go-jira#pkg-examples ).
2015-09-03 12:25:03 +02:00
2015-09-03 13:35:48 +02:00
### Get a single issue
Lets retrieve [MESOS-3325 ](https://issues.apache.org/jira/browse/MESOS-3325 ) from the [Apache Mesos ](http://mesos.apache.org/ ) project.
```go
package main
import (
"fmt"
2021-10-10 14:07:36 +02:00
"github.com/bluevare/go-jira"
2015-09-03 13:35:48 +02:00
)
func main() {
jiraClient, _ := jira.NewClient(nil, "https://issues.apache.org/jira/")
2017-01-29 17:15:45 +02:00
issue, _, _ := jiraClient.Issue.Get("MESOS-3325", nil)
2015-09-03 13:35:48 +02:00
fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)
fmt.Printf("Type: %s\n", issue.Fields.Type.Name)
fmt.Printf("Priority: %s\n", issue.Fields.Priority.Name)
// MESOS-3325: Running mesos-slave@0.23 in a container causes slave to be lost after a restart
// Type: Bug
// Priority: Critical
}
```
2015-09-03 12:25:03 +02:00
2018-02-24 22:06:14 +02:00
### Authentication
2015-09-03 13:59:13 +02:00
2018-02-24 22:06:14 +02:00
The `go-jira` library does not handle most authentication directly. Instead, authentication should be handled within
an `http.Client` . That client can then be passed into the `NewClient` function when creating a jira client.
2017-02-09 00:37:57 +02:00
2018-02-24 22:06:14 +02:00
For convenience, capability for basic and cookie-based authentication is included in the main library.
2017-02-09 00:37:57 +02:00
2020-09-20 21:05:56 +02:00
#### Token (Jira on Atlassian Cloud)
2017-02-09 00:37:57 +02:00
2020-09-20 21:05:56 +02:00
Token-based authentication uses the basic authentication scheme, with a user-generated API token in place of a user's password. You can generate a token for your user [here ](https://id.atlassian.com/manage-profile/security/api-tokens ). Additional information about Atlassian Cloud API tokens can be found [here ](https://confluence.atlassian.com/cloud/api-tokens-938839638.html ).
A more thorough, [runnable example ](examples/basicauth/main.go ) is provided in the examples directory.
2017-02-09 00:37:57 +02:00
2018-02-24 22:06:14 +02:00
```go
2017-02-09 00:37:57 +02:00
func main() {
2018-02-24 22:06:14 +02:00
tp := jira.BasicAuthTransport{
2018-02-24 22:08:07 +02:00
Username: "username",
2018-10-14 02:43:29 +02:00
Password: "token",
2017-02-09 00:37:57 +02:00
}
2018-02-24 22:06:14 +02:00
client, err := jira.NewClient(tp.Client(), "https://my.jira.com")
2017-02-09 00:37:57 +02:00
2018-02-24 22:06:14 +02:00
u, _, err := client.User.Get("some_user")
fmt.Printf("\nEmail: %v\nSuccess!\n", u.EmailAddress)
2017-02-09 00:37:57 +02:00
}
```
2020-09-20 21:05:56 +02:00
#### Basic (self-hosted Jira)
2017-02-09 00:37:57 +02:00
2020-09-20 21:05:56 +02:00
Password-based API authentication works for self-hosted Jira **only** , and has been [deprecated for users of Atlassian Cloud ](https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/ ).
2016-05-28 19:25:51 +02:00
2020-09-20 21:05:56 +02:00
The above token authentication example may be used, substituting a user's password for a generated token.
2015-09-03 13:59:13 +02:00
2017-06-15 19:11:35 +02:00
#### Authenticate with OAuth
2020-05-14 17:18:31 +02:00
If you want to connect via OAuth to your Jira Cloud instance checkout the [example of using OAuth authentication with Jira in Go ](https://gist.github.com/Lupus/edafe9a7c5c6b13407293d795442fe67 ) by [@Lupus ](https://github.com/Lupus ).
2017-06-15 19:11:35 +02:00
2021-10-10 14:07:36 +02:00
For more details have a look at the [issue #56 ](https://github.com/bluevare/go-jira/issues/56 ).
2017-06-15 19:11:35 +02:00
2016-11-04 22:03:11 +02:00
### Create an issue
Example how to create an issue.
```go
package main
import (
"fmt"
2021-10-10 14:07:36 +02:00
"github.com/bluevare/go-jira"
2016-11-04 22:03:11 +02:00
)
func main() {
2018-04-28 13:26:52 +02:00
base := "https://my.jira.com"
2018-10-14 02:43:29 +02:00
tp := jira.BasicAuthTransport{
2018-02-25 02:29:17 +02:00
Username: "username",
2018-10-14 02:43:29 +02:00
Password: "token",
2016-11-04 22:03:11 +02:00
}
2018-04-28 13:26:52 +02:00
jiraClient, err := jira.NewClient(tp.Client(), base)
2018-02-25 02:29:17 +02:00
if err != nil {
2016-11-04 22:03:11 +02:00
panic(err)
}
i := jira.Issue{
Fields: & jira.IssueFields{
Assignee: & jira.User{
Name: "myuser",
},
Reporter: & jira.User{
Name: "youruser",
},
Description: "Test Issue",
Type: jira.IssueType{
2017-05-11 20:39:10 +02:00
Name: "Bug",
2016-11-04 22:03:11 +02:00
},
Project: jira.Project{
2017-05-11 20:39:10 +02:00
Key: "PROJ1",
2016-11-04 22:03:11 +02:00
},
Summary: "Just a demo issue",
},
}
issue, _, err := jiraClient.Issue.Create(& i)
if err != nil {
panic(err)
}
fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)
}
```
2020-09-20 19:19:05 +02:00
### Change an issue status
This is how one can change an issue status. In this example, we change the issue from "To Do" to "In Progress."
```go
package main
import (
"fmt"
2021-10-10 14:07:36 +02:00
"github.com/bluevare/go-jira"
2020-09-20 19:19:05 +02:00
)
func main() {
base := "https://my.jira.com"
tp := jira.BasicAuthTransport{
Username: "username",
Password: "token",
}
jiraClient, err := jira.NewClient(tp.Client(), base)
if err != nil {
panic(err)
}
issue, _, _ := jiraClient.Issue.Get("FART-1", nil)
currentStatus := issue.Fields.Status.Name
fmt.Printf("Current status: %s\n", currentStatus)
var transitionID string
possibleTransitions, _, _ := jiraClient.Issue.GetTransitions("FART-1")
for _, v := range possibleTransitions {
if v.Name == "In Progress" {
transitionID = v.ID
break
}
}
jiraClient.Issue.DoTransition("FART-1", transitionID)
issue, _, _ = jiraClient.Issue.Get(testIssueID, nil)
fmt.Printf("Status after transition: %+v\n", issue.Fields.Status.Name)
}
```
2021-02-18 23:34:23 +02:00
### Get all the issues for JQL with Pagination
2021-03-03 01:00:55 +02:00
Jira API has limit on maxResults it can return. You may have a usecase where you need to get all issues for given JQL.
2021-02-18 23:34:23 +02:00
This example shows reference implementation of GetAllIssues function which does pagination on Jira API to get all the issues for given JQL
2021-10-10 14:07:36 +02:00
please look at [Pagination Example ](https://github.com/bluevare/go-jira/blob/master/examples/pagination/main.go )
2021-03-03 03:52:56 +02:00
2021-02-18 23:34:23 +02:00
2015-09-03 12:58:01 +02:00
### Call a not implemented API endpoint
2020-05-14 17:18:31 +02:00
Not all API endpoints of the Jira API are implemented into *go-jira* .
2016-05-28 19:25:51 +02:00
But you can call them anyway:
2020-05-14 17:18:31 +02:00
Lets get all public projects of [Atlassian`s Jira instance ](https://jira.atlassian.com/ ).
2015-09-03 13:59:13 +02:00
```go
package main
import (
"fmt"
2021-10-10 14:07:36 +02:00
"github.com/bluevare/go-jira"
2015-09-03 13:59:13 +02:00
)
func main() {
2018-04-28 13:26:52 +02:00
base := "https://my.jira.com"
2018-10-14 02:43:29 +02:00
tp := jira.BasicAuthTransport{
2018-02-25 02:29:17 +02:00
Username: "username",
2018-10-14 02:43:29 +02:00
Password: "token",
2018-02-25 02:29:17 +02:00
}
2018-04-28 13:26:52 +02:00
jiraClient, err := jira.NewClient(tp.Client(), base)
2018-06-08 18:02:24 +02:00
req, _ := jiraClient.NewRequest("GET", "rest/api/2/project", nil)
2015-09-03 13:59:13 +02:00
projects := new([]jira.Project)
2018-11-17 17:49:20 +02:00
_, err = jiraClient.Do(req, projects)
2015-09-03 13:59:13 +02:00
if err != nil {
panic(err)
}
for _, project := range *projects {
fmt.Printf("%s: %s\n", project.Key, project.Name)
}
// ...
// BAM: Bamboo
2020-05-14 17:18:31 +02:00
// BAMJ: Bamboo Jira Plugin
2015-09-03 13:59:13 +02:00
// CLOV: Clover
// CONF: Confluence
// ...
}
```
2015-09-03 12:58:01 +02:00
2015-09-03 12:25:03 +02:00
## Implementations
2021-10-10 14:07:36 +02:00
* [bluevare/jitic ](https://github.com/bluevare/jitic ) - The Jira Ticket Checker
2015-09-03 12:25:03 +02:00
2016-03-27 13:02:25 +02:00
## Code structure
The code structure of this package was inspired by [google/go-github ](https://github.com/google/go-github ).
There is one main part (the client).
Based on this main client the other endpoints, like Issues or Authentication are extracted in services. E.g. `IssueService` or `AuthenticationService` .
2020-05-14 17:18:31 +02:00
These services own a responsibility of the single endpoints / usecases of Jira.
2016-03-27 13:02:25 +02:00
## Contribution
2019-10-17 05:19:35 +02:00
We ❤️ PR's
2016-03-27 13:02:25 +02:00
Contribution, in any kind of way, is highly welcome!
It doesn't matter if you are not able to write code.
2021-10-10 14:07:36 +02:00
Creating issues or holding talks and help other people to use [go-jira ](https://github.com/bluevare/go-jira ) is contribution, too!
2016-03-27 13:02:25 +02:00
A few examples:
* Correct typos in the README / documentation
* Reporting bugs
* Implement a new feature or endpoint
2021-10-10 14:07:36 +02:00
* Sharing the love of [go-jira ](https://github.com/bluevare/go-jira ) and help people to get use to it
2016-03-27 13:02:25 +02:00
If you are new to pull requests, checkout [Collaborating on projects using issues and pull requests / Creating a pull request ](https://help.github.com/articles/creating-a-pull-request/ ).
2018-02-17 21:14:47 +02:00
### Dependency management
2019-05-05 02:22:09 +02:00
`go-jira` uses `go modules` for dependency management. After cloning the repo, it's easy to make sure you have the correct dependencies by running `go mod tidy` .
2018-02-17 21:14:47 +02:00
2019-05-05 02:22:09 +02:00
For adding new dependencies, updating dependencies, and other operations, the [Daily workflow ](https://github.com/golang/go/wiki/Modules#daily-workflow ) is a good place to start.
2018-02-17 21:14:47 +02:00
2018-02-24 21:29:55 +02:00
### Sandbox environment for testing
Jira offers sandbox test environments at http://go.atlassian.com/cloud-dev.
You can read more about them at https://developer.atlassian.com/blog/2016/04/cloud-ecosystem-dev-env/.
2019-10-17 05:18:32 +02:00
## Releasing
2020-01-24 18:33:30 +02:00
Install [standard-version ](https://github.com/conventional-changelog/standard-version )
2019-10-17 05:18:32 +02:00
```bash
npm i -g standard-version
```
```bash
standard-version
git push --tags
```
Manually copy/paste text from changelog (for this new version) into the release on Github.com. E.g.
2021-10-10 14:07:36 +02:00
[https://github.com/bluevare/go-jira/releases/edit/v1.11.0 ](https://github.com/bluevare/go-jira/releases/edit/v1.11.0 )
2019-10-17 05:18:32 +02:00
2015-09-03 12:25:03 +02:00
## License
2017-05-01 14:52:54 +02:00
This project is released under the terms of the [MIT license ](http://en.wikipedia.org/wiki/MIT_License ).