Skip to content

Commit

Permalink
feat: add GHA token setting (#4008)
Browse files Browse the repository at this point in the history
Adds a new hidden command `earthly github add` that lets the user create
a new [GHA-Satellites
integration](https://docs.google.com/document/d/1Hl42AT8OjRTw2f9Yv0J757_GewLrOk8RVZsBsoxYjbY/edit#heading=h.5imekt3ni58h)
(experimental)

---------

Co-authored-by: idodod <[email protected]>
  • Loading branch information
idelvall and idodod authored Apr 11, 2024
1 parent 5b5b8fb commit 3c44abe
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cloud/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cloud

import (
"context"
"fmt"

pb "github.com/earthly/cloud-api/compute"
)

func (c *Client) SetGithubToken(ctx context.Context, orgName string, ghOrg string, ghRepo string, token string) error {
orgID, err := c.GetOrgID(ctx, orgName)
if err != nil {
return fmt.Errorf("failed getting org ID: %w", err)
}
_, err = c.compute.SetGithubToken(c.withAuth(ctx), &pb.SetGithubTokenRequest{
OrgId: orgID,
GithubOrgName: ghOrg,
GithubRepoName: ghRepo,
GithubToken: token,
})
if err != nil {
return fmt.Errorf("failed setting Github token: %w", err)
}
return nil
}
1 change: 1 addition & 0 deletions cmd/earthly/subcmd/account_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ func promptHiddenText(hiddenTextPrompt string) (string, error) {
fmt.Println("")
return string(password), nil
}

func promptPassword() (string, error) {
return promptHiddenText("password")
}
Expand Down
105 changes: 105 additions & 0 deletions cmd/earthly/subcmd/github_cmds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package subcmd

import (
"fmt"
"os/signal"
"syscall"

"github.com/urfave/cli/v2"

"github.com/earthly/earthly/cmd/earthly/helper"
)

type Github struct {
cli CLI

Org string
GHOrg string
GHRepo string
GHToken string
}

func NewGithub(cli CLI) *Github {
return &Github{
cli: cli,
}
}

func (a *Github) Cmds() []*cli.Command {
return []*cli.Command{
{
Name: "github",
Usage: "*experimental* Manage GitHub integration",
Description: "*experimental* Manage GitHub integration.",
Hidden: true,
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "Add GHA integration",
Description: `This command sets the configuration to create a new Github-Earthly integration, to trigger satellite builds from GHA (GitHub Actions).
From the Github side, integration can be done at two levels: organization-wide and per repository.
The provided token must have enough permissions to register webhooks and to create Github self hosted runners in those two scenarios.`,
UsageText: "earthly github add --org <org> [--repo <repo>] --token <token>",
Action: a.actionAdd,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "org",
Usage: "The name of the Earthly organization to set an integration with. Defaults to selected organization",
Destination: &a.Org,
},
&cli.StringFlag{
Name: "gh-org",
Usage: "The name of the GitHub organization to set an integration with",
Required: true,
Destination: &a.GHOrg,
},
&cli.StringFlag{
Name: "gh-repo",
Usage: "The name of the GitHub repository to set an integration with",
Destination: &a.GHRepo,
},
&cli.StringFlag{
Name: "gh-token",
Usage: "The GitHub token used for the integration",
Destination: &a.GHToken,
},
},
},
},
},
}
}

func (a *Github) actionAdd(cliCtx *cli.Context) error {
a.cli.SetCommandName("githubAdd")
if a.Org == "" {
if a.cli.OrgName() == "" {
return fmt.Errorf("coudn't determine Earthly organization")
}
a.Org = a.cli.OrgName()
}
if a.GHToken == "" {
// Our signal handling under main() doesn't cause reading from stdin to cancel
// as there's no way to pass app.ctx to stdin read calls.
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
token, err := promptToken()
if err != nil {
return err
}
a.GHToken = token
}
cloudClient, err := helper.NewCloudClient(a.cli)
if err != nil {
return err
}
err = cloudClient.SetGithubToken(cliCtx.Context, a.cli.OrgName(), a.GHOrg, a.GHRepo, a.GHToken)
if err != nil {
return fmt.Errorf("error found running github add: %w", err)
}
a.cli.Console().Printf("GitHub integration successfully created")
return nil
}

func promptToken() (string, error) {
return promptHiddenText("Enter GH token")
}
1 change: 1 addition & 0 deletions cmd/earthly/subcmd/root_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (a *Root) Cmds() []*cli.Command {
NewSecret(a.cli).Cmds(),
NewWeb(a.cli).Cmds(),
NewBilling(a.cli).Cmds(),
NewGithub(a.cli).Cmds(),
})

return cmds
Expand Down
2 changes: 2 additions & 0 deletions tests/autocompletion/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ web " > expected

test-hidden-root-commands:
ENV EARTHLY_SHOW_HIDDEN=1
RUN echo "no-cache"
RUN echo "account
billing
bootstrap
Expand All @@ -33,6 +34,7 @@ debug
doc
docker-build
docker2earthly
github
init
ls
org
Expand Down

0 comments on commit 3c44abe

Please sign in to comment.