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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ sudo sh install.sh --binary ./runplane-linux-<arch> --token rp_srv_... --primary

8. Push the change.

runplane can also make the change for you. On the **First job** page or on the **GitHub** tab of **Settings**, select a repository, then select **Open pull request**. The pull request sets `runs-on` to the label for each job on `ubuntu-latest` or `ubuntu-24.04`. If the GitHub App is from an earlier release, add read and write access to Contents, Pull requests, and Workflows in the app settings on GitHub first.

The job shows on the **Jobs** page of the dashboard. If you lose the setup code, run `sudo runplane setup-code` on the server.

## The two modes
Expand Down
7 changes: 7 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ type GitHub interface {
Sync(ctx context.Context) error
// Disconnect stops the listeners, deletes the scale sets, and removes the app.
Disconnect(ctx context.Context) error
// OpenPullRequest opens a pull request in the repository (owner/name)
// that sets runs-on to label in each job on a GitHub-hosted Ubuntu
// runner. While the pull request of an earlier call is open, it returns
// that one.
OpenPullRequest(ctx context.Context, base, repository, label string) (number int, url string, err error)
}

type Servers interface {
Expand Down Expand Up @@ -273,6 +278,8 @@ func (h *Handler) routes() {
handle(h, public, "GET /api/github/callback", "github-callback", "Finish the manifest flow (browser redirect from GitHub)", h.githubCallback, redirectResponse)
handle(h, public, "GET /api/github/setup", "github-setup", "Record an installation (browser redirect from GitHub)", h.githubSetup, redirectResponse)
handle(h, auth.ActionAdmin, "POST /api/github/sync", "sync-github", "Sync the installations now", h.syncGitHub)
handle(h, auth.ActionAdmin, "POST /api/github/pull-requests", "create-github-pull-request", "Open a pull request that moves jobs to a runplane label", h.createPullRequest,
describe("The pull request sets runs-on to the label for each job on ubuntu-latest or ubuntu-24.04. While an earlier pull request of runplane is open in the repository, the response gives that one."))
handle(h, auth.ActionAdmin, "DELETE /api/github", "disconnect-github", "Disconnect GitHub", h.disconnectGitHub)

handle(h, auth.ActionRead, "GET /api/pools", "list-pools", "List the pools with live sandbox counts", h.listPools)
Expand Down
35 changes: 35 additions & 0 deletions internal/api/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"net/url"
"slices"
"strconv"
"time"

Expand Down Expand Up @@ -319,6 +320,40 @@ func (h *Handler) syncGitHub(ctx context.Context, _ *struct{}) (*body[GitHubStat
return reply(h.gitHubStatus(ctx))
}

type CreatePullRequestRequest struct {
Repository string `json:"repository" maxLength:"140" doc:"owner/name of a repository that the GitHub App can access."`
Label string `json:"label" pattern:"^[a-z0-9][a-z0-9-]{0,62}$" patternDescription:"lowercase letters, digits, and dashes" doc:"The label of an enabled pool."`
}

type PullRequest struct {
Number int `json:"number"`
HTMLURL string `json:"html_url" doc:"Page of the pull request on GitHub."`
}

func (h *Handler) createPullRequest(ctx context.Context, in *body[CreatePullRequestRequest]) (*body[PullRequest], error) {
b := in.Body
if h.d.GitHub == nil {
return nil, unavailable("The GitHub connection")
}
pools, err := h.d.Store.ListPools(ctx)
if err != nil {
return nil, err
}
if !slices.ContainsFunc(pools, func(p store.Pool) bool { return p.Enabled && p.Label == b.Label }) {
return nil, invalid("body.label", "No enabled pool has this label.", b.Label)
}
base, err := h.baseURL(ctx)
if err != nil {
return nil, err
}
number, htmlURL, err := h.d.GitHub.OpenPullRequest(ctx, base, b.Repository, b.Label)
if err != nil {
return nil, h.serviceErr(err)
}
h.audit(ctx, "github.pull_request.create", b.Repository, map[string]any{"label": b.Label, "number": number})
return &body[PullRequest]{PullRequest{Number: number, HTMLURL: htmlURL}}, nil
}

func (h *Handler) disconnectGitHub(ctx context.Context, _ *struct{}) (*empty, error) {
if h.d.GitHub == nil {
return nil, unavailable("The GitHub connection")
Expand Down
38 changes: 38 additions & 0 deletions internal/api/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"encoding/json"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -43,6 +44,43 @@ func TestGitHubLinksFollowTheAppHost(t *testing.T) {
}
}

// fakeGitHub opens pull requests. The tests use no other GitHub method.
type fakeGitHub struct {
GitHub
opened []string // "repository label" of each call
}

func (f *fakeGitHub) OpenPullRequest(_ context.Context, _, repository, label string) (int, string, error) {
f.opened = append(f.opened, repository+" "+label)
return 12, "https://github.com/" + repository + "/pull/12", nil
}

// A pull request must move jobs to a label that an enabled pool serves, or
// the jobs never start. Only an admin can write to repositories.
func TestCreatePullRequest(t *testing.T) {
gh := &fakeGitHub{}
e := newEnv(t, func(d *Deps) { d.GitHub = gh })
now := time.Now()
for _, p := range []store.Pool{
{ID: "p1", Name: "big", Label: "big", Arch: "x64", VCPU: 4, MemoryMB: 8192, DiskGB: 64, MaxRunners: 2, Enabled: true, CreatedAt: now, UpdatedAt: now},
{ID: "p2", Name: "off", Label: "off", Arch: "x64", VCPU: 2, MemoryMB: 4096, DiskGB: 64, MaxRunners: 2, CreatedAt: now, UpdatedAt: now},
} {
must(t, e.st.CreatePool(context.Background(), p))
}
const path = "/api/github/pull-requests"
e.login("bob@example.com", auth.RoleMember).do("POST", path, map[string]any{"repository": "acme/web", "label": "big"}, nil).want(t, 403)
admin := e.login("ada@example.com", auth.RoleAdmin)
for _, label := range []string{"off", "gone"} {
admin.do("POST", path, map[string]any{"repository": "acme/web", "label": label}, nil).wantField(t, 422, "body.label")
}

var pr PullRequest
admin.do("POST", path, map[string]any{"repository": "acme/web", "label": "big"}, &pr).want(t, 200)
if pr.Number != 12 || pr.HTMLURL != "https://github.com/acme/web/pull/12" || !slices.Equal(gh.opened, []string{"acme/web big"}) {
t.Errorf("pull request = %+v after the calls %q, want number 12 from one call for acme/web with big", pr, gh.opened)
}
}

func TestGitHubAndBackupEvents(t *testing.T) {
e := newEnv(t)
member := e.login("bob@example.com", auth.RoleMember).events(t)
Expand Down
6 changes: 5 additions & 1 deletion internal/github/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ func (s *Service) Manifest(ctx context.Context, base, accountType, organization

// permissions are the GitHub App permissions of an account type. An
// organization uses organization runners; a personal account can only
// use repository runners, which need administration.
// use repository runners, which need administration. The pullPermissions
// let runplane open the pull request that moves jobs to its labels.
func permissions(accountType string) map[string]string {
p := map[string]string{"actions": "write", "metadata": "read"}
for _, name := range pullPermissions {
p[name] = "write"
}
if accountType == "organization" {
p["organization_self_hosted_runners"] = "write"
} else {
Expand Down
6 changes: 4 additions & 2 deletions internal/github/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ func TestManifest(t *testing.T) {
wantPermissions map[string]any
}{
{"organization", "acme", "/organizations/acme/settings/apps/new",
map[string]any{"organization_self_hosted_runners": "write", "actions": "write", "metadata": "read"}},
map[string]any{"organization_self_hosted_runners": "write", "actions": "write", "metadata": "read",
"contents": "write", "pull_requests": "write", "workflows": "write"}},
{"user", "", "/settings/apps/new",
map[string]any{"administration": "write", "actions": "write", "metadata": "read"}},
map[string]any{"administration": "write", "actions": "write", "metadata": "read",
"contents": "write", "pull_requests": "write", "workflows": "write"}},
}
for _, tt := range tests {
t.Run(tt.accountType, func(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions internal/github/installations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ type account struct {
}

type installation struct {
ID int64 `json:"id"`
AppID int64 `json:"app_id"`
Account account `json:"account"`
RepositorySelection string `json:"repository_selection"`
SuspendedAt *time.Time `json:"suspended_at"`
CreatedAt time.Time `json:"created_at"`
ID int64 `json:"id"`
AppID int64 `json:"app_id"`
Account account `json:"account"`
RepositorySelection string `json:"repository_selection"`
Permissions map[string]string `json:"permissions"` // that the account accepted, as "contents": "write"
SuspendedAt *time.Time `json:"suspended_at"`
CreatedAt time.Time `json:"created_at"`
}

// Setup records the installation that GitHub sends the browser back with,
Expand Down
Loading
Loading