Skip to content

Commit 6a786f5

Browse files
authored
Merge pull request #93 from INCATools/comment-trigger
ontobot now reads comments of an issue too!
2 parents d5458d6 + 37f0902 commit 6a786f5

File tree

4 files changed

+198
-119
lines changed

4 files changed

+198
-119
lines changed

.github/workflows/new-pr-java.yml

+39-20
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
# on:
44
# workflow_dispatch:
55
# issues:
6-
# types: [ opened, edited ]
6+
# types: [opened, edited]
7+
# issue_comment:
8+
# types: [created, edited]
79

810
# jobs:
911
# check:
1012
# runs-on: ubuntu-latest
1113
# outputs:
1214
# phraseExists: ${{ steps.check-body.outputs.result }}
15+
# commentId: ${{ steps.check-body.outputs.commentId }}
1316
# steps:
14-
# - name: Check if issue body contains 'Hey ontobot'
17+
# - name: Check if issue body or any comment contains 'Hey ontobot'
1518
# id: check-body
1619
# uses: actions/github-script@v6
1720
# with:
@@ -21,11 +24,27 @@
2124
# repo: context.repo.repo,
2225
# issue_number: context.issue.number
2326
# });
24-
# if (!issue.data.body) {
25-
# console.log('Issue body is empty or null');
26-
# return false;
27+
28+
# let bodyText = issue.data.body ? issue.data.body.toLowerCase() : '';
29+
# let commentId = null;
30+
31+
# const comments = await github.rest.issues.listComments({
32+
# owner: context.repo.owner,
33+
# repo: context.repo.repo,
34+
# issue_number: context.issue.number
35+
# });
36+
37+
# for (const comment of comments.data) {
38+
# if (comment.body.toLowerCase().includes('hey ontobot')) {
39+
# bodyText += ' ' + comment.body.toLowerCase();
40+
# commentId = comment.id;
41+
# }
2742
# }
28-
# return issue.data.body.includes('Hey ontobot');
43+
44+
# return {
45+
# result: bodyText.includes('hey ontobot'),
46+
# commentId: commentId
47+
# };
2948

3049
# build:
3150
# needs: check
@@ -35,7 +54,7 @@
3554
# strategy:
3655
# matrix:
3756
# python-version: ["3.9"]
38-
# os: [ ubuntu-latest ]
57+
# os: [ubuntu-latest]
3958

4059
# steps:
4160
# - name: Checkout main branch
@@ -45,33 +64,31 @@
4564
# id: gh-script-issue
4665
# uses: actions/github-script@v6
4766
# with:
48-
# # github-token: ${{ secrets.GH_TOKEN }}
4967
# script: |
50-
# const issue_number = context.issue.number
51-
# const repo = context.repo.owner+"/"+context.repo.repo
52-
# return issue_number
53-
68+
# const issue_number = context.issue.number;
69+
# return issue_number;
70+
5471
# - name: Return repository name
5572
# id: gh-script-repo
5673
# uses: actions/github-script@v6
5774
# with:
58-
# # github-token: ${{ secrets.GH_TOKEN }}
5975
# script: |
60-
# const repo = context.repo.owner+"/"+context.repo.repo
61-
# return repo
76+
# const repo = context.repo.owner + "/" + context.repo.repo;
77+
# return repo;
6278

