Skip to content

Commit

Permalink
feat: improve secret handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Jan Noort committed Jun 7, 2024
1 parent 62067c6 commit c24de76
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 39 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
secret.lua
res.lua
10 changes: 5 additions & 5 deletions lua/ado.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ end
local subcommand_tbl = {
load = {
complete_args = { "context", "threads" },
impl = function(args, opts)
impl = function(args, _)
local sub_impl = {
context = function()
if not state_manager then
Expand All @@ -58,7 +58,7 @@ local subcommand_tbl = {
},
submit = {
complete_args = { "comment", "vote", "thread_status" },
impl = function(args, opts)
impl = function(args, _)
local sub_impl = {
comment = function()
require("ado.thread").submit_comment(M.get_loaded_state())
Expand All @@ -75,7 +75,7 @@ local subcommand_tbl = {
},
open = {
complete_args = { "quickfix", "thread_picker", "new_thread", "existing_thread" },
impl = function(args, opts)
impl = function(args, _)
local sub_impl = {
quickfix = function()
require("ado.quickfix").render_quickfix(M.get_loaded_state().pull_request_threads)
Expand All @@ -97,7 +97,6 @@ local subcommand_tbl = {

---@param opts table
function M.ado_pure(opts)
vim.notify(vim.inspect(opts))
local fargs = opts.fargs
local subcommand_key = fargs[1]

Expand All @@ -120,7 +119,7 @@ end

vim.api.nvim_create_user_command("AdoPure", M.ado_pure, {
nargs = "*",
range=true,
range = true,
desc = "Azure DevOps Pull Request command.",
complete = function(arg_lead, cmdline, _)
local subcmd_key, subcmd_arg_lead = cmdline:match("^AdoPure*%s(%S+)%s(.*)$")
Expand All @@ -139,4 +138,5 @@ vim.api.nvim_create_user_command("AdoPure", M.ado_pure, {
end
end,
})

return M
34 changes: 12 additions & 22 deletions lua/ado/api.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local M = {}
local curl = require("plenary.curl")
local Secret = require("ado.secret")
local config = require("ado.config.internal")

local API_VERSION = "api-version=7.1-preview.4"
local GIT_API_VERSION = "api-version=7.1-preview.1"

local access_token = Secret.access_token
PROJECT_NAME = ""
local access_token = config:access_token()
local organization_url = ""
local project_name = ""

local headers = {
["Authorization"] = "basic " .. access_token,
Expand Down Expand Up @@ -34,27 +34,17 @@ local function get_azure_devops(url, request_type)
return result, nil
end

---Get projects from Azure DevOps
---@return Project[] projects, string|nil err
function M.get_projects()
local result, err = get_azure_devops(Secret.organization_url .. "_apis/projects?" .. API_VERSION, "projects")
if not result then
return {}, err or "Could not retrieve projects"
end

---@type Project[]
local projects = result.value
return projects, err
end

---Get repository from Azure DevOps
---@param context AdoContext
---@return Repository|nil repository, string|nil err
function M.get_repository(context)
if PROJECT_NAME == "" then
PROJECT_NAME = context.project_name
if organization_url == "" then
organization_url = context.organization_url
end
if project_name == "" then
project_name = context.project_name
end
local url = Secret.organization_url .. PROJECT_NAME .. "/_apis/git/repositories?" .. GIT_API_VERSION
local url = organization_url .. project_name .. "/_apis/git/repositories?" .. GIT_API_VERSION
local result, err = get_azure_devops(url, "repositories")
if not result then
return nil, err
Expand Down Expand Up @@ -215,7 +205,8 @@ end
function M.submit_vote(pull_request, vote)
---@type ConnectionData
---@diagnostic disable-next-line: assign-type-mismatch
local connection_result, connection_err = get_azure_devops(Secret.organization_url .. "/_apis/connectionData", "connectionData")
local connection_result, connection_err =
get_azure_devops(organization_url .. "/_apis/connectionData", "connectionData")
if connection_err then
return nil, connection_err
end
Expand All @@ -232,5 +223,4 @@ function M.submit_vote(pull_request, vote)
)
return result, err
end

return M
Empty file removed lua/ado/config.lua
Empty file.
26 changes: 26 additions & 0 deletions lua/ado/config/internal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---@class adopure.InternalConfig
---@field pat_token string|nil
local InternalConfig = {}
function InternalConfig:new()
local default_config = {
pat_token = os.getenv("AZURE_DEVOPS_EXT_PAT"),
}
local user_config = type(vim.g.adopure) == "function" and vim.g.adopure() or vim.g.adopure or {}
local config = vim.tbl_deep_extend("force", default_config, user_config or {})
self.__index = self
self = setmetatable(config, self)
return self
end

function InternalConfig:access_token()
local message = table.concat({
"No pat_token found in config.",
"Set AZURE_DEVOPS_EXT_PAT environment variable,",
"or check the docs to find other ways of configuring it.",
}, " ")
assert(self.pat_token, message)
return require("b64").enc(":" .. self.pat_token)
end

local config = InternalConfig:new()
return config
9 changes: 9 additions & 0 deletions lua/ado/config/meta.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---@class adopure.Config
---@field pat_token? string

local config = {}

---@type adopure.Config | fun():adopure.Config | nil
vim.g.adopure = vim.g.adopure

return config
45 changes: 44 additions & 1 deletion lua/ado/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,54 @@ function M.get_repo_name()
})
get_git_repo_job:start()

local repository_path = require("plenary.path"):new(require("ado.utils").await_result(get_git_repo_job))
local repository_path = require("plenary.path"):new(require("ado.utils").await_result(get_git_repo_job)[1])
local path_parts = vim.split(repository_path.filename, repository_path.path.sep)
return path_parts[#path_parts]
end

---@param remote_url string
---@return string organization_url
---@return string project_name
---@return string repository_name
local function extract_git_details(remote_url)
local organization_url, project_name, repository_name
if remote_url:find("@ssh.dev.azure.com") then
local _, _, base_url, org_name, project_name_extracted, repo_name_extracted =
remote_url:find(".-@(ssh.dev.azure.com):v3/(.-)/(.-)/(.+)%s*%(fetch%)")
organization_url = "https://" .. base_url:gsub("ssh.", "") .. "/" .. org_name
project_name = project_name_extracted
repository_name = repo_name_extracted
elseif remote_url:find("https") then
local https_pattern = "(https://)[^@]*@([^/]+)/([^/]+)/([^/]+)/_git/([^%s]+)"
local _, _, protocol, domain, org_name, project_name_extracted, repo_name_extracted =
remote_url:find(https_pattern)
organization_url = protocol .. domain .. "/" .. org_name
project_name = project_name_extracted
repository_name = repo_name_extracted
end

local trim_pattern = "^%s*(.-)%s*$"
return organization_url:gsub(trim_pattern, "%1") .. "/",
project_name:gsub(trim_pattern, "%1"),
repository_name:gsub(trim_pattern, "%1")
end

---Get config from git remote
---@return string organization_url
---@return string project_name
---@return string repository_name
function M.get_remote_config()
local get_remotes = require("plenary.job"):new({
command = "git",
args = { "remote", "-v" },
cwd = ".",
})
get_remotes:start()
---@type string
local remote = require("ado.utils").await_result(get_remotes)[1]
return extract_git_details(remote)
end

---@param pull_request PullRequest
---@param open_callable function
function M.confirm_checkout_and_open(pull_request, open_callable)
Expand Down
13 changes: 5 additions & 8 deletions lua/ado/state.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
---@class AdoContext
---@field repository_name string
---@field organization_url string
---@field project_name string
---@field repository_name string
local AdoContext = {}
---@return AdoContext
function AdoContext:new()
local repository_name = require("ado.git").get_repo_name()
local project_name, err = require("ado.project").get_project_name(repository_name)
if err then
error(err)
end
assert(project_name, "No project name found;")
local organization_url, project_name, repository_name = require("ado.git").get_remote_config()
local o = {
repository_name = repository_name,
organization_url = organization_url,
project_name = project_name,
repository_name = repository_name,
}
self.__index = self
return setmetatable(o, self)
Expand Down
2 changes: 1 addition & 1 deletion lua/ado/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function M.await_result(job)
end
vim.wait(1000, function()
---@diagnostic disable-next-line: missing-return
result = job:result()[1]
result = job:result()
end)
end
end
Expand Down

0 comments on commit c24de76

Please sign in to comment.