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
4 changes: 2 additions & 2 deletions supacode/Clients/Github/GithubPullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ nonisolated struct GithubPullRequest: Decodable, Equatable, Hashable {
let deletions: Int
let isDraft: Bool
let reviewDecision: String?
let mergeable: String?
let mergeStateStatus: String?
var mergeable: String?
var mergeStateStatus: String?
let updatedAt: Date?
let url: String
let headRefName: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,12 @@ extension RepositoriesFeature {
continue
}
let loadedPullRequest = pullRequestsByWorktreeID[worktreeID] ?? nil
let pullRequest = Self.displayPullRequest(loadedPullRequest, for: worktree)
let displayedPullRequest = Self.displayPullRequest(loadedPullRequest, for: worktree)
let previousPullRequest = state.worktreeInfoByID[worktreeID]?.pullRequest
let pullRequest = Self.preservingMergeableState(
displayedPullRequest,
previous: previousPullRequest
)
guard previousPullRequest != pullRequest else {
continue
}
Expand Down Expand Up @@ -962,6 +966,22 @@ extension RepositoriesFeature {
return pullRequest
}

nonisolated private static func preservingMergeableState(
_ pullRequest: GithubPullRequest?,
previous: GithubPullRequest?
) -> GithubPullRequest? {
guard var pullRequest else { return pullRequest }
let newMergeable = pullRequest.mergeable?.uppercased()
let previousMergeable = previous?.mergeable?.uppercased()
let isUnknown = newMergeable == "UNKNOWN"
let hasKnownPrevious = previousMergeable != nil && previousMergeable != "UNKNOWN"
if isUnknown && hasKnownPrevious, let previous {
pullRequest.mergeable = previous.mergeable
pullRequest.mergeStateStatus = previous.mergeStateStatus
}
return pullRequest
}

nonisolated private static func pullRequestRefreshRemotePriorities(
_ remoteInfos: [GithubRemoteInfo]
) -> [String: Int] {
Expand Down
223 changes: 223 additions & 0 deletions supacodeTests/RepositoriesFeatureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6257,6 +6257,229 @@ struct RepositoriesFeatureTests {
}
}

@Test func repositoryPullRequestsLoadedPreservesMergeableWhenNewIsUnknown() async {
let repoRoot = "/tmp/repo"
let featureWorktree = makeWorktree(
id: "\(repoRoot)/feature",
name: "feature",
repoRoot: repoRoot
)
let repository = makeRepository(id: repoRoot, worktrees: [featureWorktree])
let previousPR = GithubPullRequest(
number: 1,
title: "PR",
state: "OPEN",
additions: 10,
deletions: 5,
isDraft: false,
reviewDecision: nil,
mergeable: "MERGEABLE",
mergeStateStatus: "CLEAN",
updatedAt: nil,
url: "https://example.com/pull/1",
headRefName: "feature",
baseRefName: "main",
commitsCount: 1,
authorLogin: "khoi",
statusCheckRollup: nil
)
var newPR = previousPR
newPR.mergeable = "UNKNOWN"
newPR.mergeStateStatus = "BEHIND"
var state = makeState(repositories: [repository])
state.worktreeInfoByID[featureWorktree.id] = WorktreeInfoEntry(
addedLines: nil,
removedLines: nil,
pullRequest: previousPR
)
let store = TestStore(initialState: state) {
RepositoriesFeature()
}
await store.send(
.githubIntegration(
.repositoryPullRequestsLoaded(
repositoryID: repository.id,
pullRequestsByWorktreeID: [featureWorktree.id: newPR]
))
)
await store.finish()
}

@Test func repositoryPullRequestsLoadedPreservesMergeableButUpdatesOtherFields() async {
let repoRoot = "/tmp/repo"
let featureWorktree = makeWorktree(
id: "\(repoRoot)/feature",
name: "feature",
repoRoot: repoRoot
)
let repository = makeRepository(id: repoRoot, worktrees: [featureWorktree])
let previousPR = GithubPullRequest(
number: 1,
title: "Old title",
state: "OPEN",
additions: 10,
deletions: 5,
isDraft: false,
reviewDecision: nil,
mergeable: "MERGEABLE",
mergeStateStatus: "CLEAN",
updatedAt: nil,
url: "https://example.com/pull/1",
headRefName: "feature",
baseRefName: "main",
commitsCount: 1,
authorLogin: "khoi",
statusCheckRollup: nil
)
let newPR = GithubPullRequest(
number: 1,
title: "New title",
state: "OPEN",
additions: 10,
deletions: 5,
isDraft: false,
reviewDecision: nil,
mergeable: "UNKNOWN",
mergeStateStatus: "BEHIND",
updatedAt: nil,
url: "https://example.com/pull/1",
headRefName: "feature",
baseRefName: "main",
commitsCount: 1,
authorLogin: "khoi",
statusCheckRollup: nil
)
var expectedPR = newPR
expectedPR.mergeable = "MERGEABLE"
expectedPR.mergeStateStatus = "CLEAN"
var state = makeState(repositories: [repository])
state.worktreeInfoByID[featureWorktree.id] = WorktreeInfoEntry(
addedLines: nil,
removedLines: nil,
pullRequest: previousPR
)
let store = TestStore(initialState: state) {
RepositoriesFeature()
}
await store.send(
.githubIntegration(
.repositoryPullRequestsLoaded(
repositoryID: repository.id,
pullRequestsByWorktreeID: [featureWorktree.id: newPR]
))
) {
$0.worktreeInfoByID[featureWorktree.id]?.pullRequest = expectedPR
}
}

@Test func repositoryPullRequestsLoadedKeepsUnknownWhenNoPreviousPR() async {
let repoRoot = "/tmp/repo"
let featureWorktree = makeWorktree(
id: "\(repoRoot)/feature",
name: "feature",
repoRoot: repoRoot
)
let repository = makeRepository(id: repoRoot, worktrees: [featureWorktree])
let newPR = GithubPullRequest(
number: 1,
title: "PR",
state: "OPEN",
additions: 10,
deletions: 5,
isDraft: false,
reviewDecision: nil,
mergeable: "UNKNOWN",
mergeStateStatus: nil,
updatedAt: nil,
url: "https://example.com/pull/1",
headRefName: "feature",
baseRefName: "main",
commitsCount: 1,
authorLogin: "khoi",
statusCheckRollup: nil
)
let store = TestStore(initialState: makeState(repositories: [repository])) {
RepositoriesFeature()
}
await store.send(
.githubIntegration(
.repositoryPullRequestsLoaded(
repositoryID: repository.id,
pullRequestsByWorktreeID: [featureWorktree.id: newPR]
))
) {
$0.worktreeInfoByID[featureWorktree.id] = WorktreeInfoEntry(
addedLines: nil,
removedLines: nil,
pullRequest: newPR
)
}
}

@Test func repositoryPullRequestsLoadedKeepsUnknownWhenPreviousAlsoUnknown() async {
let repoRoot = "/tmp/repo"
let featureWorktree = makeWorktree(
id: "\(repoRoot)/feature",
name: "feature",
repoRoot: repoRoot
)
let repository = makeRepository(id: repoRoot, worktrees: [featureWorktree])
let previousPR = GithubPullRequest(
number: 1,
title: "Old title",
state: "OPEN",
additions: 10,
deletions: 5,
isDraft: false,
reviewDecision: nil,
mergeable: "UNKNOWN",
mergeStateStatus: "BEHIND",
updatedAt: nil,
url: "https://example.com/pull/1",
headRefName: "feature",
baseRefName: "main",
commitsCount: 1,
authorLogin: "khoi",
statusCheckRollup: nil
)
let newPR = GithubPullRequest(
number: 1,
title: "New title",
state: "OPEN",
additions: 10,
deletions: 5,
isDraft: false,
reviewDecision: nil,
mergeable: "UNKNOWN",
mergeStateStatus: "BEHIND",
updatedAt: nil,
url: "https://example.com/pull/1",
headRefName: "feature",
baseRefName: "main",
commitsCount: 1,
authorLogin: "khoi",
statusCheckRollup: nil
)
var state = makeState(repositories: [repository])
state.worktreeInfoByID[featureWorktree.id] = WorktreeInfoEntry(
addedLines: nil,
removedLines: nil,
pullRequest: previousPR
)
let store = TestStore(initialState: state) {
RepositoriesFeature()
}
await store.send(
.githubIntegration(
.repositoryPullRequestsLoaded(
repositoryID: repository.id,
pullRequestsByWorktreeID: [featureWorktree.id: newPR]
))
) {
$0.worktreeInfoByID[featureWorktree.id]?.pullRequest = newPR
}
}

@Test func unarchiveWorktreeNoopsWhenNotArchived() async {
let worktree = makeWorktree(id: "/tmp/wt", name: "owl")
let repository = makeRepository(id: "/tmp/repo", worktrees: [worktree])
Expand Down
Loading