rulasg / RepoHelper

Powershell module to help manage GitHub Repositories
MIT License
0 stars 1 forks source link

refactor Set-RepoProperties to use GH command #54

Open rulasg opened 8 months ago

rulasg commented 8 months ago

Set the value of a property using GitHub CLI Online documentation

rulasg commented 8 months ago

Can´t make the gh call without error

Sugestions from Team chat

Team Chat

rulasg commented 8 months ago

We need a sample that helps us run a GH API command using -F parameter to set the value of a repo custom property repos/{owner}/{repo}/properties/values Online documentation

Works using curl

curl -L -s -H "Authorization: Bearer $env:GH_TOKEN" -X PATCH [https://api.github.com/repos/{owner}/{repo}/properties/values](https://api.github.com/repos/%7Bowner%7D/%7Brepo%7D/properties/values) -d '{"properties":[{"property_name":"{name}","value":"{value}"}]}'

Sucessfully update using input from file

>   gh api -X Patch /repos/solidifydemo/bit21/properties/values --input ./input.json
* Request at 2024-01-23 13:39:20.926331 +0100 CET m=+0.079066751
* Request to https://api.github.com/repos/solidifydemo/bit21/properties/values
> PATCH /repos/solidifydemo/bit21/properties/values HTTP/1.1
> Host: api.github.com
> Accept: */*
> Authorization: token ████████████████████
> Content-Length: 97
> Content-Type: application/json; charset=utf-8
> Time-Zone: Europe/Madrid
> User-Agent: GitHub CLI 2.40.1

{
  "properties": [
    {
      "property_name": "owner",
      "value": "rulasg22"
    }
  ]
}

< HTTP/2.0 204 No Content
< Access-Control-Allow-Origin: *
< Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
< Content-Security-Policy: default-src 'none'
< Date: Tue, 23 Jan 2024 12:39:21 GMT
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< Server: GitHub.com
< Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
< Vary: Accept-Encoding, Accept, X-Requested-With
< X-Accepted-Oauth-Scopes: repo
< X-Content-Type-Options: nosniff
< X-Frame-Options: deny
< X-Github-Api-Version-Selected: 2022-11-28
< X-Github-Media-Type: github.v3; format=json
< X-Github-Request-Id: C99F:3BBFAD:3A7FEE3:3B2E613:65AFB379
< X-Oauth-Client-Id: 178c6fc778ccc68e1d6a
< X-Oauth-Scopes: gist, read:org, repo, workflow
< X-Ratelimit-Limit: 15000
< X-Ratelimit-Remaining: 14987
< X-Ratelimit-Reset: 1706015638
< X-Ratelimit-Resource: core
< X-Ratelimit-Used: 13
< X-Xss-Protection: 0

Wrong using -Fswitch as the documentation states

Online documentation

# GitHub CLI api
# https://cli.github.com/manual/gh_api

gh api \
  --method PATCH \
  -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  /repos/OWNER/REPO/properties/values \
  -F "properties[]=[object Object]" -F "properties[]=[object Object]" -F "properties[]=[object Object]" 

Trying to make it work

>   gh api -X Patch /repos/solidifydemo/bit21/properties/values -F 'properties[]=["property_name"="owner" "value"="rulasg"]'
* Request at 2024-01-23 15:53:57.065737 +0100 CET m=+0.048965626
* Request to https://api.github.com/repos/cli/cli/releases/latest
> GET /repos/cli/cli/releases/latest HTTP/1.1
> Host: api.github.com
> Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
> Authorization: token ████████████████████
> Content-Type: application/json; charset=utf-8
> Time-Zone: Europe/Madrid
> User-Agent: GitHub CLI 2.40.1

* Request at 2024-01-23 15:53:57.068653 +0100 CET m=+0.051882334
* Request to https://api.github.com/repos/solidifydemo/bit21/properties/values
> PATCH /repos/solidifydemo/bit21/properties/values HTTP/1.1
> Host: api.github.com
> Accept: */*
> Authorization: token ████████████████████
> Content-Length: 69
> Content-Type: application/json; charset=utf-8
> Time-Zone: Europe/Madrid
> User-Agent: GitHub CLI 2.40.1

{
  "properties": [
    "[\"property_name\"=\"owner\" \"value\"=\"rulasg\"]"
  ]
}
rulasg commented 8 months ago

I will say that as it's on beta there is something wrong as gh does not form the json body property.

rulasg commented 8 months ago

Seems that this syntax works : https://github.com/githubpartners/Solidify/issues/328

gh api \
  --method PATCH \
  /repos/properties-game/chess/properties/values \
  -F 'properties[][property_name]'='color' \
  -F 'properties[][value]'='red'
rulasg commented 6 months ago

For multi value request:

echo '{"properties": [{"property_name":"kk","value":"kk2"},{"property_name":"Prop1","value":"prop1-2"}]}' | gh api --method PATCH /repos/solidifydemo/rulasg-copilot-demo1/properties/values --input -

It works with this syntax:

gh api \
  --method PATCH \
  /repos/properties-game/chess/properties/values \
  -F 'properties[][property_name]'='color' \
  -F 'properties[][value]'='red'

but only for one property (color in the example)

In order to update more than 1 property on the same request, users can use the --input param. It supports a body file, or it can be piped with standard input -:

echo '{"properties": [{"property_name": "color", "value": "red"}] }' | GH_DEBUG=api gh api --method PATCH /repos/properties-game/chess/properties/values --input -

Most of this is common guidance for the gh api command dealing with JSON request bodies.