Skip to content

Commit

Permalink
Permit push/pull and refresh of github repo status
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbrittain committed Aug 24, 2024
1 parent bccc7ce commit 9fb1fb1
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 3 deletions.
1 change: 1 addition & 0 deletions electron-app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type SettingsAPI = {
GithubUnstageFiles: (query: Query) => Promise<Query>;
GithubCommit: (query: Query) => Promise<Query>;
GithubClone: (query: Query) => Promise<Query>;
GithubCommitAllChanges: (query: Query) => Promise<Query>;
};

declare global {
Expand Down
8 changes: 8 additions & 0 deletions electron-app/src/handles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,11 @@ export async function settings_GithubClone(
) {
return await SafeProcessQuery(event, query, stderr_callback);
}

export async function settings_GithubCommitAllChanges(
event: Event,
query: Query,
stderr_callback: (cmd: string) => void,
) {
return await SafeProcessQuery(event, query, stderr_callback);
}
3 changes: 3 additions & 0 deletions electron-app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ app.whenReady().then(() => {
ipcMain.handle('settings/github-clone', (event: Event, query: Query) =>
handles.settings_GithubClone(event, query, stderr_callback),
);
ipcMain.handle('settings/github-commit-all-changes', (event: Event, query: Query) =>
handles.settings_GithubCommitAllChanges(event, query, stderr_callback),
);
});

app.on('will-quit', () => {
Expand Down
2 changes: 2 additions & 0 deletions electron-app/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ contextBridge.exposeInMainWorld('settingsAPI', {
GithubUnstageFiles: (query: Query) => ipcRenderer.invoke('settings/github-unstage-files', query),
GithubCommit: (query: Query) => ipcRenderer.invoke('settings/github-commit', query),
GithubClone: (query: Query) => ipcRenderer.invoke('settings/github-clone', query),
GithubCommitAllChanges: (query: Query) =>
ipcRenderer.invoke('settings/github-commit-all-changes', query),
});

declare global {
Expand Down
10 changes: 10 additions & 0 deletions electron-app/src/python/pyrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,16 @@ def post(request):
),
"returncode": 0,
}
elif query == "settings/github-commit-all-changes":
author = data.get("author", None)
email = data.get("email", None)
data = {
"query": query,
"body": runner.github.CommitAllChanges(
data["repo"], data["message"], author, email
),
"returncode": 0,
}

else:
raise NotImplementedError(f"Unknown query: {query}")
Expand Down
1 change: 1 addition & 0 deletions nodemapper/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export type SettingsAPI = {
GithubUnstageFiles: (query: Query) => Promise<Query>;
GithubCommit: (query: Query) => Promise<Query>;
GithubClone: (query: Query) => Promise<Query>;
GithubCommitAllChanges: (query: Query) => Promise<Query>;
};

declare global {
Expand Down
55 changes: 52 additions & 3 deletions nodemapper/src/gui/Settings/RepoOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {

// Github repo status
type ghStatusOpts =
// query states
| 'unknown'
| 'checking'
// return codes
| 'not-a-repo'
| 'up-to-date'
| 'behind'
Expand Down Expand Up @@ -72,8 +74,10 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {
const [ghUntracked, setGhUntracked] = React.useState({ status: 'unknown' } as ghUntrackedProps);

type ghIndicatorOpts =
// query states
| 'unknown'
| 'checking'
// return codes
| 'not-a-repo'
| 'up-to-date'
| 'modified'
Expand All @@ -88,6 +92,12 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {
}
}, []);

const RefreshGhStatus = () => {
setAnchorMenu(null);
setGhIndicator('checking');
GithubGetRepoStatus(repo, branch);
}

const GithubGetRepoStatus = async (repo: string, branch: string) => {
// Check if the repo is in sync with the remote
const query = {
Expand Down Expand Up @@ -146,6 +156,7 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {
};

const onGhPull = async () => {
setGhIndicator('checking');
settingsAPI
.GithubPull({
query: 'settings/github-pull',
Expand All @@ -157,16 +168,19 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {
.then((response) => {
const result = response['body'] as { status: string; message: string };
if (result.status === 'success') {
// Force refresh of the repo status
RefreshGhStatus();
} else {
dispatch(
builderLogEvent(`Error pulling changes from Github for ${repo}: ${result.message}`),
);
setGhIndicator('error');
setGhStatus({ status: 'error', message: result.message });
}
});
};

const onGhPush = async () => {
setGhIndicator('checking');
settingsAPI
.GithubPush({
query: 'settings/github-push',
Expand All @@ -178,11 +192,36 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {
.then((response) => {
const result = response['body'] as { status: string; message: string };
if (result.status === 'success') {
// Force refresh of the repo status
RefreshGhStatus();
} else {
dispatch(
builderLogEvent(`Error pushing changes to Github for ${repo}: ${result.message}`),
);
setGhIndicator('error');
setGhStatus({ status: 'error', message: result.message });
}
});
};

const ghCommitAllChanges = async () => {
settingsAPI
.GithubCommitAllChanges({
query: 'settings/github-commit-all-changes',
data: {
repo: repo,
message: 'Commit from GRAPEVNE',
author: 'GRAPEVNE',
email: null,
},
})
.then((response) => {
const result = response['body'] as { status: string; message: string };
if (result.status === 'success') {
// Force refresh of the repo status
} else {
dispatch(
builderLogEvent(`Error committing changes to Github for ${repo}: ${result.message}`),
);
}
});
};
Expand All @@ -202,7 +241,12 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {
{/* For 'unknown', 'checking' and 'not-a-repo' we don't show the github indicator */}
{ghIndicator === 'up-to-date' && (
<Tooltip title={ghStatus.message}>
<GitHubIcon style={{ color: '#424242' }} />
<GitHubIcon
style={{ color: '#424242' }}
onClick={() => {
RefreshGhStatus();
}}
/>
</Tooltip>
)}
{ghIndicator === 'checking' && (
Expand Down Expand Up @@ -240,6 +284,11 @@ const GithubMenu = ({ repo, branch }: GithubMenuProps) => {
Conflicts with github detected - try pulling the latest changes
</Alert>
)}
{(ghTracked.status === 'some' || ghUntracked.status === 'some') && (
<Button variant="contained" fullWidth onClick={ghCommitAllChanges}>
Commit all changes
</Button>
)}
{(ghStatus.status === 'behind' || ghStatus.status === 'diverged') && (
<Button variant="contained" fullWidth onClick={onGhPull}>
Pull latest changes from github
Expand Down
1 change: 1 addition & 0 deletions runner/runner/github/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .github import Push # noqa: F401
from .github import StageFiles # noqa: F401
from .github import UnstageFiles # noqa: F401
from .github import CommitAllChanges # noqa: F401
53 changes: 53 additions & 0 deletions runner/runner/github/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,56 @@ def Clone(github_url, clone_to_path):
"status": "success",
"message": "Successfully cloned the repository.",
}


def CommitAllChanges(repo_path, commit_message, author_name=None, author_email=None):
"""
Commit all changes in the Git repository with a specified commit message.
:param repo_path: The local path to the Git repository.
:param commit_message: The commit message.
:param author_name: The name of the author (optional).
:param author_email: The email of the author (optional).
:return: None
"""
try:
# Initialize the repository object
repo = git.Repo(repo_path)

# Check if there are any changes to commit
if not repo.is_dirty(untracked_files=True):
return {
"status": "none",
"message": "No changes to commit.",
}

# Stage all changes
repo.git.add("--all")

# Set up the author information if provided
if author_name and author_email:
author = git.Actor(author_name, author_email)
commit = repo.index.commit(commit_message, author=author)
else:
commit = repo.index.commit(commit_message)

except git.exc.InvalidGitRepositoryError:
return {
"status": "error",
"message": "Not a valid git repository.",
}
except git.exc.GitCommandError as e:
return {
"status": "error",
"message": f"Error committing files: {e}",
}
except Exception as e:
return {
"status": "error",
"message": f"An unexpected error occurred: {e}",
}
return {
"status": "success",
"message": "Successfully committed all changes.",
"commit_message": commit.message,
}

0 comments on commit 9fb1fb1

Please sign in to comment.