Documentation
¶
Overview ¶
Package grading is a tiny HTTP client for the build/grading service.
Orchestration is presentation/authorization only for build job data: it fetches a job (with its executions and report) from the build/grading service server-to-server and renders a staff-only detail page. The build service remains the owner of job data; this client only models the subset of fields the detail page needs.
The grading/cron subpackage registers the scheduled job that notifies the build/grading service when an exercise ends; it imports this package, mirroring the internal/forgejo + internal/forgejo/cron layout.
Index ¶
- Variables
- func SanitizeAnchor(s string) string
- type BuildStep
- type Client
- func (c *Client) GetJob(id string) (Job, error)
- func (c *Client) JobStats(ctx context.Context, f JobFilter, tz string) (JobStats, error)
- func (c *Client) LatestJobs(ctx context.Context, f JobFilter) (JobList, error)
- func (c *Client) ListJobs(ctx context.Context, f JobFilter) (JobList, error)
- func (c *Client) Trigger(ctx context.Context, req TriggerRequest) (TriggerResponse, error)
- type Execution
- type GraderResult
- type Job
- type JobDayStatusCount
- type JobExerciseCount
- type JobFilter
- type JobList
- type JobStats
- type JobStatusCount
- type Report
- type TestFailure
- type TestNote
- type TestResult
- type TriggerRequest
- type TriggerResponse
- type TriggerStudentResult
Constants ¶
This section is empty.
Variables ¶
var ErrNotConfigured = fmt.Errorf("build service not configured")
ErrNotConfigured is returned when the build client has no base URL.
var ErrNotFound = fmt.Errorf("job not found")
ErrNotFound is returned when the build service responds 404 for a job.
ErrUnavailable is returned when the build service could not serve a request: the connection failed or the service answered with an unexpected status. It lets callers distinguish an upstream failure from a local one.
Functions ¶
func SanitizeAnchor ¶
SanitizeAnchor produces a stable, URL-safe anchor fragment from an arbitrary string (step name, test method name). It mirrors the sanitization used by build/grading's status package so the anchors rendered here match the #anchor in the commit-status target_url.
Types ¶
type BuildStep ¶
type BuildStep struct {
Name string `json:"name"`
Logs []string `json:"logs"`
Failure bool `json:"failure"`
}
BuildStep is one named stage of a build.
type Client ¶
type Client struct {
BaseURL string // build service base URL (e.g. "http://build:8081")
APIKey string // X-Build-Key PSK
HTTP *http.Client
}
Client calls the build/grading service API.
func NewClient ¶
NewClient returns a client. baseURL must not have a trailing slash. The returned client has no HTTP-level timeout; each call applies its own per-request deadline (see defaultRequestTimeout and Trigger's context).
func (*Client) GetJob ¶
GetJob fetches a single job (with executions and report) by ID. The call is bounded by defaultRequestTimeout so a hung build service cannot stall the staff detail page indefinitely.
func (*Client) JobStats ¶
JobStats fetches aggregate job counts for the same filters as ListJobs. tz is an IANA timezone name for the day buckets; an empty value lets the build service default to UTC. The call is bounded by defaultRequestTimeout.
func (*Client) LatestJobs ¶
LatestJobs fetches the newest job per exercise matching the filter. It is used by the student repo page to show the last build result per exercise without paging through every historical build job. The call is bounded by defaultRequestTimeout.
func (*Client) ListJobs ¶
ListJobs fetches one filtered page of jobs from the build service. The call is bounded by defaultRequestTimeout so a hung build service cannot stall the page or fragment request indefinitely. Trigger instead uses the caller's context because the grading cron needs its longer per-trigger timeout.
func (*Client) Trigger ¶
func (c *Client) Trigger(ctx context.Context, req TriggerRequest) (TriggerResponse, error)
Trigger notifies the build/grading service that an exercise has ended and the enrolled students should be graded in one shot. The service only enqueues one grade job per student (through the same per-student path as POST /api/report) and returns immediately; a worker pool in the build service runs the containers later. Duplicate notifications are deduplicated by the service via FindReportJob, and a student whose previous grade errored is re-queued for a retry, so repeated triggers are safe. Returns ErrNotConfigured when the client has no base URL (BUILD_API_URL/BUILD_API_KEY absent).
type Execution ¶
type Execution struct {
ID uint `json:"id"`
Phase string `json:"phase"`
Failure bool `json:"failure"`
Runtime string `json:"runtime"`
Result string `json:"result"` // GraderResult JSON string
Log string `json:"log"`
Created time.Time `json:"created_at"`
}
Execution is one container run within a job.
type GraderResult ¶
type GraderResult struct {
Steps []BuildStep `json:"steps"`
Failure bool `json:"failure"`
Tests *TestResult `json:"tests"`
}
GraderResult mirrors build/grading's models.GraderResult. It is parsed from Execution.Result (a JSON string) so the detail page can render steps and tests without re-implementing the build service's models.
func ParseGraderResult ¶
func ParseGraderResult(s string) GraderResult
ParseGraderResult decodes an Execution.Result JSON string into a GraderResult. Returns a zero GraderResult (no steps, no failure) when the string is empty or unparseable so the page never crashes on a malformed execution row.
type Job ¶
type Job struct {
ID uint `json:"id"`
Kind string `json:"kind"`
Status string `json:"status"`
CourseID uint `json:"course_id"`
CourseUID string `json:"course_uid"`
Student string `json:"student"`
Exercise string `json:"exercise"`
Image string `json:"image"`
Pusher string `json:"pusher"`
Commit string `json:"commit"`
Branch string `json:"branch"`
Deadline time.Time `json:"deadline"`
Message string `json:"message"`
CreatedAt time.Time `json:"created_at"`
StartedAt *time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at"`
Executions []Execution `json:"executions"`
Report *Report `json:"report"`
}
Job is the subset of build/grading's models.Job the detail page renders. Field names mirror the build service JSON tags.
type JobDayStatusCount ¶
type JobDayStatusCount struct {
Day string `json:"day"`
Status string `json:"status"`
Count int64 `json:"count"`
}
JobDayStatusCount is the number of jobs in one status on one day.
type JobExerciseCount ¶
type JobExerciseCount struct {
Exercise string `json:"exercise"`
Total int64 `json:"total"`
Failed int64 `json:"failed"`
}
JobExerciseCount is the job total for one exercise. Failed counts the failure and error statuses.
type JobFilter ¶
type JobFilter struct {
Kind string
Statuses []string
CourseUIDs []string
Students []string
Exercise string
Since string
Until string
Q string
Page int
PageSize int
SortBy string
SortDir string
}
JobFilter selects jobs for GET /api/jobs. Zero values mean "no constraint", so callers only set the fields they want to filter on. Since and Until are RFC3339 strings bounding the job creation time (Until exclusive). Callers validate them before sending, so the build service only sees well-formed values.
type JobList ¶
type JobList struct {
Items []Job `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
TotalPages int `json:"total_pages"`
HasNext bool `json:"has_next"`
HasPrev bool `json:"has_prev"`
}
JobList is the envelope returned by GET /api/jobs. It mirrors build/grading's api.jobListResponse.
type JobStats ¶
type JobStats struct {
Total int64 `json:"total"`
ByStatus []JobStatusCount `json:"by_status"`
ByDay []JobDayStatusCount `json:"by_day"`
ByExercise []JobExerciseCount `json:"by_exercise"`
DaysTruncated bool `json:"days_truncated"`
}
JobStats mirrors build/grading's aggregate job stats response. ByDay is ordered by day and status ascending and holds at most the newest 120 days; DaysTruncated reports that older days were dropped. ByExercise is ordered by total descending and holds at most 8 exercises.
type JobStatusCount ¶
JobStatusCount is the number of jobs in one status.
type Report ¶
type Report struct {
Path string `json:"path"`
SHA string `json:"sha"`
Commit string `json:"commit"`
BuildFailure bool `json:"build_failure"`
TestsFailure bool `json:"tests_failure"`
}
Report records that a final README report was written.
type TestFailure ¶
TestFailure is a failed or errored test.
type TestNote ¶
TestNote is a non-fatal advisory message from a test runner, shown to tutors in the admin job view. Mirrors build/grading's models.TestNote.
type TestResult ¶
type TestResult struct {
Run int `json:"run"`
Passed []string `json:"passed"`
Failures []TestFailure `json:"failures"`
Errors []TestFailure `json:"errors"`
Failure bool `json:"failure"`
// Notes are optional non-fatal advisory messages from the grader's
// test runner (for example a submission that reads stdin at top
// level could not be imported). They never set Failure and are
// omitted when the grader emitted none.
Notes []TestNote `json:"notes,omitempty"`
}
TestResult is the tests object within a GraderResult.
type TriggerRequest ¶
type TriggerRequest struct {
CourseID uint `json:"course_id"`
CourseUID string `json:"course_uid"`
Exercise string `json:"exercise"`
GraderImage string `json:"grader_image"`
Branch string `json:"branch"`
Students []string `json:"students"`
}
TriggerRequest is the body of POST /api/trigger, sent by orchestration's scheduled job when an exercise ends. It mirrors build/grading's api.TriggerRequest. The request is idempotent on the build service side (FindReportJob by course/student/exercise/commit), so duplicate notifications do not create duplicate grade jobs.
type TriggerResponse ¶
type TriggerResponse struct {
CourseID uint `json:"course_id"`
Exercise string `json:"exercise"`
Students []TriggerStudentResult `json:"students"`
}
TriggerResponse is the body of the 202 response from POST /api/trigger.
type TriggerStudentResult ¶
type TriggerStudentResult struct {
Student string `json:"student"`
JobID uint `json:"job_id,omitempty"`
Status string `json:"status"`
Commit string `json:"commit,omitempty"`
Idempotent bool `json:"idempotent,omitempty"`
Skipped bool `json:"skipped,omitempty"`
Message string `json:"message,omitempty"`
}
TriggerStudentResult is the per-student outcome in the trigger response. Mirrors build/grading's api.TriggerStudentResult.