diff options
author | Amine Hilaly <hilalyamine@gmail.com> | 2019-08-13 19:51:14 +0200 |
---|---|---|
committer | Amine Hilaly <hilalyamine@gmail.com> | 2019-08-18 00:14:22 +0200 |
commit | 5ca326af83b90531d4d0c502bb1beabbe1b48c55 (patch) | |
tree | 6b7a32f2db9ab7321e9965c0ef4c715c6c517178 /bridge/launchpad/launchpad_api.go | |
parent | 6428352bd14828f670206b60862de7f71c52d235 (diff) | |
download | git-bug-5ca326af83b90531d4d0c502bb1beabbe1b48c55.tar.gz |
bridge/core: add context.Context to ImportAll and ExportAll signatures
bridge/core: add ImportResult objects to stream import events
bridge/core: launchpad support asynchronous import
bridge/github: cancellable export and import functions
bridge/gitlab: cancellable export and import functions
commands: bridge pull/push gracefull kill
bridge/github: fix github import
bridge/github: use simple context for imports
bridge/core: name parameters in interfaces
github/core: Add EventError to export and import events types
bridge/gitlab: add context support in gitlab requests functions
bridge/gitlab: remove imported events count from importer logic
bridge/github: remove imported events count from importer logic
bridge/github: add context support in query and muration requets
bridge/github: fix bug duplicate editions after multiple calls
bridge/core: import import and export events String methods
bridge/gitlab: fix error handling in note import events
commands/bridge: Add statistics about imports and exports
bridge/gitlab: properly handle context cancellation
bridge/github: improve error handling
bridge: break iterators on context cancel or timeout
bridge: add context timeout support
bridge: improve event formating and error handling
commands: handle interrupt and switch cases
bridge/github: add export mutation timeouts
bridge: fix race condition bug in the github and gitlab importers
bridge/github: improve context error handling
Diffstat (limited to 'bridge/launchpad/launchpad_api.go')
-rw-r--r-- | bridge/launchpad/launchpad_api.go | 54 |
1 files changed, 11 insertions, 43 deletions
diff --git a/bridge/launchpad/launchpad_api.go b/bridge/launchpad/launchpad_api.go index 8cafa241..763e774e 100644 --- a/bridge/launchpad/launchpad_api.go +++ b/bridge/launchpad/launchpad_api.go @@ -14,6 +14,7 @@ package launchpad */ import ( + "context" "encoding/json" "fmt" "net/http" @@ -33,43 +34,6 @@ type LPPerson struct { // https://api.launchpad.net/devel/~login var personCache = make(map[string]LPPerson) -func (owner *LPPerson) UnmarshalJSON(data []byte) error { - type LPPersonX LPPerson // Avoid infinite recursion - var ownerLink string - if err := json.Unmarshal(data, &ownerLink); err != nil { - return err - } - - // First, try to gather info about the bug owner using our cache. - if cachedPerson, hasKey := personCache[ownerLink]; hasKey { - *owner = cachedPerson - return nil - } - - // If the bug owner is not already known, we have to send a request. - req, err := http.NewRequest("GET", ownerLink, nil) - if err != nil { - return nil - } - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return nil - } - - defer resp.Body.Close() - - var p LPPersonX - if err := json.NewDecoder(resp.Body).Decode(&p); err != nil { - return nil - } - *owner = LPPerson(p) - // Do not forget to update the cache. - personCache[ownerLink] = *owner - return nil -} - // LPBug describes a Launchpad bug. type LPBug struct { Title string `json:"title"` @@ -109,11 +73,13 @@ type launchpadAPI struct { } func (lapi *launchpadAPI) Init() error { - lapi.client = &http.Client{} + lapi.client = &http.Client{ + Timeout: defaultTimeout, + } return nil } -func (lapi *launchpadAPI) SearchTasks(project string) ([]LPBug, error) { +func (lapi *launchpadAPI) SearchTasks(ctx context.Context, project string) ([]LPBug, error) { var bugs []LPBug // First, let us build the URL. Not all statuses are included by @@ -153,7 +119,7 @@ func (lapi *launchpadAPI) SearchTasks(project string) ([]LPBug, error) { } for _, bugEntry := range result.Entries { - bug, err := lapi.queryBug(bugEntry.BugLink) + bug, err := lapi.queryBug(ctx, bugEntry.BugLink) if err == nil { bugs = append(bugs, bug) } @@ -170,13 +136,14 @@ func (lapi *launchpadAPI) SearchTasks(project string) ([]LPBug, error) { return bugs, nil } -func (lapi *launchpadAPI) queryBug(url string) (LPBug, error) { +func (lapi *launchpadAPI) queryBug(ctx context.Context, url string) (LPBug, error) { var bug LPBug req, err := http.NewRequest("GET", url, nil) if err != nil { return bug, err } + req = req.WithContext(ctx) resp, err := lapi.client.Do(req) if err != nil { @@ -191,7 +158,7 @@ func (lapi *launchpadAPI) queryBug(url string) (LPBug, error) { /* Fetch messages */ messagesCollectionLink := fmt.Sprintf("%s/bugs/%d/messages", apiRoot, bug.ID) - messages, err := lapi.queryMessages(messagesCollectionLink) + messages, err := lapi.queryMessages(ctx, messagesCollectionLink) if err != nil { return bug, err } @@ -200,7 +167,7 @@ func (lapi *launchpadAPI) queryBug(url string) (LPBug, error) { return bug, nil } -func (lapi *launchpadAPI) queryMessages(messagesURL string) ([]LPMessage, error) { +func (lapi *launchpadAPI) queryMessages(ctx context.Context, messagesURL string) ([]LPMessage, error) { var messages []LPMessage for { @@ -208,6 +175,7 @@ func (lapi *launchpadAPI) queryMessages(messagesURL string) ([]LPMessage, error) if err != nil { return nil, err } + req = req.WithContext(ctx) resp, err := lapi.client.Do(req) if err != nil { |