Skip to content

Commit

Permalink
feat: update vcs provider token (#594)
Browse files Browse the repository at this point in the history
Fixes #576
  • Loading branch information
leg100 authored Sep 11, 2023
1 parent 39318f1 commit 29a0be6
Show file tree
Hide file tree
Showing 26 changed files with 410 additions and 122 deletions.
3 changes: 3 additions & 0 deletions internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ var (
// ErrInvalidName is returned when the name option has invalid value.
ErrInvalidName = errors.New("invalid value for name")

// ErrEmptyValue is returned when a value is set to an empty string
ErrEmptyValue = errors.New("value cannot be empty")

// ErrUploadTooLarge is returned when a user attempts to upload data that
// is too large.
ErrUploadTooLarge = errors.New("upload is too large")
Expand Down
16 changes: 4 additions & 12 deletions internal/http/html/static/css/output.css
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,10 @@ th, td {
width: 100%;
}

.w-20 {
width: 5rem;
}

.max-w-2xl {
max-width: 42rem;
}
Expand Down Expand Up @@ -1113,18 +1117,6 @@ th, td {
border-color: rgb(15 23 42 / var(--tw-border-opacity));
}

.border-x-black {
--tw-border-opacity: 1;
border-left-color: rgb(0 0 0 / var(--tw-border-opacity));
border-right-color: rgb(0 0 0 / var(--tw-border-opacity));
}

.border-y-black {
--tw-border-opacity: 1;
border-top-color: rgb(0 0 0 / var(--tw-border-opacity));
border-bottom-color: rgb(0 0 0 / var(--tw-border-opacity));
}

.bg-black {
--tw-bg-opacity: 1;
background-color: rgb(0 0 0 / var(--tw-bg-opacity));
Expand Down
20 changes: 20 additions & 0 deletions internal/http/html/static/templates/content/vcs_provider_edit.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ template "layout" . }}

{{ define "content-header-title" }}
<a href="{{ vcsProvidersPath .Organization }}">vcs providers</a>
/
{{ .VCSProvider.String }}
/
settings
{{ end }}

{{ define "content" }}
{{ template "vcs_provider_form" . }}
<hr class="my-4">
<h3 class="font-semibold text-lg mb-2">Advanced</h3>
<form action="{{ deleteVCSProviderPath .VCSProvider.ID }}" method="POST">
<button id="delete-vcs-provider-button" class="btn-danger" onclick="return confirm('Are you sure you want to delete?')">
Delete VCS provider
</button>
</form>
{{ end }}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
{{ define "content" }}
<div>
Create a Github VCS provider with a <a class="underline" href="https://github.com/settings/tokens">personal token</a> with the <span class="bg-gray-200">repo</span> scope.
Enter the token below along with a descriptive name.
</div>

{{ template "create_vcs_provider_form" . }}
{{ template "vcs_provider_form" . }}
{{ end }}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{{ define "content" }}
<div>
Create a Gitlab VCS provider using a <a class="underline" href="https://gitlab.com/-/profile/personal_access_tokens">personal token</a> with the <span class="bg-gray-200">api</span> scope.
Enter the token below along with a descriptive name.
</div>
{{ template "create_vcs_provider_form" . }}
{{ template "vcs_provider_form" . }}
{{ end }}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
{{ define "content-list-item" }}
<div class="widget">
<div>
<span>{{ .Name }}</span>
<span>
<a id="edit-vcs-provider-link" href="{{ editVCSProviderPath .ID }}">{{ .String }}</a>
</span>
<span>{{ durationRound .CreatedAt }} ago</span>
</div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div>
<span class="font-semibold">
<a href="{{ setupConnectionRepoWorkspacePath $.Workspace.ID }}?vcs_provider_id={{ .ID }}">
{{ .Name }}
{{ .String }}
</a>
</span>
<span>{{ durationRound .CreatedAt }} ago</span>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{ define "vcs_provider_form" }}
<form class="flex flex-col gap-5" action="{{ .FormAction }}" method="POST">
<div class="field">
<label for="name">Name</label>
<input class="text-input w-64" type="text" name="name" id="name" {{ if .VCSProvider.Name }}value="{{ .VCSProvider.Name }}"{{ else }} placeholder="{{ .VCSProvider.String }}"{{ end }}>
<span class="description">An optional display name for your VCS provider.</span>
</div>
<div class="field">
<label for="token">Token</label>
<textarea class="text-input w-96" rows="3" name="token" id="token" {{ if .EditMode }}placeholder="*****"{{ else }}required{{ end }}></textarea>
</div>
{{ if .EditMode }}
<button class="btn w-32">Update</button>
{{ else }}
<button class="btn w-32">Create</button>
<input type="hidden" name="cloud" id="cloud" value="{{ .VCSProvider.CloudConfig.Name }}">
{{ end }}
</form>
{{ end }}
4 changes: 2 additions & 2 deletions internal/integration/connect_repo_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestConnectRepoE2E(t *testing.T) {

browser.Run(t, ctx, chromedp.Tasks{
createWorkspace(t, daemon.Hostname(), org.Name, "my-test-workspace"),
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "my-test-workspace", provider.Name),
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "my-test-workspace", provider.String()),
// we can now start a run via the web ui, which'll retrieve the tarball from
// the fake github server
startRunTasks(t, daemon.Hostname(), org.Name, "my-test-workspace", run.PlanAndApplyOperation),
Expand Down Expand Up @@ -104,6 +104,6 @@ func TestConnectRepoE2E(t *testing.T) {
// click delete button for one and only vcs provider
chromedp.Click(`//button[text()='delete']`),
screenshot(t),
matchText(t, "//div[@role='alert']", "deleted provider: "+provider.Name),
matchText(t, "//div[@role='alert']", "deleted provider: "+provider.String()),
})
}
1 change: 0 additions & 1 deletion internal/integration/daemon_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ func (s *testDaemon) createVCSProvider(t *testing.T, ctx context.Context, org *o
// tests require a legitimate cloud name to avoid invalid foreign
// key error upon insert/update
Cloud: "github",
Name: uuid.NewString(),
Token: uuid.NewString(),
})
require.NoError(t, err)
Expand Down
1 change: 0 additions & 1 deletion internal/integration/vcsprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestVCSProvider(t *testing.T) {
_, err := svc.CreateVCSProvider(ctx, vcsprovider.CreateOptions{
Organization: org.Name,
Token: uuid.NewString(),
Name: uuid.NewString(),
Cloud: "github",
})
require.NoError(t, err)
Expand Down
29 changes: 25 additions & 4 deletions internal/integration/vcsprovider_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,34 @@ func TestIntegration_VCSProviderUI(t *testing.T) {
// click 'New Github VCS Provider' button
chromedp.Click(`//button[text()='New Github VCS Provider']`),
screenshot(t, "new_github_vcs_provider_form"),
// enter fake github token and name
// enter fake github token
chromedp.Focus("textarea#token", chromedp.NodeVisible, chromedp.ByQuery),
input.InsertText("fake-github-personal-token"),
chromedp.Focus("input#name", chromedp.ByQuery, chromedp.NodeVisible),
input.InsertText("my-token"),
// submit form to create provider
chromedp.Submit("textarea#token", chromedp.ByQuery),
matchText(t, "//div[@role='alert']", "created provider: my-token"),
matchText(t, "//div[@role='alert']", "created provider: github"),
// edit provider
chromedp.Click(`//a[@id='edit-vcs-provider-link']`), waitLoaded,
// give it a name
chromedp.Focus("input#name", chromedp.ByQuery, chromedp.NodeVisible),
input.InsertText("my-token"),
chromedp.Click(`//button[text()='Update']`),
matchText(t, "//div[@role='alert']", "updated provider: my-token"),
// change token
chromedp.Click(`//a[@id='edit-vcs-provider-link']`), waitLoaded,
chromedp.Focus("textarea#token", chromedp.ByQuery, chromedp.NodeVisible),
input.InsertText("my-updated-fake-github-personal-token"),
chromedp.Click(`//button[text()='Update']`),
matchText(t, "//div[@role='alert']", "updated provider: my-token"),
// clear name
chromedp.Click(`//a[@id='edit-vcs-provider-link']`), waitLoaded,
chromedp.Focus("input#name", chromedp.ByQuery, chromedp.NodeVisible),
chromedp.Clear("input#name", chromedp.ByQuery),
chromedp.Click(`//button[text()='Update']`),
matchText(t, "//div[@role='alert']", "updated provider: github"),
// delete token
chromedp.Click(`//a[@id='edit-vcs-provider-link']`), waitLoaded,
chromedp.Click(`//button[@id='delete-vcs-provider-button']`),
matchText(t, "//div[@role='alert']", "deleted provider: github"),
})
}
4 changes: 2 additions & 2 deletions internal/integration/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestWebhook(t *testing.T) {
// create and connect first workspace
browser.Run(t, ctx, chromedp.Tasks{
createWorkspace(t, daemon.Hostname(), org.Name, "workspace-1"),
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "workspace-1", provider.Name),
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "workspace-1", provider.String()),
})

