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

140 views Asked by At

Below is my github actions ISSUE_TEMPLATE that seeks input from user.

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

Below is how the issue form looks before and after user comments.

enter image description here

Desired output where the user input is read and displayed. enter image description here

I wish to submit a similar issue comment using PowerShell. Tried the below [You can try to run on your system using ISE, powershell]: Powershell code below:

$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

Running this creates a comment but not as desired output snapshot shared above.

I also tried

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


$inputJson = $inputValues | ConvertTo-Json

However, unable to comment as inputs as you can see below:

enter image description here

enter image description here

Kindly suggest.

1

There are 1 answers

0
VonC On BEST ANSWER

In your script, you are creating a JSON object representing what seems to be inputs for an action workflow and used that JSON object directly as the body of the GitHub issue:

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

Since GitHub issues created through the API do not natively support the direct population of issue form fields in this manner. GitHub Issue Forms and the GitHub API work separately; the API does not provide functionality to populate Issue Forms fields directly.

Instead of trying to use a JSON object to represent the issue body, you can try and use a Markdown formatted string to represent the issue body, which includes the default values you want to include in a structured manner that visually resembles your GitHub Issue Form:

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

---

Please fill out the information below 

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

The $IssueBody variable contains a string with Markdown formatting, which structures the information similarly to your GitHub Issue Form when viewed on the GitHub platform.

Full script:

$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