Skip to content

Commit

Permalink
Merge branch 'issue-pr-create-metadata' into issue-pr-create-metadata…
Browse files Browse the repository at this point in the history
…-wizard
  • Loading branch information
mislav committed May 8, 2020
2 parents 0bf4f16 + d0f168f commit 72e99e9
Show file tree
Hide file tree
Showing 25 changed files with 1,199 additions and 307 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Code Scanning

on:
push:
schedule:
- cron: "0 0 * * 0"

jobs:
CodeQL-Build:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: go

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
8 changes: 5 additions & 3 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io/ioutil"
"reflect"
"testing"

"github.com/cli/cli/pkg/httpmock"
)

func eq(t *testing.T, got interface{}, expected interface{}) {
Expand All @@ -15,7 +17,7 @@ func eq(t *testing.T, got interface{}, expected interface{}) {
}

func TestGraphQL(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(
ReplaceTripper(http),
AddHeader("Authorization", "token OTOKEN"),
Expand All @@ -40,7 +42,7 @@ func TestGraphQL(t *testing.T) {
}

func TestGraphQLError(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))

response := struct{}{}
Expand All @@ -52,7 +54,7 @@ func TestGraphQLError(t *testing.T) {
}

func TestRESTGetDelete(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}

client := NewClient(
ReplaceTripper(http),
Expand Down
101 changes: 0 additions & 101 deletions api/fake_http.go

This file was deleted.

3 changes: 2 additions & 1 deletion api/queries_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"testing"

"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/httpmock"
)

func TestIssueList(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))

http.StubResponse(200, bytes.NewBufferString(`
Expand Down
86 changes: 86 additions & 0 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package api

import (
"context"
"fmt"
"strings"
"time"

"github.com/shurcooL/githubv4"

"github.com/cli/cli/internal/ghrepo"
)

type PullRequestReviewState int

const (
ReviewApprove PullRequestReviewState = iota
ReviewRequestChanges
ReviewComment
)

type PullRequestReviewInput struct {
Body string
State PullRequestReviewState
}

type PullRequestsPayload struct {
ViewerCreated PullRequestAndTotalCount
ReviewRequested PullRequestAndTotalCount
Expand All @@ -24,6 +40,7 @@ type PullRequest struct {
Number int
Title string
State string
Closed bool
URL string
BaseRefName string
HeadRefName string
Expand Down Expand Up @@ -345,10 +362,12 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
query($owner: String!, $repo: String!, $pr_number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr_number) {
id
url
number
title
state
closed
body
author {
login
Expand Down Expand Up @@ -451,6 +470,7 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
repository(owner: $owner, name: $repo) {
pullRequests(headRefName: $headRefName, states: OPEN, first: 30) {
nodes {
id
number
title
state
Expand Down Expand Up @@ -654,6 +674,34 @@ func isBlank(v interface{}) bool {
}
}

func AddReview(client *Client, pr *PullRequest, input *PullRequestReviewInput) error {
var mutation struct {
AddPullRequestReview struct {
ClientMutationID string
} `graphql:"addPullRequestReview(input:$input)"`
}

state := githubv4.PullRequestReviewEventComment
switch input.State {
case ReviewApprove:
state = githubv4.PullRequestReviewEventApprove
case ReviewRequestChanges:
state = githubv4.PullRequestReviewEventRequestChanges
}

body := githubv4.String(input.Body)

gqlInput := githubv4.AddPullRequestReviewInput{
PullRequestID: pr.ID,
Event: &state,
Body: &body,
}

v4 := githubv4.NewClient(client.http)

return v4.Mutate(context.Background(), &mutation, gqlInput, nil)
}

func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) {
type prBlock struct {
Edges []struct {
Expand Down Expand Up @@ -822,6 +870,44 @@ loop:
return &res, nil
}

func PullRequestClose(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
var mutation struct {
ClosePullRequest struct {
PullRequest struct {
ID githubv4.ID
}
} `graphql:"closePullRequest(input: $input)"`
}

input := githubv4.ClosePullRequestInput{
PullRequestID: pr.ID,
}

v4 := githubv4.NewClient(client.http)
err := v4.Mutate(context.Background(), &mutation, input, nil)

return err
}

func PullRequestReopen(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
var mutation struct {
ReopenPullRequest struct {
PullRequest struct {
ID githubv4.ID
}
} `graphql:"reopenPullRequest(input: $input)"`
}

input := githubv4.ReopenPullRequestInput{
PullRequestID: pr.ID,
}

v4 := githubv4.NewClient(client.http)
err := v4.Mutate(context.Background(), &mutation, input, nil)

return err
}

func min(a, b int) int {
if a < b {
return a
Expand Down
4 changes: 3 additions & 1 deletion api/queries_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/cli/cli/pkg/httpmock"
)

func Test_RepoCreate(t *testing.T) {
http := &FakeHTTP{}
http := &httpmock.Registry{}
client := NewClient(ReplaceTripper(http))

http.StubResponse(200, bytes.NewBufferString(`{}`))
Expand Down
10 changes: 5 additions & 5 deletions command/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestCompletion_bash(t *testing.T) {
output, err := RunCommand(completionCmd, `completion`)
output, err := RunCommand(`completion`)
if err != nil {
t.Fatal(err)
}
Expand All @@ -17,7 +17,7 @@ func TestCompletion_bash(t *testing.T) {
}

func TestCompletion_zsh(t *testing.T) {
output, err := RunCommand(completionCmd, `completion -s zsh`)
output, err := RunCommand(`completion -s zsh`)
if err != nil {
t.Fatal(err)
}
Expand All @@ -28,7 +28,7 @@ func TestCompletion_zsh(t *testing.T) {
}

func TestCompletion_fish(t *testing.T) {
output, err := RunCommand(completionCmd, `completion -s fish`)
output, err := RunCommand(`completion -s fish`)
if err != nil {
t.Fatal(err)
}
Expand All @@ -39,7 +39,7 @@ func TestCompletion_fish(t *testing.T) {
}

func TestCompletion_powerShell(t *testing.T) {
output, err := RunCommand(completionCmd, `completion -s powershell`)
output, err := RunCommand(`completion -s powershell`)
if err != nil {
t.Fatal(err)
}
Expand All @@ -50,7 +50,7 @@ func TestCompletion_powerShell(t *testing.T) {
}

func TestCompletion_unsupported(t *testing.T) {
_, err := RunCommand(completionCmd, `completion -s csh`)
_, err := RunCommand(`completion -s csh`)
if err == nil || err.Error() != `unsupported shell type "csh"` {
t.Fatal(err)
}
Expand Down
Loading

0 comments on commit 72e99e9

Please sign in to comment.