// webhook should be registered with github
Expand All @@ -45,7 +45,7 @@ func TestWebhook(t *testing.T) {
// create and connect second workspace
browser.Run(t, ctx, chromedp.Tasks{
createWorkspace(t, daemon.Hostname(), org.Name, "workspace-2"),
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "workspace-2", provider.Name),
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "workspace-2", provider.String()),
})

// second workspace re-uses same webhook on github
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/workspace_ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestIntegration_WorkspaceUI(t *testing.T) {
})
// demonstrate setting vcs trigger patterns
browser.Run(t, ctx, chromedp.Tasks{
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "workspace-1", provider.Name),
connectWorkspaceTasks(t, daemon.Hostname(), org.Name, "workspace-1", provider.String()),
chromedp.Navigate(workspaceURL(daemon.Hostname(), org.Name, "workspace-1")),
// go to workspace settings
chromedp.Click(`//a[text()='settings']`),
Expand Down
20 changes: 20 additions & 0 deletions internal/sql/pggen/agent_token.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions internal/sql/pggen/vcs_provider.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions internal/sql/queries/vcs_provider.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ FROM vcs_providers
WHERE vcs_provider_id = pggen.arg('vcs_provider_id')
;

-- name: FindVCSProviderForUpdate :one
SELECT *
FROM vcs_providers
WHERE vcs_provider_id = pggen.arg('vcs_provider_id')
FOR UPDATE
;

-- name: UpdateVCSProvider :one
UPDATE vcs_providers
SET name = pggen.arg('name'), token = pggen.arg('token')
WHERE vcs_provider_id = pggen.arg('vcs_provider_id')
RETURNING *
;

-- name: DeleteVCSProviderByID :one
DELETE
FROM vcs_providers
Expand Down
3 changes: 0 additions & 3 deletions internal/tfeapi/types/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,6 @@ type VCSRepoOptions struct {
IngressSubmodules *bool `json:"ingress-submodules,omitempty"`
OAuthTokenID *string `json:"oauth-token-id,omitempty"`
TagsRegex *string `json:"tags-regex,omitempty"`

Valid bool `json:"-"`
Set bool `json:"-"`
}

// VCSRepoOptionsJSON wraps VCSRepoOptions and implements json.Unmarshaler in order to differentiate
Expand Down
Loading

0 comments on commit 29a0be6

Please sign in to comment.