X Tutup
Skip to content

Commit 95fa7f9

Browse files
committed
Merge branch 'trunk' of https://github.com/meiji163/cli into pin-ext
2 parents f350b91 + cd8d653 commit 95fa7f9

File tree

10 files changed

+353
-7
lines changed

10 files changed

+353
-7
lines changed

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
steps:
1818
- name: Check out code
19-
uses: actions/checkout@v2
19+
uses: actions/checkout@v3
2020

2121
- name: Initialize CodeQL
2222
uses: github/codeql-action/init@v1

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
go-version: 1.16
1616

1717
- name: Check out code
18-
uses: actions/checkout@v2
18+
uses: actions/checkout@v3
1919

2020
- name: Cache Go modules
2121
uses: actions/cache@v2

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
go-version: 1.16
2323

2424
- name: Check out code
25-
uses: actions/checkout@v2
25+
uses: actions/checkout@v3
2626

2727
- name: Verify dependencies
2828
run: |

.github/workflows/releases.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout
13-
uses: actions/checkout@v2
13+
uses: actions/checkout@v3
1414
- name: Set up Go 1.16
1515
uses: actions/setup-go@v2
1616
with:
@@ -44,7 +44,7 @@ jobs:
4444
GORELEASER_CURRENT_TAG: ${{steps.changelog.outputs.tag-name}}
4545
CERT_PASSWORD: ${{secrets.WINDOWS_CERT_PASSWORD}}
4646
- name: Checkout documentation site
47-
uses: actions/checkout@v2
47+
uses: actions/checkout@v3
4848
with:
4949
repository: github/cli.github.com
5050
path: site
@@ -128,7 +128,7 @@ jobs:
128128
runs-on: windows-latest
129129
steps:
130130
- name: Checkout
131-
uses: actions/checkout@v2
131+
uses: actions/checkout@v3
132132
- name: Download gh.exe
133133
id: download_exe
134134
shell: bash
@@ -188,7 +188,7 @@ jobs:
188188
env:
189189
COMMITTER_TOKEN: ${{ secrets.UPLOAD_GITHUB_TOKEN }}
190190
- name: Checkout scoop bucket
191-
uses: actions/checkout@v2
191+
uses: actions/checkout@v3
192192
with:
193193
repository: cli/scoop-gh
194194
path: scoop-gh

internal/codespaces/api/api.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ type Codespace struct {
170170
State string `json:"state"`
171171
GitStatus CodespaceGitStatus `json:"git_status"`
172172
Connection CodespaceConnection `json:"connection"`
173+
Machine CodespaceMachine `json:"machine"`
173174
}
174175