6379
# - name: Set branch name
6480
# id: vars
6581
# run: |
6682
# echo "resource=src/envo/envo-edit.owl" >> $GITHUB_ENV
67-
# echo "branch-name=kgcl_automation_"${{ steps.gh-script-issue.outputs.result }} >> $GITHUB_ENV
68-
83+
# echo "branch-name=kgcl_automation_${{ steps.gh-script-issue.outputs.result }}" >> $GITHUB_ENV
84+
# echo "comment-id=${{ needs.check.outputs.commentId }}" >> $GITHUB_ENV
85+
6986
# - name: Get jar & enable plugin.
7087
# run: |
7188
# mkdir -p robot-plugins
7289
# wget https://github.com/gouttegd/kgcl-java/releases/download/kgcl-java-0.3.2/kgcl-robot-plugin-0.3.2.jar -O robot-plugins/kgcl.jar
7390
# echo "ROBOT_PLUGINS_DIRECTORY=$(pwd)/robot-plugins/" >> "$GITHUB_ENV"
74-
91+
7592
# - name: Install dependencies
7693
# run: |
7794
# pip install ontobot-change-agent
@@ -82,20 +99,22 @@
8299
# ochange process-issue ${{ env.resource }} \
83100
# -r ${{ steps.gh-script-repo.outputs.result }} \
84101
# -n ${{ steps.gh-script-issue.outputs.result }} \
85-
# -g ${{ secrets.GH_TOKEN }}
102+
# -g ${{ secrets.GH_TOKEN }} \
103+
# -c ${{ env.comment-id }}
86104
# # ! OR using standalone JAR file.
87105
# # ! ochange process-issue ${{ env.resource }} \
88106
# # ! -r ${{ steps.gh-script-repo.outputs.result }} \
89107
# # ! -n ${{ steps.gh-script-issue.outputs.result }} \
90108
# # ! -g ${{ secrets.GH_TOKEN }} \
109+
# # ! -p ${{ env.comment-id }} \
91110
# # ! -j kgcl-robot.jar
92111

93112
# - name: Clean-up
94113
# run: rm -rf robot-plugins
95114

96115
# - name: Create Pull Request
97116
# uses: peter-evans/create-pull-request@v5
98-
# if: ${{ env.PR_TITLE}}
117+
# if: ${{ env.PR_TITLE }}
99118
# with:
100119
# token: ${{ secrets.GH_TOKEN }}
101120
# branch-suffix: short-commit-hash

.github/workflows/new-pr.yml

+38-20
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
# name: Create new pull request
1+
# # name: Create new pull request
22

33
# on:
44
# workflow_dispatch:
55
# issues:
6-
# types: [ opened, edited ]
6+
# types: [opened, edited]
7+
# issue_comment:
8+
# types: [created, edited]
79

810
# jobs:
911
# check:
1012
# runs-on: ubuntu-latest
1113
# outputs:
1214
# phraseExists: ${{ steps.check-body.outputs.result }}
15+
# commentId: ${{ steps.check-body.outputs.commentId }}
1316
# steps:
14-
# - name: Check if issue body contains 'Hey ontobot'
17+
# - name: Check if issue body or any comment contains 'Hey ontobot'
1518
# id: check-body
1619
# uses: actions/github-script@v6
1720
# with:
@@ -21,11 +24,29 @@
2124
# repo: context.repo.repo,
2225
# issue_number: context.issue.number
2326
# });
24-
# if (!issue.data.body) {
25-
# console.log('Issue body is empty or null');
26-
# return false;
27+
28+
# let bodyText = issue.data.body ? issue.data.body.toLowerCase() : '';
29+
# let commentId = null;
30+
31+
# // Fetch all comments for the issue
32+
# const comments = await github.rest.issues.listComments({
33+
# owner: context.repo.owner,
34+
# repo: context.repo.repo,
35+
# issue_number: context.issue.number
36+
# });
37+
38+
# // Check each comment for the phrase
39+
# for (const comment of comments.data) {
40+
# if (comment.body.toLowerCase().includes('hey ontobot')) {
41+
# bodyText += ' ' + comment.body.toLowerCase();
42+
# commentId = comment.id;
43+
# }
2744
# }
28-
# return issue.data.body.toLowerCase().includes('hey ontobot');
45+
46+
# return {
47+
# result: bodyText.includes('hey ontobot'),
48+
# commentId: commentId
49+
# };
2950

3051
# build:
3152
# needs: check
@@ -34,7 +55,7 @@
3455
# strategy:
3556
# matrix:
3657
# python-version: ["3.9"]
37-
# os: [ ubuntu-latest ]
58+
# os: [ubuntu-latest]
3859

