community icon indicating copy to clipboard operation
community copied to clipboard

plugin: add gh cli to vela-git

Open plyr4 opened this issue 1 year ago • 5 comments

Description

it would be sweet if users could automatically interact with git using gh the official cli.

we can add it to the plugin, meaning, throw in a gh installation into the Dockerfile and call it good https://github.com/go-vela/vela-git/blob/main/Dockerfile#L19

but i dont think gh supports using the netrc for gh auth login so it likely wont work "out of the box" for users unless they provide a PAT

https://github.com/cli/cli/issues/288 https://github.com/cli/cli/issues/3623

so... if we figure out auth, or move Vela away from netrc then this will be easier. or we can just install gh and let users figure that out on their own, but i dont think thats the best user experience. personally, i would expect it to "just work".

plyr4 avatar May 10 '24 14:05 plyr4

I'm okay with the baby steps approach. Add in the CLI pkg and let the user figure out how to get the token in there for now. Maybe someone will come along and add netrc support in CLI/cli#288 or Vela or this plugin can provision an envvar secret or build the gh-compatible config at runtime.

colindean avatar May 10 '24 15:05 colindean

I think it might be possible to write a simple netrc -> gh hosts.yaml converter.

machine git.mycompany.com login myusername password gho_0000000000000000000
git.mycompany.com:
    user: myusername
    oauth_token: gho_0000000000000000000
    git_protocol: https
    users:
        myusername:
            oauth_token: gho_0000000000000000000

https://pkg.go.dev/golang.org/x/tools/cmd/auth/netrcauth provides a netrc impl, could spit out the YAML without much fuss.

Could include it as a separate tool or a subcommand of vela-git, invocable like:

steps:
  - name: Convert netrc to gh yaml
    image: target/vela-git:latest
    commands:
      # subcommand
      - vela-git convert-netrc-to-gh-hosts
      # helper script
      - convert-netrc-to-gh-hosts
  - name: Do stuff with gh
    image: target/vela-git:latest
    commands:
      # subcommand
      - gh pr list

colindean avatar May 10 '24 18:05 colindean

A S.O. question on handling the netrc parsing:

https://stackoverflow.com/questions/30024237/search-netrc-file-in-bash-and-return-username-for-given-machine

colindean avatar May 10 '24 18:05 colindean

I polished up something for myself: https://github.com/colindean/hejmo/commit/0062264618a45ec9cd31f072830e38481777a9f3

colindean avatar May 10 '24 19:05 colindean