From 0c0bb939e5833f3aa0a63bcd0f0cda6b3bf50587 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Tue, 22 Mar 2022 22:58:05 -0400 Subject: [PATCH] Add mgr-gitlab --- .doom.d/config.el | 16 ++++- .doom.d/lisp/mgr-gitlab/mgr-gitlab.el | 95 +++++++++++++++++++++++++++ .doom.d/packages.el | 4 +- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 .doom.d/lisp/mgr-gitlab/mgr-gitlab.el diff --git a/.doom.d/config.el b/.doom.d/config.el index adeaf04..d198a3a 100644 --- a/.doom.d/config.el +++ b/.doom.d/config.el @@ -3,7 +3,6 @@ ;; Place your private configuration here! Remember, you do not need to run 'doom ;; sync' after modifying this file! - ;; Some functionality uses this to identify you, e.g. GPG configuration, email ;; clients, file templates and snippets. (setq user-full-name "Max Regan" @@ -63,3 +62,18 @@ (after! counsel (ivy-rich-mode 1) (all-the-icons-ivy-rich-mode 1)) + +(use-package! mgr-gitlab + :load-path "~/.doom.d/lisp/mgr-gitlab" + :config + (setq gitlab-host "gitlab.maxregan.me" + gitlab-token-id (if (file-exists-p mgr-gitlab-token-file) + (with-temp-buffer + (insert-file-contents mgr-gitlab-token-file) + (string-trim (buffer-string))) + nil) + dashboard-projects-backend 'projectile + dashboard-items '((mgr-gitlab-gitlab-projects . 10) + (recents . 5) + (agenda . 5) + (projects . 5)))) diff --git a/.doom.d/lisp/mgr-gitlab/mgr-gitlab.el b/.doom.d/lisp/mgr-gitlab/mgr-gitlab.el new file mode 100644 index 0000000..de244b3 --- /dev/null +++ b/.doom.d/lisp/mgr-gitlab/mgr-gitlab.el @@ -0,0 +1,95 @@ +;;; mgr-gitlab.el --- Description -*- lexical-binding: t; -*- +;; +;; Copyright (C) 2022 Max Regan +;; +;; Author: Max Regan +;; Maintainer: Max Regan +;; Created: March 17, 2022 +;; Modified: March 17, 2022 +;; Version: 0.0.1 +;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp +;; Homepage: https://github.com/reganmax/mgr-gitlab +;; Package-Requires: ((emacs "27.1")) +;; +;; This file is not part of GNU Emacs. +;; +;;; Commentary: +;; +;; Description: My personal configuration for Gitlab +;; +;;; Code: + +(require 'gitlab) +(require 'dashboard) +(require 'projectile) +(require 'seq) + +(defvar mgr-gitlab-project-path "~/repos/" "The location into which gitlab projects will be cloned.") +(defvar mgr-gitlab-token-file "~/.gitlab_token" "The location into which gitlab projects will be cloned.") + +(defun mgr-gitlab-list-remote-project-names () + "Return a list of all of the project names on the remote server." + (seq-map (lambda (el) (alist-get 'name el)) (gitlab-list-all-projects))) + +(defun mgr-gitlab-list-local-project-names () + "Return a list of all of the project names in the mgr-gitlab-project-path." + (seq-remove #'file-directory-p (directory-files mgr-gitlab-project-path))) + +(defun mgr-gitlab--get-project-by-name (name) + "Return the alist for the project named NAME." + (seq-find (lambda (el) (string-equal (alist-get 'name el) name)) (gitlab-list-projects))) + +(defun mgr-gitlab--get-id-for-name (name) + "Return an ID for a given project NAME." + (alist-get 'id (mgr-gitlab--get-project-by-name name))) + +(defun mgr-gitlab-clone (name) + "Clone the project NAME." + ;; TODO: Automatically set remote.pushDefault so it does not prompt + ;; TODO: Clone with API token, if there is one + (interactive + (list (completing-read "Project: " (mgr-gitlab-list-remote-project-names)))) + (let* ((url (mgr-gitlab--get-url-by-name name)) + (dest mgr-gitlab-project-path)) + (magit-clone-regular url dest nil))) + +(defun mgr-gitlab-clone-all () + "Clone all projects in Gitlab, iteratively." + (interactive) + (seq-do (lambda (name) + (if (not (seq-contains-p (mgr-gitlab-list-local-project-names) name)) + (progn + (message (format "Cloning %s..." name)) + (mgr-gitlab-clone name)) + (message (format "Skipping %s." name)))) + (mgr-gitlab-list-remote-project-names))) + +(defun mgr-gitlab--get-url-by-name (name) + "Return a fixed-up repository url for the given project NAME. +This also fixes up the capitalization in the project name that +gitlab so kindly removes." + (let* ((repo (mgr-gitlab--get-project-by-name name)) + (orig-url (alist-get 'http_url_to_repo repo))) + (concat (mapconcat 'identity (butlast (split-string orig-url "/")) "/") + "/" + name + ".git"))) + +(defun mgr-gitlab-dashboard-insert-gitlab-projects (list-size) + "A custom dashboard.el widget to display LIST-SIZE gitlab projects." + (dashboard-insert-section + "Gitlab Projects:" + (cl-map 'list (lambda (el) (alist-get 'name el)) (gitlab-list-all-projects)) + list-size + 'projects + "P" + `(lambda (&rest _) + (let ((dir (concat mgr-gitlab-project-path ,el))) + (if (not (file-directory-p dir)) + (mgr-gitlab-clone ,el)) + (magit-status (concat mgr-gitlab-project-path ,el)))) + (format "%s" el))) + +(add-to-list 'dashboard-item-generators '(mgr-gitlab-gitlab-projects . mgr-gitlab-dashboard-insert-gitlab-projects)) + +(provide 'mgr-gitlab) diff --git a/.doom.d/packages.el b/.doom.d/packages.el index 4352b4c..17ad829 100644 --- a/.doom.d/packages.el +++ b/.doom.d/packages.el @@ -55,6 +55,8 @@ (package! git-auto-commit-mode) +;; required for mgr-gitlab +(package! gitlab) + ;; Testing (package! dashboard) -