3960
# steps:
4061
# - uses: actions/checkout@v3
@@ -47,26 +68,23 @@
4768
# id: gh-script-issue
4869
# uses: actions/github-script@v6
4970
# with:
50-
# # github-token: ${{ secrets.GH_TOKEN }}
5171
# script: |
52-
# const issue_number = context.issue.number
53-
# const repo = context.repo.owner+"/"+context.repo.repo
54-
# return issue_number
55-
72+
# const issue_number = context.issue.number;
73+
# return issue_number;
74+
5675
# - name: Return repository name
5776
# id: gh-script-repo
5877
# uses: actions/github-script@v6
5978
# with:
60-
# # github-token: ${{ secrets.GH_TOKEN }}
6179
# script: |
62-
# const repo = context.repo.owner+"/"+context.repo.repo
63-
# return repo
80+
# const repo = context.repo.owner + "/" + context.repo.repo;
81+
# return repo;
6482

6583
# - name: Set branch name
6684
# id: vars
6785
# run: |
6886
# echo "resource=src/ontology/mondo-edit.obo" >> $GITHUB_ENV
69-
# echo "branch-name=ochange_automation_"${{ steps.gh-script-issue.outputs.result }} >> $GITHUB_ENV
87+
# echo "branch-name=ochange_automation_${{ steps.gh-script-issue.outputs.result }}" >> $GITHUB_ENV
7088

7189
# - name: Install dependencies
7290
# run: |
@@ -79,16 +97,16 @@
7997
# -r ${{ steps.gh-script-repo.outputs.result }} \
8098
# -n ${{ steps.gh-script-issue.outputs.result }} \
8199
# -g ${{ secrets.GH_TOKEN }} \
82-
# -p XXXX
100+
# -p PREFIX_OF_RESOURCE \
101+
# -c ${{ needs.check.outputs.commentId }}
83102

84103
# - name: Create Pull Request
85104
# uses: peter-evans/create-pull-request@v4
86-
# if: ${{ env.PR_TITLE}}
105+
# if: ${{ env.PR_TITLE }}
87106
# with:
88107
# branch-suffix: short-commit-hash
89108
# labels: Automated
90109
# body: ${{ env.PR_BODY }}
91110
# title: ${{ env.PR_TITLE }}
92111
# base: ${{ github.head_ref }}
93112
# branch: ${{ env.branch-name }}
94-
# # token: ${{ secrets.GH_TOKEN }}

src/ontobot_change_agent/api.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ def get_issues(
7777
:param state: State of the issue e.g. open, close etc., defaults to "open"
7878
:yield: Issue names that match the regex/label/number.
7979
"""
80-
if token is None:
81-
token_file = TOKEN_FILE
82-
with open(token_file, "r") as t:
83-
token = t.read().rstrip()
84-
85-
g = Github(token)
86-
repo = g.get_repo(repository_name)
80+
repo = _get_repo_object(repository_name, token)
8781
global ISSUE_TEMPLATE_DIR
8882
ISSUE_TEMPLATE_DIR = repo.contents_url.replace("{+path}", ISSUE_TEMPLATE_DIR)
8983
label_object = None
@@ -104,6 +98,26 @@ def get_issues(
10498
yield None
10599

106100

101+
def get_comment_from_repo(repository_name: str, token: str = None, comment_id: int = 0):
102+
"""Get a comment from a Github repository."""
103+
repo = _get_repo_object(repository_name, token)
104+
105+
for comment in repo.get_issues_comments():
106+
if comment.id == comment_id:
107+
return comment.body
108+
return None
109+
110+
111+
def _get_repo_object(repository_name: str, token: str = None):
112+
if token is None:
113+
token_file = TOKEN_FILE
114+
with open(token_file, "r") as t:
115+
token = t.read().rstrip()
116+
117+
g = Github(token)
118+
return g.get_repo(repository_name)
119+
120+
107121
def _extract_info_from_issue_object(issue: Issue) -> dict:
108122
issue_as_dict = issue.__dict__
109123
important_info = {k: issue_as_dict[RAW_DATA][k] for k in ISSUE_KEYS}

0 commit comments

Comments
 (0)