Skip to content

Commit

Permalink
Merge branch 'trunk' into unix-socket
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed May 9, 2022
2 parents b3f8b4f + ff0ff65 commit 4ce1d83
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 437 deletions.
78 changes: 18 additions & 60 deletions .github/workflows/codeql-analysis.yml
@@ -1,70 +1,28 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

name: Code Scanning
on:
push:
branches: [ trunk ]
branches: [trunk]
pull_request:
# The branches below must be a subset of the branches above
branches: [ trunk ]
branches: [trunk]
schedule:
- cron: '41 10 * * 2'

- cron: '0 0 * * 0'
permissions:
actions: read
contents: read
security-events: write
jobs:
analyze:
name: Analyze
CodeQL-Build:
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
- name: Checkout repository
uses: actions/checkout@v3

#- run: |
# make bootstrap
# make release
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: go
queries: security-and-quality

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
34 changes: 31 additions & 3 deletions .github/workflows/lint.yml
@@ -1,6 +1,29 @@
name: Lint
on: [push, pull_request]
permissions:
contents: read
jobs:
go-mod:
strategy:
matrix:
os: [ubuntu-latest]
go: [1.16]
runs-on: ${{ matrix.os }}

steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}

- name: Checkout repository
uses: actions/checkout@v3

- name: Check go mod
run: |
go mod tidy
git diff --exit-code go.mod
lint:
strategy:
matrix:
Expand All @@ -9,10 +32,15 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}

- name: Checkout repository
uses: actions/checkout@v3

- name: Lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3.1.0
with:
version: latest
8 changes: 5 additions & 3 deletions .github/workflows/test.yml
@@ -1,5 +1,7 @@
name: Test
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
strategy:
Expand All @@ -11,12 +13,12 @@ jobs:

steps:
- name: Set up Go
uses: actions/setup-go@v1
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}

- name: Checkout code
uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v3

- name: Run tests
run: go test -v ./...
60 changes: 35 additions & 25 deletions gh_test.go
Expand Up @@ -3,12 +3,13 @@ package gh
import (
"fmt"
"os"
"strings"
"testing"

"github.com/cli/go-gh/internal/config"
"github.com/cli/go-gh/internal/httpmock"
"github.com/cli/go-gh/pkg/api"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)

func TestHelperProcess(t *testing.T) {
Expand Down Expand Up @@ -47,6 +48,7 @@ func TestRunError(t *testing.T) {
}

func TestRESTClient(t *testing.T) {
t.Cleanup(gock.Off)
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
Expand All @@ -57,24 +59,24 @@ func TestRESTClient(t *testing.T) {
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.REST("GET", "some/test/path"),
httpmock.StatusStringResponse(200, `{"message": "success"}`),
)
gock.New("https://api.github.com").
Get("/some/test/path").
MatchHeader("Authorization", "token GH_TOKEN").
Reply(200).
JSON(`{"message": "success"}`)

client, err := RESTClient(&api.ClientOptions{Transport: http})
client, err := RESTClient(nil)
assert.NoError(t, err)

res := struct{ Message string }{}
err = client.Do("GET", "some/test/path", nil, &res)
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, "success", res.Message)
assert.Equal(t, "api.github.com", http.Requests[0].URL.Hostname())
assert.Equal(t, "token GH_TOKEN", http.Requests[0].Header.Get("Authorization"))
}

func TestGQLClient(t *testing.T) {
t.Cleanup(gock.Off)
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
Expand All @@ -85,25 +87,26 @@ func TestGQLClient(t *testing.T) {
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.GQL("QUERY"),
httpmock.StringResponse(`{"data":{"viewer":{"login":"hubot"}}}`),
)
gock.New("https://api.github.com").
Post("/graphql").
MatchHeader("Authorization", "token GH_TOKEN").
BodyString(`{"query":"QUERY","variables":{"var":"test"}}`).
Reply(200).
JSON(`{"data":{"viewer":{"login":"hubot"}}}`)

client, err := GQLClient(&api.ClientOptions{Transport: http})
client, err := GQLClient(nil)
assert.NoError(t, err)

vars := map[string]interface{}{"var": "test"}
res := struct{ Viewer struct{ Login string } }{}
err = client.Do("QUERY", vars, &res)
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, "hubot", res.Viewer.Login)
assert.Equal(t, "api.github.com", http.Requests[0].URL.Hostname())
assert.Equal(t, "token GH_TOKEN", http.Requests[0].Header.Get("Authorization"))
}

func TestHTTPClient(t *testing.T) {
t.Cleanup(gock.Off)
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
Expand All @@ -114,20 +117,19 @@ func TestHTTPClient(t *testing.T) {
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.REST("GET", "some/test/path"),
httpmock.StatusStringResponse(200, `{"message": "success"}`),
)
gock.New("https://api.github.com").
Get("/some/test/path").
MatchHeader("Authorization", "token GH_TOKEN").
Reply(200).
JSON(`{"message": "success"}`)

client, err := HTTPClient(&api.ClientOptions{Transport: http})
client, err := HTTPClient(nil)
assert.NoError(t, err)

res, err := client.Get("https://api.github.com/some/test/path")
assert.NoError(t, err)
assert.True(t, gock.IsDone(), printPendingMocks(gock.Pending()))
assert.Equal(t, 200, res.StatusCode)
assert.Equal(t, "api.github.com", http.Requests[0].URL.Hostname())
assert.Equal(t, "token GH_TOKEN", http.Requests[0].Header.Get("Authorization"))
}

func TestResolveOptions(t *testing.T) {
Expand Down Expand Up @@ -183,3 +185,11 @@ http_unix_socket: socket
cfg, _ := config.FromString(data)
return cfg
}

func printPendingMocks(mocks []gock.Mock) string {
paths := []string{}
for _, mock := range mocks {
paths = append(paths, mock.Request().URLStruct.String())
}
return fmt.Sprintf("%d unmatched mocks: %s", len(paths), strings.Join(paths, ", "))
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -10,5 +10,6 @@ require (
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.7.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/h2non/gock.v1 v1.1.2
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -6,13 +6,17 @@ github.com/cli/shurcooL-graphql v0.0.1 h1:/9J3t9O6p1B8zdBBtQighq5g7DQRItBwuwGh3S
github.com/cli/shurcooL-graphql v0.0.1/go.mod h1:U7gCSuMZP/Qy7kbqkk5PrqXEeDgtfG5K+W+u8weorps=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -28,6 +32,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 4ce1d83

Please sign in to comment.