Skip to content

Bitbucket Cloud: Support for pull request tasks #874

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

Merged
merged 4 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions atlassian/bitbucket/cloud/repositories/pullRequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@ def comment(self, raw_message):

return self.post("comments", data)

def tasks(self):
"""
Returns generator object of the tasks endpoint

This is feature currently undocumented.
But confirmed by an Atlassian employee (BCLOUD-16682).
"""
for task in self._get_paged("tasks"):
yield Task(task, **self._new_session_args)

def add_task(self, raw_message):
"""
Adding a task to the pull request in raw format.

This is feature currently undocumented.
But confirmed by an Atlassian employee (BCLOUD-16682).
"""
if not raw_message:
raise ValueError("No message set")

data = {
"content": {
"raw": raw_message,
}
}

return Task(self.post("tasks", data), **self._new_session_args)

def approve(self):
"""
Approve a pull request if open
Expand Down Expand Up @@ -483,3 +511,70 @@ def website(self):
def refname(self):
"""Returns the refname"""
return self.get_data("refname")


class Task(BitbucketCloudBase):
STATE_RESOLVED = "RESOLVED"
STATE_UNRESOLVED = "UNRESOLVED"

def __init__(self, data, *args, **kwargs):
super().__init__(None, None, *args, data=data, **kwargs)

@property
def id(self):
"""Task id."""
return self.get_data("id")

@property
def description(self):
"""The task description."""
return self.get_data("content")["raw"]

@property
def created_on(self):
"""time of creation"""
return self.get_time("created_on")

@property
def resolved_on(self):
"""resolve timestamp"""
return self.get_time("resolved_on")

@property
def is_resolved(self):
"""True if the task was already resolved."""
return self.get_data("state") == self.STATE_RESOLVED

@property
def creator(self):
"""User object with user information of the task creator"""
return User(None, self.get_data("creator"), **self._new_session_args)

@property
def resolved_by(self):
"""User object with user information of the task resolver"""
return User(None, self.get_data("resolved_by"), **self._new_session_args)

def update(self, raw_message):
"""
Update a task in raw format

This is feature currently undocumented.
"""
if not raw_message:
raise ValueError("No message set")

data = {
"content": {
"raw": raw_message,
}
}
return self._update_data(self.put(None, data=data))

def delete(self):
"""
Delete the pullrequest tasl.

