Skip to content

Commit

Permalink
Merge pull request desktop#15704 from desktop/pr-preview-should-alway…
Browse files Browse the repository at this point in the history
…s-open

PR Preview: Handle no default branch.
  • Loading branch information
tidy-dev authored Dec 13, 2022
2 parents 3f1845d + d0110bf commit ec7ef10
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
4 changes: 2 additions & 2 deletions app/src/lib/app-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ export interface IPullRequestState {
* The base branch of a a pull request - the branch the currently checked out
* branch would merge into
*/
readonly baseBranch: Branch
readonly baseBranch: Branch | null

/** The SHAs of commits of the pull request */
readonly commitSHAs: ReadonlyArray<string> | null
Expand All @@ -981,7 +981,7 @@ export interface IPullRequestState {
* repositories commit selection where the diff of all commits represents the
* diff between the latest commit and the earliest commits parent.
*/
readonly commitSelection: ICommitSelection
readonly commitSelection: ICommitSelection | null

/** The result of merging the pull request branch into the base branch */
readonly mergeStatus: MergeTreeResult | null
Expand Down
55 changes: 44 additions & 11 deletions app/src/lib/stores/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7309,23 +7309,28 @@ export class AppStore extends TypedBaseStore<IAppState> {
}

public async _startPullRequest(repository: Repository) {
const { branchesState } = this.repositoryStateCache.get(repository)
const { defaultBranch, tip } = branchesState
const { tip, defaultBranch } =
this.repositoryStateCache.get(repository).branchesState

if (defaultBranch === null || tip.kind !== TipState.Valid) {
if (tip.kind !== TipState.Valid) {
// Shouldn't even be able to get here if so - just a type check
return
}

const currentBranch = tip.branch
this._initializePullRequestPreview(repository, defaultBranch, currentBranch)
}

private async _initializePullRequestPreview(
repository: Repository,
baseBranch: Branch,
baseBranch: Branch | null,
currentBranch: Branch
) {
const { branchesState, localCommitSHAs } =
this.repositoryStateCache.get(repository)
if (baseBranch === null) {
this.showPullRequestPopupNoBaseBranch(repository, currentBranch)
return
}

const gitStore = this.gitStoreCache.get(repository)

const pullRequestCommits = await gitStore.getCommitsBetweenBranches(
Expand Down Expand Up @@ -7392,15 +7397,46 @@ export class AppStore extends TypedBaseStore<IAppState> {
)
}

this.showPullRequestPopup(repository, currentBranch, commitSHAs)
}

public showPullRequestPopupNoBaseBranch(
repository: Repository,
currentBranch: Branch
) {
this.repositoryStateCache.initializePullRequestState(repository, {
baseBranch: null,
commitSHAs: null,
commitSelection: null,
mergeStatus: null,
})

this.emitUpdate()

this.showPullRequestPopup(repository, currentBranch, [])
}

public showPullRequestPopup(
repository: Repository,
currentBranch: Branch,
commitSHAs: ReadonlyArray<string>
) {
if (this.popupManager.areTherePopupsOfType(PopupType.StartPullRequest)) {
return
}

const { branchesState, localCommitSHAs } =
this.repositoryStateCache.get(repository)
const { allBranches, recentBranches, defaultBranch, currentPullRequest } =
branchesState
const { imageDiffType, selectedExternalEditor, showSideBySideDiff } =
this.getState()

const nonLocalCommitSHA =
commitSHAs.length > 0 && !localCommitSHAs.includes(commitSHAs[0])
? commitSHAs[0]
: null

this._showPopup({
type: PopupType.StartPullRequest,
allBranches,
Expand All @@ -7410,10 +7446,7 @@ export class AppStore extends TypedBaseStore<IAppState> {
recentBranches,
repository,
externalEditorLabel: selectedExternalEditor ?? undefined,
nonLocalCommitSHA:
commitSHAs.length > 0 && !localCommitSHAs.includes(commitSHAs[0])
? commitSHAs[0]
: null,
nonLocalCommitSHA,
showSideBySideDiff,
currentBranchHasPullRequest: currentPullRequest !== null,
})
Expand All @@ -7435,7 +7468,7 @@ export class AppStore extends TypedBaseStore<IAppState> {

const currentBranch = branchesState.tip.branch
const { baseBranch, commitSHAs } = pullRequestState
if (commitSHAs === null) {
if (commitSHAs === null || baseBranch === null) {
return
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/lib/stores/repository-state-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ export class RepositoryStateCache {
}

const oldState = pullRequestState.commitSelection
const commitSelection = merge(oldState, fn(oldState))
const commitSelection =
oldState === null ? null : merge(oldState, fn(oldState))
this.updatePullRequestState(repository, () => ({
commitSelection,
}))
Expand Down
2 changes: 1 addition & 1 deletion app/src/ui/branches/branch-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IBranchListItem } from './group-branches'

interface IBranchSelectProps {
/** The initially selected branch. */
readonly branch: Branch
readonly branch: Branch | null

/**
* See IBranchesState.defaultBranch
Expand Down
34 changes: 31 additions & 3 deletions app/src/ui/open-pull-request/open-pull-request-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class OpenPullRequestDialog extends React.Component<IOpenPullRequestDialo
dispatcher.showPullRequest(repository)
} else {
const { baseBranch } = this.props.pullRequestState
dispatcher.createPullRequest(repository, baseBranch)
dispatcher.createPullRequest(repository, baseBranch ?? undefined)
// TODO: create pr from dialog pr stat?
dispatcher.recordCreatePullRequest()
}
Expand Down Expand Up @@ -123,6 +123,7 @@ export class OpenPullRequestDialog extends React.Component<IOpenPullRequestDialo
return (
<div className="open-pull-request-content">
{this.renderNoChanges()}
{this.renderNoDefaultBranch()}
{this.renderFilesChanged()}
</div>
)
Expand All @@ -140,6 +141,11 @@ export class OpenPullRequestDialog extends React.Component<IOpenPullRequestDialo
nonLocalCommitSHA,
} = this.props
const { commitSelection } = pullRequestState
if (commitSelection === null) {
// type checking - will render no default branch message
return
}

const { diff, file, changesetData, shas } = commitSelection
const { files } = changesetData

Expand Down Expand Up @@ -167,8 +173,12 @@ export class OpenPullRequestDialog extends React.Component<IOpenPullRequestDialo
private renderNoChanges() {
const { pullRequestState, currentBranch } = this.props
const { commitSelection, baseBranch, mergeStatus } = pullRequestState
const { shas } = commitSelection
if (commitSelection === null || baseBranch === null) {
// type checking - will render no default branch message
return
}

const { shas } = commitSelection
if (shas.length !== 0) {
return
}
Expand All @@ -185,7 +195,7 @@ export class OpenPullRequestDialog extends React.Component<IOpenPullRequestDialo
</>
)
return (
<div className="open-pull-request-no-changes">
<div className="open-pull-request-message">
<div>
<Octicon symbol={OcticonSymbol.gitPullRequest} />
<h3>There are no changes.</h3>
Expand All @@ -195,6 +205,24 @@ export class OpenPullRequestDialog extends React.Component<IOpenPullRequestDialo
)
}

private renderNoDefaultBranch() {
const { baseBranch } = this.props.pullRequestState

if (baseBranch !== null) {
return
}

return (
<div className="open-pull-request-message">
<div>
<Octicon symbol={OcticonSymbol.gitPullRequest} />
<h3>Could not find a default branch to compare against.</h3>
Select a base branch above.
</div>
</div>
)
}

private renderFooter() {
const { currentBranchHasPullRequest, pullRequestState, repository } =
this.props
Expand Down
2 changes: 1 addition & 1 deletion app/src/ui/open-pull-request/open-pull-request-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Ref } from '../lib/ref'

interface IOpenPullRequestDialogHeaderProps {
/** The base branch of the pull request */
readonly baseBranch: Branch
readonly baseBranch: Branch | null

/** The branch of the pull request */
readonly currentBranch: Branch
Expand Down
2 changes: 1 addition & 1 deletion app/styles/ui/dialogs/_open-pull-request.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
flex-grow: 1;
}

.open-pull-request-no-changes {
.open-pull-request-message {
height: 100%;
display: flex;
align-items: center;
Expand Down

0 comments on commit ec7ef10

Please sign in to comment.