175176
type CodespaceGitStatus struct {
@@ -180,6 +181,15 @@ type CodespaceGitStatus struct {
180181
HasUncommitedChanges bool `json:"has_uncommited_changes"`
181182
}
182183

184+
type CodespaceMachine struct {
185+
Name string `json:"name"`
186+
DisplayName string `json:"display_name"`
187+
OperatingSystem string `json:"operating_system"`
188+
StorageInBytes int `json:"storage_in_bytes"`
189+
MemoryInBytes int `json:"memory_in_bytes"`
190+
CPUCount int `json:"cpus"`
191+
}
192+
183193
const (
184194
// CodespaceStateAvailable is the state for a running codespace environment.
185195
CodespaceStateAvailable = "Available"
@@ -207,6 +217,7 @@ var CodespaceFields = []string{
207217
"gitStatus",
208218
"createdAt",
209219
"lastUsedAt",
220+
"machineName",
210221
}
211222

212223
func (c *Codespace) ExportData(fields []string) map[string]interface{} {
@@ -219,6 +230,8 @@ func (c *Codespace) ExportData(fields []string) map[string]interface{} {
219230
data[f] = c.Owner.Login
220231
case "repository":
221232
data[f] = c.Repository.FullName
233+
case "machineName":
234+
data[f] = c.Machine.Name
222235
case "gitStatus":
223236
data[f] = map[string]interface{}{
224237
"ref": c.GitStatus.Ref,
@@ -265,6 +278,7 @@ func (a *API) ListCodespaces(ctx context.Context, limit int) (codespaces []*Code
265278
var response struct {
266279
Codespaces []*Codespace `json:"codespaces"`
267280
}
281+
268282
dec := json.NewDecoder(resp.Body)
269283
if err := dec.Decode(&response); err != nil {
270284
return nil, fmt.Errorf("error unmarshaling response: %w", err)
@@ -721,6 +735,48 @@ func (a *API) DeleteCodespace(ctx context.Context, codespaceName string) error {
721735
return nil
722736
}
723737

738+
type EditCodespaceParams struct {
739+
DisplayName string `json:"display_name,omitempty"`
740+
IdleTimeoutMinutes int `json:"idle_timeout_minutes,omitempty"`
741+
Machine string `json:"machine,omitempty"`
742+
}
743+
744+
func (a *API) EditCodespace(ctx context.Context, codespaceName string, params *EditCodespaceParams) (*Codespace, error) {
745+
requestBody, err := json.Marshal(params)
746+
747+
if err != nil {
748+
return nil, fmt.Errorf("error marshaling request: %w", err)
749+
}
750+
751+
req, err := http.NewRequest(http.MethodPatch, a.githubAPI+"/user/codespaces/"+codespaceName, bytes.NewBuffer(requestBody))
752+
if err != nil {
753+
return nil, fmt.Errorf("error creating request: %w", err)
754+
}
755+
756+
a.setHeaders(req)
757+
resp, err := a.do(ctx, req, "/user/codespaces")
758+
if err != nil {
759+
return nil, fmt.Errorf("error making request: %w", err)
760+
}
761+
defer resp.Body.Close()
762+
763+
if resp.StatusCode != http.StatusOK {
764+
return nil, api.HandleHTTPError(resp)
765+
}
766+
767+
b, err := ioutil.ReadAll(resp.Body)
768+
if err != nil {
769+
return nil, fmt.Errorf("error reading response body: %w", err)
770+
}
771+
772+
var response Codespace
773+
if err := json.Unmarshal(b, &response); err != nil {
774+
return nil, fmt.Errorf("error unmarshaling response: %w", err)
775+
}
776+
777+
return &response, nil
778+
}
779+
724780
type getCodespaceRepositoryContentsResponse struct {
725781
Content string `json:"content"`
726782
}

internal/codespaces/api/api_test.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"net/http/httptest"
9+
"reflect"
910
"strconv"
1011
"testing"
1112
)
@@ -265,3 +266,171 @@ func TestRetries(t *testing.T) {
265266
t.Fatalf("expected codespace name to be %q but got %q", csName, cs.Name)
266267
}
267268
}
269+
270+
func TestCodespace_ExportData(t *testing.T) {
271+
type fields struct {
272+
Name string
273+
CreatedAt string
274+
DisplayName string
275+
LastUsedAt string
276+
Owner User
277+
Repository Repository
278+
State string
279+
GitStatus CodespaceGitStatus
280+
Connection CodespaceConnection
281+
Machine CodespaceMachine
282+
}
283+
type args struct {
284+
fields []string
285+
}
286+
tests := []struct {
287+
name string
288+
fields fields
289+
args args
290+
want map[string]interface{}
291+
}{
292+
{
293+
name: "just name",
294+
fields: fields{
295+
Name: "test",
296+
},
297+
args: args{
298+
fields: []string{"name"},
299+
},
300+
want: map[string]interface{}{
301+
"name": "test",
302+
},
303+
},
304+
{
305+
name: "just owner",
306+
fields: fields{
307+
Owner: User{
308+
Login: "test",
309+
},
310+
},
311+
args: args{
312+
fields: []string{"owner"},
313+
},
314+
want: map[string]interface{}{
315+
"owner": "test",
316+
},
317+
},
318+
{
319+
name: "just machine",
320+
fields: fields{
321+
Machine: CodespaceMachine{
322+
Name: "test",
323+
},
324+
},
325+
args: args{
326+
fields: []string{"machineName"},
327+
},
328+
want: map[string]interface{}{
329+
"machineName": "test",
330+
},
331+
},
332+
}
333+
for _, tt := range tests {
334+
t.Run(tt.name, func(t *testing.T) {
335+
c := &Codespace{
336+
Name: tt.fields.Name,
337+
CreatedAt: tt.fields.CreatedAt,
338+
DisplayName: tt.fields.DisplayName,
339+
LastUsedAt: tt.fields.LastUsedAt,
340+
Owner: tt.fields.Owner,
341+
Repository: tt.fields.Repository,
342+
State: tt.fields.State,
343+
GitStatus: tt.fields.GitStatus,
344+
Connection: tt.fields.Connection,
345+
Machine: tt.fields.Machine,
346+
}
347+
if got := c.ExportData(tt.args.fields); !reflect.DeepEqual(got, tt.want) {
348+
t.Errorf("Codespace.ExportData() = %v, want %v", got, tt.want)
349+
}
350+
})
351+
}
352+
}
353+
354+
func createFakeEditServer(t *testing.T, codespaceName string) *httptest.Server {
355+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
356+
checkPath := "/user/codespaces/" + codespaceName
357+
358+
if r.URL.Path != checkPath {
359+
t.Fatal("Incorrect path")
360+
}
361+
362+
if r.Method != http.MethodPatch {
363+
t.Fatal("Incorrect method")
364+
}
365+
366+
body := r.Body
367+
if body == nil {
368+
t.Fatal("No body")
369+
}
370+
defer body.Close()
371+
372+
var data map[string]interface{}
373+
err := json.NewDecoder(body).Decode(&data)
374+
375+
if err != nil {
376+
t.Fatal(err)
377+
}
378+
379+
if data["display_name"] != "changeTo" {
380+
t.Fatal("Incorrect display name")
381+
}
382+
383+
response := Codespace{
384+
DisplayName: "changeTo",
385+
}
386+
387+
responseData, _ := json.Marshal(response)
388+
fmt.Fprint(w, string(responseData))
389+
}))
390+
}
391+
func TestAPI_EditCodespace(t *testing.T) {
392+
type args struct {
393+
ctx context.Context
394+
codespaceName string
395+
params *EditCodespaceParams
396+
}
397+
tests := []struct {
398+
name string
399+
args args
400+
want *Codespace
401+
wantErr bool
402+
}{
403+
{
404+
name: "success",
405+
args: args{
406+
ctx: context.Background(),
407+
codespaceName: "test",
408+
params: &EditCodespaceParams{
409+
DisplayName: "changeTo",
410+
},
411+
},
412+
want: &Codespace{
413+
DisplayName: "changeTo",
414+
},
415+
},
416+
}
417+
for _, tt := range tests {
418+
t.Run(tt.name, func(t *testing.T) {
419+
svr := createFakeEditServer(t, tt.args.codespaceName)
420+
defer svr.Close()
421+
422+
a := &API{
423+
client: &http.Client{},
424+
githubAPI: svr.URL,
425+
}
426+
got, err := a.EditCodespace(tt.args.ctx, tt.args.codespaceName, tt.args.params)
427+
if (err != nil) != tt.wantErr {
428+
t.Errorf("API.EditCodespace() error = %v, wantErr %v", err, tt.wantErr)
429+
return
430+
}
431+
if !reflect.DeepEqual(got, tt.want) {
432+
t.Errorf("API.EditCodespace() = %v, want %v", got.DisplayName, tt.want.DisplayName)
433+
}
434+
})
435+
}
436+
}

pkg/cmd/codespace/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type apiClient interface {
6868
StartCodespace(ctx context.Context, name string) error
6969
StopCodespace(ctx context.Context, name string) error
7070
CreateCodespace(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error)
71+
EditCodespace(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error)
7172
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)
7273
AuthorizedKeys(ctx context.Context, user string) ([]byte, error)
7374
GetCodespaceRegionLocation(ctx context.Context) (string, error)

0 commit comments

Comments
 (0)
X Tutup