aboutsummaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
Diffstat (limited to 'bridge')
-rw-r--r--bridge/bridges.go2
-rw-r--r--bridge/core/auth/credential.go53
-rw-r--r--bridge/core/auth/credential_base.go90
-rw-r--r--bridge/core/auth/credential_test.go24
-rw-r--r--bridge/core/auth/login.go67
-rw-r--r--bridge/core/auth/login_password.go76
-rw-r--r--bridge/core/auth/login_password_test.go14
-rw-r--r--bridge/core/auth/login_test.go13
-rw-r--r--bridge/core/auth/options.go11
-rw-r--r--bridge/core/auth/token.go84
-rw-r--r--bridge/core/auth/token_test.go13
-rw-r--r--bridge/core/bridge.go65
-rw-r--r--bridge/core/config.go5
-rw-r--r--bridge/core/export.go6
-rw-r--r--bridge/core/import.go6
-rw-r--r--bridge/core/interfaces.go7
-rw-r--r--bridge/core/params.go36
-rw-r--r--bridge/github/config.go228
-rw-r--r--bridge/github/config_test.go4
-rw-r--r--bridge/github/export.go18
-rw-r--r--bridge/github/export_test.go17
-rw-r--r--bridge/github/github.go4
-rw-r--r--bridge/github/import.go4
-rw-r--r--bridge/github/import_test.go11
-rw-r--r--bridge/gitlab/config.go223
-rw-r--r--bridge/gitlab/export.go16
-rw-r--r--bridge/gitlab/export_test.go17
-rw-r--r--bridge/gitlab/gitlab.go10
-rw-r--r--bridge/gitlab/import.go12
-rw-r--r--bridge/gitlab/import_test.go11
-rw-r--r--bridge/jira/client.go1461
-rw-r--r--bridge/jira/config.go192
-rw-r--r--bridge/jira/export.go475
-rw-r--r--bridge/jira/import.go655
-rw-r--r--bridge/jira/jira.go143
-rw-r--r--bridge/launchpad/config.go28
-rw-r--r--bridge/launchpad/import.go2
-rw-r--r--bridge/launchpad/launchpad.go4
38 files changed, 3580 insertions, 527 deletions
diff --git a/bridge/bridges.go b/bridge/bridges.go
index 5d3066f9..d74a58fa 100644
--- a/bridge/bridges.go
+++ b/bridge/bridges.go
@@ -5,6 +5,7 @@ import (
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/bridge/github"
"github.com/MichaelMure/git-bug/bridge/gitlab"
+ "github.com/MichaelMure/git-bug/bridge/jira"
"github.com/MichaelMure/git-bug/bridge/launchpad"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
@@ -14,6 +15,7 @@ func init() {
core.Register(&github.Github{})
core.Register(&gitlab.Gitlab{})
core.Register(&launchpad.Launchpad{})
+ core.Register(&jira.Jira{})
}
// Targets return all known bridge implementation target
diff --git a/bridge/core/auth/credential.go b/bridge/core/auth/credential.go
index 6dcac09f..d95b23c7 100644
--- a/bridge/core/auth/credential.go
+++ b/bridge/core/auth/credential.go
@@ -1,6 +1,8 @@
package auth
import (
+ "crypto/rand"
+ "encoding/base64"
"errors"
"fmt"
"regexp"
@@ -16,6 +18,7 @@ const (
configKeyKind = "kind"
configKeyTarget = "target"
configKeyCreateTime = "createtime"
+ configKeySalt = "salt"
configKeyPrefixMeta = "meta."
MetaKeyLogin = "login"
@@ -26,6 +29,7 @@ type CredentialKind string
const (
KindToken CredentialKind = "token"
+ KindLogin CredentialKind = "login"
KindLoginPassword CredentialKind = "login-password"
)
@@ -37,9 +41,10 @@ func NewErrMultipleMatchCredential(matching []entity.Id) *entity.ErrMultipleMatc
type Credential interface {
ID() entity.Id
- Target() string
Kind() CredentialKind
+ Target() string
CreateTime() time.Time
+ Salt() []byte
Validate() error
Metadata() map[string]string
@@ -47,7 +52,7 @@ type Credential interface {
SetMetadata(key string, value string)
// Return all the specific properties of the credential that need to be saved into the configuration.
- // This does not include Target, Kind, CreateTime and Metadata.
+ // This does not include Target, Kind, CreateTime, Metadata or Salt.
toConfig() map[string]string
}
@@ -108,13 +113,21 @@ func loadFromConfig(rawConfigs map[string]string, id entity.Id) (Credential, err
}
var cred Credential
+ var err error
switch CredentialKind(configs[configKeyKind]) {
case KindToken:
- cred = NewTokenFromConfig(configs)
+ cred, err = NewTokenFromConfig(configs)
+ case KindLogin:
+ cred, err = NewLoginFromConfig(configs)
case KindLoginPassword:
+ cred, err = NewLoginPasswordFromConfig(configs)
default:
- return nil, fmt.Errorf("unknown credential type %s", configs[configKeyKind])
+ return nil, fmt.Errorf("unknown credential type \"%s\"", configs[configKeyKind])
+ }
+
+ if err != nil {
+ return nil, fmt.Errorf("loading credential: %v", err)
}
return cred, nil
@@ -134,6 +147,23 @@ func metaFromConfig(configs map[string]string) map[string]string {
return result
}
+func makeSalt() []byte {
+ result := make([]byte, 16)
+ _, err := rand.Read(result)
+ if err != nil {
+ panic(err)
+ }
+ return result
+}
+
+func saltFromConfig(configs map[string]string) ([]byte, error) {
+ val, ok := configs[configKeySalt]
+ if !ok {
+ return nil, fmt.Errorf("no credential salt found")
+ }
+ return base64.StdEncoding.DecodeString(val)
+}
+
// List load all existing credentials
func List(repo repository.RepoConfig, opts ...Option) ([]Credential, error) {
rawConfigs, err := repo.GlobalConfig().ReadAll(configKeyPrefix + ".")
@@ -141,10 +171,7 @@ func List(repo repository.RepoConfig, opts ...Option) ([]Credential, error) {
return nil, err
}
- re, err := regexp.Compile(`^` + configKeyPrefix + `\.([^.]+)\.([^.]+(?:\.[^.]+)*)$`)
- if err != nil {
- panic(err)
- }
+ re := regexp.MustCompile(`^` + configKeyPrefix + `\.([^.]+)\.([^.]+(?:\.[^.]+)*)$`)
mapped := make(map[string]map[string]string)
@@ -211,6 +238,16 @@ func Store(repo repository.RepoConfig, cred Credential) error {
return err
}
+ // Salt
+ if len(cred.Salt()) != 16 {
+ panic("credentials need to be salted")
+ }
+ encoded := base64.StdEncoding.EncodeToString(cred.Salt())
+ err = repo.GlobalConfig().StoreString(prefix+configKeySalt, encoded)
+ if err != nil {
+ return err
+ }
+
// Metadata
for key, val := range cred.Metadata() {
err := repo.GlobalConfig().StoreString(prefix+configKeyPrefixMeta+key, val)
diff --git a/bridge/core/auth/credential_base.go b/bridge/core/auth/credential_base.go
new file mode 100644
index 00000000..488c223c
--- /dev/null
+++ b/bridge/core/auth/credential_base.go
@@ -0,0 +1,90 @@
+package auth
+
+import (
+ "fmt"
+ "time"
+
+ "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+type credentialBase struct {
+ target string
+ createTime time.Time
+ salt []byte
+ meta map[string]string
+}
+
+func newCredentialBase(target string) *credentialBase {
+ return &credentialBase{
+ target: target,
+ createTime: time.Now(),
+ salt: makeSalt(),
+ }
+}
+
+func newCredentialBaseFromConfig(conf map[string]string) (*credentialBase, error) {
+ base := &credentialBase{
+ target: conf[configKeyTarget],
+ meta: metaFromConfig(conf),
+ }
+
+ if createTime, ok := conf[configKeyCreateTime]; ok {
+ t, err := repository.ParseTimestamp(createTime)
+ if err != nil {
+ return nil, err
+ }
+ base.createTime = t
+ } else {
+ return nil, fmt.Errorf("missing create time")
+ }
+
+ salt, err := saltFromConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+ base.salt = salt
+
+ return base, nil
+}
+
+func (cb *credentialBase) Target() string {
+ return cb.target
+}
+
+func (cb *credentialBase) CreateTime() time.Time {
+ return cb.createTime
+}
+
+func (cb *credentialBase) Salt() []byte {
+ return cb.salt
+}
+
+func (cb *credentialBase) validate() error {
+ if cb.target == "" {
+ return fmt.Errorf("missing target")
+ }
+ if cb.createTime.IsZero() || cb.createTime.Equal(time.Time{}) {
+ return fmt.Errorf("missing creation time")
+ }
+ if !core.TargetExist(cb.target) {
+ return fmt.Errorf("unknown target")
+ }
+ return nil
+}
+
+func (cb *credentialBase) Metadata() map[string]string {
+ return cb.meta
+}
+
+func (cb *credentialBase) GetMetadata(key string) (string, bool) {
+ val, ok := cb.meta[key]
+ return val, ok
+}
+
+func (cb *credentialBase) SetMetadata(key string, value string) {
+ if cb.meta == nil {
+ cb.meta = make(map[string]string)
+ }
+ cb.meta[key] = value
+}
diff --git a/bridge/core/auth/credential_test.go b/bridge/core/auth/credential_test.go
index 2f8806c9..60c631d7 100644
--- a/bridge/core/auth/credential_test.go
+++ b/bridge/core/auth/credential_test.go
@@ -14,7 +14,7 @@ func TestCredential(t *testing.T) {
repo := repository.NewMockRepoForTest()
storeToken := func(val string, target string) *Token {
- token := NewToken(val, target)
+ token := NewToken(target, val)
err := Store(repo, token)
require.NoError(t, err)
return token
@@ -100,3 +100,25 @@ func sameIds(t *testing.T, a []Credential, b []Credential) {
assert.ElementsMatch(t, ids(a), ids(b))
}
+
+func testCredentialSerial(t *testing.T, original Credential) Credential {
+ repo := repository.NewMockRepoForTest()
+
+ original.SetMetadata("test", "value")
+
+ assert.NotEmpty(t, original.ID().String())
+ assert.NotEmpty(t, original.Salt())
+ assert.NoError(t, Store(repo, original))
+
+ loaded, err := LoadWithId(repo, original.ID())
+ assert.NoError(t, err)
+
+ assert.Equal(t, original.ID(), loaded.ID())
+ assert.Equal(t, original.Kind(), loaded.Kind())
+ assert.Equal(t, original.Target(), loaded.Target())
+ assert.Equal(t, original.CreateTime().Unix(), loaded.CreateTime().Unix())
+ assert.Equal(t, original.Salt(), loaded.Salt())
+ assert.Equal(t, original.Metadata(), loaded.Metadata())
+
+ return loaded
+}
diff --git a/bridge/core/auth/login.go b/bridge/core/auth/login.go
new file mode 100644
index 00000000..ea74835a
--- /dev/null
+++ b/bridge/core/auth/login.go
@@ -0,0 +1,67 @@
+package auth
+
+import (
+ "crypto/sha256"
+ "fmt"
+
+ "github.com/MichaelMure/git-bug/entity"
+)
+
+const (
+ configKeyLoginLogin = "login"
+)
+
+var _ Credential = &Login{}
+
+type Login struct {
+ *credentialBase
+ Login string
+}
+
+func NewLogin(target, login string) *Login {
+ return &Login{
+ credentialBase: newCredentialBase(target),
+ Login: login,
+ }
+}
+
+func NewLoginFromConfig(conf map[string]string) (*Login, error) {
+ base, err := newCredentialBaseFromConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+
+ return &Login{
+ credentialBase: base,
+ Login: conf[configKeyLoginLogin],
+ }, nil
+}
+
+func (lp *Login) ID() entity.Id {
+ h := sha256.New()
+ _, _ = h.Write(lp.salt)
+ _, _ = h.Write([]byte(lp.target))
+ _, _ = h.Write([]byte(lp.Login))
+ return entity.Id(fmt.Sprintf("%x", h.Sum(nil)))
+}
+
+func (lp *Login) Kind() CredentialKind {
+ return KindLogin
+}
+
+func (lp *Login) Validate() error {
+ err := lp.credentialBase.validate()
+ if err != nil {
+ return err
+ }
+ if lp.Login == "" {
+ return fmt.Errorf("missing login")
+ }
+ return nil
+}
+
+func (lp *Login) toConfig() map[string]string {
+ return map[string]string{
+ configKeyLoginLogin: lp.Login,
+ }
+}
diff --git a/bridge/core/auth/login_password.go b/bridge/core/auth/login_password.go
new file mode 100644
index 00000000..1981026a
--- /dev/null
+++ b/bridge/core/auth/login_password.go
@@ -0,0 +1,76 @@
+package auth
+
+import (
+ "crypto/sha256"
+ "fmt"
+
+ "github.com/MichaelMure/git-bug/entity"
+)
+
+const (
+ configKeyLoginPasswordLogin = "login"
+ configKeyLoginPasswordPassword = "password"
+)
+
+var _ Credential = &LoginPassword{}
+
+type LoginPassword struct {
+ *credentialBase
+ Login string
+ Password string
+}
+
+func NewLoginPassword(target, login, password string) *LoginPassword {
+ return &LoginPassword{
+ credentialBase: newCredentialBase(target),
+ Login: login,
+ Password: password,
+ }
+}
+
+func NewLoginPasswordFromConfig(conf map[string]string) (*LoginPassword, error) {
+ base, err := newCredentialBaseFromConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+
+ return &LoginPassword{
+ credentialBase: base,
+ Login: conf[configKeyLoginPasswordLogin],
+ Password: conf[configKeyLoginPasswordPassword],
+ }, nil
+}
+
+func (lp *LoginPassword) ID() entity.Id {
+ h := sha256.New()
+ _, _ = h.Write(lp.salt)
+ _, _ = h.Write([]byte(lp.target))
+ _, _ = h.Write([]byte(lp.Login))
+ _, _ = h.Write([]byte(lp.Password))
+ return entity.Id(fmt.Sprintf("%x", h.Sum(nil)))
+}
+
+func (lp *LoginPassword) Kind() CredentialKind {
+ return KindLoginPassword
+}
+
+func (lp *LoginPassword) Validate() error {
+ err := lp.credentialBase.validate()
+ if err != nil {
+ return err
+ }
+ if lp.Login == "" {
+ return fmt.Errorf("missing login")
+ }
+ if lp.Password == "" {
+ return fmt.Errorf("missing password")
+ }
+ return nil
+}
+
+func (lp *LoginPassword) toConfig() map[string]string {
+ return map[string]string{
+ configKeyLoginPasswordLogin: lp.Login,
+ configKeyLoginPasswordPassword: lp.Password,
+ }
+}
diff --git a/bridge/core/auth/login_password_test.go b/bridge/core/auth/login_password_test.go
new file mode 100644
index 00000000..d9d82f52
--- /dev/null
+++ b/bridge/core/auth/login_password_test.go
@@ -0,0 +1,14 @@
+package auth
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestLoginPasswordSerial(t *testing.T) {
+ original := NewLoginPassword("github", "jean", "jacques")
+ loaded := testCredentialSerial(t, original)
+ assert.Equal(t, original.Login, loaded.(*LoginPassword).Login)
+ assert.Equal(t, original.Password, loaded.(*LoginPassword).Password)
+}
diff --git a/bridge/core/auth/login_test.go b/bridge/core/auth/login_test.go
new file mode 100644
index 00000000..3fc4a391
--- /dev/null
+++ b/bridge/core/auth/login_test.go
@@ -0,0 +1,13 @@
+package auth
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestLoginSerial(t *testing.T) {
+ original := NewLogin("github", "jean")
+ loaded := testCredentialSerial(t, original)
+ assert.Equal(t, original.Login, loaded.(*Login).Login)
+}
diff --git a/bridge/core/auth/options.go b/bridge/core/auth/options.go
index 74189874..1d8c44d1 100644
--- a/bridge/core/auth/options.go
+++ b/bridge/core/auth/options.go
@@ -2,7 +2,7 @@ package auth
type options struct {
target string
- kind CredentialKind
+ kind map[CredentialKind]interface{}
meta map[string]string
}
@@ -21,7 +21,8 @@ func (opts *options) Match(cred Credential) bool {
return false
}
- if opts.kind != "" && cred.Kind() != opts.kind {
+ _, has := opts.kind[cred.Kind()]
+ if len(opts.kind) > 0 && !has {
return false
}
@@ -40,9 +41,13 @@ func WithTarget(target string) Option {
}
}
+// WithKind match credentials with the given kind. Can be specified multiple times.
func WithKind(kind CredentialKind) Option {
return func(opts *options) {
- opts.kind = kind
+ if opts.kind == nil {
+ opts.kind = make(map[CredentialKind]interface{})
+ }
+ opts.kind[kind] = nil
}
}
diff --git a/bridge/core/auth/token.go b/bridge/core/auth/token.go
index 42f960bf..1f019f44 100644
--- a/bridge/core/auth/token.go
+++ b/bridge/core/auth/token.go
@@ -3,104 +3,68 @@ package auth
import (
"crypto/sha256"
"fmt"
- "time"
- "github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/entity"
- "github.com/MichaelMure/git-bug/repository"
)
const (
- tokenValueKey = "value"
+ configKeyTokenValue = "value"
)
var _ Credential = &Token{}
// Token holds an API access token data
type Token struct {
- target string
- createTime time.Time
- Value string
- meta map[string]string
+ *credentialBase
+ Value string
}
// NewToken instantiate a new token
-func NewToken(value, target string) *Token {
+func NewToken(target, value string) *Token {
return &Token{
- target: target,
- createTime: time.Now(),
- Value: value,
+ credentialBase: newCredentialBase(target),
+ Value: value,
}
}
-func NewTokenFromConfig(conf map[string]string) *Token {
- token := &Token{}
-
- token.target = conf[configKeyTarget]
- if createTime, ok := conf[configKeyCreateTime]; ok {
- if t, err := repository.ParseTimestamp(createTime); err == nil {
- token.createTime = t
- }
+func NewTokenFromConfig(conf map[string]string) (*Token, error) {
+ base, err := newCredentialBaseFromConfig(conf)
+ if err != nil {
+ return nil, err
}
- token.Value = conf[tokenValueKey]
- token.meta = metaFromConfig(conf)
-
- return token
+ return &Token{
+ credentialBase: base,
+ Value: conf[configKeyTokenValue],
+ }, nil
}
func (t *Token) ID() entity.Id {
- sum := sha256.Sum256([]byte(t.target + t.Value))
- return entity.Id(fmt.Sprintf("%x", sum))
-}
-
-func (t *Token) Target() string {
- return t.target
+ h := sha256.New()
+ _, _ = h.Write(t.salt)
+ _, _ = h.Write([]byte(t.target))
+ _, _ = h.Write([]byte(t.Value))
+ return entity.Id(fmt.Sprintf("%x", h.Sum(nil)))
}
func (t *Token) Kind() CredentialKind {
return KindToken
}
-func (t *Token) CreateTime() time.Time {
- return t.createTime
-}
-
// Validate ensure token important fields are valid
func (t *Token) Validate() error {
+ err := t.credentialBase.validate()
+ if err != nil {
+ return err
+ }
if t.Value == "" {
return fmt.Errorf("missing value")
}
- if t.target == "" {
- return fmt.Errorf("missing target")
- }
- if t.createTime.IsZero() || t.createTime.Equal(time.Time{}) {
- return fmt.Errorf("missing creation time")
- }
- if !core.TargetExist(t.target) {
- return fmt.Errorf("unknown target")
- }
return nil
}
-func (t *Token) Metadata() map[string]string {
- return t.meta
-}
-
-func (t *Token) GetMetadata(key string) (string, bool) {
- val, ok := t.meta[key]
- return val, ok
-}
-
-func (t *Token) SetMetadata(key string, value string) {
- if t.meta == nil {
- t.meta = make(map[string]string)
- }
- t.meta[key] = value
-}
-
func (t *Token) toConfig() map[string]string {
return map[string]string{
- tokenValueKey: t.Value,
+ configKeyTokenValue: t.Value,
}
}
diff --git a/bridge/core/auth/token_test.go b/bridge/core/auth/token_test.go
new file mode 100644
index 00000000..d8cd6652
--- /dev/null
+++ b/bridge/core/auth/token_test.go
@@ -0,0 +1,13 @@
+package auth
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestTokenSerial(t *testing.T) {
+ original := NewToken("github", "value")
+ loaded := testCredentialSerial(t, original)
+ assert.Equal(t, original.Value, loaded.(*Token).Value)
+}
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index ac0d47d7..8c1f9714 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -4,6 +4,7 @@ package core
import (
"context"
"fmt"
+ "os"
"reflect"
"regexp"
"sort"
@@ -30,18 +31,6 @@ const (
var bridgeImpl map[string]reflect.Type
var bridgeLoginMetaKey map[string]string
-// BridgeParams holds parameters to simplify the bridge configuration without
-// having to make terminal prompts.
-type BridgeParams struct {
- Owner string // owner of the repo (Github)
- Project string // name of the repo (Github, Launchpad)
- URL string // complete URL of a repo (Github, Gitlab, Launchpad)
- BaseURL string // base URL for self-hosted instance ( Gitlab)
- CredPrefix string // ID prefix of the credential to use (Github, Gitlab)
- TokenRaw string // pre-existing token to use (Github, Gitlab)
- Login string // username for the passed credential (Github, Gitlab)
-}
-
// Bridge is a wrapper around a BridgeImpl that will bind low-level
// implementation with utility code to provide high-level functions.
type Bridge struct {
@@ -63,7 +52,7 @@ func Register(impl BridgeImpl) {
if bridgeLoginMetaKey == nil {
bridgeLoginMetaKey = make(map[string]string)
}
- bridgeImpl[impl.Target()] = reflect.TypeOf(impl)
+ bridgeImpl[impl.Target()] = reflect.TypeOf(impl).Elem()
bridgeLoginMetaKey[impl.Target()] = impl.LoginMetaKey()
}
@@ -105,7 +94,7 @@ func NewBridge(repo *cache.RepoCache, target string, name string) (*Bridge, erro
return nil, fmt.Errorf("unknown bridge target %v", target)
}
- impl := reflect.New(implType).Elem().Interface().(BridgeImpl)
+ impl := reflect.New(implType).Interface().(BridgeImpl)
bridge := &Bridge{
Name: name,
@@ -166,10 +155,7 @@ func ConfiguredBridges(repo repository.RepoConfig) ([]string, error) {
return nil, errors.Wrap(err, "can't read configured bridges")
}
- re, err := regexp.Compile(bridgeConfigKeyPrefix + `.([^.]+)`)
- if err != nil {
- panic(err)
- }
+ re := regexp.MustCompile(bridgeConfigKeyPrefix + `.([^.]+)`)
set := make(map[string]interface{})
@@ -205,10 +191,7 @@ func BridgeExist(repo repository.RepoConfig, name string) bool {
// Remove a configured bridge
func RemoveBridge(repo repository.RepoConfig, name string) error {
- re, err := regexp.Compile(`^[a-zA-Z0-9]+`)
- if err != nil {
- panic(err)
- }
+ re := regexp.MustCompile(`^[a-zA-Z0-9]+`)
if !re.MatchString(name) {
return fmt.Errorf("bad bridge fullname: %s", name)
@@ -220,6 +203,8 @@ func RemoveBridge(repo repository.RepoConfig, name string) error {
// Configure run the target specific configuration process
func (b *Bridge) Configure(params BridgeParams) error {
+ validateParams(params, b.impl)
+
conf, err := b.impl.Configure(b.repo, params)
if err != nil {
return err
@@ -234,6 +219,22 @@ func (b *Bridge) Configure(params BridgeParams) error {
return b.storeConfig(conf)
}
+func validateParams(params BridgeParams, impl BridgeImpl) {
+ validParams := impl.ValidParams()
+
+ paramsValue := reflect.ValueOf(params)
+ paramsType := paramsValue.Type()
+
+ for i := 0; i < paramsValue.NumField(); i++ {
+ name := paramsType.Field(i).Name
+ val := paramsValue.Field(i).Interface().(string)
+ _, valid := validParams[name]
+ if val != "" && !valid {
+ _, _ = fmt.Fprintln(os.Stderr, params.fieldWarning(name, impl.Target()))
+ }
+ }
+}
+
func (b *Bridge) storeConfig(conf Configuration) error {
for key, val := range conf {
storeKey := fmt.Sprintf("git-bug.bridge.%s.%s", b.Name, key)
@@ -292,14 +293,14 @@ func (b *Bridge) getExporter() Exporter {
return b.exporter
}
-func (b *Bridge) ensureImportInit() error {
+func (b *Bridge) ensureImportInit(ctx context.Context) error {
if b.initImportDone {
return nil
}
importer := b.getImporter()
if importer != nil {
- err := importer.Init(b.repo, b.conf)
+ err := importer.Init(ctx, b.repo, b.conf)
if err != nil {
return err
}
@@ -309,22 +310,14 @@ func (b *Bridge) ensureImportInit() error {
return nil
}
-func (b *Bridge) ensureExportInit() error {
+func (b *Bridge) ensureExportInit(ctx context.Context) error {
if b.initExportDone {
return nil
}
- importer := b.getImporter()
- if importer != nil {
- err := importer.Init(b.repo, b.conf)
- if err != nil {
- return err
- }
- }
-
exporter := b.getExporter()
if exporter != nil {
- err := exporter.Init(b.repo, b.conf)
+ err := exporter.Init(ctx, b.repo, b.conf)
if err != nil {
return err
}
@@ -348,7 +341,7 @@ func (b *Bridge) ImportAllSince(ctx context.Context, since time.Time) (<-chan Im
return nil, err
}
- err = b.ensureImportInit()
+ err = b.ensureImportInit(ctx)
if err != nil {
return nil, err
}
@@ -402,7 +395,7 @@ func (b *Bridge) ExportAll(ctx context.Context, since time.Time) (<-chan ExportR
return nil, err
}
- err = b.ensureExportInit()
+ err = b.ensureExportInit(ctx)
if err != nil {
return nil, err
}
diff --git a/bridge/core/config.go b/bridge/core/config.go
index afcda560..7f8d7e13 100644
--- a/bridge/core/config.go
+++ b/bridge/core/config.go
@@ -1,6 +1,8 @@
package core
import (
+ "fmt"
+
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/identity"
)
@@ -24,6 +26,7 @@ func FinishConfig(repo *cache.RepoCache, metaKey string, login string) error {
return err
}
if err == nil {
+ fmt.Printf("Current identity %v tagged with login %v\n", user.Id().Human(), login)
// found one
user.SetMetadata(metaKey, login)
return user.CommitAsNeeded()
@@ -42,5 +45,7 @@ func FinishConfig(repo *cache.RepoCache, metaKey string, login string) error {
return err
}
+ fmt.Printf("Identity %v created, set as current\n", i.Id().Human())
+
return nil
}
diff --git a/bridge/core/export.go b/bridge/core/export.go
index 4397a527..fa531c5f 100644
--- a/bridge/core/export.go
+++ b/bridge/core/export.go
@@ -27,12 +27,12 @@ const (
// Nothing changed on the bug
ExportEventNothing
- // Error happened during export
- ExportEventError
-
// Something wrong happened during export that is worth notifying to the user
// but not severe enough to consider the export a failure.
ExportEventWarning
+
+ // Error happened during export
+ ExportEventError
)
// ExportResult is an event that is emitted during the export process, to
diff --git a/bridge/core/import.go b/bridge/core/import.go
index f0a6f0c8..0b0b4c68 100644
--- a/bridge/core/import.go
+++ b/bridge/core/import.go
@@ -30,12 +30,12 @@ const (
// Identity has been created
ImportEventIdentity
- // Error happened during import
- ImportEventError
-
// Something wrong happened during import that is worth notifying to the user
// but not severe enough to consider the import a failure.
ImportEventWarning
+
+ // Error happened during import
+ ImportEventError
)
// ImportResult is an event that is emitted during the import process, to
diff --git a/bridge/core/interfaces.go b/bridge/core/interfaces.go
index ab2f3977..f20f1642 100644
--- a/bridge/core/interfaces.go
+++ b/bridge/core/interfaces.go
@@ -18,6 +18,9 @@ type BridgeImpl interface {
// credentials.
LoginMetaKey() string
+ // The set of the BridgeParams fields supported
+ ValidParams() map[string]interface{}
+
// Configure handle the user interaction and return a key/value configuration
// for future use
Configure(repo *cache.RepoCache, params BridgeParams) (Configuration, error)
@@ -33,11 +36,11 @@ type BridgeImpl interface {
}
type Importer interface {
- Init(repo *cache.RepoCache, conf Configuration) error
+ Init(ctx context.Context, repo *cache.RepoCache, conf Configuration) error
ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan ImportResult, error)
}
type Exporter interface {
- Init(repo *cache.RepoCache, conf Configuration) error
+ Init(ctx context.Context, repo *cache.RepoCache, conf Configuration) error
ExportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan ExportResult, error)
}
diff --git a/bridge/core/params.go b/bridge/core/params.go
new file mode 100644
index 00000000..e398b81a
--- /dev/null
+++ b/bridge/core/params.go
@@ -0,0 +1,36 @@
+package core
+
+import "fmt"
+
+// BridgeParams holds parameters to simplify the bridge configuration without
+// having to make terminal prompts.
+type BridgeParams struct {
+ URL string // complete URL of a repo (Github, Gitlab, , Launchpad)
+ BaseURL string // base URL for self-hosted instance ( Gitlab, Jira, )
+ Login string // username for the passed credential (Github, Gitlab, Jira, )
+ CredPrefix string // ID prefix of the credential to use (Github, Gitlab, Jira, )
+ TokenRaw string // pre-existing token to use (Github, Gitlab, , )
+ Owner string // owner of the repo (Github, , , )
+ Project string // name of the repo or project key (Github, , Jira, Launchpad)
+}
+
+func (BridgeParams) fieldWarning(field string, target string) string {
+ switch field {
+ case "URL":
+ return fmt.Sprintf("warning: --url is ineffective for a %s bridge", target)
+ case "BaseURL":
+ return fmt.Sprintf("warning: --base-url is ineffective for a %s bridge", target)
+ case "Login":
+ return fmt.Sprintf("warning: --login is ineffective for a %s bridge", target)
+ case "CredPrefix":
+ return fmt.Sprintf("warning: --credential is ineffective for a %s bridge", target)
+ case "TokenRaw":
+ return fmt.Sprintf("warning: tokens are ineffective for a %s bridge", target)
+ case "Owner":
+ return fmt.Sprintf("warning: --owner is ineffective for a %s bridge", target)
+ case "Project":
+ return fmt.Sprintf("warning: --project is ineffective for a %s bridge", target)
+ default:
+ panic("unknown field")
+ }
+}
diff --git a/bridge/github/config.go b/bridge/github/config.go
index cc312230..b6f69d74 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -1,7 +1,6 @@
package github
import (
- "bufio"
"bytes"
"context"
"encoding/json"
@@ -10,14 +9,11 @@ import (
"io/ioutil"
"math/rand"
"net/http"
- "os"
"regexp"
"sort"
- "strconv"
"strings"
"time"
- text "github.com/MichaelMure/go-term-text"
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/bridge/core"
@@ -25,19 +21,24 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/input"
"github.com/MichaelMure/git-bug/repository"
- "github.com/MichaelMure/git-bug/util/colors"
)
var (
ErrBadProjectURL = errors.New("bad project url")
)
-func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
- if params.BaseURL != "" {
- fmt.Println("warning: --base-url is ineffective for a Github bridge")
+func (g *Github) ValidParams() map[string]interface{} {
+ return map[string]interface{}{
+ "URL": nil,
+ "Login": nil,
+ "CredPrefix": nil,
+ "TokenRaw": nil,
+ "Owner": nil,
+ "Project": nil,
}
+}
- conf := make(core.Configuration)
+func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
var err error
var owner string
var project string
@@ -86,7 +87,7 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
}
login = l
case params.TokenRaw != "":
- token := auth.NewToken(params.TokenRaw, target)
+ token := auth.NewToken(target, params.TokenRaw)
login, err = getLoginFromToken(token)
if err != nil {
return nil, err
@@ -121,9 +122,10 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
return nil, fmt.Errorf("project doesn't exist or authentication token has an incorrect scope")
}
+ conf := make(core.Configuration)
conf[core.ConfigKeyTarget] = target
- conf[keyOwner] = owner
- conf[keyProject] = project
+ conf[confKeyOwner] = owner
+ conf[confKeyProject] = project
err = g.ValidateConfig(conf)
if err != nil {
@@ -148,18 +150,18 @@ func (*Github) ValidateConfig(conf core.Configuration) error {
return fmt.Errorf("unexpected target name: %v", v)
}
- if _, ok := conf[keyOwner]; !ok {
- return fmt.Errorf("missing %s key", keyOwner)
+ if _, ok := conf[confKeyOwner]; !ok {
+ return fmt.Errorf("missing %s key", confKeyOwner)
}
- if _, ok := conf[keyProject]; !ok {
- return fmt.Errorf("missing %s key", keyProject)
+ if _, ok := conf[confKeyProject]; !ok {
+ return fmt.Errorf("missing %s key", confKeyProject)
}
return nil
}
-func usernameValidator(name string, value string) (string, error) {
+func usernameValidator(_ string, value string) (string, error) {
ok, err := validateUsername(value)
if err != nil {
return "", err
@@ -241,67 +243,36 @@ func randomFingerprint() string {
}
func promptTokenOptions(repo repository.RepoConfig, login, owner, project string) (auth.Credential, error) {
- for {
- creds, err := auth.List(repo,
- auth.WithTarget(target),
- auth.WithKind(auth.KindToken),
- auth.WithMeta(auth.MetaKeyLogin, login),
- )
- if err != nil {
- return nil, err
- }
-
- fmt.Println()
- fmt.Println("[1]: enter my token")
- fmt.Println("[2]: interactive token creation")
-
- if len(creds) > 0 {
- sort.Sort(auth.ById(creds))
-
- fmt.Println()
- fmt.Println("Existing tokens for Github:")
- for i, cred := range creds {
- token := cred.(*auth.Token)
- fmt.Printf("[%d]: %s => %s (login: %s, %s)\n",
- i+3,
- colors.Cyan(token.ID().Human()),
- colors.Red(text.TruncateMax(token.Value, 10)),
- token.Metadata()[auth.MetaKeyLogin],
- token.CreateTime().Format(time.RFC822),
- )
- }
- }
-
- fmt.Println()
- fmt.Print("Select option: ")
+ creds, err := auth.List(repo,
+ auth.WithTarget(target),
+ auth.WithKind(auth.KindToken),
+ auth.WithMeta(auth.MetaKeyLogin, login),
+ )
+ if err != nil {
+ return nil, err
+ }
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- fmt.Println()
+ cred, index, err := input.PromptCredential(target, "token", creds, []string{
+ "enter my token",
+ "interactive token creation",
+ })
+ switch {
+ case err != nil:
+ return nil, err
+ case cred != nil:
+ return cred, nil
+ case index == 0:
+ return promptToken()
+ case index == 1:
+ value, err := loginAndRequestToken(login, owner, project)
if err != nil {
return nil, err
}
-
- line = strings.TrimSpace(line)
- index, err := strconv.Atoi(line)
- if err != nil || index < 1 || index > len(creds)+2 {
- fmt.Println("invalid input")
- continue
- }
-
- switch index {
- case 1:
- return promptToken()
- case 2:
- value, err := loginAndRequestToken(login, owner, project)
- if err != nil {
- return nil, err
- }
- token := auth.NewToken(value, target)
- token.SetMetadata(auth.MetaKeyLogin, login)
- return token, nil
- default:
- return creds[index-3], nil
- }
+ token := auth.NewToken(target, value)
+ token.SetMetadata(auth.MetaKeyLogin, login)
+ return token, nil
+ default:
+ panic("missed case")
}
}
@@ -316,10 +287,7 @@ func promptToken() (*auth.Token, error) {
fmt.Println(" - 'repo' : to be able to read private repositories")
fmt.Println()
- re, err := regexp.Compile(`^[a-zA-Z0-9]{40}$`)
- if err != nil {
- panic("regexp compile:" + err.Error())
- }
+ re := regexp.MustCompile(`^[a-zA-Z0-9]{40}$`)
var login string
@@ -327,7 +295,7 @@ func promptToken() (*auth.Token, error) {
if !re.MatchString(value) {
return "token has incorrect format", nil
}
- login, err = getLoginFromToken(auth.NewToken(value, target))
+ login, err = getLoginFromToken(auth.NewToken(target, value))
if err != nil {
return fmt.Sprintf("token is invalid: %v", err), nil
}
@@ -339,7 +307,7 @@ func promptToken() (*auth.Token, error) {
return nil, err
}
- token := auth.NewToken(rawToken, target)
+ token := auth.NewToken(target, rawToken)
token.SetMetadata(auth.MetaKeyLogin, login)
return token, nil
@@ -356,31 +324,19 @@ func loginAndRequestToken(login, owner, project string) (string, error) {
fmt.Println()
// prompt project visibility to know the token scope needed for the repository
- i, err := input.PromptChoice("repository visibility", []string{"public", "private"})
+ index, err := input.PromptChoice("repository visibility", []string{"public", "private"})
if err != nil {
return "", err
}
- isPublic := i == 0
+ scope := []string{"public_repo", "repo"}[index]
password, err := input.PromptPassword("Password", "password", input.Required)
if err != nil {
return "", err
}
- var scope string
- if isPublic {
- // public_repo is requested to be able to read public repositories
- scope = "public_repo"
- } else {
- // 'repo' is request to be able to read private repositories
- // /!\ token will have read/write rights on every private repository you have access to
- scope = "repo"
- }
-
// Attempt to authenticate and create a token
-
note := fmt.Sprintf("git-bug - %s/%s", owner, project)
-
resp, err := requestToken(note, login, password, scope)
if err != nil {
return "", err
@@ -413,73 +369,25 @@ func loginAndRequestToken(login, owner, project string) (string, error) {
}
func promptURL(repo repository.RepoCommon) (string, string, error) {
- // remote suggestions
- remotes, err := repo.GetRemotes()
+ validRemotes, err := getValidGithubRemoteURLs(repo)
if err != nil {
return "", "", err
}
- validRemotes := getValidGithubRemoteURLs(remotes)
- if len(validRemotes) > 0 {
- for {
- fmt.Println("\nDetected projects:")
-
- // print valid remote github urls
- for i, remote := range validRemotes {
- fmt.Printf("[%d]: %v\n", i+1, remote)
- }
-
- fmt.Printf("\n[0]: Another project\n\n")
- fmt.Printf("Select option: ")
-
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- if err != nil {
- return "", "", err
- }
-
- line = strings.TrimSpace(line)
-
- index, err := strconv.Atoi(line)
- if err != nil || index < 0 || index > len(validRemotes) {
- fmt.Println("invalid input")
- continue
- }
-
- // if user want to enter another project url break this loop
- if index == 0 {
- break
- }
-
- // get owner and project with index
- owner, project, _ := splitURL(validRemotes[index-1])
- return owner, project, nil
- }
- }
-
- // manually enter github url
- for {
- fmt.Print("Github project URL: ")
-
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
+ validator := func(name, value string) (string, error) {
+ _, _, err := splitURL(value)
if err != nil {
- return "", "", err
- }
-
- line = strings.TrimSpace(line)
- if line == "" {
- fmt.Println("URL is empty")
- continue
- }
-
- // get owner and project from url
- owner, project, err := splitURL(line)
- if err != nil {
- fmt.Println(err)
- continue
+ return err.Error(), nil
}
+ return "", nil
+ }
- return owner, project, nil
+ url, err := input.PromptURLWithRemote("Github project URL", "URL", validRemotes, input.Required, validator)
+ if err != nil {
+ return "", "", err
}
+
+ return splitURL(url)
}
// splitURL extract the owner and project from a github repository URL. It will remove the
@@ -488,10 +396,7 @@ func promptURL(repo repository.RepoCommon) (string, string, error) {
func splitURL(url string) (owner string, project string, err error) {
cleanURL := strings.TrimSuffix(url, ".git")
- re, err := regexp.Compile(`github\.com[/:]([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_.]+)`)
- if err != nil {
- panic("regexp compile:" + err.Error())
- }
+ re := regexp.MustCompile(`github\.com[/:]([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_.]+)`)
res := re.FindStringSubmatch(cleanURL)
if res == nil {
@@ -503,7 +408,12 @@ func splitURL(url string) (owner string, project string, err error) {
return
}
-func getValidGithubRemoteURLs(remotes map[string]string) []string {
+func getValidGithubRemoteURLs(repo repository.RepoCommon) ([]string, error) {
+ remotes, err := repo.GetRemotes()
+ if err != nil {
+ return nil, err
+ }
+
urls := make([]string, 0, len(remotes))
for _, url := range remotes {
// split url can work again with shortURL
@@ -516,7 +426,7 @@ func getValidGithubRemoteURLs(remotes map[string]string) []string {
sort.Strings(urls)
- return urls
+ return urls, nil
}
func validateUsername(username string) (bool, error) {
diff --git a/bridge/github/config_test.go b/bridge/github/config_test.go
index d7b1b38d..fe54c209 100644
--- a/bridge/github/config_test.go
+++ b/bridge/github/config_test.go
@@ -154,8 +154,8 @@ func TestValidateProject(t *testing.T) {
t.Skip("Env var GITHUB_TOKEN_PUBLIC missing")
}
- tokenPrivate := auth.NewToken(envPrivate, target)
- tokenPublic := auth.NewToken(envPublic, target)
+ tokenPrivate := auth.NewToken(target, envPrivate)
+ tokenPublic := auth.NewToken(target, envPublic)
type args struct {
owner string
diff --git a/bridge/github/export.go b/bridge/github/export.go
index c363e188..12b62fa6 100644
--- a/bridge/github/export.go
+++ b/bridge/github/export.go
@@ -53,7 +53,7 @@ type githubExporter struct {
}
// Init .
-func (ge *githubExporter) Init(repo *cache.RepoCache, conf core.Configuration) error {
+func (ge *githubExporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
ge.conf = conf
ge.identityClient = make(map[entity.Id]*githubv4.Client)
ge.cachedOperationIDs = make(map[entity.Id]string)
@@ -139,8 +139,8 @@ func (ge *githubExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
ge.repositoryID, err = getRepositoryNodeID(
ctx,
ge.defaultToken,
- ge.conf[keyOwner],
- ge.conf[keyProject],
+ ge.conf[confKeyOwner],
+ ge.conf[confKeyProject],
)
if err != nil {
return nil, err
@@ -187,7 +187,7 @@ func (ge *githubExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
if snapshot.HasAnyActor(allIdentitiesIds...) {
// try to export the bug and it associated events
- ge.exportBug(ctx, b, since, out)
+ ge.exportBug(ctx, b, out)
}
}
}
@@ -197,7 +197,7 @@ func (ge *githubExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
}
// exportBug publish bugs and related events
-func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, since time.Time, out chan<- core.ExportResult) {
+func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, out chan<- core.ExportResult) {
snapshot := b.Snapshot()
var bugUpdated bool
@@ -238,7 +238,7 @@ func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
}
// ignore issue coming from other repositories
- if owner != ge.conf[keyOwner] && project != ge.conf[keyProject] {
+ if owner != ge.conf[confKeyOwner] && project != ge.conf[confKeyProject] {
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("skipping issue from url:%s", githubURL))
return
}
@@ -481,8 +481,8 @@ func markOperationAsExported(b *cache.BugCache, target entity.Id, githubID, gith
func (ge *githubExporter) cacheGithubLabels(ctx context.Context, gc *githubv4.Client) error {
variables := map[string]interface{}{
- "owner": githubv4.String(ge.conf[keyOwner]),
- "name": githubv4.String(ge.conf[keyProject]),
+ "owner": githubv4.String(ge.conf[confKeyOwner]),
+ "name": githubv4.String(ge.conf[confKeyProject]),
"first": githubv4.Int(10),
"after": (*githubv4.String)(nil),
}
@@ -526,7 +526,7 @@ func (ge *githubExporter) getLabelID(gc *githubv4.Client, label string) (string,
// NOTE: since createLabel mutation is still in preview mode we use github api v3 to create labels
// see https://developer.github.com/v4/mutation/createlabel/ and https://developer.github.com/v4/previews/#labels-preview
func (ge *githubExporter) createGithubLabel(ctx context.Context, label, color string) (string, error) {
- url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject])
+ url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[confKeyOwner], ge.conf[confKeyProject])
client := &http.Client{}
params := struct {
diff --git a/bridge/github/export_test.go b/bridge/github/export_test.go
index 7d6e6fb1..acbd657a 100644
--- a/bridge/github/export_test.go
+++ b/bridge/github/export_test.go
@@ -157,7 +157,7 @@ func TestPushPull(t *testing.T) {
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
- token := auth.NewToken(envToken, target)
+ token := auth.NewToken(target, envToken)
token.SetMetadata(auth.MetaKeyLogin, login)
err = auth.Store(repo, token)
require.NoError(t, err)
@@ -185,15 +185,16 @@ func TestPushPull(t *testing.T) {
return deleteRepository(projectName, envUser, envToken)
})
+ ctx := context.Background()
+
// initialize exporter
exporter := &githubExporter{}
- err = exporter.Init(backend, core.Configuration{
- keyOwner: envUser,
- keyProject: projectName,
+ err = exporter.Init(ctx, backend, core.Configuration{
+ confKeyOwner: envUser,
+ confKeyProject: projectName,
})
require.NoError(t, err)
- ctx := context.Background()
start := time.Now()
// export all bugs
@@ -215,9 +216,9 @@ func TestPushPull(t *testing.T) {
require.NoError(t, err)
importer := &githubImporter{}
- err = importer.Init(backend, core.Configuration{
- keyOwner: envUser,
- keyProject: projectName,
+ err = importer.Init(ctx, backend, core.Configuration{
+ confKeyOwner: envUser,
+ confKeyProject: projectName,
})
require.NoError(t, err)
diff --git a/bridge/github/github.go b/bridge/github/github.go
index 19dc8a08..3a99cec7 100644
--- a/bridge/github/github.go
+++ b/bridge/github/github.go
@@ -19,8 +19,8 @@ const (
metaKeyGithubUrl = "github-url"
metaKeyGithubLogin = "github-login"
- keyOwner = "owner"
- keyProject = "project"
+ confKeyOwner = "owner"
+ confKeyProject = "project"
githubV3Url = "https://api.github.com"
defaultTimeout = 60 * time.Second
diff --git a/bridge/github/import.go b/bridge/github/import.go
index f7840217..e80b9cfd 100644
--- a/bridge/github/import.go
+++ b/bridge/github/import.go
@@ -29,7 +29,7 @@ type githubImporter struct {
out chan<- core.ImportResult
}
-func (gi *githubImporter) Init(repo *cache.RepoCache, conf core.Configuration) error {
+func (gi *githubImporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
gi.conf = conf
creds, err := auth.List(repo, auth.WithTarget(target), auth.WithKind(auth.KindToken))
@@ -49,7 +49,7 @@ func (gi *githubImporter) Init(repo *cache.RepoCache, conf core.Configuration) e
// ImportAll iterate over all the configured repository issues and ensure the creation of the
// missing issues / timeline items / edits / label events ...
func (gi *githubImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
- gi.iterator = NewIterator(ctx, gi.client, 10, gi.conf[keyOwner], gi.conf[keyProject], since)
+ gi.iterator = NewIterator(ctx, gi.client, 10, gi.conf[confKeyOwner], gi.conf[confKeyProject], since)
out := make(chan core.ImportResult)
gi.out = out
diff --git a/bridge/github/import_test.go b/bridge/github/import_test.go
index a8f8e346..20b1b71e 100644
--- a/bridge/github/import_test.go
+++ b/bridge/github/import_test.go
@@ -144,19 +144,20 @@ func Test_Importer(t *testing.T) {
login := "test-identity"
author.SetMetadata(metaKeyGithubLogin, login)
- token := auth.NewToken(envToken, target)
+ token := auth.NewToken(target, envToken)
token.SetMetadata(auth.MetaKeyLogin, login)
err = auth.Store(repo, token)
require.NoError(t, err)
+ ctx := context.Background()
+
importer := &githubImporter{}
- err = importer.Init(backend, core.Configuration{
- keyOwner: "MichaelMure",
- keyProject: "git-bug-test-github-bridge",
+ err = importer.Init(ctx, backend, core.Configuration{
+ confKeyOwner: "MichaelMure",
+ confKeyProject: "git-bug-test-github-bridge",
})
require.NoError(t, err)
- ctx := context.Background()
start := time.Now()
events, err := importer.ImportAll(ctx, backend, time.Time{})
diff --git a/bridge/gitlab/config.go b/bridge/gitlab/config.go
index 62d385dc..d5dc418c 100644
--- a/bridge/gitlab/config.go
+++ b/bridge/gitlab/config.go
@@ -1,18 +1,13 @@
package gitlab
import (
- "bufio"
"fmt"
"net/url"
- "os"
"path"
"regexp"
- "sort"
"strconv"
"strings"
- "time"
- text "github.com/MichaelMure/go-term-text"
"github.com/pkg/errors"
"github.com/xanzy/go-gitlab"
@@ -21,22 +16,23 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/input"
"github.com/MichaelMure/git-bug/repository"
- "github.com/MichaelMure/git-bug/util/colors"
)
var (
ErrBadProjectURL = errors.New("bad project url")
)
-func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
- if params.Project != "" {
- fmt.Println("warning: --project is ineffective for a gitlab bridge")
- }
- if params.Owner != "" {
- fmt.Println("warning: --owner is ineffective for a gitlab bridge")
+func (g *Gitlab) ValidParams() map[string]interface{} {
+ return map[string]interface{}{
+ "URL": nil,
+ "BaseURL": nil,
+ "Login": nil,
+ "CredPrefix": nil,
+ "TokenRaw": nil,
}
+}
- conf := make(core.Configuration)
+func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
var err error
var baseUrl string
@@ -44,7 +40,7 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
case params.BaseURL != "":
baseUrl = params.BaseURL
default:
- baseUrl, err = promptBaseUrlOptions()
+ baseUrl, err = input.PromptDefault("Gitlab server URL", "URL", defaultBaseURL, input.Required, input.IsURL)
if err != nil {
return nil, errors.Wrap(err, "base url prompt")
}
@@ -83,7 +79,7 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
}
login = l
case params.TokenRaw != "":
- token := auth.NewToken(params.TokenRaw, target)
+ token := auth.NewToken(target, params.TokenRaw)
login, err = getLoginFromToken(baseUrl, token)
if err != nil {
return nil, err
@@ -117,9 +113,10 @@ func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
return nil, errors.Wrap(err, "project validation")
}
+ conf := make(core.Configuration)
conf[core.ConfigKeyTarget] = target
- conf[keyProjectID] = strconv.Itoa(id)
- conf[keyGitlabBaseUrl] = baseUrl
+ conf[confKeyProjectID] = strconv.Itoa(id)
+ conf[confKeyGitlabBaseUrl] = baseUrl
err = g.ValidateConfig(conf)
if err != nil {
@@ -143,107 +140,39 @@ func (g *Gitlab) ValidateConfig(conf core.Configuration) error {
} else if v != target {
return fmt.Errorf("unexpected target name: %v", v)
}
- if _, ok := conf[keyGitlabBaseUrl]; !ok {
- return fmt.Errorf("missing %s key", keyGitlabBaseUrl)
+ if _, ok := conf[confKeyGitlabBaseUrl]; !ok {
+ return fmt.Errorf("missing %s key", confKeyGitlabBaseUrl)
}
- if _, ok := conf[keyProjectID]; !ok {
- return fmt.Errorf("missing %s key", keyProjectID)
+ if _, ok := conf[confKeyProjectID]; !ok {
+ return fmt.Errorf("missing %s key", confKeyProjectID)
}
return nil
}
-func promptBaseUrlOptions() (string, error) {
- index, err := input.PromptChoice("Gitlab base url", []string{
- "https://gitlab.com",
- "enter your own base url",
- })
-
+func promptTokenOptions(repo repository.RepoConfig, login, baseUrl string) (auth.Credential, error) {
+ creds, err := auth.List(repo,
+ auth.WithTarget(target),
+ auth.WithKind(auth.KindToken),
+ auth.WithMeta(auth.MetaKeyLogin, login),
+ auth.WithMeta(auth.MetaKeyBaseURL, baseUrl),
+ )
if err != nil {
- return "", err
- }
-
- if index == 0 {
- return defaultBaseURL, nil
- } else {
- return promptBaseUrl()
- }
-}
-
-func promptBaseUrl() (string, error) {
- validator := func(name string, value string) (string, error) {
- u, err := url.Parse(value)
- if err != nil {
- return err.Error(), nil
- }
- if u.Scheme == "" {
- return "missing scheme", nil
- }
- if u.Host == "" {
- return "missing host", nil
- }
- return "", nil
+ return nil, err
}
- return input.Prompt("Base url", "url", input.Required, validator)
-}
-
-func promptTokenOptions(repo repository.RepoConfig, login, baseUrl string) (auth.Credential, error) {
- for {
- creds, err := auth.List(repo,
- auth.WithTarget(target),
- auth.WithKind(auth.KindToken),
- auth.WithMeta(auth.MetaKeyLogin, login),
- auth.WithMeta(auth.MetaKeyBaseURL, baseUrl),
- )
- if err != nil {
- return nil, err
- }
-
- // if we don't have existing token, fast-track to the token prompt
- if len(creds) == 0 {
- return promptToken(baseUrl)
- }
-
- fmt.Println()
- fmt.Println("[1]: enter my token")
-
- fmt.Println()
- fmt.Println("Existing tokens for Gitlab:")
-
- sort.Sort(auth.ById(creds))
- for i, cred := range creds {
- token := cred.(*auth.Token)
- fmt.Printf("[%d]: %s => %s (%s)\n",
- i+2,
- colors.Cyan(token.ID().Human()),
- colors.Red(text.TruncateMax(token.Value, 10)),
- token.CreateTime().Format(time.RFC822),
- )
- }
-
- fmt.Println()
- fmt.Print("Select option: ")
-
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- fmt.Println()
- if err != nil {
- return nil, err
- }
-
- line = strings.TrimSpace(line)
- index, err := strconv.Atoi(line)
- if err != nil || index < 1 || index > len(creds)+1 {
- fmt.Println("invalid input")
- continue
- }
-
- switch index {
- case 1:
- return promptToken(baseUrl)
- default:
- return creds[index-2], nil
- }
+ cred, index, err := input.PromptCredential(target, "token", creds, []string{
+ "enter my token",
+ })
+ switch {
+ case err != nil:
+ return nil, err
+ case cred != nil:
+ return cred, nil
+ case index == 0:
+ return promptToken(baseUrl)
+ default:
+ panic("missed case")
}
}
@@ -254,10 +183,7 @@ func promptToken(baseUrl string) (*auth.Token, error) {
fmt.Println("'api' access scope: to be able to make api calls")
fmt.Println()
- re, err := regexp.Compile(`^[a-zA-Z0-9\-\_]{20}$`)
- if err != nil {
- panic("regexp compile:" + err.Error())
- }
+ re := regexp.MustCompile(`^[a-zA-Z0-9\-\_]{20}$`)
var login string
@@ -265,7 +191,7 @@ func promptToken(baseUrl string) (*auth.Token, error) {
if !re.MatchString(value) {
return "token has incorrect format", nil
}
- login, err = getLoginFromToken(baseUrl, auth.NewToken(value, target))
+ login, err = getLoginFromToken(baseUrl, auth.NewToken(target, value))
if err != nil {
return fmt.Sprintf("token is invalid: %v", err), nil
}
@@ -277,7 +203,7 @@ func promptToken(baseUrl string) (*auth.Token, error) {
return nil, err
}
- token := auth.NewToken(rawToken, target)
+ token := auth.NewToken(target, rawToken)
token.SetMetadata(auth.MetaKeyLogin, login)
token.SetMetadata(auth.MetaKeyBaseURL, baseUrl)
@@ -285,64 +211,12 @@ func promptToken(baseUrl string) (*auth.Token, error) {
}
func promptProjectURL(repo repository.RepoCommon, baseUrl string) (string, error) {
- // remote suggestions
- remotes, err := repo.GetRemotes()
+ validRemotes, err := getValidGitlabRemoteURLs(repo, baseUrl)
if err != nil {
- return "", errors.Wrap(err, "getting remotes")
- }
-
- validRemotes := getValidGitlabRemoteURLs(baseUrl, remotes)
- if len(validRemotes) > 0 {
- for {
- fmt.Println("\nDetected projects:")
-
- // print valid remote gitlab urls
- for i, remote := range validRemotes {
- fmt.Printf("[%d]: %v\n", i+1, remote)
- }
-
- fmt.Printf("\n[0]: Another project\n\n")
- fmt.Printf("Select option: ")
-
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- if err != nil {
- return "", err
- }
-
- line = strings.TrimSpace(line)
-
- index, err := strconv.Atoi(line)
- if err != nil || index < 0 || index > len(validRemotes) {
- fmt.Println("invalid input")
- continue
- }
-
- // if user want to enter another project url break this loop
- if index == 0 {
- break
- }
-
- return validRemotes[index-1], nil
- }
+ return "", err
}
- // manually enter gitlab url
- for {
- fmt.Print("Gitlab project URL: ")
-
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- if err != nil {
- return "", err
- }
-
- projectURL := strings.TrimSpace(line)
- if projectURL == "" {
- fmt.Println("URL is empty")
- continue
- }
-
- return projectURL, nil
- }
+ return input.PromptURLWithRemote("Gitlab project URL", "URL", validRemotes, input.Required)
}
func getProjectPath(baseUrl, projectUrl string) (string, error) {
@@ -364,7 +238,12 @@ func getProjectPath(baseUrl, projectUrl string) (string, error) {
return objectUrl.Path[1:], nil
}
-func getValidGitlabRemoteURLs(baseUrl string, remotes map[string]string) []string {
+func getValidGitlabRemoteURLs(repo repository.RepoCommon, baseUrl string) ([]string, error) {
+ remotes, err := repo.GetRemotes()
+ if err != nil {
+ return nil, err
+ }
+
urls := make([]string, 0, len(remotes))
for _, u := range remotes {
path, err := getProjectPath(baseUrl, u)
@@ -375,7 +254,7 @@ func getValidGitlabRemoteURLs(baseUrl string, remotes map[string]string) []strin
urls = append(urls, fmt.Sprintf("%s/%s", baseUrl, path))
}
- return urls
+ return urls, nil
}
func validateProjectURL(baseUrl, url string, token *auth.Token) (int, error) {
diff --git a/bridge/gitlab/export.go b/bridge/gitlab/export.go
index d747c6ac..918e6b5e 100644
--- a/bridge/gitlab/export.go
+++ b/bridge/gitlab/export.go
@@ -38,16 +38,16 @@ type gitlabExporter struct {
}
// Init .
-func (ge *gitlabExporter) Init(repo *cache.RepoCache, conf core.Configuration) error {
+func (ge *gitlabExporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
ge.conf = conf
ge.identityClient = make(map[entity.Id]*gitlab.Client)
ge.cachedOperationIDs = make(map[string]string)
// get repository node id
- ge.repositoryID = ge.conf[keyProjectID]
+ ge.repositoryID = ge.conf[confKeyProjectID]
// preload all clients
- err := ge.cacheAllClient(repo, ge.conf[keyGitlabBaseUrl])
+ err := ge.cacheAllClient(repo, ge.conf[confKeyGitlabBaseUrl])
if err != nil {
return err
}
@@ -81,7 +81,7 @@ func (ge *gitlabExporter) cacheAllClient(repo *cache.RepoCache, baseURL string)
}
if _, ok := ge.identityClient[user.Id()]; !ok {
- client, err := buildClient(ge.conf[keyGitlabBaseUrl], creds[0].(*auth.Token))
+ client, err := buildClient(ge.conf[confKeyGitlabBaseUrl], creds[0].(*auth.Token))
if err != nil {
return err
}
@@ -138,7 +138,7 @@ func (ge *gitlabExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
if snapshot.HasAnyActor(allIdentitiesIds...) {
// try to export the bug and it associated events
- ge.exportBug(ctx, b, since, out)
+ ge.exportBug(ctx, b, out)
}
}
}
@@ -148,7 +148,7 @@ func (ge *gitlabExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
}
// exportBug publish bugs and related events
-func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, since time.Time, out chan<- core.ExportResult) {
+func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, out chan<- core.ExportResult) {
snapshot := b.Snapshot()
var bugUpdated bool
@@ -177,7 +177,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
gitlabID, ok := snapshot.GetCreateMetadata(metaKeyGitlabId)
if ok {
gitlabBaseUrl, ok := snapshot.GetCreateMetadata(metaKeyGitlabBaseUrl)
- if ok && gitlabBaseUrl != ge.conf[keyGitlabBaseUrl] {
+ if ok && gitlabBaseUrl != ge.conf[confKeyGitlabBaseUrl] {
out <- core.NewExportNothing(b.Id(), "skipping issue imported from another Gitlab instance")
return
}
@@ -189,7 +189,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
return
}
- if projectID != ge.conf[keyProjectID] {
+ if projectID != ge.conf[confKeyProjectID] {
out <- core.NewExportNothing(b.Id(), "skipping issue imported from another repository")
return
}
diff --git a/bridge/gitlab/export_test.go b/bridge/gitlab/export_test.go
index c97416d8..d704ac3b 100644
--- a/bridge/gitlab/export_test.go
+++ b/bridge/gitlab/export_test.go
@@ -162,7 +162,7 @@ func TestPushPull(t *testing.T) {
defer backend.Close()
interrupt.RegisterCleaner(backend.Close)
- token := auth.NewToken(envToken, target)
+ token := auth.NewToken(target, envToken)
token.SetMetadata(auth.MetaKeyLogin, login)
token.SetMetadata(auth.MetaKeyBaseURL, defaultBaseURL)
err = auth.Store(repo, token)
@@ -191,15 +191,16 @@ func TestPushPull(t *testing.T) {
return deleteRepository(context.TODO(), projectID, token)
})
+ ctx := context.Background()
+
// initialize exporter
exporter := &gitlabExporter{}
- err = exporter.Init(backend, core.Configuration{
- keyProjectID: strconv.Itoa(projectID),
- keyGitlabBaseUrl: defaultBaseURL,
+ err = exporter.Init(ctx, backend, core.Configuration{
+ confKeyProjectID: strconv.Itoa(projectID),
+ confKeyGitlabBaseUrl: defaultBaseURL,
})
require.NoError(t, err)
- ctx := context.Background()
start := time.Now()
// export all bugs
@@ -221,9 +222,9 @@ func TestPushPull(t *testing.T) {
require.NoError(t, err)
importer := &gitlabImporter{}
- err = importer.Init(backend, core.Configuration{
- keyProjectID: strconv.Itoa(projectID),
- keyGitlabBaseUrl: defaultBaseURL,
+ err = importer.Init(ctx, backend, core.Configuration{
+ confKeyProjectID: strconv.Itoa(projectID),
+ confKeyGitlabBaseUrl: defaultBaseURL,
})
require.NoError(t, err)
diff --git a/bridge/gitlab/gitlab.go b/bridge/gitlab/gitlab.go
index 8512379c..ec7b7e57 100644
--- a/bridge/gitlab/gitlab.go
+++ b/bridge/gitlab/gitlab.go
@@ -19,8 +19,8 @@ const (
metaKeyGitlabProject = "gitlab-project-id"
metaKeyGitlabBaseUrl = "gitlab-base-url"
- keyProjectID = "project-id"
- keyGitlabBaseUrl = "base-url"
+ confKeyProjectID = "project-id"
+ confKeyGitlabBaseUrl = "base-url"
defaultBaseURL = "https://gitlab.com/"
defaultTimeout = 60 * time.Second
@@ -30,7 +30,7 @@ var _ core.BridgeImpl = &Gitlab{}
type Gitlab struct{}
-func (*Gitlab) Target() string {
+func (Gitlab) Target() string {
return target
}
@@ -38,11 +38,11 @@ func (g *Gitlab) LoginMetaKey() string {
return metaKeyGitlabLogin
}
-func (*Gitlab) NewImporter() core.Importer {
+func (Gitlab) NewImporter() core.Importer {
return &gitlabImporter{}
}
-func (*Gitlab) NewExporter() core.Exporter {
+func (Gitlab) NewExporter() core.Exporter {
return &gitlabExporter{}
}
diff --git a/bridge/gitlab/import.go b/bridge/gitlab/import.go
index 4fccb47e..5faf5c48 100644
--- a/bridge/gitlab/import.go
+++ b/bridge/gitlab/import.go
@@ -30,13 +30,13 @@ type gitlabImporter struct {
out chan<- core.ImportResult
}
-func (gi *gitlabImporter) Init(repo *cache.RepoCache, conf core.Configuration) error {
+func (gi *gitlabImporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
gi.conf = conf
creds, err := auth.List(repo,
auth.WithTarget(target),
auth.WithKind(auth.KindToken),
- auth.WithMeta(auth.MetaKeyBaseURL, conf[keyGitlabBaseUrl]),
+ auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyGitlabBaseUrl]),
)
if err != nil {
return err
@@ -46,7 +46,7 @@ func (gi *gitlabImporter) Init(repo *cache.RepoCache, conf core.Configuration) e
return ErrMissingIdentityToken
}
- gi.client, err = buildClient(conf[keyGitlabBaseUrl], creds[0].(*auth.Token))
+ gi.client, err = buildClient(conf[confKeyGitlabBaseUrl], creds[0].(*auth.Token))
if err != nil {
return err
}
@@ -57,7 +57,7 @@ func (gi *gitlabImporter) Init(repo *cache.RepoCache, conf core.Configuration) e
// ImportAll iterate over all the configured repository issues (notes) and ensure the creation
// of the missing issues / comments / label events / title changes ...
func (gi *gitlabImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
- gi.iterator = NewIterator(ctx, gi.client, 10, gi.conf[keyProjectID], since)
+ gi.iterator = NewIterator(ctx, gi.client, 10, gi.conf[confKeyProjectID], since)
out := make(chan core.ImportResult)
gi.out = out
@@ -147,8 +147,8 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
core.MetaKeyOrigin: target,
metaKeyGitlabId: parseID(issue.IID),
metaKeyGitlabUrl: issue.WebURL,
- metaKeyGitlabProject: gi.conf[keyProjectID],
- metaKeyGitlabBaseUrl: gi.conf[keyGitlabBaseUrl],
+ metaKeyGitlabProject: gi.conf[confKeyProjectID],
+ metaKeyGitlabBaseUrl: gi.conf[confKeyGitlabBaseUrl],
},
)
diff --git a/bridge/gitlab/import_test.go b/bridge/gitlab/import_test.go
index a300acf1..ea7acc18 100644
--- a/bridge/gitlab/import_test.go
+++ b/bridge/gitlab/import_test.go
@@ -98,20 +98,21 @@ func TestImport(t *testing.T) {
login := "test-identity"
author.SetMetadata(metaKeyGitlabLogin, login)
- token := auth.NewToken(envToken, target)
+ token := auth.NewToken(target, envToken)
token.SetMetadata(auth.MetaKeyLogin, login)
token.SetMetadata(auth.MetaKeyBaseURL, defaultBaseURL)
err = auth.Store(repo, token)
require.NoError(t, err)
+ ctx := context.Background()
+
importer := &gitlabImporter{}
- err = importer.Init(backend, core.Configuration{
- keyProjectID: projectID,
- keyGitlabBaseUrl: defaultBaseURL,
+ err = importer.Init(ctx, backend, core.Configuration{
+ confKeyProjectID: projectID,
+ confKeyGitlabBaseUrl: defaultBaseURL,
})
require.NoError(t, err)
- ctx := context.Background()
start := time.Now()
events, err := importer.ImportAll(ctx, backend, time.Time{})
diff --git a/bridge/jira/client.go b/bridge/jira/client.go
new file mode 100644
index 00000000..5e1db26f
--- /dev/null
+++ b/bridge/jira/client.go
@@ -0,0 +1,1461 @@
+package jira
+
+import (
+ "bytes"
+ "context"
+ "encoding/base64"
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "net/http/cookiejar"
+ "net/url"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/pkg/errors"
+
+ "github.com/MichaelMure/git-bug/bug"
+)
+
+var errDone = errors.New("Iteration Done")
+var errTransitionNotFound = errors.New("Transition not found")
+var errTransitionNotAllowed = errors.New("Transition not allowed")
+
+// =============================================================================
+// Extended JSON
+// =============================================================================
+
+const TimeFormat = "2006-01-02T15:04:05.999999999Z0700"
+
+// ParseTime parse an RFC3339 string with nanoseconds
+func ParseTime(timeStr string) (time.Time, error) {
+ out, err := time.Parse(time.RFC3339Nano, timeStr)
+ if err != nil {
+ out, err = time.Parse(TimeFormat, timeStr)
+ }
+ return out, err
+}
+
+// Time is just a time.Time with a JSON serialization
+type Time struct {
+ time.Time
+}
+
+// UnmarshalJSON parses an RFC3339 date string into a time object
+// borrowed from: https://stackoverflow.com/a/39180230/141023
+func (t *Time) UnmarshalJSON(data []byte) (err error) {
+ str := string(data)
+
+ // Get rid of the quotes "" around the value.
+ // A second option would be to include them in the date format string
+ // instead, like so below:
+ // time.Parse(`"`+time.RFC3339Nano+`"`, s)
+ str = str[1 : len(str)-1]
+
+ timeObj, err := ParseTime(str)
+ t.Time = timeObj
+ return
+}
+
+// =============================================================================
+// JSON Objects
+// =============================================================================
+
+// Session credential cookie name/value pair received after logging in and
+// required to be sent on all subsequent requests
+type Session struct {
+ Name string `json:"name"`
+ Value string `json:"value"`
+}
+
+// SessionResponse the JSON object returned from a /session query (login)
+type SessionResponse struct {
+ Session Session `json:"session"`
+}
+
+// SessionQuery the JSON object that is POSTed to the /session endpoint
+// in order to login and get a session cookie
+type SessionQuery struct {
+ Username string `json:"username"`
+ Password string `json:"password"`
+}
+
+// User the JSON object representing a JIRA user
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/user
+type User struct {
+ DisplayName string `json:"displayName"`
+ EmailAddress string `json:"emailAddress"`
+ Key string `json:"key"`
+ Name string `json:"name"`
+}
+
+// Comment the JSON object for a single comment item returned in a list of
+// comments
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getComments
+type Comment struct {
+ ID string `json:"id"`
+ Body string `json:"body"`
+ Author User `json:"author"`
+ UpdateAuthor User `json:"updateAuthor"`
+ Created Time `json:"created"`
+ Updated Time `json:"updated"`
+}
+
+// CommentPage the JSON object holding a single page of comments returned
+// either by direct query or within an issue query
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getComments
+type CommentPage struct {
+ StartAt int `json:"startAt"`
+ MaxResults int `json:"maxResults"`
+ Total int `json:"total"`
+ Comments []Comment `json:"comments"`
+}
+
+// NextStartAt return the index of the first item on the next page
+func (cp *CommentPage) NextStartAt() int {
+ return cp.StartAt + len(cp.Comments)
+}
+
+// IsLastPage return true if there are no more items beyond this page
+func (cp *CommentPage) IsLastPage() bool {
+ return cp.NextStartAt() >= cp.Total
+}
+
+// IssueFields the JSON object returned as the "fields" member of an issue.
+// There are a very large number of fields and many of them are custom. We
+// only grab a few that we need.
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getIssue
+type IssueFields struct {
+ Creator User `json:"creator"`
+ Created Time `json:"created"`
+ Description string `json:"description"`
+ Summary string `json:"summary"`
+ Comments CommentPage `json:"comment"`
+ Labels []string `json:"labels"`
+}
+
+// ChangeLogItem "field-change" data within a changelog entry. A single
+// changelog entry might effect multiple fields. For example, closing an issue
+// generally requires a change in "status" and "resolution"
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getIssue
+type ChangeLogItem struct {
+ Field string `json:"field"`
+ FieldType string `json:"fieldtype"`
+ From string `json:"from"`
+ FromString string `json:"fromString"`
+ To string `json:"to"`
+ ToString string `json:"toString"`
+}
+
+// ChangeLogEntry One entry in a changelog
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getIssue
+type ChangeLogEntry struct {
+ ID string `json:"id"`
+ Author User `json:"author"`
+ Created Time `json:"created"`
+ Items []ChangeLogItem `json:"items"`
+}
+
+// ChangeLogPage A collection of changes to issue metadata
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getIssue
+type ChangeLogPage struct {
+ StartAt int `json:"startAt"`
+ MaxResults int `json:"maxResults"`
+ Total int `json:"total"`
+ IsLast bool `json:"isLast"` // Cloud-only
+ Entries []ChangeLogEntry `json:"histories"`
+ Values []ChangeLogEntry `json:"values"`
+}
+
+// NextStartAt return the index of the first item on the next page
+func (clp *ChangeLogPage) NextStartAt() int {
+ return clp.StartAt + len(clp.Entries)
+}
+
+// IsLastPage return true if there are no more items beyond this page
+func (clp *ChangeLogPage) IsLastPage() bool {
+ // NOTE(josh): The "isLast" field is returned on JIRA cloud, but not on
+ // JIRA server. If we can distinguish which one we are working with, we can
+ // possibly rely on that instead.
+ return clp.NextStartAt() >= clp.Total
+}
+
+// Issue Top-level object for an issue
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getIssue
+type Issue struct {
+ ID string `json:"id"`
+ Key string `json:"key"`
+ Fields IssueFields `json:"fields"`
+ ChangeLog ChangeLogPage `json:"changelog"`
+}
+
+// SearchResult The result type from querying the search endpoint
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/search
+type SearchResult struct {
+ StartAt int `json:"startAt"`
+ MaxResults int `json:"maxResults"`
+ Total int `json:"total"`
+ Issues []Issue `json:"issues"`
+}
+
+// NextStartAt return the index of the first item on the next page
+func (sr *SearchResult) NextStartAt() int {
+ return sr.StartAt + len(sr.Issues)
+}
+
+// IsLastPage return true if there are no more items beyond this page
+func (sr *SearchResult) IsLastPage() bool {
+ return sr.NextStartAt() >= sr.Total
+}
+
+// SearchRequest the JSON object POSTed to the /search endpoint
+type SearchRequest struct {
+ JQL string `json:"jql"`
+ StartAt int `json:"startAt"`
+ MaxResults int `json:"maxResults"`
+ Fields []string `json:"fields"`
+}
+
+// Project the JSON object representing a project. Note that we don't use all
+// the fields so we have only implemented a couple.
+type Project struct {
+ ID string `json:"id,omitempty"`
+ Key string `json:"key,omitempty"`
+}
+
+// IssueType the JSON object representing an issue type (i.e. "bug", "task")
+// Note that we don't use all the fields so we have only implemented a couple.
+type IssueType struct {
+ ID string `json:"id"`
+}
+
+// IssueCreateFields fields that are included in an IssueCreate request
+type IssueCreateFields struct {
+ Project Project `json:"project"`
+ Summary string `json:"summary"`
+ Description string `json:"description"`
+ IssueType IssueType `json:"issuetype"`
+}
+
+// IssueCreate the JSON object that is POSTed to the /issue endpoint to create
+// a new issue
+type IssueCreate struct {
+ Fields IssueCreateFields `json:"fields"`
+}
+
+// IssueCreateResult the JSON object returned after issue creation.
+type IssueCreateResult struct {
+ ID string `json:"id"`
+ Key string `json:"key"`
+}
+
+// CommentCreate the JSOn object that is POSTed to the /comment endpoint to
+// create a new comment
+type CommentCreate struct {
+ Body string `json:"body"`
+}
+
+// StatusCategory the JSON object representing a status category
+type StatusCategory struct {
+ ID int `json:"id"`
+ Key string `json:"key"`
+ Self string `json:"self"`
+ ColorName string `json:"colorName"`
+ Name string `json:"name"`
+}
+
+// Status the JSON object representing a status (i.e. "Open", "Closed")
+type Status struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ Self string `json:"self"`
+ Description string `json:"description"`
+ StatusCategory StatusCategory `json:"statusCategory"`
+}
+
+// Transition the JSON object represenging a transition from one Status to
+// another Status in a JIRA workflow
+type Transition struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ To Status `json:"to"`
+}
+
+// TransitionList the JSON object returned from the /transitions endpoint
+type TransitionList struct {
+ Transitions []Transition `json:"transitions"`
+}
+
+// ServerInfo general server information returned by the /serverInfo endpoint.
+// Notably `ServerTime` will tell you the time on the server.
+type ServerInfo struct {
+ BaseURL string `json:"baseUrl"`
+ Version string `json:"version"`
+ VersionNumbers []int `json:"versionNumbers"`
+ BuildNumber int `json:"buildNumber"`
+ BuildDate Time `json:"buildDate"`
+ ServerTime Time `json:"serverTime"`
+ ScmInfo string `json:"scmInfo"`
+ BuildPartnerName string `json:"buildPartnerName"`
+ ServerTitle string `json:"serverTitle"`
+}
+
+// =============================================================================
+// REST Client
+// =============================================================================
+
+// ClientTransport wraps http.RoundTripper by adding a
+// "Content-Type=application/json" header
+type ClientTransport struct {
+ underlyingTransport http.RoundTripper
+ basicAuthString string
+}
+
+// RoundTrip overrides the default by adding the content-type header
+func (ct *ClientTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ req.Header.Add("Content-Type", "application/json")
+ if ct.basicAuthString != "" {
+ req.Header.Add("Authorization",
+ fmt.Sprintf("Basic %s", ct.basicAuthString))
+ }
+
+ return ct.underlyingTransport.RoundTrip(req)
+}
+
+func (ct *ClientTransport) SetCredentials(username string, token string) {
+ credString := fmt.Sprintf("%s:%s", username, token)
+ ct.basicAuthString = base64.StdEncoding.EncodeToString([]byte(credString))
+}
+
+// Client Thin wrapper around the http.Client providing jira-specific methods
+// for API endpoints
+type Client struct {
+ *http.Client
+ serverURL string
+ ctx context.Context
+}
+
+// NewClient Construct a new client connected to the provided server and
+// utilizing the given context for asynchronous events
+func NewClient(ctx context.Context, serverURL string) *Client {
+ cookiJar, _ := cookiejar.New(nil)
+ client := &http.Client{
+ Transport: &ClientTransport{underlyingTransport: http.DefaultTransport},
+ Jar: cookiJar,
+ }
+
+ return &Client{client, serverURL, ctx}
+}
+
+// Login POST credentials to the /session endpoint and get a session cookie
+func (client *Client) Login(credType, login, password string) error {
+ switch credType {
+ case "SESSION":
+ return client.RefreshSessionToken(login, password)
+ case "TOKEN":
+ return client.SetTokenCredentials(login, password)
+ default:
+ panic("unknown Jira cred type")
+ }
+}
+
+// RefreshSessionToken formulate the JSON request object from the user
+// credentials and POST it to the /session endpoint and get a session cookie
+func (client *Client) RefreshSessionToken(username, password string) error {
+ params := SessionQuery{
+ Username: username,
+ Password: password,
+ }
+
+ data, err := json.Marshal(params)
+ if err != nil {
+ return err
+ }
+
+ return client.RefreshSessionTokenRaw(data)
+}
+
+// SetTokenCredentials POST credentials to the /session endpoint and get a
+// session cookie
+func (client *Client) SetTokenCredentials(username, password string) error {
+ switch transport := client.Transport.(type) {
+ case *ClientTransport:
+ transport.SetCredentials(username, password)
+ default:
+ return fmt.Errorf("Invalid transport type")
+ }
+ return nil
+}
+
+// RefreshSessionTokenRaw POST credentials to the /session endpoint and get a
+// session cookie
+func (client *Client) RefreshSessionTokenRaw(credentialsJSON []byte) error {
+ postURL := fmt.Sprintf("%s/rest/auth/1/session", client.serverURL)
+
+ req, err := http.NewRequest("POST", postURL, bytes.NewBuffer(credentialsJSON))
+ if err != nil {
+ return err
+ }
+
+ urlobj, err := url.Parse(client.serverURL)
+ if err != nil {
+ fmt.Printf("Failed to parse %s\n", client.serverURL)
+ } else {
+ // Clear out cookies
+ client.Jar.SetCookies(urlobj, []*http.Cookie{})
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ req = req.WithContext(ctx)
+ }
+
+ response, err := client.Do(req)
+ if err != nil {
+ return err
+ }
+
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ content, _ := ioutil.ReadAll(response.Body)
+ return fmt.Errorf(
+ "error creating token %v: %s", response.StatusCode, content)
+ }
+
+ data, _ := ioutil.ReadAll(response.Body)
+ var aux SessionResponse
+ err = json.Unmarshal(data, &aux)
+ if err != nil {
+ return err
+ }
+
+ var cookies []*http.Cookie
+ cookie := &http.Cookie{
+ Name: aux.Session.Name,
+ Value: aux.Session.Value,
+ }
+ cookies = append(cookies, cookie)
+ client.Jar.SetCookies(urlobj, cookies)
+
+ return nil
+}
+
+// =============================================================================
+// Endpoint Wrappers
+// =============================================================================
+
+// Search Perform an issue a JQL search on the /search endpoint
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/search
+func (client *Client) Search(jql string, maxResults int, startAt int) (*SearchResult, error) {
+ url := fmt.Sprintf("%s/rest/api/2/search", client.serverURL)
+
+ requestBody, err := json.Marshal(SearchRequest{
+ JQL: jql,
+ StartAt: startAt,
+ MaxResults: maxResults,
+ Fields: []string{
+ "comment",
+ "created",
+ "creator",
+ "description",
+ "labels",
+ "status",
+ "summary"}})
+ if err != nil {
+ return nil, err
+ }
+
+ request, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
+ if err != nil {
+ return nil, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s, %s", response.StatusCode,
+ url, requestBody)
+ return nil, err
+ }
+
+ var message SearchResult
+
+ data, _ := ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &message)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &message, nil
+}
+
+// SearchIterator cursor within paginated results from the /search endpoint
+type SearchIterator struct {
+ client *Client
+ jql string
+ searchResult *SearchResult
+ Err error
+
+ pageSize int
+ itemIdx int
+}
+
+// HasError returns true if the iterator is holding an error
+func (si *SearchIterator) HasError() bool {
+ if si.Err == errDone {
+ return false
+ }
+ if si.Err == nil {
+ return false
+ }
+ return true
+}
+
+// HasNext returns true if there is another item available in the result set
+func (si *SearchIterator) HasNext() bool {
+ return si.Err == nil && si.itemIdx < len(si.searchResult.Issues)
+}
+
+// Next Return the next item in the result set and advance the iterator.
+// Advancing the iterator may require fetching a new page.
+func (si *SearchIterator) Next() *Issue {
+ if si.Err != nil {
+ return nil
+ }
+
+ issue := si.searchResult.Issues[si.itemIdx]
+ if si.itemIdx+1 < len(si.searchResult.Issues) {
+ // We still have an item left in the currently cached page
+ si.itemIdx++
+ } else {
+ if si.searchResult.IsLastPage() {
+ si.Err = errDone
+ } else {
+ // There are still more pages to fetch, so fetch the next page and
+ // cache it
+ si.searchResult, si.Err = si.client.Search(
+ si.jql, si.pageSize, si.searchResult.NextStartAt())
+ // NOTE(josh): we don't deal with the error now, we just cache it.
+ // HasNext() will return false and the caller can check the error
+ // afterward.
+ si.itemIdx = 0
+ }
+ }
+ return &issue
+}
+
+// IterSearch return an iterator over paginated results for a JQL search
+func (client *Client) IterSearch(jql string, pageSize int) *SearchIterator {
+ result, err := client.Search(jql, pageSize, 0)
+
+ iter := &SearchIterator{
+ client: client,
+ jql: jql,
+ searchResult: result,
+ Err: err,
+ pageSize: pageSize,
+ itemIdx: 0,
+ }
+
+ return iter
+}
+
+// GetIssue fetches an issue object via the /issue/{IssueIdOrKey} endpoint
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue
+func (client *Client) GetIssue(idOrKey string, fields []string, expand []string,
+ properties []string) (*Issue, error) {
+
+ url := fmt.Sprintf("%s/rest/api/2/issue/%s", client.serverURL, idOrKey)
+
+ request, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ err := fmt.Errorf("Creating request %v", err)
+ return nil, err
+ }
+
+ query := request.URL.Query()
+ if len(fields) > 0 {
+ query.Add("fields", strings.Join(fields, ","))
+ }
+ if len(expand) > 0 {
+ query.Add("expand", strings.Join(expand, ","))
+ }
+ if len(properties) > 0 {
+ query.Add("properties", strings.Join(properties, ","))
+ }
+ request.URL.RawQuery = query.Encode()
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s", response.StatusCode,
+ request.URL.String())
+ return nil, err
+ }
+
+ var issue Issue
+
+ data, _ := ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &issue)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &issue, nil
+}
+
+// GetComments returns a page of comments via the issue/{IssueIdOrKey}/comment
+// endpoint
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue-getComment
+func (client *Client) GetComments(idOrKey string, maxResults int, startAt int) (*CommentPage, error) {
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s/comment", client.serverURL, idOrKey)
+
+ request, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ err := fmt.Errorf("Creating request %v", err)
+ return nil, err
+ }
+
+ query := request.URL.Query()
+ if maxResults > 0 {
+ query.Add("maxResults", fmt.Sprintf("%d", maxResults))
+ }
+ if startAt > 0 {
+ query.Add("startAt", fmt.Sprintf("%d", startAt))
+ }
+ request.URL.RawQuery = query.Encode()
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s", response.StatusCode,
+ request.URL.String())
+ return nil, err
+ }
+
+ var comments CommentPage
+
+ data, _ := ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &comments)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &comments, nil
+}
+
+// CommentIterator cursor within paginated results from the /comment endpoint
+type CommentIterator struct {
+ client *Client
+ idOrKey string
+ message *CommentPage
+ Err error
+
+ pageSize int
+ itemIdx int
+}
+
+// HasError returns true if the iterator is holding an error
+func (ci *CommentIterator) HasError() bool {
+ if ci.Err == errDone {
+ return false
+ }
+ if ci.Err == nil {
+ return false
+ }
+ return true
+}
+
+// HasNext returns true if there is another item available in the result set
+func (ci *CommentIterator) HasNext() bool {
+ return ci.Err == nil && ci.itemIdx < len(ci.message.Comments)
+}
+
+// Next Return the next item in the result set and advance the iterator.
+// Advancing the iterator may require fetching a new page.
+func (ci *CommentIterator) Next() *Comment {
+ if ci.Err != nil {
+ return nil
+ }
+
+ comment := ci.message.Comments[ci.itemIdx]
+ if ci.itemIdx+1 < len(ci.message.Comments) {
+ // We still have an item left in the currently cached page
+ ci.itemIdx++
+ } else {
+ if ci.message.IsLastPage() {
+ ci.Err = errDone
+ } else {
+ // There are still more pages to fetch, so fetch the next page and
+ // cache it
+ ci.message, ci.Err = ci.client.GetComments(
+ ci.idOrKey, ci.pageSize, ci.message.NextStartAt())
+ // NOTE(josh): we don't deal with the error now, we just cache it.
+ // HasNext() will return false and the caller can check the error
+ // afterward.
+ ci.itemIdx = 0
+ }
+ }
+ return &comment
+}
+
+// IterComments returns an iterator over paginated comments within an issue
+func (client *Client) IterComments(idOrKey string, pageSize int) *CommentIterator {
+ message, err := client.GetComments(idOrKey, pageSize, 0)
+
+ iter := &CommentIterator{
+ client: client,
+ idOrKey: idOrKey,
+ message: message,
+ Err: err,
+ pageSize: pageSize,
+ itemIdx: 0,
+ }
+
+ return iter
+}
+
+// GetChangeLog fetch one page of the changelog for an issue via the
+// /issue/{IssueIdOrKey}/changelog endpoint (for JIRA cloud) or
+// /issue/{IssueIdOrKey} with (fields=*none&expand=changelog)
+// (for JIRA server)
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue
+func (client *Client) GetChangeLog(idOrKey string, maxResults int, startAt int) (*ChangeLogPage, error) {
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s/changelog", client.serverURL, idOrKey)
+
+ request, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ err := fmt.Errorf("Creating request %v", err)
+ return nil, err
+ }
+
+ query := request.URL.Query()
+ if maxResults > 0 {
+ query.Add("maxResults", fmt.Sprintf("%d", maxResults))
+ }
+ if startAt > 0 {
+ query.Add("startAt", fmt.Sprintf("%d", startAt))
+ }
+ request.URL.RawQuery = query.Encode()
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode == http.StatusNotFound {
+ // The issue/{IssueIdOrKey}/changelog endpoint is only available on JIRA cloud
+ // products, not on JIRA server. In order to get the information we have to
+ // query the issue and ask for a changelog expansion. Unfortunately this means
+ // that the changelog is not paginated and we have to fetch the entire thing
+ // at once. Hopefully things don't break for very long changelogs.
+ issue, err := client.GetIssue(
+ idOrKey, []string{"*none"}, []string{"changelog"}, []string{})
+ if err != nil {
+ return nil, err
+ }
+
+ return &issue.ChangeLog, nil
+ }
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s", response.StatusCode,
+ request.URL.String())
+ return nil, err
+ }
+
+ var changelog ChangeLogPage
+
+ data, _ := ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &changelog)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ // JIRA cloud returns changelog entries in the "values" list, whereas JIRA
+ // server returns them in the "histories" list when embedded in an issue
+ // object.
+ changelog.Entries = changelog.Values
+ changelog.Values = nil
+
+ return &changelog, nil
+}
+
+// ChangeLogIterator cursor within paginated results from the /search endpoint
+type ChangeLogIterator struct {
+ client *Client
+ idOrKey string
+ message *ChangeLogPage
+ Err error
+
+ pageSize int
+ itemIdx int
+}
+
+// HasError returns true if the iterator is holding an error
+func (cli *ChangeLogIterator) HasError() bool {
+ if cli.Err == errDone {
+ return false
+ }
+ if cli.Err == nil {
+ return false
+ }
+ return true
+}
+
+// HasNext returns true if there is another item available in the result set
+func (cli *ChangeLogIterator) HasNext() bool {
+ return cli.Err == nil && cli.itemIdx < len(cli.message.Entries)
+}
+
+// Next Return the next item in the result set and advance the iterator.
+// Advancing the iterator may require fetching a new page.
+func (cli *ChangeLogIterator) Next() *ChangeLogEntry {
+ if cli.Err != nil {
+ return nil
+ }
+
+ item := cli.message.Entries[cli.itemIdx]
+ if cli.itemIdx+1 < len(cli.message.Entries) {
+ // We still have an item left in the currently cached page
+ cli.itemIdx++
+ } else {
+ if cli.message.IsLastPage() {
+ cli.Err = errDone
+ } else {
+ // There are still more pages to fetch, so fetch the next page and
+ // cache it
+ cli.message, cli.Err = cli.client.GetChangeLog(
+ cli.idOrKey, cli.pageSize, cli.message.NextStartAt())
+ // NOTE(josh): we don't deal with the error now, we just cache it.
+ // HasNext() will return false and the caller can check the error
+ // afterward.
+ cli.itemIdx = 0
+ }
+ }
+ return &item
+}
+
+// IterChangeLog returns an iterator over entries in the changelog for an issue
+func (client *Client) IterChangeLog(idOrKey string, pageSize int) *ChangeLogIterator {
+ message, err := client.GetChangeLog(idOrKey, pageSize, 0)
+
+ iter := &ChangeLogIterator{
+ client: client,
+ idOrKey: idOrKey,
+ message: message,
+ Err: err,
+ pageSize: pageSize,
+ itemIdx: 0,
+ }
+
+ return iter
+}
+
+// GetProject returns the project JSON object given its id or key
+func (client *Client) GetProject(projectIDOrKey string) (*Project, error) {
+ url := fmt.Sprintf(
+ "%s/rest/api/2/project/%s", client.serverURL, projectIDOrKey)
+
+ request, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ return nil, err
+ }
+
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s", response.StatusCode, url)
+ return nil, err
+ }
+
+ var project Project
+
+ data, _ := ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &project)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &project, nil
+}
+
+// CreateIssue creates a new JIRA issue and returns it
+func (client *Client) CreateIssue(projectIDOrKey, title, body string,
+ extra map[string]interface{}) (*IssueCreateResult, error) {
+
+ url := fmt.Sprintf("%s/rest/api/2/issue", client.serverURL)
+
+ fields := make(map[string]interface{})
+ fields["summary"] = title
+ fields["description"] = body
+ for key, value := range extra {
+ fields[key] = value
+ }
+
+ // If the project string is an integer than assume it is an ID. Otherwise it
+ // is a key.
+ _, err := strconv.Atoi(projectIDOrKey)
+ if err == nil {
+ fields["project"] = map[string]string{"id": projectIDOrKey}
+ } else {
+ fields["project"] = map[string]string{"key": projectIDOrKey}
+ }
+
+ message := make(map[string]interface{})
+ message["fields"] = fields
+
+ data, err := json.Marshal(message)
+ if err != nil {
+ return nil, err
+ }
+
+ request, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
+ if err != nil {
+ return nil, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusCreated {
+ content, _ := ioutil.ReadAll(response.Body)
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s\n data: %s\n response: %s",
+ response.StatusCode, request.URL.String(), data, content)
+ return nil, err
+ }
+
+ var result IssueCreateResult
+
+ data, _ = ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &result)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &result, nil
+}
+
+// UpdateIssueTitle changes the "summary" field of a JIRA issue
+func (client *Client) UpdateIssueTitle(issueKeyOrID, title string) (time.Time, error) {
+
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s", client.serverURL, issueKeyOrID)
+ var responseTime time.Time
+
+ // NOTE(josh): Since updates are a list of heterogeneous objects let's just
+ // manually build the JSON text
+ data, err := json.Marshal(title)
+ if err != nil {
+ return responseTime, err
+ }
+
+ var buffer bytes.Buffer
+ _, _ = fmt.Fprintf(&buffer, `{"update":{"summary":[`)
+ _, _ = fmt.Fprintf(&buffer, `{"set":%s}`, data)
+ _, _ = fmt.Fprintf(&buffer, `]}}`)
+
+ data = buffer.Bytes()
+ request, err := http.NewRequest("PUT", url, bytes.NewBuffer(data))
+ if err != nil {
+ return responseTime, err
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return responseTime, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusNoContent {
+ content, _ := ioutil.ReadAll(response.Body)
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s\n data: %s\n response: %s",
+ response.StatusCode, request.URL.String(), data, content)
+ return responseTime, err
+ }
+
+ dateHeader, ok := response.Header["Date"]
+ if !ok || len(dateHeader) != 1 {
+ // No "Date" header, or empty, or multiple of them. Regardless, we don't
+ // have a date we can return
+ return responseTime, nil
+ }
+
+ responseTime, err = http.ParseTime(dateHeader[0])
+ if err != nil {
+ return time.Time{}, err
+ }
+
+ return responseTime, nil
+}
+
+// UpdateIssueBody changes the "description" field of a JIRA issue
+func (client *Client) UpdateIssueBody(issueKeyOrID, body string) (time.Time, error) {
+
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s", client.serverURL, issueKeyOrID)
+ var responseTime time.Time
+ // NOTE(josh): Since updates are a list of heterogeneous objects let's just
+ // manually build the JSON text
+ data, err := json.Marshal(body)
+ if err != nil {
+ return responseTime, err
+ }
+
+ var buffer bytes.Buffer
+ _, _ = fmt.Fprintf(&buffer, `{"update":{"description":[`)
+ _, _ = fmt.Fprintf(&buffer, `{"set":%s}`, data)
+ _, _ = fmt.Fprintf(&buffer, `]}}`)
+
+ data = buffer.Bytes()
+ request, err := http.NewRequest("PUT", url, bytes.NewBuffer(data))
+ if err != nil {
+ return responseTime, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return responseTime, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusNoContent {
+ content, _ := ioutil.ReadAll(response.Body)
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s\n data: %s\n response: %s",
+ response.StatusCode, request.URL.String(), data, content)
+ return responseTime, err
+ }
+
+ dateHeader, ok := response.Header["Date"]
+ if !ok || len(dateHeader) != 1 {
+ // No "Date" header, or empty, or multiple of them. Regardless, we don't
+ // have a date we can return
+ return responseTime, nil
+ }
+
+ responseTime, err = http.ParseTime(dateHeader[0])
+ if err != nil {
+ return time.Time{}, err
+ }
+
+ return responseTime, nil
+}
+
+// AddComment adds a new comment to an issue (and returns it).
+func (client *Client) AddComment(issueKeyOrID, body string) (*Comment, error) {
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s/comment", client.serverURL, issueKeyOrID)
+
+ params := CommentCreate{Body: body}
+ data, err := json.Marshal(params)
+ if err != nil {
+ return nil, err
+ }
+
+ request, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
+ if err != nil {
+ return nil, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusCreated {
+ content, _ := ioutil.ReadAll(response.Body)
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s\n data: %s\n response: %s",
+ response.StatusCode, request.URL.String(), data, content)
+ return nil, err
+ }
+
+ var result Comment
+
+ data, _ = ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &result)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &result, nil
+}
+
+// UpdateComment changes the text of a comment
+func (client *Client) UpdateComment(issueKeyOrID, commentID, body string) (
+ *Comment, error) {
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s/comment/%s", client.serverURL, issueKeyOrID,
+ commentID)
+
+ params := CommentCreate{Body: body}
+ data, err := json.Marshal(params)
+ if err != nil {
+ return nil, err
+ }
+
+ request, err := http.NewRequest("PUT", url, bytes.NewBuffer(data))
+ if err != nil {
+ return nil, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s", response.StatusCode,
+ request.URL.String())
+ return nil, err
+ }
+
+ var result Comment
+
+ data, _ = ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &result)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &result, nil
+}
+
+// UpdateLabels changes labels for an issue
+func (client *Client) UpdateLabels(issueKeyOrID string, added, removed []bug.Label) (time.Time, error) {
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s/", client.serverURL, issueKeyOrID)
+ var responseTime time.Time
+
+ // NOTE(josh): Since updates are a list of heterogeneous objects let's just
+ // manually build the JSON text
+ var buffer bytes.Buffer
+ _, _ = fmt.Fprintf(&buffer, `{"update":{"labels":[`)
+ first := true
+ for _, label := range added {
+ if !first {
+ _, _ = fmt.Fprintf(&buffer, ",")
+ }
+ _, _ = fmt.Fprintf(&buffer, `{"add":"%s"}`, label)
+ first = false
+ }
+ for _, label := range removed {
+ if !first {
+ _, _ = fmt.Fprintf(&buffer, ",")
+ }
+ _, _ = fmt.Fprintf(&buffer, `{"remove":"%s"}`, label)
+ first = false
+ }
+ _, _ = fmt.Fprintf(&buffer, "]}}")
+
+ data := buffer.Bytes()
+ request, err := http.NewRequest("PUT", url, bytes.NewBuffer(data))
+ if err != nil {
+ return responseTime, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return responseTime, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusNoContent {
+ content, _ := ioutil.ReadAll(response.Body)
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s\n data: %s\n response: %s",
+ response.StatusCode, request.URL.String(), data, content)
+ return responseTime, err
+ }
+
+ dateHeader, ok := response.Header["Date"]
+ if !ok || len(dateHeader) != 1 {
+ // No "Date" header, or empty, or multiple of them. Regardless, we don't
+ // have a date we can return
+ return responseTime, nil
+ }
+
+ responseTime, err = http.ParseTime(dateHeader[0])
+ if err != nil {
+ return time.Time{}, err
+ }
+
+ return responseTime, nil
+}
+
+// GetTransitions returns a list of available transitions for an issue
+func (client *Client) GetTransitions(issueKeyOrID string) (*TransitionList, error) {
+
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s/transitions", client.serverURL, issueKeyOrID)
+
+ request, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ err := fmt.Errorf("Creating request %v", err)
+ return nil, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s", response.StatusCode,
+ request.URL.String())
+ return nil, err
+ }
+
+ var message TransitionList
+
+ data, _ := ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &message)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &message, nil
+}
+
+func getTransitionTo(tlist *TransitionList, desiredStateNameOrID string) *Transition {
+ for _, transition := range tlist.Transitions {
+ if transition.To.ID == desiredStateNameOrID {
+ return &transition
+ } else if transition.To.Name == desiredStateNameOrID {
+ return &transition
+ }
+ }
+ return nil
+}
+
+// DoTransition changes the "status" of an issue
+func (client *Client) DoTransition(issueKeyOrID string, transitionID string) (time.Time, error) {
+ url := fmt.Sprintf(
+ "%s/rest/api/2/issue/%s/transitions", client.serverURL, issueKeyOrID)
+ var responseTime time.Time
+
+ // TODO(josh)[767ee72]: Figure out a good way to "configure" the
+ // open/close state mapping. It would be *great* if we could actually
+ // *compute* the necessary transitions and prompt for missing metatdata...
+ // but that is complex
+ var buffer bytes.Buffer
+ _, _ = fmt.Fprintf(&buffer,
+ `{"transition":{"id":"%s"}, "resolution": {"name": "Done"}}`,
+ transitionID)
+ request, err := http.NewRequest("POST", url, bytes.NewBuffer(buffer.Bytes()))
+ if err != nil {
+ return responseTime, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return responseTime, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusNoContent {
+ err := errors.Wrap(errTransitionNotAllowed, fmt.Sprintf(
+ "HTTP response %d, query was %s", response.StatusCode,
+ request.URL.String()))
+ return responseTime, err
+ }
+
+ dateHeader, ok := response.Header["Date"]
+ if !ok || len(dateHeader) != 1 {
+ // No "Date" header, or empty, or multiple of them. Regardless, we don't
+ // have a date we can return
+ return responseTime, nil
+ }
+
+ responseTime, err = http.ParseTime(dateHeader[0])
+ if err != nil {
+ return time.Time{}, err
+ }
+
+ return responseTime, nil
+}
+
+// GetServerInfo Fetch server information from the /serverinfo endpoint
+// https://docs.atlassian.com/software/jira/docs/api/REST/8.2.6/#api/2/issue
+func (client *Client) GetServerInfo() (*ServerInfo, error) {
+ url := fmt.Sprintf("%s/rest/api/2/serverinfo", client.serverURL)
+
+ request, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ err := fmt.Errorf("Creating request %v", err)
+ return nil, err
+ }
+
+ if client.ctx != nil {
+ ctx, cancel := context.WithTimeout(client.ctx, defaultTimeout)
+ defer cancel()
+ request = request.WithContext(ctx)
+ }
+
+ response, err := client.Do(request)
+ if err != nil {
+ err := fmt.Errorf("Performing request %v", err)
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ if response.StatusCode != http.StatusOK {
+ err := fmt.Errorf(
+ "HTTP response %d, query was %s", response.StatusCode,
+ request.URL.String())
+ return nil, err
+ }
+
+ var message ServerInfo
+
+ data, _ := ioutil.ReadAll(response.Body)
+ err = json.Unmarshal(data, &message)
+ if err != nil {
+ err := fmt.Errorf("Decoding response %v", err)
+ return nil, err
+ }
+
+ return &message, nil
+}
+
+// GetServerTime returns the current time on the server
+func (client *Client) GetServerTime() (Time, error) {
+ var result Time
+ info, err := client.GetServerInfo()
+ if err != nil {
+ return result, err
+ }
+ return info.ServerTime, nil
+}
diff --git a/bridge/jira/config.go b/bridge/jira/config.go
new file mode 100644
index 00000000..79fd8507
--- /dev/null
+++ b/bridge/jira/config.go
@@ -0,0 +1,192 @@
+package jira
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
+ "github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/input"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+const moreConfigText = `
+NOTE: There are a few optional configuration values that you can additionally
+set in your git configuration to influence the behavior of the bridge. Please
+see the notes at:
+https://github.com/MichaelMure/git-bug/blob/master/doc/jira_bridge.md
+`
+
+const credTypeText = `
+JIRA has recently altered it's authentication strategies. Servers deployed
+prior to October 1st 2019 must use "SESSION" authentication, whereby the REST
+client logs in with an actual username and password, is assigned a session, and
+passes the session cookie with each request. JIRA Cloud and servers deployed
+after October 1st 2019 must use "TOKEN" authentication. You must create a user
+API token and the client will provide this along with your username with each
+request.`
+
+func (*Jira) ValidParams() map[string]interface{} {
+ return map[string]interface{}{
+ "BaseURL": nil,
+ "Login": nil,
+ "CredPrefix": nil,
+ "Project": nil,
+ }
+}
+
+// Configure sets up the bridge configuration
+func (j *Jira) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
+ var err error
+
+ baseURL := params.BaseURL
+ if baseURL == "" {
+ // terminal prompt
+ baseURL, err = input.Prompt("JIRA server URL", "URL", input.Required, input.IsURL)
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ project := params.Project
+ if project == "" {
+ project, err = input.Prompt("JIRA project key", "project", input.Required)
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ fmt.Println(credTypeText)
+ credTypeInput, err := input.PromptChoice("Authentication mechanism", []string{"SESSION", "TOKEN"})
+ if err != nil {
+ return nil, err
+ }
+ credType := []string{"SESSION", "TOKEN"}[credTypeInput]
+
+ var login string
+ var cred auth.Credential
+
+ switch {
+ case params.CredPrefix != "":
+ cred, err = auth.LoadWithPrefix(repo, params.CredPrefix)
+ if err != nil {
+ return nil, err
+ }
+ l, ok := cred.GetMetadata(auth.MetaKeyLogin)
+ if !ok {
+ return nil, fmt.Errorf("credential doesn't have a login")
+ }
+ login = l
+ default:
+ login := params.Login
+ if login == "" {
+ // TODO: validate username
+ login, err = input.Prompt("JIRA login", "login", input.Required)
+ if err != nil {
+ return nil, err
+ }
+ }
+ cred, err = promptCredOptions(repo, login, baseURL)
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ conf := make(core.Configuration)
+ conf[core.ConfigKeyTarget] = target
+ conf[confKeyBaseUrl] = baseURL
+ conf[confKeyProject] = project
+ conf[confKeyCredentialType] = credType
+
+ err = j.ValidateConfig(conf)
+ if err != nil {
+ return nil, err
+ }
+
+ fmt.Printf("Attempting to login with credentials...\n")
+ client, err := buildClient(context.TODO(), baseURL, credType, cred)
+ if err != nil {
+ return nil, err
+ }
+
+ // verify access to the project with credentials
+ fmt.Printf("Checking project ...\n")
+ _, err = client.GetProject(project)
+ if err != nil {
+ return nil, fmt.Errorf(
+ "Project %s doesn't exist on %s, or authentication credentials for (%s)"+
+ " are invalid",
+ project, baseURL, login)
+ }
+
+ // don't forget to store the now known valid token
+ if !auth.IdExist(repo, cred.ID()) {
+ err = auth.Store(repo, cred)
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ err = core.FinishConfig(repo, metaKeyJiraLogin, login)
+ if err != nil {
+ return nil, err
+ }
+
+ fmt.Print(moreConfigText)
+ return conf, nil
+}
+
+// ValidateConfig returns true if all required keys are present
+func (*Jira) ValidateConfig(conf core.Configuration) error {
+ if v, ok := conf[core.ConfigKeyTarget]; !ok {
+ return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
+ } else if v != target {
+ return fmt.Errorf("unexpected target name: %v", v)
+ }
+
+ if _, ok := conf[confKeyProject]; !ok {
+ return fmt.Errorf("missing %s key", confKeyProject)
+ }
+
+ return nil
+}
+
+func promptCredOptions(repo repository.RepoConfig, login, baseUrl string) (auth.Credential, error) {
+ creds, err := auth.List(repo,
+ auth.WithTarget(target),
+ auth.WithKind(auth.KindToken),
+ auth.WithMeta(auth.MetaKeyLogin, login),
+ auth.WithMeta(auth.MetaKeyBaseURL, baseUrl),
+ )
+ if err != nil {
+ return nil, err
+ }
+
+ cred, index, err := input.PromptCredential(target, "password", creds, []string{
+ "enter my password",
+ "ask my password each time",
+ })
+ switch {
+ case err != nil:
+ return nil, err
+ case cred != nil:
+ return cred, nil
+ case index == 0:
+ password, err := input.PromptPassword("Password", "password", input.Required)
+ if err != nil {
+ return nil, err
+ }
+ lp := auth.NewLoginPassword(target, login, password)
+ lp.SetMetadata(auth.MetaKeyLogin, login)
+ lp.SetMetadata(auth.MetaKeyBaseURL, baseUrl)
+ return lp, nil
+ case index == 1:
+ l := auth.NewLogin(target, login)
+ l.SetMetadata(auth.MetaKeyLogin, login)
+ l.SetMetadata(auth.MetaKeyBaseURL, baseUrl)
+ return l, nil
+ default:
+ panic("missed case")
+ }
+}
diff --git a/bridge/jira/export.go b/bridge/jira/export.go
new file mode 100644
index 00000000..37066263
--- /dev/null
+++ b/bridge/jira/export.go
@@ -0,0 +1,475 @@
+package jira
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "os"
+ "time"
+
+ "github.com/pkg/errors"
+
+ "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
+ "github.com/MichaelMure/git-bug/bug"
+ "github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/identity"
+)
+
+var (
+ ErrMissingCredentials = errors.New("missing credentials")
+)
+
+// jiraExporter implement the Exporter interface
+type jiraExporter struct {
+ conf core.Configuration
+
+ // cache identities clients
+ identityClient map[entity.Id]*Client
+
+ // the mapping from git-bug "status" to JIRA "status" id
+ statusMap map[string]string
+
+ // cache identifiers used to speed up exporting operations
+ // cleared for each bug
+ cachedOperationIDs map[entity.Id]string
+
+ // cache labels used to speed up exporting labels events
+ cachedLabels map[string]string
+
+ // store JIRA project information
+ project *Project
+}
+
+// Init .
+func (je *jiraExporter) Init(ctx context.Context, repo *cache.RepoCache, conf core.Configuration) error {
+ je.conf = conf
+ je.identityClient = make(map[entity.Id]*Client)
+ je.cachedOperationIDs = make(map[entity.Id]string)
+ je.cachedLabels = make(map[string]string)
+
+ statusMap, err := getStatusMap(je.conf)
+ if err != nil {
+ return err
+ }
+ je.statusMap = statusMap
+
+ // preload all clients
+ err = je.cacheAllClient(ctx, repo)
+ if err != nil {
+ return err
+ }
+
+ if len(je.identityClient) == 0 {
+ return fmt.Errorf("no credentials for this bridge")
+ }
+
+ var client *Client
+ for _, c := range je.identityClient {
+ client = c
+ break
+ }
+
+ if client == nil {
+ panic("nil client")
+ }
+
+ je.project, err = client.GetProject(je.conf[confKeyProject])
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (je *jiraExporter) cacheAllClient(ctx context.Context, repo *cache.RepoCache) error {
+ creds, err := auth.List(repo,
+ auth.WithTarget(target),
+ auth.WithKind(auth.KindLoginPassword), auth.WithKind(auth.KindLogin),
+ auth.WithMeta(auth.MetaKeyBaseURL, je.conf[confKeyBaseUrl]),
+ )
+ if err != nil {
+ return err
+ }
+
+ for _, cred := range creds {
+ login, ok := cred.GetMetadata(auth.MetaKeyLogin)
+ if !ok {
+ _, _ = fmt.Fprintf(os.Stderr, "credential %s is not tagged with a Jira login\n", cred.ID().Human())
+ continue
+ }
+
+ user, err := repo.ResolveIdentityImmutableMetadata(metaKeyJiraLogin, login)
+ if err == identity.ErrIdentityNotExist {
+ continue
+ }
+ if err != nil {
+ return nil
+ }
+
+ if _, ok := je.identityClient[user.Id()]; !ok {
+ client, err := buildClient(ctx, je.conf[confKeyBaseUrl], je.conf[confKeyCredentialType], creds[0])
+ if err != nil {
+ return err
+ }
+ je.identityClient[user.Id()] = client
+ }
+ }
+
+ return nil
+}
+
+// getClientForIdentity return an API client configured with the credentials
+// of the given identity. If no client were found it will initialize it from
+// the known credentials and cache it for next use.
+func (je *jiraExporter) getClientForIdentity(userId entity.Id) (*Client, error) {
+ client, ok := je.identityClient[userId]
+ if ok {
+ return client, nil
+ }
+
+ return nil, ErrMissingCredentials
+}
+
+// ExportAll export all event made by the current user to Jira
+func (je *jiraExporter) ExportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
+ out := make(chan core.ExportResult)
+
+ go func() {
+ defer close(out)
+
+ var allIdentitiesIds []entity.Id
+ for id := range je.identityClient {
+ allIdentitiesIds = append(allIdentitiesIds, id)
+ }
+
+ allBugsIds := repo.AllBugsIds()
+
+ for _, id := range allBugsIds {
+ b, err := repo.ResolveBug(id)
+ if err != nil {
+ out <- core.NewExportError(errors.Wrap(err, "can't load bug"), id)
+ return
+ }
+
+ select {
+
+ case <-ctx.Done():
+ // stop iterating if context cancel function is called
+ return
+
+ default:
+ snapshot := b.Snapshot()
+
+ // ignore issues whose last modification date is before the query date
+ // TODO: compare the Lamport time instead of using the unix time
+ if snapshot.CreatedAt.Before(since) {
+ out <- core.NewExportNothing(b.Id(), "bug created before the since date")
+ continue
+ }
+
+ if snapshot.HasAnyActor(allIdentitiesIds...) {
+ // try to export the bug and it associated events
+ err := je.exportBug(ctx, b, out)
+ if err != nil {
+ out <- core.NewExportError(errors.Wrap(err, "can't export bug"), id)
+ return
+ }
+ } else {
+ out <- core.NewExportNothing(id, "not an actor")
+ }
+ }
+ }
+ }()
+
+ return out, nil
+}
+
+// exportBug publish bugs and related events
+func (je *jiraExporter) exportBug(ctx context.Context, b *cache.BugCache, out chan<- core.ExportResult) error {
+ snapshot := b.Snapshot()
+
+ var bugJiraID string
+
+ // Special case:
+ // if a user try to export a bug that is not already exported to jira (or
+ // imported from jira) and we do not have the token of the bug author,
+ // there is nothing we can do.
+
+ // first operation is always createOp
+ createOp := snapshot.Operations[0].(*bug.CreateOperation)
+ author := snapshot.Author
+
+ // skip bug if it was imported from some other bug system
+ origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
+ if ok && origin != target {
+ out <- core.NewExportNothing(
+ b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
+ return nil
+ }
+
+ // skip bug if it is a jira bug but is associated with another project
+ // (one bridge per JIRA project)
+ project, ok := snapshot.GetCreateMetadata(metaKeyJiraProject)
+ if ok && !stringInSlice(project, []string{je.project.ID, je.project.Key}) {
+ out <- core.NewExportNothing(
+ b.Id(), fmt.Sprintf("issue tagged with project: %s", project))
+ return nil
+ }
+
+ // get jira bug ID
+ jiraID, ok := snapshot.GetCreateMetadata(metaKeyJiraId)
+ if ok {
+ // will be used to mark operation related to a bug as exported
+ bugJiraID = jiraID
+ } else {
+ // check that we have credentials for operation author
+ client, err := je.getClientForIdentity(author.Id())
+ if err != nil {
+ // if bug is not yet exported and we do not have the author's credentials
+ // then there is nothing we can do, so just skip this bug
+ out <- core.NewExportNothing(
+ b.Id(), fmt.Sprintf("missing author credentials for user %.8s",
+ author.Id().String()))
+ return err
+ }
+
+ // Load any custom fields required to create an issue from the git
+ // config file.
+ fields := make(map[string]interface{})
+ defaultFields, hasConf := je.conf[confKeyCreateDefaults]
+ if hasConf {
+ err = json.Unmarshal([]byte(defaultFields), &fields)
+ if err != nil {
+ return err
+ }
+ } else {
+ // If there is no configuration provided, at the very least the
+ // "issueType" field is always required. 10001 is "story" which I'm
+ // pretty sure is standard/default on all JIRA instances.
+ fields["issuetype"] = map[string]interface{}{
+ "id": "10001",
+ }
+ }
+ bugIDField, hasConf := je.conf[confKeyCreateGitBug]
+ if hasConf {
+ // If the git configuration also indicates it, we can assign the git-bug
+ // id to a custom field to assist in integrations
+ fields[bugIDField] = b.Id().String()
+ }
+
+ // create bug
+ result, err := client.CreateIssue(
+ je.project.ID, createOp.Title, createOp.Message, fields)
+ if err != nil {
+ err := errors.Wrap(err, "exporting jira issue")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+
+ id := result.ID
+ out <- core.NewExportBug(b.Id())
+ // mark bug creation operation as exported
+ err = markOperationAsExported(
+ b, createOp.Id(), id, je.project.Key, time.Time{})
+ if err != nil {
+ err := errors.Wrap(err, "marking operation as exported")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+
+ // commit operation to avoid creating multiple issues with multiple pushes
+ err = b.CommitAsNeeded()
+ if err != nil {
+ err := errors.Wrap(err, "bug commit")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+
+ // cache bug jira ID
+ bugJiraID = id
+ }
+
+ // cache operation jira id
+ je.cachedOperationIDs[createOp.Id()] = bugJiraID
+
+ for _, op := range snapshot.Operations[1:] {
+ // ignore SetMetadata operations
+ if _, ok := op.(*bug.SetMetadataOperation); ok {
+ continue
+ }
+
+ // ignore operations already existing in jira (due to import or export)
+ // cache the ID of already exported or imported issues and events from
+ // Jira
+ if id, ok := op.GetMetadata(metaKeyJiraId); ok {
+ je.cachedOperationIDs[op.Id()] = id
+ continue
+ }
+
+ opAuthor := op.GetAuthor()
+ client, err := je.getClientForIdentity(opAuthor.Id())
+ if err != nil {
+ out <- core.NewExportError(
+ fmt.Errorf("missing operation author credentials for user %.8s",
+ author.Id().String()), op.Id())
+ continue
+ }
+
+ var id string
+ var exportTime time.Time
+ switch opr := op.(type) {
+ case *bug.AddCommentOperation:
+ comment, err := client.AddComment(bugJiraID, opr.Message)
+ if err != nil {
+ err := errors.Wrap(err, "adding comment")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+ id = comment.ID
+ out <- core.NewExportComment(op.Id())
+
+ // cache comment id
+ je.cachedOperationIDs[op.Id()] = id
+
+ case *bug.EditCommentOperation:
+ if opr.Target == createOp.Id() {
+ // An EditCommentOpreation with the Target set to the create operation
+ // encodes a modification to the long-description/summary.
+ exportTime, err = client.UpdateIssueBody(bugJiraID, opr.Message)
+ if err != nil {
+ err := errors.Wrap(err, "editing issue")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+ out <- core.NewExportCommentEdition(op.Id())
+ id = bugJiraID
+ } else {
+ // Otherwise it's an edit to an actual comment. A comment cannot be
+ // edited before it was created, so it must be the case that we have
+ // already observed and cached the AddCommentOperation.
+ commentID, ok := je.cachedOperationIDs[opr.Target]
+ if !ok {
+ // Since an edit has to come after the creation, we expect we would
+ // have cached the creation id.
+ panic("unexpected error: comment id not found")
+ }
+ comment, err := client.UpdateComment(bugJiraID, commentID, opr.Message)
+ if err != nil {
+ err := errors.Wrap(err, "editing comment")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+ out <- core.NewExportCommentEdition(op.Id())
+ // JIRA doesn't track all comment edits, they will only tell us about
+ // the most recent one. We must invent a consistent id for the operation
+ // so we use the comment ID plus the timestamp of the update, as
+ // reported by JIRA. Note that this must be consistent with the importer
+ // during ensureComment()
+ id = getTimeDerivedID(comment.ID, comment.Updated)
+ }
+
+ case *bug.SetStatusOperation:
+ jiraStatus, hasStatus := je.statusMap[opr.Status.String()]
+ if hasStatus {
+ exportTime, err = UpdateIssueStatus(client, bugJiraID, jiraStatus)
+ if err != nil {
+ err := errors.Wrap(err, "editing status")
+ out <- core.NewExportWarning(err, b.Id())
+ // Failure to update status isn't necessarily a big error. It's
+ // possible that we just don't have enough information to make that
+ // update. In this case, just don't export the operation.
+ continue
+ }
+ out <- core.NewExportStatusChange(op.Id())
+ id = bugJiraID
+ } else {
+ out <- core.NewExportError(fmt.Errorf(
+ "No jira status mapped for %.8s", opr.Status.String()), b.Id())
+ }
+
+ case *bug.SetTitleOperation:
+ exportTime, err = client.UpdateIssueTitle(bugJiraID, opr.Title)
+ if err != nil {
+ err := errors.Wrap(err, "editing title")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+ out <- core.NewExportTitleEdition(op.Id())
+ id = bugJiraID
+
+ case *bug.LabelChangeOperation:
+ exportTime, err = client.UpdateLabels(
+ bugJiraID, opr.Added, opr.Removed)
+ if err != nil {
+ err := errors.Wrap(err, "updating labels")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+ out <- core.NewExportLabelChange(op.Id())
+ id = bugJiraID
+
+ default:
+ panic("unhandled operation type case")
+ }
+
+ // mark operation as exported
+ err = markOperationAsExported(
+ b, op.Id(), id, je.project.Key, exportTime)
+ if err != nil {
+ err := errors.Wrap(err, "marking operation as exported")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+
+ // commit at each operation export to avoid exporting same events multiple
+ // times
+ err = b.CommitAsNeeded()
+ if err != nil {
+ err := errors.Wrap(err, "bug commit")
+ out <- core.NewExportError(err, b.Id())
+ return err
+ }
+ }
+
+ return nil
+}
+
+func markOperationAsExported(b *cache.BugCache, target entity.Id, jiraID, jiraProject string, exportTime time.Time) error {
+ newMetadata := map[string]string{
+ metaKeyJiraId: jiraID,
+ metaKeyJiraProject: jiraProject,
+ }
+ if !exportTime.IsZero() {
+ newMetadata[metaKeyJiraExportTime] = exportTime.Format(http.TimeFormat)
+ }
+
+ _, err := b.SetMetadata(target, newMetadata)
+ return err
+}
+
+// UpdateIssueStatus attempts to change the "status" field by finding a
+// transition which achieves the desired state and then performing that
+// transition
+func UpdateIssueStatus(client *Client, issueKeyOrID string, desiredStateNameOrID string) (time.Time, error) {
+ var responseTime time.Time
+
+ tlist, err := client.GetTransitions(issueKeyOrID)
+ if err != nil {
+ return responseTime, err
+ }
+
+ transition := getTransitionTo(tlist, desiredStateNameOrID)
+ if transition == nil {
+ return responseTime, errTransitionNotFound
+ }
+
+ responseTime, err = client.DoTransition(issueKeyOrID, transition.ID)
+ if err != nil {
+ return responseTime, err
+ }
+
+ return responseTime, nil
+}
diff --git a/bridge/jira/import.go b/bridge/jira/import.go
new file mode 100644
index 00000000..f35f114f
--- /dev/null
+++ b/bridge/jira/import.go
@@ -0,0 +1,655 @@
+package jira
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "sort"
+ "strings"
+ "time"
+
+ "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
+ "github.com/MichaelMure/git-bug/bug"
+ "github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/util/text"
+)
+
+const (
+ defaultPageSize = 10
+)
+
+// jiraImporter implement the Importer interface
+type jiraImporter struct {
+ conf core.Configuration
+
+ client *Client
+
+ // send only channel
+ out chan<- core.ImportResult
+}
+
+// Init .
+func (ji *jiraImporter) Init(ctx context.Context, repo *cache.RepoCache, conf core.Configuration) error {
+ ji.conf = conf
+
+ var cred auth.Credential
+
+ // Prioritize LoginPassword credentials to avoid a prompt
+ creds, err := auth.List(repo,
+ auth.WithTarget(target),
+ auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyBaseUrl]),
+ auth.WithKind(auth.KindLoginPassword),
+ )
+ if err != nil {
+ return err
+ }
+ if len(creds) > 0 {
+ cred = creds[0]
+ goto end
+ }
+
+ creds, err = auth.List(repo,
+ auth.WithTarget(target),
+ auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyBaseUrl]),
+ auth.WithKind(auth.KindLogin),
+ )
+ if err != nil {
+ return err
+ }
+ if len(creds) > 0 {
+ cred = creds[0]
+ }
+
+end:
+ if cred == nil {
+ return fmt.Errorf("no credential for this bridge")
+ }
+
+ // TODO(josh)[da52062]: Validate token and if it is expired then prompt for
+ // credentials and generate a new one
+ ji.client, err = buildClient(ctx, conf[confKeyBaseUrl], conf[confKeyCredentialType], cred)
+ return err
+}
+
+// ImportAll iterate over all the configured repository issues and ensure the
+// creation of the missing issues / timeline items / edits / label events ...
+func (ji *jiraImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
+ sinceStr := since.Format("2006-01-02 15:04")
+ project := ji.conf[confKeyProject]
+
+ out := make(chan core.ImportResult)
+ ji.out = out
+
+ go func() {
+ defer close(ji.out)
+
+ message, err := ji.client.Search(
+ fmt.Sprintf("project=%s AND updatedDate>\"%s\"", project, sinceStr), 0, 0)
+ if err != nil {
+ out <- core.NewImportError(err, "")
+ return
+ }
+
+ fmt.Printf("So far so good. Have %d issues to import\n", message.Total)
+
+ jql := fmt.Sprintf("project=%s AND updatedDate>\"%s\"", project, sinceStr)
+ var searchIter *SearchIterator
+ for searchIter =
+ ji.client.IterSearch(jql, defaultPageSize); searchIter.HasNext(); {
+ issue := searchIter.Next()
+ b, err := ji.ensureIssue(repo, *issue)
+ if err != nil {
+ err := fmt.Errorf("issue creation: %v", err)
+ out <- core.NewImportError(err, "")
+ return
+ }
+
+ var commentIter *CommentIterator
+ for commentIter =
+ ji.client.IterComments(issue.ID, defaultPageSize); commentIter.HasNext(); {
+ comment := commentIter.Next()
+ err := ji.ensureComment(repo, b, *comment)
+ if err != nil {
+ out <- core.NewImportError(err, "")
+ }
+ }
+ if commentIter.HasError() {
+ out <- core.NewImportError(commentIter.Err, "")
+ }
+
+ snapshot := b.Snapshot()
+ opIdx := 0
+
+ var changelogIter *ChangeLogIterator
+ for changelogIter =
+ ji.client.IterChangeLog(issue.ID, defaultPageSize); changelogIter.HasNext(); {
+ changelogEntry := changelogIter.Next()
+
+ // Advance the operation iterator up to the first operation which has
+ // an export date not before the changelog entry date. If the changelog
+ // entry was created in response to an exported operation, then this
+ // will be that operation.
+ var exportTime time.Time
+ for ; opIdx < len(snapshot.Operations); opIdx++ {
+ exportTimeStr, hasTime := snapshot.Operations[opIdx].GetMetadata(
+ metaKeyJiraExportTime)
+ if !hasTime {
+ continue
+ }
+ exportTime, err = http.ParseTime(exportTimeStr)
+ if err != nil {
+ continue
+ }
+ if !exportTime.Before(changelogEntry.Created.Time) {
+ break
+ }
+ }
+ if opIdx < len(snapshot.Operations) {
+ err = ji.ensureChange(repo, b, *changelogEntry, snapshot.Operations[opIdx])
+ } else {
+ err = ji.ensureChange(repo, b, *changelogEntry, nil)
+ }
+ if err != nil {
+ out <- core.NewImportError(err, "")
+ }
+
+ }
+ if changelogIter.HasError() {
+ out <- core.NewImportError(changelogIter.Err, "")
+ }
+
+ if !b.NeedCommit() {
+ out <- core.NewImportNothing(b.Id(), "no imported operation")
+ } else if err := b.Commit(); err != nil {
+ err = fmt.Errorf("bug commit: %v", err)
+ out <- core.NewImportError(err, "")
+ return
+ }
+ }
+ if searchIter.HasError() {
+ out <- core.NewImportError(searchIter.Err, "")
+ }
+ }()
+
+ return out, nil
+}
+
+// Create a bug.Person from a JIRA user
+func (ji *jiraImporter) ensurePerson(repo *cache.RepoCache, user User) (*cache.IdentityCache, error) {
+ // Look first in the cache
+ i, err := repo.ResolveIdentityImmutableMetadata(
+ metaKeyJiraUser, string(user.Key))
+ if err == nil {
+ return i, nil
+ }
+ if _, ok := err.(entity.ErrMultipleMatch); ok {
+ return nil, err
+ }
+
+ i, err = repo.NewIdentityRaw(
+ user.DisplayName,
+ user.EmailAddress,
+ "",
+ map[string]string{
+ metaKeyJiraUser: string(user.Key),
+ },
+ )
+
+ if err != nil {
+ return nil, err
+ }
+
+ ji.out <- core.NewImportIdentity(i.Id())
+ return i, nil
+}
+
+// Create a bug.Bug based from a JIRA issue
+func (ji *jiraImporter) ensureIssue(repo *cache.RepoCache, issue Issue) (*cache.BugCache, error) {
+ author, err := ji.ensurePerson(repo, issue.Fields.Creator)
+ if err != nil {
+ return nil, err
+ }
+
+ b, err := repo.ResolveBugCreateMetadata(metaKeyJiraId, issue.ID)
+ if err != nil && err != bug.ErrBugNotExist {
+ return nil, err
+ }
+
+ if err == bug.ErrBugNotExist {
+ cleanText, err := text.Cleanup(string(issue.Fields.Description))
+ if err != nil {
+ return nil, err
+ }
+
+ // NOTE(josh): newlines in titles appears to be rare, but it has been seen
+ // in the wild. It does not appear to be allowed in the JIRA web interface.
+ title := strings.Replace(issue.Fields.Summary, "\n", "", -1)
+ b, _, err = repo.NewBugRaw(
+ author,
+ issue.Fields.Created.Unix(),
+ title,
+ cleanText,
+ nil,
+ map[string]string{
+ core.MetaKeyOrigin: target,
+ metaKeyJiraId: issue.ID,
+ metaKeyJiraKey: issue.Key,
+ metaKeyJiraProject: ji.conf[confKeyProject],
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ ji.out <- core.NewImportBug(b.Id())
+ }
+
+ return b, nil
+}
+
+// Return a unique string derived from a unique jira id and a timestamp
+func getTimeDerivedID(jiraID string, timestamp Time) string {
+ return fmt.Sprintf("%s-%d", jiraID, timestamp.Unix())
+}
+
+// Create a bug.Comment from a JIRA comment
+func (ji *jiraImporter) ensureComment(repo *cache.RepoCache, b *cache.BugCache, item Comment) error {
+ // ensure person
+ author, err := ji.ensurePerson(repo, item.Author)
+ if err != nil {
+ return err
+ }
+
+ targetOpID, err := b.ResolveOperationWithMetadata(
+ metaKeyJiraId, item.ID)
+ if err != nil && err != cache.ErrNoMatchingOp {
+ return err
+ }
+
+ // If the comment is a new comment then create it
+ if targetOpID == "" && err == cache.ErrNoMatchingOp {
+ var cleanText string
+ if item.Updated != item.Created {
+ // We don't know the original text... we only have the updated text.
+ cleanText = ""
+ } else {
+ cleanText, err = text.Cleanup(string(item.Body))
+ if err != nil {
+ return err
+ }
+ }
+
+ // add comment operation
+ op, err := b.AddCommentRaw(
+ author,
+ item.Created.Unix(),
+ cleanText,
+ nil,
+ map[string]string{
+ metaKeyJiraId: item.ID,
+ },
+ )
+ if err != nil {
+ return err
+ }
+
+ ji.out <- core.NewImportComment(op.Id())
+ targetOpID = op.Id()
+ }
+
+ // If there are no updates to this comment, then we are done
+ if item.Updated == item.Created {
+ return nil
+ }
+
+ // If there has been an update to this comment, we try to find it in the
+ // database. We need a unique id so we'll concat the issue id with the update
+ // timestamp. Note that this must be consistent with the exporter during
+ // export of an EditCommentOperation
+ derivedID := getTimeDerivedID(item.ID, item.Updated)
+ _, err = b.ResolveOperationWithMetadata(metaKeyJiraId, derivedID)
+ if err == nil {
+ // Already imported this edition
+ return nil
+ }
+
+ if err != cache.ErrNoMatchingOp {
+ return err
+ }
+
+ // ensure editor identity
+ editor, err := ji.ensurePerson(repo, item.UpdateAuthor)
+ if err != nil {
+ return err
+ }
+
+ // comment edition
+ cleanText, err := text.Cleanup(string(item.Body))
+ if err != nil {
+ return err
+ }
+ op, err := b.EditCommentRaw(
+ editor,
+ item.Updated.Unix(),
+ targetOpID,
+ cleanText,
+ map[string]string{
+ metaKeyJiraId: derivedID,
+ },
+ )
+
+ if err != nil {
+ return err
+ }
+
+ ji.out <- core.NewImportCommentEdition(op.Id())
+
+ return nil
+}
+
+// Return a unique string derived from a unique jira id and an index into the
+// data referred to by that jira id.
+func getIndexDerivedID(jiraID string, idx int) string {
+ return fmt.Sprintf("%s-%d", jiraID, idx)
+}
+
+func labelSetsMatch(jiraSet []string, gitbugSet []bug.Label) bool {
+ if len(jiraSet) != len(gitbugSet) {
+ return false
+ }
+
+ sort.Strings(jiraSet)
+ gitbugStrSet := make([]string, len(gitbugSet))
+ for idx, label := range gitbugSet {
+ gitbugStrSet[idx] = label.String()
+ }
+ sort.Strings(gitbugStrSet)
+
+ for idx, value := range jiraSet {
+ if value != gitbugStrSet[idx] {
+ return false
+ }
+ }
+
+ return true
+}
+
+// Create a bug.Operation (or a series of operations) from a JIRA changelog
+// entry
+func (ji *jiraImporter) ensureChange(repo *cache.RepoCache, b *cache.BugCache, entry ChangeLogEntry, potentialOp bug.Operation) error {
+
+ // If we have an operation which is already mapped to the entire changelog
+ // entry then that means this changelog entry was induced by an export
+ // operation and we've already done the match, so we skip this one
+ _, err := b.ResolveOperationWithMetadata(metaKeyJiraDerivedId, entry.ID)
+ if err == nil {
+ return nil
+ } else if err != cache.ErrNoMatchingOp {
+ return err
+ }
+
+ // In general, multiple fields may be changed in changelog entry on
+ // JIRA. For example, when an issue is closed both its "status" and its
+ // "resolution" are updated within a single changelog entry.
+ // I don't thing git-bug has a single operation to modify an arbitrary
+ // number of fields in one go, so we break up the single JIRA changelog
+ // entry into individual field updates.
+ author, err := ji.ensurePerson(repo, entry.Author)
+ if err != nil {
+ return err
+ }
+
+ if len(entry.Items) < 1 {
+ return fmt.Errorf("Received changelog entry with no item! (%s)", entry.ID)
+ }
+
+ statusMap, err := getStatusMapReverse(ji.conf)
+ if err != nil {
+ return err
+ }
+
+ // NOTE(josh): first do an initial scan and see if any of the changed items
+ // matches the current potential operation. If it does, then we know that this
+ // entire changelog entry was created in response to that git-bug operation.
+ // So we associate the operation with the entire changelog, and not a specific
+ // entry.
+ for _, item := range entry.Items {
+ switch item.Field {
+ case "labels":
+ fromLabels := removeEmpty(strings.Split(item.FromString, " "))
+ toLabels := removeEmpty(strings.Split(item.ToString, " "))
+ removedLabels, addedLabels, _ := setSymmetricDifference(fromLabels, toLabels)
+
+ opr, isRightType := potentialOp.(*bug.LabelChangeOperation)
+ if isRightType && labelSetsMatch(addedLabels, opr.Added) && labelSetsMatch(removedLabels, opr.Removed) {
+ _, err := b.SetMetadata(opr.Id(), map[string]string{
+ metaKeyJiraDerivedId: entry.ID,
+ })
+ if err != nil {
+ return err
+ }
+ return nil
+ }
+
+ case "status":
+ opr, isRightType := potentialOp.(*bug.SetStatusOperation)
+ if isRightType && statusMap[opr.Status.String()] == item.To {
+ _, err := b.SetMetadata(opr.Id(), map[string]string{
+ metaKeyJiraDerivedId: entry.ID,
+ })
+ if err != nil {
+ return err
+ }
+ return nil
+ }
+
+ case "summary":
+ // NOTE(josh): JIRA calls it "summary", which sounds more like the body
+ // text, but it's the title
+ opr, isRightType := potentialOp.(*bug.SetTitleOperation)
+ if isRightType && opr.Title == item.To {
+ _, err := b.SetMetadata(opr.Id(), map[string]string{
+ metaKeyJiraDerivedId: entry.ID,
+ })
+ if err != nil {
+ return err
+ }
+ return nil
+ }
+
+ case "description":
+ // NOTE(josh): JIRA calls it "description", which sounds more like the
+ // title but it's actually the body
+ opr, isRightType := potentialOp.(*bug.EditCommentOperation)
+ if isRightType &&
+ opr.Target == b.Snapshot().Operations[0].Id() &&
+ opr.Message == item.ToString {
+ _, err := b.SetMetadata(opr.Id(), map[string]string{
+ metaKeyJiraDerivedId: entry.ID,
+ })
+ if err != nil {
+ return err
+ }
+ return nil
+ }
+ }
+ }
+
+ // Since we didn't match the changelog entry to a known export operation,
+ // then this is a changelog entry that we should import. We import each
+ // changelog entry item as a separate git-bug operation.
+ for idx, item := range entry.Items {
+ derivedID := getIndexDerivedID(entry.ID, idx)
+ _, err := b.ResolveOperationWithMetadata(metaKeyJiraDerivedId, derivedID)
+ if err == nil {
+ continue
+ }
+ if err != cache.ErrNoMatchingOp {
+ return err
+ }
+
+ switch item.Field {
+ case "labels":
+ fromLabels := removeEmpty(strings.Split(item.FromString, " "))
+ toLabels := removeEmpty(strings.Split(item.ToString, " "))
+ removedLabels, addedLabels, _ := setSymmetricDifference(fromLabels, toLabels)
+
+ op, err := b.ForceChangeLabelsRaw(
+ author,
+ entry.Created.Unix(),
+ addedLabels,
+ removedLabels,
+ map[string]string{
+ metaKeyJiraId: entry.ID,
+ metaKeyJiraDerivedId: derivedID,
+ },
+ )
+ if err != nil {
+ return err
+ }
+
+ ji.out <- core.NewImportLabelChange(op.Id())
+
+ case "status":
+ statusStr, hasMap := statusMap[item.To]
+ if hasMap {
+ switch statusStr {
+ case bug.OpenStatus.String():
+ op, err := b.OpenRaw(
+ author,
+ entry.Created.Unix(),
+ map[string]string{
+ metaKeyJiraId: entry.ID,
+ metaKeyJiraDerivedId: derivedID,
+ },
+ )
+ if err != nil {
+ return err
+ }
+ ji.out <- core.NewImportStatusChange(op.Id())
+
+ case bug.ClosedStatus.String():
+ op, err := b.CloseRaw(
+ author,
+ entry.Created.Unix(),
+ map[string]string{
+ metaKeyJiraId: entry.ID,
+ metaKeyJiraDerivedId: derivedID,
+ },
+ )
+ if err != nil {
+ return err
+ }
+ ji.out <- core.NewImportStatusChange(op.Id())
+ }
+ } else {
+ ji.out <- core.NewImportError(
+ fmt.Errorf(
+ "No git-bug status mapped for jira status %s (%s)",
+ item.ToString, item.To), "")
+ }
+
+ case "summary":
+ // NOTE(josh): JIRA calls it "summary", which sounds more like the body
+ // text, but it's the title
+ op, err := b.SetTitleRaw(
+ author,
+ entry.Created.Unix(),
+ string(item.ToString),
+ map[string]string{
+ metaKeyJiraId: entry.ID,
+ metaKeyJiraDerivedId: derivedID,
+ },
+ )
+ if err != nil {
+ return err
+ }
+
+ ji.out <- core.NewImportTitleEdition(op.Id())
+
+ case "description":
+ // NOTE(josh): JIRA calls it "description", which sounds more like the
+ // title but it's actually the body
+ op, err := b.EditCreateCommentRaw(
+ author,
+ entry.Created.Unix(),
+ string(item.ToString),
+ map[string]string{
+ metaKeyJiraId: entry.ID,
+ metaKeyJiraDerivedId: derivedID,
+ },
+ )
+ if err != nil {
+ return err
+ }
+
+ ji.out <- core.NewImportCommentEdition(op.Id())
+
+ default:
+ ji.out <- core.NewImportWarning(
+ fmt.Errorf(
+ "Unhandled changelog event %s", item.Field), "")
+ }
+
+ // Other Examples:
+ // "assignee" (jira)
+ // "Attachment" (jira)
+ // "Epic Link" (custom)
+ // "Rank" (custom)
+ // "resolution" (jira)
+ // "Sprint" (custom)
+ }
+ return nil
+}
+
+func getStatusMap(conf core.Configuration) (map[string]string, error) {
+ mapStr, hasConf := conf[confKeyIDMap]
+ if !hasConf {
+ return map[string]string{
+ bug.OpenStatus.String(): "1",
+ bug.ClosedStatus.String(): "6",
+ }, nil
+ }
+
+ statusMap := make(map[string]string)
+ err := json.Unmarshal([]byte(mapStr), &statusMap)
+ return statusMap, err
+}
+
+func getStatusMapReverse(conf core.Configuration) (map[string]string, error) {
+ fwdMap, err := getStatusMap(conf)
+ if err != nil {
+ return fwdMap, err
+ }
+
+ outMap := map[string]string{}
+ for key, val := range fwdMap {
+ outMap[val] = key
+ }
+
+ mapStr, hasConf := conf[confKeyIDRevMap]
+ if !hasConf {
+ return outMap, nil
+ }
+
+ revMap := make(map[string]string)
+ err = json.Unmarshal([]byte(mapStr), &revMap)
+ for key, val := range revMap {
+ outMap[key] = val
+ }
+
+ return outMap, err
+}
+
+func removeEmpty(values []string) []string {
+ output := make([]string, 0, len(values))
+ for _, value := range values {
+ value = strings.TrimSpace(value)
+ if value != "" {
+ output = append(output, value)
+ }
+ }
+ return output
+}
diff --git a/bridge/jira/jira.go b/bridge/jira/jira.go
new file mode 100644
index 00000000..b891ee3d
--- /dev/null
+++ b/bridge/jira/jira.go
@@ -0,0 +1,143 @@
+// Package jira contains the Jira bridge implementation
+package jira
+
+import (
+ "context"
+ "fmt"
+ "sort"
+ "time"
+
+ "github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
+ "github.com/MichaelMure/git-bug/input"
+)
+
+const (
+ target = "jira"
+
+ metaKeyJiraId = "jira-id"
+ metaKeyJiraDerivedId = "jira-derived-id"
+ metaKeyJiraKey = "jira-key"
+ metaKeyJiraUser = "jira-user"
+ metaKeyJiraProject = "jira-project"
+ metaKeyJiraExportTime = "jira-export-time"
+ metaKeyJiraLogin = "jira-login"
+
+ confKeyBaseUrl = "base-url"
+ confKeyProject = "project"
+ confKeyCredentialType = "credentials-type" // "SESSION" or "TOKEN"
+ confKeyIDMap = "bug-id-map"
+ confKeyIDRevMap = "bug-id-revmap"
+ // the issue type when exporting a new bug. Default is Story (10001)
+ confKeyCreateDefaults = "create-issue-defaults"
+ // if set, the bridge fill this JIRA field with the `git-bug` id when exporting
+ confKeyCreateGitBug = "create-issue-gitbug-id"
+
+ defaultTimeout = 60 * time.Second
+)
+
+var _ core.BridgeImpl = &Jira{}
+
+// Jira Main object for the bridge
+type Jira struct{}
+
+// Target returns "jira"
+func (*Jira) Target() string {
+ return target
+}
+
+func (*Jira) LoginMetaKey() string {
+ return metaKeyJiraLogin
+}
+
+// NewImporter returns the jira importer
+func (*Jira) NewImporter() core.Importer {
+ return &jiraImporter{}
+}
+
+// NewExporter returns the jira exporter
+func (*Jira) NewExporter() core.Exporter {
+ return &jiraExporter{}
+}
+
+func buildClient(ctx context.Context, baseURL string, credType string, cred auth.Credential) (*Client, error) {
+ client := NewClient(ctx, baseURL)
+
+ var login, password string
+
+ switch cred := cred.(type) {
+ case *auth.LoginPassword:
+ login = cred.Login
+ password = cred.Password
+ case *auth.Login:
+ login = cred.Login
+ p, err := input.PromptPassword(fmt.Sprintf("Password for %s", login), "password", input.Required)
+ if err != nil {
+ return nil, err
+ }
+ password = p
+ }
+
+ err := client.Login(credType, login, password)
+ if err != nil {
+ return nil, err
+ }
+
+ return client, nil
+}
+
+// stringInSlice returns true if needle is found in haystack
+func stringInSlice(needle string, haystack []string) bool {
+ for _, match := range haystack {
+ if match == needle {
+ return true
+ }
+ }
+ return false
+}
+
+// Given two string slices, return three lists containing:
+// 1. elements found only in the first input list
+// 2. elements found only in the second input list
+// 3. elements found in both input lists
+func setSymmetricDifference(setA, setB []string) ([]string, []string, []string) {
+ sort.Strings(setA)
+ sort.Strings(setB)
+
+ maxLen := len(setA) + len(setB)
+ onlyA := make([]string, 0, maxLen)
+ onlyB := make([]string, 0, maxLen)
+ both := make([]string, 0, maxLen)
+
+ idxA := 0
+ idxB := 0
+
+ for idxA < len(setA) && idxB < len(setB) {
+ if setA[idxA] < setB[idxB] {
+ // In the first set, but not the second
+ onlyA = append(onlyA, setA[idxA])
+ idxA++
+ } else if setA[idxA] > setB[idxB] {
+ // In the second set, but not the first
+ onlyB = append(onlyB, setB[idxB])
+ idxB++
+ } else {
+ // In both
+ both = append(both, setA[idxA])
+ idxA++
+ idxB++
+ }
+ }
+
+ for ; idxA < len(setA); idxA++ {
+ // Leftovers in the first set, not the second
+ onlyA = append(onlyA, setA[idxA])
+ }
+
+ for ; idxB < len(setB); idxB++ {
+ // Leftovers in the second set, not the first
+ onlyB = append(onlyB, setB[idxB])
+ }
+
+ return onlyA, onlyB, both
+}
diff --git a/bridge/launchpad/config.go b/bridge/launchpad/config.go
index e029fad3..dfff0d3d 100644
--- a/bridge/launchpad/config.go
+++ b/bridge/launchpad/config.go
@@ -13,18 +13,14 @@ import (
var ErrBadProjectURL = errors.New("bad Launchpad project URL")
-func (l *Launchpad) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
- if params.TokenRaw != "" {
- fmt.Println("warning: token params are ineffective for a Launchpad bridge")
- }
- if params.Owner != "" {
- fmt.Println("warning: --owner is ineffective for a Launchpad bridge")
- }
- if params.BaseURL != "" {
- fmt.Println("warning: --base-url is ineffective for a Launchpad bridge")
+func (Launchpad) ValidParams() map[string]interface{} {
+ return map[string]interface{}{
+ "URL": nil,
+ "Project": nil,
}
+}
- conf := make(core.Configuration)
+func (l *Launchpad) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
var err error
var project string
@@ -52,8 +48,9 @@ func (l *Launchpad) Configure(repo *cache.RepoCache, params core.BridgeParams) (
return nil, fmt.Errorf("project doesn't exist")
}
+ conf := make(core.Configuration)
conf[core.ConfigKeyTarget] = target
- conf[keyProject] = project
+ conf[confKeyProject] = project
err = l.ValidateConfig(conf)
if err != nil {
@@ -70,8 +67,8 @@ func (*Launchpad) ValidateConfig(conf core.Configuration) error {
return fmt.Errorf("unexpected target name: %v", v)
}
- if _, ok := conf[keyProject]; !ok {
- return fmt.Errorf("missing %s key", keyProject)
+ if _, ok := conf[confKeyProject]; !ok {
+ return fmt.Errorf("missing %s key", confKeyProject)
}
return nil
@@ -94,10 +91,7 @@ func validateProject(project string) (bool, error) {
// extract project name from url
func splitURL(url string) (string, error) {
- re, err := regexp.Compile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
- if err != nil {
- panic("regexp compile:" + err.Error())
- }
+ re := regexp.MustCompile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
res := re.FindStringSubmatch(url)
if res == nil {
diff --git a/bridge/launchpad/import.go b/bridge/launchpad/import.go
index 5bca8e63..dfcbb95e 100644
--- a/bridge/launchpad/import.go
+++ b/bridge/launchpad/import.go
@@ -15,7 +15,7 @@ type launchpadImporter struct {
conf core.Configuration
}
-func (li *launchpadImporter) Init(repo *cache.RepoCache, conf core.Configuration) error {
+func (li *launchpadImporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error {
li.conf = conf
return nil
}
diff --git a/bridge/launchpad/launchpad.go b/bridge/launchpad/launchpad.go
index b4fcdd00..51ee79d2 100644
--- a/bridge/launchpad/launchpad.go
+++ b/bridge/launchpad/launchpad.go
@@ -13,7 +13,7 @@ const (
metaKeyLaunchpadID = "launchpad-id"
metaKeyLaunchpadLogin = "launchpad-login"
- keyProject = "project"
+ confKeyProject = "project"
defaultTimeout = 60 * time.Second
)
@@ -26,7 +26,7 @@ func (*Launchpad) Target() string {
return "launchpad-preview"
}
-func (l *Launchpad) LoginMetaKey() string {
+func (Launchpad) LoginMetaKey() string {
return metaKeyLaunchpadLogin
}