Skip to content

Instantly share code, notes, and snippets.

@Link-
Last active April 26, 2024 19:52
Show Gist options
  • Save Link-/f817f7523a3ac5de6861efd7be6bc3fb to your computer and use it in GitHub Desktop.
Save Link-/f817f7523a3ac5de6861efd7be6bc3fb to your computer and use it in GitHub Desktop.
Write your first workflow with GitHub Actions and GitHub APIs - beginner friendly tutorial!
name: Create a comment on new issues
on:
issues:
types: [opened]
permissions: write-all
jobs:
comment-with-action:
runs-on: ubuntu-latest
steps:
- name: "dump github context"
run: echo '${{ toJSON(github.event) }}' | jq
shell: bash
- name: Create comment
uses: peter-evans/create-or-update-comment@v1
with:
issue-number: ${{ github.event.issue.number }}
body: |
This is a multi-line test comment
- With GitHub **Markdown** :sparkles:
- Created by [create-or-update-comment][1]
[1]: https://github.com/peter-evans/create-or-update-comment
reactions: '+1'
comment-with-api:
runs-on: ubuntu-latest
steps:
- name: Create comment with API
run: |
gh api -X POST \
https://api.github.com/repos/${ORGANIZATION}/${REPOSITORY}/issues/${ISSUE_NUMBER}/comments \
-f body='
Comment but from the API call not action
'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ORGANIZATION: ${{ github.event.organization.login }}
REPOSITORY: ${{ github.event.repository.name }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
@Mawulijo
Copy link

this worked.
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ORGANIZATION: ${{ github.event.repository.owner.login }} #JSON structure changed
REPOSITORY: ${{ github.event.repository.name }}
ISSUE_NUMBER: ${{ github.event.issue.number }}

@ritik-gupta
Copy link

The action version has been upgraded. Please use peter-evans/create-or-update-comment@v2

@dgwyer
Copy link

dgwyer commented May 17, 2023

The action version has been upgraded. Please use peter-evans/create-or-update-comment@v2

It's on v3 now! :)

@Link-
Copy link
Author

Link- commented Jun 13, 2023

this worked. env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ORGANIZATION: ${{ github.event.repository.owner.login }} #JSON structure changed REPOSITORY: ${{ github.event.repository.name }} ISSUE_NUMBER: ${{ github.event.issue.number }}

This changes the API call from a repository belonging to an organisation to one belonging to a specific user. I know many are facing this difficulty but it's not really hard and I encourage everyone to create a free organisation and experiment with it (as opposed to just repositories).

@h3ct0rjs
Copy link

h3ct0rjs commented Aug 8, 2023

Sometimes it is required to add permission to the Github actions, so you can use :

permissions: write-all

@SashaDesigN
Copy link

The action version has been upgraded. Please use peter-evans/create-or-update-comment@v2

thx worked with permission: write-all flag

@erobakiewicz
Copy link

omg, it worked with permissions: write-all added to both actions and with ${{ github.event.repository.owner.login }}

thanks guys @Mawulijo @h3ct0rjs

@Link- please update your otherwise great course

@Link-
Copy link
Author

Link- commented Aug 16, 2023

I added the permissions: write-all - this was created before GITHUB_TOKEN permissions were introduced :)

@h3ct0rjs
Copy link

Nice @Link- thank you for adding it. I was coming from working with Gitlab CICD and your course was a great resource. Also the official docs so thank you

@Gerald-Izuchukwu
Copy link

I am currently facing this issue and I will appreciate any help I can get

Run gh api -X POST
gh api -X POST
https://api.github.com/repo/${ORGANIZATION}/${REPOSITORY}/issues/${ISSUE_NUMBER/comments}
-f body=" Comment from Github API Bot"
shell: /usr/bin/bash -e {0}
env:
GITHUB_TOKEN: ***
ORGANIZATION: Gerald-Izuchukwu
REPOSITORY: Meal-Microservice-App
ISSUE_NUMBER: 10
gh: Not Found (HTTP 404)
{"message":"Not Found","documentation_url":"https://docs.github.com/rest"}
Error: Process completed with exit code 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment