Skip to content
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
82 changes: 82 additions & 0 deletions github/classroom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2025 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"fmt"
"net/http"
)

// ClassroomService handles communication with the GitHub Classroom related
// methods of the GitHub API.
//
// GitHub API docs: https://docs.github.com/rest/classroom/classroom
type ClassroomService service

// Classroom represents a GitHub Classroom.
type Classroom struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Archived *bool `json:"archived,omitempty"`
Organization *Organization `json:"organization,omitempty"`
URL *string `json:"url,omitempty"`
}

func (c Classroom) String() string {
return Stringify(c)
}

// ClassroomAssignment represents a GitHub Classroom assignment.
type ClassroomAssignment struct {
ID *int64 `json:"id,omitempty"`
PublicRepo *bool `json:"public_repo,omitempty"`
Title *string `json:"title,omitempty"`
Type *string `json:"type,omitempty"`
InviteLink *string `json:"invite_link,omitempty"`
InvitationsEnabled *bool `json:"invitations_enabled,omitempty"`
Slug *string `json:"slug,omitempty"`
StudentsAreRepoAdmins *bool `json:"students_are_repo_admins,omitempty"`
FeedbackPullRequestsEnabled *bool `json:"feedback_pull_requests_enabled,omitempty"`
MaxTeams *int `json:"max_teams,omitempty"`
MaxMembers *int `json:"max_members,omitempty"`
Editor *string `json:"editor,omitempty"`
Accepted *int `json:"accepted,omitempty"`
Submitted *int `json:"submitted,omitempty"`
Passing *int `json:"passing,omitempty"`
Language *string `json:"language,omitempty"`
Deadline *Timestamp `json:"deadline,omitempty"`
StarterCodeRepository *Repository `json:"starter_code_repository,omitempty"`
Classroom *Classroom `json:"classroom,omitempty"`
}

func (a ClassroomAssignment) String() string {
return Stringify(a)
}

// GetAssignment gets a GitHub Classroom assignment. Assignment will only be
// returned if the current user is an administrator of the GitHub Classroom
// for the assignment.
//
// GitHub API docs: https://docs.github.com/rest/classroom/classroom#get-an-assignment
//
//meta:operation GET /assignments/{assignment_id}
func (s *ClassroomService) GetAssignment(ctx context.Context, assignmentID int64) (*ClassroomAssignment, *Response, error) {
u := fmt.Sprintf("assignments/%v", assignmentID)

req, err := s.client.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}

assignment := new(ClassroomAssignment)
resp, err := s.client.Do(ctx, req, assignment)
if err != nil {
return nil, resp, err
}

return assignment, resp, nil
}
211 changes: 211 additions & 0 deletions github/classroom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
// Copyright 2025 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"fmt"
"net/http"
"testing"
"time"

"github.com/google/go-cmp/cmp"
)

func TestClassroom_Marshal(t *testing.T) {
t.Parallel()
testJSONMarshal(t, &Classroom{}, "{}")

c := &Classroom{
ID: Ptr(int64(1296269)),
Name: Ptr("Programming Elixir"),
Archived: Ptr(false),
Organization: &Organization{
ID: Ptr(int64(1)),
Login: Ptr("programming-elixir"),
NodeID: Ptr("MDEyOk9yZ2FuaXphdGlvbjE="),
HTMLURL: Ptr("https://github.com/programming-elixir"),
Name: Ptr("Learn how to build fault tolerant applications"),
AvatarURL: Ptr("https://avatars.githubusercontent.com/u/9919?v=4"),
},
URL: Ptr("https://classroom.github.com/classrooms/1-programming-elixir"),
}

want := `{
"id": 1296269,
"name": "Programming Elixir",
"archived": false,
"organization": {
"id": 1,
"login": "programming-elixir",
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=",
"html_url": "https://github.com/programming-elixir",
"name": "Learn how to build fault tolerant applications",
"avatar_url": "https://avatars.githubusercontent.com/u/9919?v=4"
},
"url": "https://classroom.github.com/classrooms/1-programming-elixir"
}`

testJSONMarshal(t, c, want)
}

