( progress: (message: string) => void, originalFileName: string, newFileName: string, headline: string, )
| 44 | } |
| 45 | |
| 46 | async function discardChanges( |
| 47 | progress: (message: string) => void, |
| 48 | originalFileName: string, |
| 49 | newFileName: string, |
| 50 | headline: string, |
| 51 | ): Promise<void> { |
| 52 | const [headReference, file] = await Promise.all([ |
| 53 | getHeadReference(), |
| 54 | getFile(originalFileName), |
| 55 | ]); |
| 56 | |
| 57 | const isNewFile = file === undefined; |
| 58 | const isRenamed = originalFileName !== newFileName; |
| 59 | |
| 60 | const contents = file ?? ''; |
| 61 | const newFileDeletion = {deletions: [{path: newFileName}]}; |
| 62 | const restoreOldFile = {additions: [{path: originalFileName, contents}]}; |
| 63 | const fileChanges = isRenamed |
| 64 | ? {...restoreOldFile, ...newFileDeletion} // Renamed, maybe also changed |
| 65 | : isNewFile |
| 66 | ? newFileDeletion // New |
| 67 | : restoreOldFile; // Changes |
| 68 | |
| 69 | const {nameWithOwner, branch: prBranch} = getBranches().head; |
| 70 | progress('Committing…'); |
| 71 | |
| 72 | await api.v4( |
| 73 | ` |
| 74 | mutation discardChanges ($input: CreateCommitOnBranchInput!) { |
| 75 | createCommitOnBranch(input: $input) { |
| 76 | commit { |
| 77 | oid |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | `, |
| 82 | { |
| 83 | variables: { |
| 84 | input: { |
| 85 | branch: { |
| 86 | repositoryNameWithOwner: nameWithOwner, |
| 87 | branchName: prBranch, |
| 88 | }, |
| 89 | expectedHeadOid: headReference, |
| 90 | fileChanges, |
| 91 | message: { |
| 92 | headline, |
| 93 | }, |
| 94 | }, |
| 95 | }, |
| 96 | }, |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | function getFilenames(menuItem: HTMLElement): {original: string; new: string} { |
| 101 | // Legacy view: get filenames from the data-path and Link--primary elements |
no test coverage detected