mirror of https://github.com/go-gitea/gitea
Add priority to protected branch (#32286)
## Solves Currently for rules to re-order them you have to alter the creation date. so you basicly have to delete and recreate them in the right order. This is more than just inconvinient ... ## Solution Add a new col for prioritization ## Demo WebUI Video https://github.com/user-attachments/assets/92182a31-9705-4ac5-b6e3-9bb74108cbd1 --- *Sponsored by Kithara Software GmbH*pull/32658/head
parent
3fc1bbe971
commit
846f618716
@ -0,0 +1,16 @@ |
|||||||
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package v1_23 //nolint
|
||||||
|
|
||||||
|
import ( |
||||||
|
"xorm.io/xorm" |
||||||
|
) |
||||||
|
|
||||||
|
func AddPriorityToProtectedBranch(x *xorm.Engine) error { |
||||||
|
type ProtectedBranch struct { |
||||||
|
Priority int64 `xorm:"NOT NULL DEFAULT 0"` |
||||||
|
} |
||||||
|
|
||||||
|
return x.Sync(new(ProtectedBranch)) |
||||||
|
} |
@ -0,0 +1,71 @@ |
|||||||
|
import {beforeEach, describe, expect, test, vi} from 'vitest'; |
||||||
|
import {initRepoBranchesSettings} from './repo-settings-branches.ts'; |
||||||
|
import {POST} from '../modules/fetch.ts'; |
||||||
|
import {createSortable} from '../modules/sortable.ts'; |
||||||
|
|
||||||
|
vi.mock('../modules/fetch.ts', () => ({ |
||||||
|
POST: vi.fn(), |
||||||
|
})); |
||||||
|
|
||||||
|
vi.mock('../modules/sortable.ts', () => ({ |
||||||
|
createSortable: vi.fn(), |
||||||
|
})); |
||||||
|
|
||||||
|
describe('Repository Branch Settings', () => { |
||||||
|
beforeEach(() => { |
||||||
|
document.body.innerHTML = ` |
||||||
|
<div id="protected-branches-list" data-update-priority-url="some/repo/branches/priority"> |
||||||
|
<div class="flex-item tw-items-center item" data-id="1" > |
||||||
|
<div class="drag-handle"></div> |
||||||
|
</div> |
||||||
|
<div class="flex-item tw-items-center item" data-id="2" > |
||||||
|
<div class="drag-handle"></div> |
||||||
|
</div> |
||||||
|
<div class="flex-item tw-items-center item" data-id="3" > |
||||||
|
<div class="drag-handle"></div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
`;
|
||||||
|
|
||||||
|
vi.clearAllMocks(); |
||||||
|
}); |
||||||
|
|
||||||
|
test('should initialize sortable for protected branches list', () => { |
||||||
|
initRepoBranchesSettings(); |
||||||
|
|
||||||
|
expect(createSortable).toHaveBeenCalledWith( |
||||||
|
document.querySelector('#protected-branches-list'), |
||||||
|
expect.objectContaining({ |
||||||
|
handle: '.drag-handle', |
||||||
|
animation: 150, |
||||||
|
}), |
||||||
|
); |
||||||
|
}); |
||||||
|
|
||||||
|
test('should not initialize if protected branches list is not present', () => { |
||||||
|
document.body.innerHTML = ''; |
||||||
|
|
||||||
|
initRepoBranchesSettings(); |
||||||
|
|
||||||
|
expect(createSortable).not.toHaveBeenCalled(); |
||||||
|
}); |
||||||
|
|
||||||
|
test('should post new order after sorting', async () => { |
||||||
|
vi.mocked(POST).mockResolvedValue({ok: true} as Response); |
||||||
|
|
||||||
|
// Mock createSortable to capture and execute the onEnd callback
|
||||||
|
vi.mocked(createSortable).mockImplementation((_el, options) => { |
||||||
|
options.onEnd(); |
||||||
|
return {destroy: vi.fn()}; |
||||||
|
}); |
||||||
|
|
||||||
|
initRepoBranchesSettings(); |
||||||
|
|
||||||
|
expect(POST).toHaveBeenCalledWith( |
||||||
|
'some/repo/branches/priority', |
||||||
|
expect.objectContaining({ |
||||||
|
data: {ids: [1, 2, 3]}, |
||||||
|
}), |
||||||
|
); |
||||||
|
}); |
||||||
|
}); |
@ -0,0 +1,32 @@ |
|||||||
|
import {createSortable} from '../modules/sortable.ts'; |
||||||
|
import {POST} from '../modules/fetch.ts'; |
||||||
|
import {showErrorToast} from '../modules/toast.ts'; |
||||||
|
import {queryElemChildren} from '../utils/dom.ts'; |
||||||
|
|
||||||
|
export function initRepoBranchesSettings() { |
||||||
|
const protectedBranchesList = document.querySelector('#protected-branches-list'); |
||||||
|
if (!protectedBranchesList) return; |
||||||
|
|
||||||
|
createSortable(protectedBranchesList, { |
||||||
|
handle: '.drag-handle', |
||||||
|
animation: 150, |
||||||
|
|
||||||
|
onEnd: () => { |
||||||
|
(async () => { |
||||||
|
const itemElems = queryElemChildren(protectedBranchesList, '.item[data-id]'); |
||||||
|
const itemIds = Array.from(itemElems, (el) => parseInt(el.getAttribute('data-id'))); |
||||||
|
|
||||||
|
try { |
||||||
|
await POST(protectedBranchesList.getAttribute('data-update-priority-url'), { |
||||||
|
data: { |
||||||
|
ids: itemIds, |
||||||
|
}, |
||||||
|
}); |
||||||
|
} catch (err) { |
||||||
|
const errorMessage = String(err); |
||||||
|
showErrorToast(`Failed to update branch protection rule priority:, error: ${errorMessage}`); |
||||||
|
} |
||||||
|
})(); |
||||||
|
}, |
||||||
|
}); |
||||||
|
} |
Loading…
Reference in new issue