mirror of
https://github.com/peter-evans/create-pull-request.git
synced 2026-12-02 04:40:29 +00:00
fix: submodule deletion with sign-commits (#4473)
* Fix submodule deletion with `sign-commits` * Unstale the staleness
This commit is contained in:
1 parent
3afad46a91
commit
15733dbdfe
3 files changed
+126
-4
No files matched your search
@@ -0,0 +1,118 @@
|
||||
import {GitHubHelper} from '../lib/github-helper'
|
||||
import {Commit, GitCommandManager} from '../lib/git-command-manager'
|
||||
|
||||
const mockOctokit = {
|
||||
rest: {
|
||||
git: {
|
||||
createBlob: jest.fn(),
|
||||
createTree: jest.fn(),
|
||||
createCommit: jest.fn(),
|
||||
createRef: jest.fn()
|
||||
},
|
||||
repos: {
|
||||
getBranch: jest.fn()
|
||||
}
|
||||
}
|
||||
}
|
||||
jest.mock('../lib/octokit-client', () => ({
|
||||
Octokit: jest.fn(() => mockOctokit),
|
||||
throttleOptions: {},
|
||||
retryOptions: {}
|
||||
}))
|
||||
jest.mock('@octokit/request-error', () => ({RequestError: class {}}))
|
||||
jest.mock('p-limit', () => () => (fn: () => unknown) => fn())
|
||||
|
||||
const NULL_SHA = '0000000000000000000000000000000000000000'
|
||||
|
||||
function makeCommit(changes: Commit['changes']): Commit {
|
||||
return {
|
||||
sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
||||
tree: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
|
||||
parents: ['cccccccccccccccccccccccccccccccccccccccc'],
|
||||
signed: false,
|
||||
subject: 'Test commit',
|
||||
body: '',
|
||||
changes,
|
||||
unparsedChanges: []
|
||||
}
|
||||
}
|
||||
|
||||
describe('github-helper unit tests', () => {
|
||||
test('pushSignedCommits deletes a removed submodule instead of pointing it at the null sha', async () => {
|
||||
const {git: gitApi, repos: reposApi} = mockOctokit.rest
|
||||
gitApi.createBlob.mockResolvedValue({
|
||||
data: {sha: 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'}
|
||||
})
|
||||
gitApi.createTree.mockResolvedValue({
|
||||
data: {sha: 'dddddddddddddddddddddddddddddddddddddddd'}
|
||||
})
|
||||
gitApi.createCommit.mockResolvedValue({
|
||||
data: {
|
||||
sha: 'ffffffffffffffffffffffffffffffffffffffff',
|
||||
tree: {sha: 'dddddddddddddddddddddddddddddddddddddddd'},
|
||||
verification: {verified: true, reason: 'valid'}
|
||||
}
|
||||
})
|
||||
gitApi.createRef.mockResolvedValue({})
|
||||
reposApi.getBranch.mockRejectedValue(new Error('Branch not found'))
|
||||
const git = {
|
||||
showFileAtRefBase64: jest.fn().mockResolvedValue('Y29udGVudA==')
|
||||
}
|
||||
|
||||
const helper = new GitHubHelper('github.com', 'token')
|
||||
|
||||
const baseCommit = makeCommit([])
|
||||
const commit = makeCommit([
|
||||
{
|
||||
mode: '160000',
|
||||
dstSha: NULL_SHA,
|
||||
status: 'D',
|
||||
path: 'submodules/removed'
|
||||
},
|
||||
{
|
||||
mode: '160000',
|
||||
dstSha: '1111111111111111111111111111111111111111',
|
||||
status: 'M',
|
||||
path: 'submodules/updated'
|
||||
},
|
||||
{
|
||||
mode: '100644',
|
||||
dstSha: '2222222222222222222222222222222222222222',
|
||||
status: 'M',
|
||||
path: '.gitmodules'
|
||||
}
|
||||
])
|
||||
|
||||
await helper.pushSignedCommits(
|
||||
git as unknown as GitCommandManager,
|
||||
[commit],
|
||||
baseCommit,
|
||||
'/repo',
|
||||
'owner/repo',
|
||||
'branch'
|
||||
)
|
||||
|
||||
expect(gitApi.createTree).toHaveBeenCalledTimes(1)
|
||||
const {tree} = gitApi.createTree.mock.calls[0][0]
|
||||
expect(tree).toEqual([
|
||||
{
|
||||
path: 'submodules/removed',
|
||||
mode: '160000',
|
||||
sha: null,
|
||||
type: 'commit'
|
||||
},
|
||||
{
|
||||
path: 'submodules/updated',
|
||||
mode: '160000',
|
||||
sha: '1111111111111111111111111111111111111111',
|
||||
type: 'commit'
|
||||
},
|
||||
{
|
||||
path: '.gitmodules',
|
||||
mode: '100644',
|
||||
sha: 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
|
||||
type: 'blob'
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
||||
Vendored
+3
-2
@@ -1576,11 +1576,12 @@ class GitHubHelper {
|
||||
const treeObjects = yield Promise.all(commit.changes.map((_a) => __awaiter(this, [_a], void 0, function* ({ path, mode, status, dstSha }) {
|
||||
if (mode === '160000') {
|
||||
// submodule
|
||||
core.info(`Creating tree object for submodule commit at '${path}'`);
|
||||
core.info(`Creating tree object for submodule commit at '${path}' with status '${status}'`);
|
||||
return {
|
||||
path,
|
||||
mode,
|
||||
sha: dstSha,
|
||||
// A deleted submodule has a null dstSha; a null sha deletes the entry
|
||||
sha: status === 'D' ? null : dstSha,
|
||||
type: 'commit'
|
||||
};
|
||||
}
|
||||
|
||||
@@ -332,11 +332,14 @@ export class GitHubHelper {
|
||||
commit.changes.map(async ({path, mode, status, dstSha}) => {
|
||||
if (mode === '160000') {
|
||||
// submodule
|
||||
core.info(`Creating tree object for submodule commit at '${path}'`)
|
||||
core.info(
|
||||
`Creating tree object for submodule commit at '${path}' with status '${status}'`
|
||||
)
|
||||
return <TreeObject>{
|
||||
path,
|
||||
mode,
|
||||
sha: dstSha,
|
||||
// A deleted submodule has a null dstSha; a null sha deletes the entry
|
||||
sha: status === 'D' ? null : dstSha,
|
||||
type: 'commit'
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user