Skip to content

Commit

Permalink
add a queue system for branch-merger requests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelpz committed Jan 27, 2025
1 parent f6e1a43 commit 053ec01
Show file tree
Hide file tree
Showing 14 changed files with 453 additions and 134 deletions.
2 changes: 1 addition & 1 deletion public/build_number
Original file line number Diff line number Diff line change
@@ -1 +1 @@
280-f9e231e
281-f6e1a43
52 changes: 41 additions & 11 deletions src/App.context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';

import { useStateReducer } from './hooks/useStateReducer';
Expand All @@ -8,6 +8,10 @@ import BranchMergerProvider from './components/branch-merger/context/BranchMerge

export const AppContext = React.createContext();

/**
* Main application context provider that manages global state and integrates
* various features including branch merging functionality.
*/
export function AppContextProvider({
authentication,
language,
Expand All @@ -32,7 +36,7 @@ export function AppContextProvider({
});

const giteaReactToolkit = useGiteaReactToolkit({ state, actions });
const warning = useWarning({ state, actions })
const warning = useWarning({ state, actions });

const value = {
state,
Expand All @@ -41,22 +45,48 @@ export function AppContextProvider({
warning,
};

const params = {
server: state.authentication?.config?.server,
owner: state.targetRepository?.owner?.login,
repo: state.targetRepository?.name,
userBranch: state.targetRepository?.branch,
tokenid: state.authentication?.token?.sha1,
}
// Memoize branch merger params to prevent unnecessary re-renders
const branchMergerParams = useMemo(() => {
const params = {
server: state.authentication?.config?.server,
owner: state.targetRepository?.owner?.login,
repo: state.targetRepository?.name,
userBranch: state.targetRepository?.branch,
tokenid: state.authentication?.token?.sha1,
autoCheck: true,
autoCheckInterval: 300000
};

// Validate required params
const missingParams = Object.entries(params)
.filter(([key, value]) => !value && key !== 'autoCheck' && key !== 'autoCheckInterval')
.map(([key]) => key);

if (missingParams.length > 0) {
console.warn(
`BranchMerger: Missing required parameters: ${missingParams.join(', ')}. ` +
'Branch merger functionality will be disabled.'
);
return null;
}

return params;
}, [
state.authentication?.config?.server,
state.targetRepository?.owner?.login,
state.targetRepository?.name,
state.targetRepository?.branch,
state.authentication?.token?.sha1
]);

return (
<AppContext.Provider value={value}>
<BranchMergerProvider {...params}>
<BranchMergerProvider {...branchMergerParams}>
{children}
</BranchMergerProvider>
</AppContext.Provider>
);
};
}

AppContextProvider.propTypes = {
authentication: PropTypes.object,
Expand Down
22 changes: 22 additions & 0 deletions src/components/branch-merger/branchOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
mergeDefaultIntoUserBranch,
checkMergeDefaultIntoUserBranch,
checkMergeUserIntoDefaultBranch,
mergeUserIntoDefaultBranch
} from "dcs-branch-merger"

/**
* Defines operations for syncing between default/master branch and user branch.
*
* Operations:
* - pullFromDefault: Pull changes FROM default/master branch INTO user branch
* - checkPullFromDefault: Check if user branch needs updates from default/master
* - pushToDefault: Push changes FROM user branch TO default/master
* - checkPushToDefault: Check if user branch can be merged into default/master
*/
export const branchOperations = {
pullFromDefault: mergeDefaultIntoUserBranch,
checkPullFromDefault: checkMergeDefaultIntoUserBranch,
pushToDefault: mergeUserIntoDefaultBranch,
checkPushToDefault: checkMergeUserIntoDefaultBranch,
}
14 changes: 14 additions & 0 deletions src/components/branch-merger/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const defaultStatus = {
mergeNeeded: false,
conflict: false,
success: false,
userBranchDeleted: false,
error: false,
message: "",
pullRequest: "",
}

export const RETRY_ATTEMPTS = 3;
export const RETRY_DELAY = 1000; // ms
export const RATE_LIMIT_DELAY = 1000; // 1 second between API calls
export const DEFAULT_AUTO_CHECK_INTERVAL = 30000; // 30 seconds
15 changes: 15 additions & 0 deletions src/components/branch-merger/context/BranchMergerContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createContext, useContext } from 'react';

export const BranchMergerContext = createContext(null);

/**
* Hook to use branch merger context.
* Throws if used outside of BranchMergerProvider.
*/
export function useBranchMergerContext() {
const context = useContext(BranchMergerContext);
if (!context) {
throw new Error('useBranchMergerContext must be used within a BranchMergerProvider');
}
return context;
}
46 changes: 34 additions & 12 deletions src/components/branch-merger/context/BranchMergerProvider.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
import React, { createContext } from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { BranchMergerContext } from './BranchMergerContext';
import useBranchMerger from '../hooks/useBranchMerger';
import { DEFAULT_AUTO_CHECK_INTERVAL } from '../constants';

export const BranchMergerContext = createContext();
/**
* Provider component for branch merger functionality.
* Provides branch operations and status through context.
*/
export function BranchMergerProvider({
children,
server,
owner,
repo,
userBranch,
tokenid,
autoCheck = false,
autoCheckInterval = DEFAULT_AUTO_CHECK_INTERVAL
}) {
const branchMerger = useBranchMerger(
{ server, owner, repo, userBranch, tokenid },
{ autoCheck, autoCheckInterval }
);

// Memoize context value to prevent unnecessary re-renders
const contextValue = useMemo(() => branchMerger, [branchMerger]);

const BranchMergerProvider = ({ children, server, owner, repo, userBranch, tokenid }) => {
const {state,actions} = useBranchMerger({server, owner, repo, userBranch, tokenid})
return (
<BranchMergerContext.Provider value={{state,actions}}>
<BranchMergerContext.Provider value={contextValue}>
{children}
</BranchMergerContext.Provider>
);
};
}

BranchMergerProvider.propTypes = {
children: PropTypes.element,
server: PropTypes.string,
owner: PropTypes.string,
repo: PropTypes.string,
userBranch: PropTypes.string,
tokenid: PropTypes.string,
children: PropTypes.node.isRequired,
server: PropTypes.string.isRequired,
owner: PropTypes.string.isRequired,
repo: PropTypes.string.isRequired,
userBranch: PropTypes.string.isRequired,
tokenid: PropTypes.string.isRequired,
autoCheck: PropTypes.bool,
autoCheckInterval: PropTypes.number
};

export default BranchMergerProvider;
Loading

0 comments on commit 053ec01

Please sign in to comment.