Skip to content

Commit

Permalink
feat: add support for custom pull request template paths (#4814)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 authored Sep 4, 2024
1 parent e06e93e commit 36e9845
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 28 deletions.
4 changes: 4 additions & 0 deletions apps/desktop/src/lib/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ export function persistedCommitMessage(projectId: string, branchId: string): Per
export function gitHostUsePullRequestTemplate(): Persisted<boolean> {
return persisted(false, 'gitHostUsePullRequestTemplate');
}

export function gitHostPullRequestTemplatePath(): Persisted<string> {
return persisted('', 'gitHostPullRequestTemplatePath');
}
6 changes: 4 additions & 2 deletions apps/desktop/src/lib/gitHost/gitHostFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export class DefaultGitHostFactory implements GitHostFactory {
repo: RepoInfo,
baseBranch: string,
fork?: RepoInfo,
usePullRequestTemplate?: Persisted<boolean>
usePullRequestTemplate?: Persisted<boolean>,
pullRequestTemplatePath?: Persisted<string>
) {
const domain = repo.domain;
const forkStr = fork ? `${fork.owner}:${fork.name}` : undefined;
Expand All @@ -33,7 +34,8 @@ export class DefaultGitHostFactory implements GitHostFactory {
forkStr,
octokit: this.octokit,
projectMetrics: new ProjectMetrics(),
usePullRequestTemplate
usePullRequestTemplate,
pullRequestTemplatePath
});
}
if (domain === GITLAB_DOMAIN || domain.startsWith(GITLAB_SUB_DOMAIN + '.')) {
Expand Down
9 changes: 7 additions & 2 deletions apps/desktop/src/lib/gitHost/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ export class GitHub implements GitHost {
private octokit?: Octokit;
private projectMetrics?: ProjectMetrics;
private usePullRequestTemplate?: Persisted<boolean>;
private pullRequestTemplatePath?: Persisted<string>;

constructor({
repo,
baseBranch,
forkStr,
octokit,
projectMetrics,
usePullRequestTemplate
usePullRequestTemplate,
pullRequestTemplatePath
}: GitHostArguments & {
octokit?: Octokit;
projectMetrics?: ProjectMetrics;
usePullRequestTemplate?: Persisted<boolean>;
pullRequestTemplatePath?: Persisted<string>;
}) {
this.baseUrl = `https://${GITHUB_DOMAIN}/${repo.owner}/${repo.name}`;
this.repo = repo;
Expand All @@ -39,6 +42,7 @@ export class GitHub implements GitHost {
this.octokit = octokit;
this.projectMetrics = projectMetrics;
this.usePullRequestTemplate = usePullRequestTemplate;
this.pullRequestTemplatePath = pullRequestTemplatePath;
}

listService() {
Expand All @@ -57,7 +61,8 @@ export class GitHub implements GitHost {
this.repo,
baseBranch,
upstreamName,
this.usePullRequestTemplate
this.usePullRequestTemplate,
this.pullRequestTemplatePath
);
}

Expand Down
20 changes: 15 additions & 5 deletions apps/desktop/src/lib/gitHost/github/githubPrService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GitHubPrMonitor } from './githubPrMonitor';
import { DEFAULT_HEADERS } from './headers';
import { ghResponseToInstance, parseGitHubDetailedPullRequest } from './types';
import { showError } from '$lib/notifications/toasts';
import { showToast } from '$lib/notifications/toasts';
import { sleep } from '$lib/utils/sleep';
import posthog from 'posthog-js';
import { get, writable } from 'svelte/store';
Expand All @@ -21,7 +21,8 @@ export class GitHubPrService implements GitHostPrService {
private repo: RepoInfo,
private baseBranch: string,
private upstreamName: string,
private usePullRequestTemplate?: Persisted<boolean>
private usePullRequestTemplate?: Persisted<boolean>,
private pullRequestTemplatePath?: Persisted<string>
) {}

async createPr(title: string, body: string, draft: boolean): Promise<PullRequest> {
Expand Down Expand Up @@ -67,19 +68,28 @@ export class GitHubPrService implements GitHostPrService {
}

async fetchPrTemplate() {
const path = this.pullRequestTemplatePath
? get(this.pullRequestTemplatePath)
: DEFAULT_PULL_REQUEST_TEMPLATE_PATH;

try {
const response = await this.octokit.rest.repos.getContent({
owner: this.repo.owner,
repo: this.repo.name,
path: DEFAULT_PULL_REQUEST_TEMPLATE_PATH
path
});
const b64Content = (response.data as any)?.content;
if (b64Content) {
return decodeURIComponent(escape(atob(b64Content)));
}
} catch (err) {
console.error('Error fetching pull request template: ', err);
showError('Failed to fetch pull request template', err);
console.error(`Error fetching pull request template at path: ${path}`, err);

showToast({
title: 'Failed to fetch pull request template',
message: `Template not found at path: <code>${path}</code>.`,
style: 'neutral'
});
}
}

Expand Down
57 changes: 57 additions & 0 deletions apps/desktop/src/lib/settings/GitHostForm.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<script lang="ts">
import SectionCard from '$lib/components/SectionCard.svelte';
import {
gitHostPullRequestTemplatePath,
gitHostUsePullRequestTemplate
} from '$lib/config/config';
import Section from '$lib/settings/Section.svelte';
import Spacer from '$lib/shared/Spacer.svelte';
import TextBox from '$lib/shared/TextBox.svelte';
import Toggle from '$lib/shared/Toggle.svelte';
const usePullRequestTemplate = gitHostUsePullRequestTemplate();
const pullRequestTemplatePath = gitHostPullRequestTemplatePath();
</script>

<Section>
<svelte:fragment slot="title">Pull Request Template</svelte:fragment>
<svelte:fragment slot="description">
Use Pull Request template when creating a Pull Requests.
</svelte:fragment>

<div>
<SectionCard
roundedBottom={false}
orientation="row"
labelFor="use-pull-request-template-boolean"
>
<svelte:fragment slot="title">Enable Pull Request Templates</svelte:fragment>
<svelte:fragment slot="actions">
<Toggle
id="use-pull-request-template-boolean"
value="false"
bind:checked={$usePullRequestTemplate}
/>
</svelte:fragment>
<svelte:fragment slot="caption">
If enabled, we will use the path below to set the initial body of any pull requested created
on this project through GitButler.
</svelte:fragment>
</SectionCard>
<SectionCard roundedTop={false} orientation="row" labelFor="use-pull-request-template-path">
<svelte:fragment slot="caption">
<form>
<fieldset class="fields-wrapper">
<TextBox
label="Pull request template path"
id="use-pull-request-template-path"
bind:value={$pullRequestTemplatePath}
placeholder=".github/pull_request_template.md"
/>
</fieldset>
</form>
</svelte:fragment>
</SectionCard>
</div>
</Section>
<Spacer />
20 changes: 3 additions & 17 deletions apps/desktop/src/lib/settings/GithubIntegration.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script lang="ts">
import { checkAuthStatus, initDeviceOauth } from '$lib/backend/github';
import SectionCard from '$lib/components/SectionCard.svelte';
import { gitHostUsePullRequestTemplate } from '$lib/config/config';
import { getGitHubUserServiceStore } from '$lib/gitHost/github/githubUserService';
import Toggle from '$lib/shared/Toggle.svelte';
import { UserService } from '$lib/stores/user';
import { copyToClipboard } from '$lib/utils/clipboard';
import { getContext } from '$lib/utils/context';
Expand All @@ -17,7 +15,6 @@
export let minimal = false;
export let disabled = false;
const usePullRequestTemplate = gitHostUsePullRequestTemplate();
const githubUserService = getGitHubUserServiceStore();
const userService = getContext(UserService);
const user = userService.user;
Expand Down Expand Up @@ -98,22 +95,13 @@
<svelte:fragment slot="title">GitHub</svelte:fragment>
<svelte:fragment slot="caption">Allows you to view and create Pull Requests.</svelte:fragment>
{#if $user?.github_access_token}
<Button style="ghost" outline {disabled} icon="bin-small" onclick={forgetGitHub}
>Forget</Button
>
<Button style="ghost" outline {disabled} icon="bin-small" onclick={forgetGitHub}>
Forget
</Button>
{:else}
<Button style="pop" kind="solid" {disabled} onclick={gitHubStartOauth}>Authorize</Button>
{/if}
</SectionCard>
<SectionCard roundedBottom={false} orientation="row" labelFor="use-pull-request-template">
<svelte:fragment slot="title">Pull Request Template</svelte:fragment>
<svelte:fragment slot="caption"
>Use Pull Request template when creating a Pull Requests.</svelte:fragment
>
<svelte:fragment slot="actions">
<Toggle id="use-pull-request-template" value="false" bind:checked={$usePullRequestTemplate} />
</svelte:fragment>
</SectionCard>
{/if}

<Modal
Expand Down Expand Up @@ -286,8 +274,6 @@
}
}
/* */
.icon-wrapper {
align-self: flex-start;
position: relative;
Expand Down
14 changes: 12 additions & 2 deletions apps/desktop/src/routes/[projectId]/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import NoBaseBranch from '$lib/components/NoBaseBranch.svelte';
import NotOnGitButlerBranch from '$lib/components/NotOnGitButlerBranch.svelte';
import ProblemLoadingRepo from '$lib/components/ProblemLoadingRepo.svelte';
import { gitHostUsePullRequestTemplate } from '$lib/config/config';
import {
gitHostPullRequestTemplatePath,
gitHostUsePullRequestTemplate
} from '$lib/config/config';
import { ReorderDropzoneManagerFactory } from '$lib/dragging/reorderDropzoneManager';
import { DefaultGitHostFactory } from '$lib/gitHost/gitHostFactory';
import { octokitFromAccessToken } from '$lib/gitHost/github/octokit';
Expand Down Expand Up @@ -80,6 +83,7 @@
const showHistoryView = persisted(false, 'showHistoryView');
const usePullRequestTemplate = gitHostUsePullRequestTemplate();
const pullRequestTemplatePath = gitHostPullRequestTemplatePath();
const octokit = $derived(accessToken ? octokitFromAccessToken(accessToken) : undefined);
const gitHostFactory = $derived(new DefaultGitHostFactory(octokit));
const repoInfo = $derived(remoteUrl ? parseRemoteUrl(remoteUrl) : undefined);
Expand Down Expand Up @@ -122,7 +126,13 @@
$effect.pre(() => {
const gitHost =
repoInfo && baseBranchName
? gitHostFactory.build(repoInfo, baseBranchName, forkInfo, usePullRequestTemplate)
? gitHostFactory.build(
repoInfo,
baseBranchName,
forkInfo,
usePullRequestTemplate,
pullRequestTemplatePath
)
: undefined;
const ghListService = gitHost?.listService();
Expand Down
7 changes: 7 additions & 0 deletions apps/desktop/src/routes/[projectId]/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
import { platformName } from '$lib/platform/platform';
import CloudForm from '$lib/settings/CloudForm.svelte';
import DetailsForm from '$lib/settings/DetailsForm.svelte';
import GitHostForm from '$lib/settings/GitHostForm.svelte';
import KeysForm from '$lib/settings/KeysForm.svelte';
import PreferencesForm from '$lib/settings/PreferencesForm.svelte';
import Spacer from '$lib/shared/Spacer.svelte';
import { UserService } from '$lib/stores/user';
import { getContext } from '$lib/utils/context';
import * as toasts from '$lib/utils/toasts';
import { goto } from '$app/navigation';
const baseBranchSwitching = featureBaseBranchSwitching();
const projectService = getContext(ProjectService);
const project = getContext(Project);
const userService = getContext(UserService);
const user = userService.user;
let deleteConfirmationModal: RemoveProjectButton;
let isDeleting = false;
Expand All @@ -45,6 +49,9 @@
{/if}
<CloudForm />
<DetailsForm />
{#if $user?.github_access_token}
<GitHostForm />
{/if}
{#if $platformName !== 'win32'}
<KeysForm showProjectName={false} />
<Spacer />
Expand Down

0 comments on commit 36e9845

Please sign in to comment.