Skip to content

Commit adfb2db

Browse files
committed
Merge branch 'master' into issue-284-product-naming
* master: fix(tests): Fix TestIssueService_PostAttachment unit test style: Fix typos style: Make code go fmt conform chore(test): Remove unit testing log output for success cases (#293) feat(project): Add GitHub Actions testing workflow (#289) feat(context): Add support for context package fix(issue): IssueService.Search() with a not empty JQL triggers 400 bad request (#292) feat(IssueService): allow empty JQL (#268) style: Fix staticcheck (static analysis) errors for this library (#283) feat(project): Add workflow to greet new contributors (#288) feat(project): Add cronjob to check for stale issues (#287) feat(issues): Add GetEditMeta on issue feat: Add Names support on Issue struct (#278)
2 parents 2da7787 + f6b1dca commit adfb2db

39 files changed

+1157
-375
lines changed

.github/workflows/greetings.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Greetings
2+
on: [pull_request, issues]
3+
4+
jobs:
5+
greeting:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/first-interaction@v1
9+
with:
10+
repo-token: ${{ secrets.GITHUB_TOKEN }}
11+
issue-message: 'Hi! Thank you for taking the time to create your first issue! Really cool to see you here for the first time. Please give us a bit of time to review it.'
12+
pr-message: 'Great! Thank you for taking the time to create your first pull request. It is always a pleasure to see people like you who spent time contributing. Please give us a bit of time to review it!'

.github/workflows/stale.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: "Close stale issues"
2+
on:
3+
schedule:
4+
- cron: "0 4 * * *"
5+
6+
jobs:
7+
stale:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/[email protected]
11+
with:
12+
repo-token: ${{ secrets.GITHUB_TOKEN }}
13+
stale-issue-message: >
14+
This issue has been automatically marked as stale because it has not had
15+
recent activity in the last 60 days. It will be closed in 7 days if no further activity occurs.
16+
Thank you for your contributions.
17+
days-before-stale: 60
18+
days-before-close: 7
19+
stale-issue-label: stale

.github/workflows/testing.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
name: Test and lint
12+
strategy:
13+
matrix:
14+
go-version: [1.x, 1.13.x, 1.12.x]
15+
platform: [ubuntu-latest, windows-latest]
16+
runs-on: ${{ matrix.platform }}
17+
18+
steps:
19+
- uses: actions/checkout@v2
20+
- uses: actions/setup-go@v2
21+
with:
22+
go-version: 1.14
23+
24+
# Caching go modules to speed up the run
25+
- uses: actions/cache@v1
26+
with:
27+
path: ~/go/pkg/mod
28+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
29+
restore-keys: |
30+
${{ runner.os }}-go-
31+
32+
- name: Run go fmt
33+
if: runner.os != 'Windows'
34+
run: diff -u <(echo -n) <(gofmt -d -s .)
35+
36+
- name: Run go vet
37+
run: make vet
38+
39+
- name: Run staticcheck
40+
run: make staticcheck
41+
42+
- name: Run Unit tests.
43+
run: make test

authentication.go

+42-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jira
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io/ioutil"
@@ -47,7 +48,7 @@ type Session struct {
4748
Cookies []*http.Cookie
4849
}
4950

50-
// AcquireSessionCookie creates a new session for a user in Jira.
51+
// AcquireSessionCookieWithContext creates a new session for a user in Jira.
5152
// Once a session has been successfully created it can be used to access any of Jira's remote APIs and also the web UI by passing the appropriate HTTP Cookie header.
5253
// The header will by automatically applied to every API request.
5354
// Note that it is generally preferrable to use HTTP BASIC authentication with the REST API.
@@ -56,7 +57,7 @@ type Session struct {
5657
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#auth/1/session
5758
//
5859
// Deprecated: Use CookieAuthTransport instead
59-
func (s *AuthenticationService) AcquireSessionCookie(username, password string) (bool, error) {
60+
func (s *AuthenticationService) AcquireSessionCookieWithContext(ctx context.Context, username, password string) (bool, error) {
6061
apiEndpoint := "rest/auth/1/session"
6162
body := struct {
6263
Username string `json:"username"`
@@ -66,7 +67,7 @@ func (s *AuthenticationService) AcquireSessionCookie(username, password string)
6667
password,
6768
}
6869

69-
req, err := s.client.NewRequest("POST", apiEndpoint, body)
70+
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndpoint, body)
7071
if err != nil {
7172
return false, err
7273
}
@@ -79,10 +80,10 @@ func (s *AuthenticationService) AcquireSessionCookie(username, password string)
7980
}
8081

8182
if err != nil {
82-
return false, fmt.Errorf("Auth at Jira instance failed (HTTP(S) request). %s", err)
83+
return false, fmt.Errorf("auth at Jira instance failed (HTTP(S) request). %s", err)
8384
}
8485
if resp != nil && resp.StatusCode != 200 {
85-
return false, fmt.Errorf("Auth at Jira instance failed (HTTP(S) request). Status code: %d", resp.StatusCode)
86+
return false, fmt.Errorf("auth at Jira instance failed (HTTP(S) request). Status code: %d", resp.StatusCode)
8687
}
8788

8889
s.client.session = session
@@ -91,6 +92,13 @@ func (s *AuthenticationService) AcquireSessionCookie(username, password string)
9192
return true, nil
9293
}
9394

95+
// AcquireSessionCookie wraps AcquireSessionCookieWithContext using the background context.
96+
//
97+
// Deprecated: Use CookieAuthTransport instead
98+
func (s *AuthenticationService) AcquireSessionCookie(username, password string) (bool, error) {
99+
return s.AcquireSessionCookieWithContext(context.Background(), username, password)
100+
}
101+
94102
// SetBasicAuth sets username and password for the basic auth against the Jira instance.
95103
//
96104
// Deprecated: Use BasicAuthTransport instead
@@ -113,29 +121,29 @@ func (s *AuthenticationService) Authenticated() bool {
113121
return false
114122
}
115123

116-
// Logout logs out the current user that has been authenticated and the session in the client is destroyed.
124+
// LogoutWithContext logs out the current user that has been authenticated and the session in the client is destroyed.
117125
//
118126
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#auth/1/session
119127
//
120128
// Deprecated: Use CookieAuthTransport to create base client. Logging out is as simple as not using the
121129
// client anymore
122-
func (s *AuthenticationService) Logout() error {
130+
func (s *AuthenticationService) LogoutWithContext(ctx context.Context) error {
123131
if s.authType != authTypeSession || s.client.session == nil {
124132
return fmt.Errorf("no user is authenticated")
125133
}
126134

127135
apiEndpoint := "rest/auth/1/session"
128-
req, err := s.client.NewRequest("DELETE", apiEndpoint, nil)
136+
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndpoint, nil)
129137
if err != nil {
130-
return fmt.Errorf("Creating the request to log the user out failed : %s", err)
138+
return fmt.Errorf("creating the request to log the user out failed : %s", err)
131139
}
132140

133141
resp, err := s.client.Do(req, nil)
134142
if err != nil {
135-
return fmt.Errorf("Error sending the logout request: %s", err)
143+
return fmt.Errorf("error sending the logout request: %s", err)
136144
}
137145
if resp.StatusCode != 204 {
138-
return fmt.Errorf("The logout was unsuccessful with status %d", resp.StatusCode)
146+
return fmt.Errorf("the logout was unsuccessful with status %d", resp.StatusCode)
139147
}
140148

141149
// If logout successful, delete session
@@ -145,43 +153,56 @@ func (s *AuthenticationService) Logout() error {
145153

146154
}
147155

148-
// GetCurrentUser gets the details of the current user.
156+
// Logout wraps LogoutWithContext using the background context.
157+
//
158+
// Deprecated: Use CookieAuthTransport to create base client. Logging out is as simple as not using the
159+
// client anymore
160+
func (s *AuthenticationService) Logout() error {
161+
return s.LogoutWithContext(context.Background())
162+
}
163+
164+
// GetCurrentUserWithContext gets the details of the current user.
149165
//
150166
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#auth/1/session
151-
func (s *AuthenticationService) GetCurrentUser() (*Session, error) {
167+
func (s *AuthenticationService) GetCurrentUserWithContext(ctx context.Context) (*Session, error) {
152168
if s == nil {
153-
return nil, fmt.Errorf("AUthenticaiton Service is not instantiated")
169+
return nil, fmt.Errorf("authentication Service is not instantiated")
154170
}
155171
if s.authType != authTypeSession || s.client.session == nil {
156-
return nil, fmt.Errorf("No user is authenticated yet")
172+
return nil, fmt.Errorf("no user is authenticated yet")
157173
}
158174

159175
apiEndpoint := "rest/auth/1/session"
160-
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
176+
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
161177
if err != nil {
162-
return nil, fmt.Errorf("Could not create request for getting user info : %s", err)
178+
return nil, fmt.Errorf("could not create request for getting user info : %s", err)
163179
}
164180

165181
resp, err := s.client.Do(req, nil)
166182
if err != nil {
167-
return nil, fmt.Errorf("Error sending request to get user info : %s", err)
183+
return nil, fmt.Errorf("error sending request to get user info : %s", err)
168184
}
169185
if resp.StatusCode != 200 {
170-
return nil, fmt.Errorf("Getting user info failed with status : %d", resp.StatusCode)
186+
return nil, fmt.Errorf("getting user info failed with status : %d", resp.StatusCode)
171187
}
172188

173189
defer resp.Body.Close()
174190
ret := new(Session)
175191
data, err := ioutil.ReadAll(resp.Body)
176192
if err != nil {
177-
return nil, fmt.Errorf("Couldn't read body from the response : %s", err)
193+
return nil, fmt.Errorf("couldn't read body from the response : %s", err)
178194
}
179195

180196
err = json.Unmarshal(data, &ret)
181197

182198
if err != nil {
183-
return nil, fmt.Errorf("Could not unmarshall received user info : %s", err)
199+
return nil, fmt.Errorf("could not unmarshall received user info : %s", err)
184200
}
185201

186202
return ret, nil
187203
}
204+
205+
// GetCurrentUser wraps GetCurrentUserWithContext using the background context.
206+
func (s *AuthenticationService) GetCurrentUser() (*Session, error) {
207+
return s.GetCurrentUserWithContext(context.Background())
208+
}

authentication_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ func TestAuthenticationService_AcquireSessionCookie_Failure(t *testing.T) {
1919
if err != nil {
2020
t.Errorf("Error in read body: %s", err)
2121
}
22-
if bytes.Index(b, []byte(`"username":"foo"`)) < 0 {
22+
if !bytes.Contains(b, []byte(`"username":"foo"`)) {
2323
t.Error("No username found")
2424
}
25-
if bytes.Index(b, []byte(`"password":"bar"`)) < 0 {
25+
if !bytes.Contains(b, []byte(`"password":"bar"`)) {
2626
t.Error("No password found")
2727
}
2828

@@ -53,10 +53,10 @@ func TestAuthenticationService_AcquireSessionCookie_Success(t *testing.T) {
5353
if err != nil {
5454
t.Errorf("Error in read body: %s", err)
5555
}
56-
if bytes.Index(b, []byte(`"username":"foo"`)) < 0 {
56+
if !bytes.Contains(b, []byte(`"username":"foo"`)) {
5757
t.Error("No username found")
5858
}
59-
if bytes.Index(b, []byte(`"password":"bar"`)) < 0 {
59+
if !bytes.Contains(b, []byte(`"password":"bar"`)) {
6060
t.Error("No password found")
6161
}
6262

@@ -144,10 +144,10 @@ func TestAithenticationService_GetUserInfo_AccessForbidden_Fail(t *testing.T) {
144144
if err != nil {
145145
t.Errorf("Error in read body: %s", err)
146146
}
147-
if bytes.Index(b, []byte(`"username":"foo"`)) < 0 {
147+
if !bytes.Contains(b, []byte(`"username":"foo"`)) {
148148
t.Error("No username found")
149149
}
150-
if bytes.Index(b, []byte(`"password":"bar"`)) < 0 {
150+
if !bytes.Contains(b, []byte(`"password":"bar"`)) {
151151
t.Error("No password found")
152152
}
153153

@@ -182,10 +182,10 @@ func TestAuthenticationService_GetUserInfo_NonOkStatusCode_Fail(t *testing.T) {
182182
if err != nil {
183183
t.Errorf("Error in read body: %s", err)
184184
}
185-
if bytes.Index(b, []byte(`"username":"foo"`)) < 0 {
185+
if !bytes.Contains(b, []byte(`"username":"foo"`)) {
186186
t.Error("No username found")
187187
}
188-
if bytes.Index(b, []byte(`"password":"bar"`)) < 0 {
188+
if !bytes.Contains(b, []byte(`"password":"bar"`)) {
189189
t.Error("No password found")
190190
}
191191

@@ -238,10 +238,10 @@ func TestAuthenticationService_GetUserInfo_Success(t *testing.T) {
238238
if err != nil {
239239
t.Errorf("Error in read body: %s", err)
240240
}
241-
if bytes.Index(b, []byte(`"username":"foo"`)) < 0 {
241+
if !bytes.Contains(b, []byte(`"username":"foo"`)) {
242242
t.Error("No username found")
243243
}
244-
if bytes.Index(b, []byte(`"password":"bar"`)) < 0 {
244+
if !bytes.Contains(b, []byte(`"password":"bar"`)) {
245245
t.Error("No password found")
246246
}
247247

@@ -280,10 +280,10 @@ func TestAuthenticationService_Logout_Success(t *testing.T) {
280280
if err != nil {
281281
t.Errorf("Error in read body: %s", err)
282282
}
283-
if bytes.Index(b, []byte(`"username":"foo"`)) < 0 {
283+
if !bytes.Contains(b, []byte(`"username":"foo"`)) {
284284
t.Error("No username found")
285285
}
286-
if bytes.Index(b, []byte(`"password":"bar"`)) < 0 {
286+
if !bytes.Contains(b, []byte(`"password":"bar"`)) {
287287
t.Error("No password found")
288288
}
289289

0 commit comments

Comments
 (0)