go-vela / community

Community Information for Vela (Target's official Pipeline Automation Framework)
https://go-vela.github.io/docs/
Apache License 2.0
22 stars 3 forks source link

plugin: add gh cli to vela-git #975

Open plyr4 opened 2 months ago

plyr4 commented 2 months ago

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".

colindean commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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

colindean commented 1 month ago

Done in bd222d4f543275c1085025f0f2bdff570a96691c