jira.go

  1// Package jira contains the Jira bridge implementation
  2package jira
  3
  4import (
  5	"sort"
  6	"time"
  7
  8	"github.com/MichaelMure/git-bug/bridge/core"
  9)
 10
 11const (
 12	target = "jira"
 13
 14	metaKeyJiraLogin = "jira-login"
 15
 16	keyServer          = "server"
 17	keyProject         = "project"
 18	keyCredentialsType = "credentials-type"
 19	keyCredentialsFile = "credentials-file"
 20	keyUsername        = "username"
 21	keyPassword        = "password"
 22	keyIDMap           = "bug-id-map"
 23	keyIDRevMap        = "bug-id-revmap"
 24	keyCreateDefaults  = "create-issue-defaults"
 25	keyCreateGitBug    = "create-issue-gitbug-id"
 26
 27	defaultTimeout = 60 * time.Second
 28)
 29
 30var _ core.BridgeImpl = &Jira{}
 31
 32// Jira Main object for the bridge
 33type Jira struct{}
 34
 35// Target returns "jira"
 36func (*Jira) Target() string {
 37	return target
 38}
 39
 40func (*Jira) LoginMetaKey() string {
 41	return metaKeyJiraLogin
 42}
 43
 44// NewImporter returns the jira importer
 45func (*Jira) NewImporter() core.Importer {
 46	return &jiraImporter{}
 47}
 48
 49// NewExporter returns the jira exporter
 50func (*Jira) NewExporter() core.Exporter {
 51	return &jiraExporter{}
 52}
 53
 54// stringInSlice returns true if needle is found in haystack
 55func stringInSlice(needle string, haystack []string) bool {
 56	for _, match := range haystack {
 57		if match == needle {
 58			return true
 59		}
 60	}
 61	return false
 62}
 63
 64// Given two string slices, return three lists containing:
 65// 1. elements found only in the first input list
 66// 2. elements found only in the second input list
 67// 3. elements found in both input lists
 68func setSymmetricDifference(setA, setB []string) ([]string, []string, []string) {
 69	sort.Strings(setA)
 70	sort.Strings(setB)
 71
 72	maxLen := len(setA) + len(setB)
 73	onlyA := make([]string, 0, maxLen)
 74	onlyB := make([]string, 0, maxLen)
 75	both := make([]string, 0, maxLen)
 76
 77	idxA := 0
 78	idxB := 0
 79
 80	for idxA < len(setA) && idxB < len(setB) {
 81		if setA[idxA] < setB[idxB] {
 82			// In the first set, but not the second
 83			onlyA = append(onlyA, setA[idxA])
 84			idxA++
 85		} else if setA[idxA] > setB[idxB] {
 86			// In the second set, but not the first
 87			onlyB = append(onlyB, setB[idxB])
 88			idxB++
 89		} else {
 90			// In both
 91			both = append(both, setA[idxA])
 92			idxA++
 93			idxB++
 94		}
 95	}
 96
 97	for ; idxA < len(setA); idxA++ {
 98		// Leftovers in the first set, not the second
 99		onlyA = append(onlyA, setA[idxA])
100	}
101
102	for ; idxB < len(setB); idxB++ {
103		// Leftovers in the second set, not the first
104		onlyB = append(onlyB, setB[idxB])
105	}
106
107	return onlyA, onlyB, both
108}