Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a 'commit-message-body' field? #2634

Closed
hwhsu1231 opened this issue Dec 26, 2023 · 3 comments
Closed

Provide a 'commit-message-body' field? #2634

hwhsu1231 opened this issue Dec 26, 2023 · 3 comments

Comments

@hwhsu1231
Copy link

hwhsu1231 commented Dec 26, 2023

Subject of the issue

Hello, create-pull-request Development Team.

From the README.md, I understand that we can customize the PR Description using either the body or body-path field, but there is only a commit-message field for customizing the content of the Commit Message.

If possible, I hope it can provide a commit-message-body field as well.

Here's why I'm requesting this.

(1) Storing Formatted JSON Objects

I want to read a specific object (.object) from a JSON file (source.json) and make it accessible to subsequent steps.

{
  "object" :
  {
    "name1" : "value1",
    "name2" : "value2"
  }
}

However, I found many problems when trying to store a formatted JSON object via an environment variable, mainly because the formatted JSON object contains newline characters \n.

After some attempts, my solution was:

  1. Use cat command to read the entire content of the source.json file.
  2. Use jq command to query the JSON object I want.
  3. Use sed command to replace \\n with \n.
  4. Store it in an environment variable JSON_OBJ.
  5. Use echo command to print the content of this environment variable to a new file, json-obj.txt.

If needed later, it can be read using $(<json-obj.txt). Here is a simple demonstration:

- name: jq from source.json to json-obj.txt
  run: |
    JSON_OBJ=$(cat source.json | jq '.object' -r | sed 's/\\n/\n/g')
    echo "$JSON_OBJ" > json-obj.txt

(2) Wanting to Include json-obj.txt in the PR's Description or Commit Message

Furthermore, if I want to include the contents of json-obj.txt in the Description or Commit Message of the PR I'm creating. For example, the following content:

The following JSON object:
{
  "name1" : "value1",
  "name2" : "value2"
}
is what I want!

I found that I couldn't use $(<json-obj.txt) in the body or commit-message fields. So, I first saved the content I wanted for the Description to a file named body.txt. Then I used the body-path field to specify the body.txt file as the content for the Description. The implementation is as follows:

- name: Prepare PR's description
  run: |
    echo "The following JSON object:"  > $(pwd)/body.txt
    echo "$(<json-obj.txt)"           >> $(pwd)/body.txt
    echo "is what I want!"            >> $(pwd)/body.txt
    echo "BODY_PATH=$(pwd)/body.txt"  >> $GITHUB_ENV

- name: Create a Pull Request
  id: cpr
  uses: peter-evans/create-pull-request@v5
  with:
    token: ${{ secrets.GITHUB_TOKEN }}
    base: 'master'
    branch: 'test/json/object'
    title: |
      chore: Test json object'
    body-path:
      ${{ env.BODY_PATH }}
      

But since create-pull-request only provides a body-path, this method can only be applied to the PR Description.

That's why I hope create-pull-request could provide a commit-message-body field.

@peter-evans
Copy link
Owner

Hi @hwhsu1231

The commit-message input works the same as it does in git. If you leave a blank line after the message, anything else will populate the commit message description.

e.g.

          commit-message: |
            Add report file

            This is a test commit message body.
            You can use multiple lines.
            Some JSON:
            {
              "test": "test",
              "test2": "test2"
            }

image

Hope that helps you with what you are trying to do.

@hwhsu1231
Copy link
Author

Hello, @peter-evans

That's not what I want! According to the demo you provided:

          commit-message: |
            Add report file

            This is a test commit message body.
            You can use multiple lines.
            Some JSON:
            {
              "test": "test",
              "test2": "test2"
            }

That means the JSON content will be hardcoded into the commit message whenver the PR is created by this GitHub Workflow.

However, in my consideration, the content of source.json will be updated continuously. Or should I say source.json file will be updated due to this GitHub Workflow is triggered.

That's why I need to extract some information from source.json file and record them into the commit message of the PR.

@peter-evans
Copy link
Owner

@hwhsu1231 Sorry, I misunderstood what you were trying to do.

You can handle multiline content from a file into a step variable like this.

      - id: get-commit-message
        run: |
          body=$(cat commit-message.txt)
          delimiter="$(openssl rand -hex 8)"
          echo "body<<$delimiter" >> $GITHUB_OUTPUT
          echo "$body" >> $GITHUB_OUTPUT
          echo "$delimiter" >> $GITHUB_OUTPUT

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          body: ${{ steps.get-commit-message.outputs.body }}

Just make sure you format the commit-message.txt file correctly. The first line is the "title," then leave one blank line before the "description."

commit message title

commit message body which can be multiline
another line
Some JSON:
{
  "test": "test",
  "test2": "test2"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants