GitLab agent#
1# You may need to add your working directory to the Python path. To do so, uncomment the following lines of code
2# import sys
3# sys.path.append("/Path/to/directory/agentic-framework") # Replace with your directory path
4
5import logging
6
7from besser.agent.core.agent import Agent
8from besser.agent.core.session import Session
9from besser.agent.exceptions.logger import logger
10from besser.agent.platforms.gitlab.gitlab_objects import Issue
11from besser.agent.library.transition.events.gitlab_webhooks_events import IssuesOpened, GitLabEvent, IssueCommentCreated, IssuesUpdated
12
13# Configure the logging module (optional)
14logger.setLevel(logging.INFO)
15
16agent = Agent('issue_thanking_agent')
17# Load agent properties stored in a dedicated file
18agent.load_properties('config.ini')
19# Define the platform your agent will use
20gitlab_platform = agent.use_gitlab_platform()
21
22# STATES
23
24idle = agent.new_state('idle', initial=True)
25issue_state = agent.new_state('issue_opened')
26
27
28# STATES BODIES' DEFINITION + TRANSITIONS
29
30
31def global_fallback_body(session: Session):
32 print('Greetings from global fallback')
33
34
35# Assigned to all agent states (overriding all currently assigned fallback bodies).
36agent.set_global_fallback_body(global_fallback_body)
37
38
39def idle_body(session: Session):
40 pass
41
42
43idle.set_body(idle_body)
44# The following transitions allow to flush the events created by the actions of the agent
45idle.when_event(IssuesOpened()).go_to(issue_state)
46idle.when_event(IssuesUpdated()).go_to(idle)
47idle.when_event(IssueCommentCreated()).go_to(idle)
48
49
50def issue_body(session: Session):
51 event: GitLabEvent = session.event
52 user_repo = event.payload['project']['path_with_namespace'].split('/')
53 issue_iid = event.payload['object_attributes']['iid']
54 issue: Issue = gitlab_platform.get_issue(
55 user=user_repo[0],
56 repository=user_repo[1],
57 issue_number=issue_iid)
58 gitlab_platform.comment_issue(issue,
59 'Hey,\n\nThanks for opening an issue!<br>We will look at that as soon as possible.')
60
61
62issue_state.set_body(issue_body)
63issue_state.go_to(idle)
64
65# RUN APPLICATION
66
67if __name__ == '__main__':
68 agent.run()