我们能否在为 Github Issue 设置的工作流文件中按需设置输入的默认值

Can we set default values for inputs on demand in your workflow file set for Github Issue

提问人:Ashar 提问时间:9/15/2023 更新时间:9/17/2023 访问量:75

问:

下面是我寻求用户输入的 github 操作ISSUE_TEMPLATE。

name: GH Action details

description: GH Issue form

title: "GH Action details"

assignees:

  - my-devops

labels:

  - "gh inventory"

body:

  - type: markdown

    attributes:

      value: |

        ### Want to add?

       

        ---

       

        Please fill out the information below 

  - type: input

    id: repository-name

    attributes:

      label: Repository Name

      description: Provide the name of the repository

    validations:

      required: true

以下是用户评论前后问题表单的外观。

enter image description here

读取和显示用户输入的所需输出。enter image description here

我希望使用 PowerShell 提交类似的问题评论。尝试了以下操作 [您可以尝试使用 ISE、powershell 在系统上运行]:Powershell 代码如下:

$Token = "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"
$RepoOwner = "knowyrtech"
$RepoName = "mongomaskwinvslinux"

 

$IssueTitle = "GH Action Onboard inventory details"

 

# Create a hashtable for the input values
$inputValues = @{
    "ref" = "main"
    "inputs" = @{
        "repository-name" = "YourRepositoryName"
    }
}

# Convert the hashtable to JSON
$inputJson = $inputValues | ConvertTo-Json


 
$ApiUrl = "https://api.github.com/repos/$RepoOwner/$RepoName/issues"

$IssueData = @{
    title = $IssueTitle
    body  = $inputJson
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $ApiUrl -Method Post -Headers @{
    "Authorization" = "token $Token"
    "Accept"        = "application/vnd.github.v3+json"
} -ContentType "application/json" -Body $IssueData


$response

运行此操作会创建一个注释,但不是上面共享的所需输出快照。

我也试过了

$inputValues = @{
    "ref" = "main"
    "inputs" = @{
        "repository-name" = @{
            "default" = 'wowo'
        }
    }
}


$inputJson = $inputValues | ConvertTo-Json

但是,无法注释为输入,如下所示:

enter image description here

enter image description here

恳请建议。

PowerShell github-actions github-api 接口 默认值 github-issues

评论


答:

1赞 VonC 9/17/2023 #1

在脚本中,您将创建一个 JSON 对象,该对象表示似乎是操作工作流的输入,并将该 JSON 对象直接用作 GitHub 问题的正文:

$inputValues = @{
    "ref" = "main"
    "inputs" = @{
        "repository-name" = "YourRepositoryName"
    }
}
$inputJson = $inputValues | ConvertTo-Json
...
$IssueData = @{
    title = $IssueTitle
    body  = $inputJson
} | ConvertTo-Json

由于通过 API 创建的 GitHub 问题本身不支持以这种方式直接填充问题表单字段。GitHub Issue Forms 和 GitHub API 分开工作;API 不提供直接填充“问题表单”字段的功能。

您可以尝试使用 Markdown 格式的字符串来表示问题正文,而不是尝试使用 JSON 对象来表示问题正文,其中包括要以结构化方式包含的默认值,该结构化方式在视觉上类似于 GitHub 问题表单:

$IssueBody = @"
### Want to add?

---

Please fill out the information below 

**Repository Name**
$RepositoryName
"@
...
$IssueData = @{
    title = $IssueTitle
    body  = $IssueBody
} | ConvertTo-Json

该变量包含一个具有 Markdown 格式的字符串,在 GitHub 平台上查看时,该字符串的结构类似于 GitHub Issue Form 的信息。$IssueBody

完整脚本:

$Token = "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"
$RepoOwner = "knowyrtech"
$RepoName = "mongomaskwinvslinux"

$IssueTitle = "GH Action Onboard inventory details"

# Define the repository name (or any other values you want to set as default)
$RepositoryName = "YourRepositoryName"

# Create a Markdown formatted string that represents the issue body
$IssueBody = @"
### Want to add?

---

Please fill out the information below 

**Repository Name**
$RepositoryName
"@

$ApiUrl = "https://api.github.com/repos/$RepoOwner/$RepoName/issues"

$IssueData = @{
    title = $IssueTitle
    body  = $IssueBody
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $ApiUrl -Method Post -Headers @{
    "Authorization" = "token $Token"
    "Accept"        = "application/vnd.github.v3+json"
} -ContentType "application/json" -Body $IssueData

$response