Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated code style #308

Merged
merged 15 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
name: Lint

on:
push:
branches:
- main
- feature/*
pull_request:
branches: [ main ]

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: GolangCI-Lint
strategy:
max-parallel: 6
matrix:
go: [1.22, 1.23]
os: [ubuntu-latest, macos-latest, windows-latest]
name: lint
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v5
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v4

- name: Checkout code
uses: actions/checkout@v4

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
# version: v1.51.2
# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0
version: latest
args: --timeout=10m

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
24 changes: 24 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
run:
concurrency: 4
timeout: 20m
tests: false
# This gives us the ability to efficiently skip whole files by using "//go:build !codeanalysis" at the top of a file.
build-tags:
- codeanalysis

linters:
enable:
- revive

linters-settings:
revive:
rules:
- name: unused-parameter
disabled: true # forces all unused to be "_" which leads to confusion

issues:
max-issues-per-linter: 0
max-same-issues: 0

severity:
default-severity: critical
25 changes: 13 additions & 12 deletions admin/api_client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/ctreminiom/go-atlassian/admin/internal"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service/common"
"io"
"net/http"
"net/url"

"github.com/ctreminiom/go-atlassian/admin/internal"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service/common"
)

const defaultApiEndpoint = "https://api.atlassian.com/"
const defaultAPIEndpoint = "https://api.atlassian.com/"

// New creates a new instance of Client.
// It takes a common.HttpClient as input and returns a pointer to Client and an error.
func New(httpClient common.HttpClient) (*Client, error) {
// It takes a common.HTTPClient as input and returns a pointer to Client and an error.
func New(httpClient common.HTTPClient) (*Client, error) {

// If no HTTP client is provided, use the default HTTP client.
if httpClient == nil {
httpClient = http.DefaultClient
}

// Parse the default API endpoint URL.
u, err := url.Parse(defaultApiEndpoint)
u, err := url.Parse(defaultAPIEndpoint)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -61,7 +62,7 @@ func New(httpClient common.HttpClient) (*Client, error) {
// Client represents a client for interacting with the Atlassian Administration API.
type Client struct {
// HTTP is the HTTP client used for making requests.
HTTP common.HttpClient
HTTP common.HTTPClient
// Site is the base URL for the API.
Site *url.URL
// Auth is the authentication service.
Expand All @@ -76,7 +77,7 @@ type Client struct {

// NewRequest creates a new HTTP request with the given context, method, URL string, content type, and body.
// It returns an HTTP request and an error.
func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, body interface{}) (*http.Request, error) {
func (c *Client) NewRequest(ctx context.Context, method, urlStr, contentType string, body interface{}) (*http.Request, error) {

// Parse the relative URL.
rel, err := url.Parse(urlStr)
Expand All @@ -103,12 +104,12 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr, type_ string, b
req.Header.Set("Accept", "application/json")

// Set the Content-Type header if a body is provided.
if body != nil && type_ == "" {
if body != nil && contentType == "" {
req.Header.Set("Content-Type", "application/json")
}

if body != nil && type_ != "" {
req.Header.Set("Content-Type", type_)
if body != nil && contentType != "" {
req.Header.Set("Content-Type", contentType)
}

// Add the Authorization header if a bearer token is available.
Expand Down
58 changes: 30 additions & 28 deletions admin/api_client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package admin
import (
"bytes"
"context"
"github.com/ctreminiom/go-atlassian/admin/internal"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service/common"
"github.com/ctreminiom/go-atlassian/service/mocks"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/url"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/ctreminiom/go-atlassian/admin/internal"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service/common"
"github.com/ctreminiom/go-atlassian/service/mocks"
)

func TestClient_Call(t *testing.T) {
Expand Down Expand Up @@ -63,7 +65,7 @@ func TestClient_Call(t *testing.T) {
}

type fields struct {
HTTP common.HttpClient
HTTP common.HTTPClient
Site *url.URL
Auth common.Authentication
}
Expand All @@ -85,7 +87,7 @@ func TestClient_Call(t *testing.T) {
name: "when the parameters are correct",
on: func(fields *fields) {

client := mocks.NewHttpClient(t)
client := mocks.NewHTTPClient(t)

client.On("Do", (*http.Request)(nil)).
Return(expectedResponse, nil)
Expand All @@ -109,7 +111,7 @@ func TestClient_Call(t *testing.T) {
name: "when the response status is a bad request",
on: func(fields *fields) {

client := mocks.NewHttpClient(t)
client := mocks.NewHTTPClient(t)

client.On("Do", (*http.Request)(nil)).
Return(badRequestResponse, nil)
Expand All @@ -134,7 +136,7 @@ func TestClient_Call(t *testing.T) {
name: "when the response status is an internal service error",
on: func(fields *fields) {

client := mocks.NewHttpClient(t)
client := mocks.NewHTTPClient(t)

client.On("Do", (*http.Request)(nil)).
Return(internalServerResponse, nil)
Expand All @@ -159,7 +161,7 @@ func TestClient_Call(t *testing.T) {
name: "when the response status is a not found",
on: func(fields *fields) {

client := mocks.NewHttpClient(t)
client := mocks.NewHTTPClient(t)

client.On("Do", (*http.Request)(nil)).
Return(notFoundResponse, nil)
Expand All @@ -184,7 +186,7 @@ func TestClient_Call(t *testing.T) {
name: "when the response status is unauthorized",
on: func(fields *fields) {

client := mocks.NewHttpClient(t)
client := mocks.NewHTTPClient(t)

client.On("Do", (*http.Request)(nil)).
Return(unauthorizedResponse, nil)
Expand Down Expand Up @@ -262,17 +264,17 @@ func TestClient_NewRequest(t *testing.T) {
requestMocked.Header.Set("Content-Type", "application/json")

type fields struct {
HTTP common.HttpClient
HTTP common.HTTPClient
Auth common.Authentication
Site *url.URL
}

type args struct {
ctx context.Context
method string
urlStr string
type_ string
body interface{}
ctx context.Context
method string
urlStr string
contentType string
body interface{}
}

testCases := []struct {
Expand All @@ -290,11 +292,11 @@ func TestClient_NewRequest(t *testing.T) {
Site: siteAsURL,
},
args: args{
ctx: context.Background(),
method: http.MethodGet,
urlStr: "rest/2/issue/attachment",
type_: "",
body: bytes.NewReader([]byte("Hello World")),
ctx: context.Background(),
method: http.MethodGet,
urlStr: "rest/2/issue/attachment",
contentType: "",
body: bytes.NewReader([]byte("Hello World")),
},
want: requestMocked,
wantErr: false,
Expand Down Expand Up @@ -348,7 +350,7 @@ func TestClient_NewRequest(t *testing.T) {
testCase.args.ctx,
testCase.args.method,
testCase.args.urlStr,
testCase.args.type_,
testCase.args.contentType,
testCase.args.body,
)

Expand All @@ -371,7 +373,7 @@ func TestClient_NewRequest(t *testing.T) {

func TestClient_processResponse(t *testing.T) {

expectedJsonResponse := `
expectedJSONResponse := `
{
"id": 4,
"self": "https://ctreminiom.atlassian.net/rest/agile/1.0/board/4",
Expand All @@ -381,15 +383,15 @@ func TestClient_processResponse(t *testing.T) {

expectedResponse := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(expectedJsonResponse)),
Body: io.NopCloser(strings.NewReader(expectedJSONResponse)),
Request: &http.Request{
Method: http.MethodGet,
URL: &url.URL{},
},
}

type fields struct {
HTTP common.HttpClient
HTTP common.HTTPClient
Site *url.URL
Authentication common.Authentication
}
Expand Down Expand Up @@ -417,7 +419,7 @@ func TestClient_processResponse(t *testing.T) {
Response: expectedResponse,
Code: http.StatusOK,
Method: http.MethodGet,
Bytes: *bytes.NewBufferString(expectedJsonResponse),
Bytes: *bytes.NewBufferString(expectedJSONResponse),
},
wantErr: false,
},
Expand Down Expand Up @@ -462,7 +464,7 @@ func TestNew(t *testing.T) {
mockClient.Auth.SetUserAgent("aaa")

type args struct {
httpClient common.HttpClient
httpClient common.HTTPClient
site string
}

Expand Down
5 changes: 3 additions & 2 deletions admin/internal/organization_directory_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package internal
import (
"context"
"fmt"
"net/http"

model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/admin"
"net/http"
)

// NewOrganizationDirectoryService creates a new instance of OrganizationDirectoryService.
Expand All @@ -31,7 +32,7 @@ type OrganizationDirectoryService struct {
//
// The added_to_org date field is available only to customers using the new user management experience.
//
// GET /admin/v1/orgs/{orgId}/directory/users/{accountId}/last-active-dates
// GET /admin/v1/orgs/{orgId}/directory/users/{accountID}/last-active-dates
//
// https://docs.go-atlassian.io/atlassian-admin-cloud/organization/directory#users-last-active-dates
func (o *OrganizationDirectoryService) Activity(ctx context.Context, organizationID, accountID string) (*model.UserProductAccessScheme, *model.ResponseScheme, error) {
Expand Down
Loading
Loading