-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from gin-melodic/dev
Fix issues and support Query and Activity.
- Loading branch information
Showing
41 changed files
with
2,564 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 1,3 @@ | ||
.idea | ||
schemas/*.json | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,68 @@ | ||
package openproject | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestActivitiesServiceGet(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
testAPIEdpoint := "/api/v3/activities/357978" | ||
|
||
raw, err := os.ReadFile("./mocks/get/get-activity.json") | ||
if err != nil { | ||
t.Error(err.Error()) | ||
return | ||
} | ||
testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testRequestURL(t, r, testAPIEdpoint) | ||
fmt.Fprint(w, string(raw)) | ||
}) | ||
|
||
activity, _, err := testClient.Activities.Get("357978") | ||
if err != nil { | ||
t.Errorf("Error given: %s", err) | ||
} | ||
if activity == nil { | ||
t.Error("Expected activities. Activities is nil") | ||
return | ||
} | ||
if activity.Id != 357978 { | ||
t.Errorf("Unexpected activity id %d", activity.Id) | ||
} | ||
} | ||
|
||
func TestActivitiesService_GetFromWPHref(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
testAPIEdpoint := "/api/v3/work_packages/36353/activities" | ||
|
||
raw, err := os.ReadFile("./mocks/get/get-activities.json") | ||
if err != nil { | ||
t.Error(err.Error()) | ||
return | ||
} | ||
|
||
testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testRequestURL(t, r, testAPIEdpoint) | ||
fmt.Fprint(w, string(raw)) | ||
}) | ||
|
||
activities, _, err := testClient.Activities.GetFromWPHref("/api/v3/work_packages/36353/activities") | ||
if err != nil { | ||
t.Errorf("Error given: %s", err) | ||
return | ||
} | ||
if activities == nil { | ||
t.Error("Expected activities. Activities is nil") | ||
return | ||
} | ||
if activities.Count != 6 { | ||
t.Errorf("Unexpected activities count %d", activities.Count) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,76 @@ | ||
package openproject | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// ActivitiesService handles activities for the OpenProject instance / API. | ||
type ActivitiesService struct { | ||
client *Client | ||
} | ||
|
||
type Activity struct { | ||
Type string `json:"_type"` | ||
Id int `json:"id"` | ||
Comment OPGenericDescription `json:"comment"` | ||
Details []OPGenericDescription `json:"details"` | ||
Version int `json:"version"` | ||
CreatedAt time.Time `json:"createdAt"` | ||
Links struct { | ||
Self OPGenericLink `json:"self"` | ||
WorkPackage OPGenericLink `json:"workPackage"` | ||
User OPGenericLink `json:"user"` | ||
Update OPGenericLink `json:"update"` | ||
} `json:"_links"` | ||
} | ||
|
||
type Activities struct { | ||
Type string `json:"_type"` | ||
Total int `json:"total"` | ||
Count int `json:"count"` | ||
Embedded struct { | ||
Elements []Activity `json:"elements"` | ||
} `json:"_embedded"` | ||
Links struct { | ||
Self OPGenericLink `json:"self"` | ||
} `json:"_links"` | ||
} | ||
|
||
// GetWithContext gets activity from OpenProject using its ID | ||
func (s *ActivitiesService) GetWithContext(ctx context.Context, activitiesID string) (*Activity, *Response, error) { | ||
apiEndPoint := fmt.Sprintf("api/v3/activities/%s", activitiesID) | ||
obj, resp, err := GetWithContext(ctx, s, apiEndPoint) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
return obj.(*Activity), resp, err | ||
} | ||
|
||
// Get wraps GetWithContext using the background context. | ||
func (s *ActivitiesService) Get(activitiesID string) (*Activity, *Response, error) { | ||
return s.GetWithContext(context.Background(), activitiesID) | ||
} | ||
|
||
// GetFromWPHrefWithContext gets activities from OpenProject using work package href string, like '/api/v3/work_packages/36353/activities' | ||
func (s *ActivitiesService) GetFromWPHrefWithContext(ctx context.Context, href string) (*Activities, *Response, error) { | ||
if strings.HasPrefix(href, "/") && len(href) > 1 { | ||
href = href[1:] | ||
} | ||
if href == "" { | ||
return nil, nil, fmt.Errorf("href is empty") | ||
} | ||
apiEndPoint := href | ||
obj, resp, err := GetListWithContext(ctx, s, apiEndPoint, nil, 0, 0) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
return obj.(*Activities), resp, err | ||
} | ||
|
||
// GetFromWPHref wraps GetFromWPHrefWithContext using the background context. | ||
func (s *ActivitiesService) GetFromWPHref(href string) (*Activities, *Response, error) { | ||
return s.GetFromWPHrefWithContext(context.Background(), href) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.