This is feature currently undocumented.
"""
return super(Task, self).delete(None)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
responses[None]=None
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
responses['{"content": {"raw": "ToDo 10"}}'] = {
"links": {
"self": {"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/tasks/123456"},
"html": {
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/_/diff#task-123456"
},
},
"creator": {
"display_name": "User04DisplayName",
"uuid": "{User04UUID}",
"links": {
"self": {"href": "users/%7BUser04UUID%7D"},
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
"avatar": {
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU4-2.png"
},
},
"nickname": "User04Nickname",
"type": "user",
"account_id": "User04AccountID",
},
"resolved_on": null,
"created_on": "2021-10-19T20:20:49.288763+00:00",
"content": {"raw": "ToDo 10"},
"state": "UNRESOLVED",
"resolved_by": null,
"updated_on": "2021-10-19T20:20:49.288843+00:00",
"id": 123456,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
responses[None] = {
"pagelen": 10,
"values": [
{
"links": {
"self": {
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/tasks/234567"
},
"html": {
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/_/diff#task-234567"
},
},
"creator": {
"display_name": "User04DisplayName",
"uuid": "{User04UUID}",
"links": {
"self": {"href": "users/%7BUser04UUID%7D"},
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
"avatar": {
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU4-2.png"
},
},
"nickname": "User04Nickname",
"type": "user",
"account_id": "User04AccountID",
},
"resolved_on": "2021-10-19T20:28:47.493275+00:00",
"created_on": "2021-10-19T20:26:35.443146+00:00",
"content": {"raw": "ToDo 2"},
"state": "RESOLVED",
"resolved_by": {
"display_name": "User04DisplayName",
"uuid": "{User04UUID}",
"links": {
"self": {"href": "users/%7BUser04UUID%7D"},
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
"avatar": {
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU4-2.png"
},
},
"nickname": "User04Nickname",
"type": "user",
"account_id": "User04AccountID",
},
"updated_on": "2021-10-19T20:28:47.493283+00:00",
"id": 234567,
},
{
"links": {
"self": {
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/tasks/123456"
},
"html": {
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/_/diff#task-123456"
},
},
"creator": {
"display_name": "User04DisplayName",
"uuid": "{User04UUID}",
"links": {
"self": {"href": "users/%7BUser04UUID%7D"},
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
"avatar": {
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU4-2.png"
},
},
"nickname": "User04Nickname",
"type": "user",
"account_id": "User04AccountID",
},
"resolved_on": null,
"created_on": "2021-10-19T20:20:49.288763+00:00",
"content": {"raw": "ToDo 1"},
"state": "UNRESOLVED",
"resolved_by": null,
"updated_on": "2021-10-19T20:20:49.288843+00:00",
"id": 123456,
},
],
"page": 1,
"size": 2,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
responses['{"content": {"raw": "ToDo 1"}}'] = {
"links": {
"self": {"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/tasks/123456"},
"html": {
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/_/diff#task-123456"
},
},
"creator": {
"display_name": "User04DisplayName",
"uuid": "{User04UUID}",
"links": {
"self": {"href": "users/%7BUser04UUID%7D"},
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
"avatar": {
"href": "https://secure.gravatar.com/avatar/ad2424dafaasdssaew12232344434432?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FU4-2.png"
},
},
"nickname": "User04Nickname",
"type": "user",
"account_id": "User04AccountID",
},
"resolved_on": null,
"created_on": "2021-10-19T20:20:49.288763+00:00",
"content": {"raw": "ToDo 1"},
"state": "UNRESOLVED",
"resolved_by": null,
"updated_on": "2021-10-19T20:20:49.288843+00:00",
"id": 123456,
}
41 changes: 40 additions & 1 deletion tests/test_bitbucket_cloud_oo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from atlassian import Bitbucket
from atlassian.bitbucket import Cloud
from atlassian.bitbucket.cloud.common.users import User
from atlassian.bitbucket.cloud.repositories.pullRequests import Comment, Participant, PullRequest, Build
from atlassian.bitbucket.cloud.repositories.pullRequests import Comment, Participant, PullRequest, Build, Task

BITBUCKET = None
try:
Expand Down Expand Up @@ -277,6 +277,45 @@ def test_comments(self, tc1):
assert c1.user.display_name == "User04DisplayName"
assert c2.html == "<p>Test comment 2</p>"

def test_add_task(self, tc1):
msg = "ToDo 1"
task = tc1.add_task(msg)
assert isinstance(task, Task)
assert task.id == 123456
assert task.description == msg
assert not task.is_resolved

with pytest.raises(ValueError):
tc1.add_task(None)

def test_update_task(self, tc1):
task = [t for t in tc1.tasks() if t.id == 123456][0]
task = task.update("ToDo 10")
assert task.description == "ToDo 10"

with pytest.raises(ValueError):
task.update(None)

def test_delete_task(self, tc1):
task = [t for t in tc1.tasks() if t.id == 123456][0]
assert task.delete() is None

def test_tasks(self, tc1):
tasks = list(tc1.tasks())
assert len(tasks) == 2
t1, t2 = tasks
assert isinstance(t1, Task)
assert isinstance(t2, Task)
assert t2.id == 123456
assert t1.id == 234567
assert not t2.is_resolved
assert t1.is_resolved
assert isinstance(t2.creator, User)
assert isinstance(t1.resolved_by, User)
assert t2.description == "ToDo 1"
assert _datetimetostr(t1.resolved_on) == _datetimetostr(datetime(2021, 10, 19, 20, 28, 47, 493275))
assert _datetimetostr(t2.created_on) == _datetimetostr(datetime(2021, 10, 19, 20, 20, 49, 288763))

def test_approve(self, tc1):
ap = tc1.approve()
assert ap["approved"]
Expand Down