func TestClassroomAssignment_Marshal(t *testing.T) {
t.Parallel()
testJSONMarshal(t, &ClassroomAssignment{}, "{}")

a := &ClassroomAssignment{
ID: Ptr(int64(12)),
PublicRepo: Ptr(false),
Title: Ptr("Intro to Binaries"),
Type: Ptr("individual"),
InviteLink: Ptr("https://classroom.github.com/a/Lx7jiUgx"),
InvitationsEnabled: Ptr(true),
Slug: Ptr("intro-to-binaries"),
StudentsAreRepoAdmins: Ptr(false),
FeedbackPullRequestsEnabled: Ptr(true),
MaxTeams: Ptr(0),
MaxMembers: Ptr(0),
Editor: Ptr("codespaces"),
Accepted: Ptr(100),
Submitted: Ptr(40),
Passing: Ptr(10),
Language: Ptr("ruby"),
Deadline: &Timestamp{referenceTime},
StarterCodeRepository: &Repository{
ID: Ptr(int64(1296269)),
FullName: Ptr("octocat/Hello-World"),
},
Classroom: &Classroom{
ID: Ptr(int64(1296269)),
Name: Ptr("Programming Elixir"),
},
}

want := `{
"id": 12,
"public_repo": false,
"title": "Intro to Binaries",
"type": "individual",
"invite_link": "https://classroom.github.com/a/Lx7jiUgx",
"invitations_enabled": true,
"slug": "intro-to-binaries",
"students_are_repo_admins": false,
"feedback_pull_requests_enabled": true,
"max_teams": 0,
"max_members": 0,
"editor": "codespaces",
"accepted": 100,
"submitted": 40,
"passing": 10,
"language": "ruby",
"deadline": ` + referenceTimeStr + `,
"starter_code_repository": {
"id": 1296269,
"full_name": "octocat/Hello-World"
},
"classroom": {
"id": 1296269,
"name": "Programming Elixir"
}
}`

testJSONMarshal(t, a, want)
}

func TestClassroomService_GetAssignment(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/assignments/12", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"id": 12,
"public_repo": false,
"title": "Intro to Binaries",
"type": "individual",
"invite_link": "https://classroom.github.com/a/Lx7jiUgx",
"invitations_enabled": true,
"slug": "intro-to-binaries",
"students_are_repo_admins": false,
"feedback_pull_requests_enabled": true,
"max_teams": 0,
"max_members": 0,
"editor": "codespaces",
"accepted": 100,
"submitted": 40,
"passing": 10,
"language": "ruby",
"deadline": "2011-01-26T19:06:43Z",
"starter_code_repository": {
"id": 1296269,
"full_name": "octocat/Hello-World",
"html_url": "https://github.com/octocat/Hello-World",
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"private": false,
"default_branch": "main"
},
"classroom": {
"id": 1296269,
"name": "Programming Elixir",
"archived": false,
"url": "https://classroom.github.com/classrooms/1-programming-elixir"
}
}`)
})

ctx := context.Background()
assignment, _, err := client.Classroom.GetAssignment(ctx, 12)
if err != nil {
t.Errorf("Classroom.GetAssignment returned error: %v", err)
}

want := &ClassroomAssignment{
ID: Ptr(int64(12)),
PublicRepo: Ptr(false),
Title: Ptr("Intro to Binaries"),
Type: Ptr("individual"),
InviteLink: Ptr("https://classroom.github.com/a/Lx7jiUgx"),
InvitationsEnabled: Ptr(true),
Slug: Ptr("intro-to-binaries"),
StudentsAreRepoAdmins: Ptr(false),
FeedbackPullRequestsEnabled: Ptr(true),
MaxTeams: Ptr(0),
MaxMembers: Ptr(0),
Editor: Ptr("codespaces"),
Accepted: Ptr(100),
Submitted: Ptr(40),
Passing: Ptr(10),
Language: Ptr("ruby"),
Deadline: func() *Timestamp { t, _ := time.Parse(time.RFC3339, "2011-01-26T19:06:43Z"); return &Timestamp{t} }(),
StarterCodeRepository: &Repository{
ID: Ptr(int64(1296269)),
FullName: Ptr("octocat/Hello-World"),
HTMLURL: Ptr("https://github.com/octocat/Hello-World"),
NodeID: Ptr("MDEwOlJlcG9zaXRvcnkxMjk2MjY5"),
Private: Ptr(false),
DefaultBranch: Ptr("main"),
},
Classroom: &Classroom{
ID: Ptr(int64(1296269)),
Name: Ptr("Programming Elixir"),
Archived: Ptr(false),
URL: Ptr("https://classroom.github.com/classrooms/1-programming-elixir"),
},
}

if !cmp.Equal(assignment, want) {
t.Errorf("Classroom.GetAssignment returned %+v, want %+v", assignment, want)
}

const methodName = "GetAssignment"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Classroom.GetAssignment(ctx, 12)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
Loading
Loading