From 295411dcf504f7d93fa3ed257fe080e74d3541d6939d40e8403e583546436d55 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Wed, 10 Jul 2024 10:14:56 +0200 Subject: [PATCH] . --- bots-common/conststring_test.go | 1 + bots-common/request_pr.go | 13 +- bots-common/request_pr_test.go | 653 +++++++++++++++++++++++++++++++ bots-common/request_push_test.go | 3 + pr-review/main_test.go | 1 + 5 files changed, 664 insertions(+), 7 deletions(-) create mode 100644 bots-common/request_pr_test.go diff --git a/bots-common/conststring_test.go b/bots-common/conststring_test.go index 33d25b9..8cb69e6 100644 --- a/bots-common/conststring_test.go +++ b/bots-common/conststring_test.go @@ -430,3 +430,4 @@ const repoCreateJSON = `{ "username": "adamm" } }` + diff --git a/bots-common/request_pr.go b/bots-common/request_pr.go index ac5b03d..3dc9ba4 100644 --- a/bots-common/request_pr.go +++ b/bots-common/request_pr.go @@ -20,10 +20,10 @@ type PullRequest struct { Base Head Labels []string - Head + Head Head - Repository - User + Repo Repository + User User } type PullRequestAction struct { @@ -31,11 +31,11 @@ type PullRequestAction struct { Number int Pull_Request PullRequest - Repository - Sender User + Repository Repository + Sender User } -func (h *RequestHandler) parsePullRequest(data io.ReadCloser) *PullRequestAction { +func (h *RequestHandler) parsePullRequest(data io.Reader) *PullRequestAction { if h.HasError() { return nil } @@ -61,4 +61,3 @@ func (h *RequestHandler) parsePullRequest(data io.ReadCloser) *PullRequestAction return &action } - diff --git a/bots-common/request_pr_test.go b/bots-common/request_pr_test.go new file mode 100644 index 0000000..b41efe4 --- /dev/null +++ b/bots-common/request_pr_test.go @@ -0,0 +1,653 @@ +package common + +import ( + "strings" + "testing" +) + +func TestPrParsing(t *testing.T) { + t.Run("Test parsing", func(t *testing.T) { + var h RequestHandler + h.Logger = CreateTestLogger(t) + + pr := h.parsePullRequest(strings.NewReader(samplePR_JSON)) + if pr == nil { + t.Fatalf("parsing PR failed: %v", h.Error) + } + }) +} + +const samplePR_JSON = `{ + "action": "opened", + "number": 1, + "pull_request": { + "id": 469, + "url": "https://src.opensuse.org/autogits/nodejs21/pulls/1", + "number": 1, + "user": { + "id": 129, + "login": "adamm", + "login_name": "", + "source_id": 0, + "full_name": "Adam Majer", + "email": "adamm@noreply@src.opensuse.org", + "avatar_url": "https://src.opensuse.org/avatar/babb6ae054a709c238a4e9b0f2b53d53", + "html_url": "https://src.opensuse.org/adamm", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-07-21T16:43:48+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "adamm" + }, + "title": "Update test", + "body": "This is a test of PR workflow", + "labels": [], + "milestone": null, + "assignee": null, + "assignees": null, + "requested_reviewers": null, + "state": "open", + "draft": false, + "is_locked": false, + "comments": 0, + "review_comments": 0, + "additions": 1, + "deletions": 1, + "changed_files": 1, + "html_url": "https://src.opensuse.org/autogits/nodejs21/pulls/1", + "diff_url": "https://src.opensuse.org/autogits/nodejs21/pulls/1.diff", + "patch_url": "https://src.opensuse.org/autogits/nodejs21/pulls/1.patch", + "mergeable": true, + "merged": false, + "merged_at": null, + "merge_commit_sha": null, + "merged_by": null, + "allow_maintainer_edit": false, + "base": { + "label": "factory", + "ref": "factory", + "sha": "24ee6bee7459a386dadabf8a9f6ae4fa15c3f810bfa01cee5238138aa214d180", + "repo_id": 90578, + "repo": { + "id": 90578, + "owner": { + "id": 983, + "login": "autogits", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "", + "avatar_url": "https://src.opensuse.org/avatars/80a61ef3a14c3c22f0b8b1885d1a75d4", + "html_url": "https://src.opensuse.org/autogits", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2024-06-20T09:46:37+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "autogits" + }, + "name": "nodejs21", + "full_name": "autogits/nodejs21", + "description": "", + "empty": false, + "private": false, + "fork": true, + "template": false, + "parent": { + "id": 71125, + "owner": { + "id": 64, + "login": "pool", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "", + "avatar_url": "https://src.opensuse.org/avatars/b10a8c0bede9eb4ea771b04db3149f28", + "html_url": "https://src.opensuse.org/pool", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-03-01T14:41:17+01:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "pool" + }, + "name": "nodejs21", + "full_name": "pool/nodejs21", + "description": "", + "empty": false, + "private": false, + "fork": false, + "template": false, + "parent": null, + "mirror": false, + "size": 1471, + "language": "", + "languages_url": "https://src.opensuse.org/api/v1/repos/pool/nodejs21/languages", + "html_url": "https://src.opensuse.org/pool/nodejs21", + "url": "https://src.opensuse.org/api/v1/repos/pool/nodejs21", + "link": "", + "ssh_url": "gitea@src.opensuse.org:pool/nodejs21.git", + "clone_url": "https://src.opensuse.org/pool/nodejs21.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 2, + "watchers_count": 2, + "open_issues_count": 0, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "factory", + "archived": false, + "created_at": "2024-06-17T17:17:37+02:00", + "updated_at": "2024-06-18T09:03:36+02:00", + "archived_at": "1970-01-01T01:00:00+01:00", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "has_issues": true, + "internal_tracker": { + "enable_time_tracker": false, + "allow_only_contributors_to_track_time": true, + "enable_issue_dependencies": true + }, + "has_wiki": true, + "has_pull_requests": true, + "has_projects": true, + "projects_mode": "all", + "has_releases": true, + "has_packages": true, + "has_actions": true, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "allow_fast_forward_only_merge": true, + "allow_rebase_update": true, + "default_delete_branch_after_merge": false, + "default_merge_style": "merge", + "default_allow_maintainer_edit": false, + "avatar_url": "https://src.opensuse.org/", + "internal": false, + "mirror_interval": "", + "object_format_name": "sha256", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null + }, + "mirror": false, + "size": 1471, + "language": "", + "languages_url": "https://src.opensuse.org/api/v1/repos/autogits/nodejs21/languages", + "html_url": "https://src.opensuse.org/autogits/nodejs21", + "url": "https://src.opensuse.org/api/v1/repos/autogits/nodejs21", + "link": "", + "ssh_url": "gitea@src.opensuse.org:autogits/nodejs21.git", + "clone_url": "https://src.opensuse.org/autogits/nodejs21.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 1, + "watchers_count": 2, + "open_issues_count": 0, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "factory", + "archived": false, + "created_at": "2024-07-09T23:40:35+02:00", + "updated_at": "2024-07-09T23:40:35+02:00", + "archived_at": "1970-01-01T01:00:00+01:00", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "has_issues": false, + "has_wiki": false, + "has_pull_requests": true, + "has_projects": false, + "projects_mode": "all", + "has_releases": false, + "has_packages": false, + "has_actions": false, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "allow_fast_forward_only_merge": true, + "allow_rebase_update": true, + "default_delete_branch_after_merge": false, + "default_merge_style": "merge", + "default_allow_maintainer_edit": false, + "avatar_url": "https://src.opensuse.org/", + "internal": false, + "mirror_interval": "", + "object_format_name": "sha256", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null + } + }, + "head": { + "label": "factory", + "ref": "factory", + "sha": "c678c57007d496a98bec668ae38f2c26a695f94af78012f15d044ccf066ccb41", + "repo_id": 90579, + "repo": { + "id": 90579, + "owner": { + "id": 129, + "login": "adamm", + "login_name": "", + "source_id": 0, + "full_name": "Adam Majer", + "email": "adamm@noreply@src.opensuse.org", + "avatar_url": "https://src.opensuse.org/avatar/babb6ae054a709c238a4e9b0f2b53d53", + "html_url": "https://src.opensuse.org/adamm", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-07-21T16:43:48+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "adamm" + }, + "name": "nodejs21", + "full_name": "adamm/nodejs21", + "description": "", + "empty": false, + "private": false, + "fork": true, + "template": false, + "parent": { + "id": 90578, + "owner": { + "id": 983, + "login": "autogits", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "", + "avatar_url": "https://src.opensuse.org/avatars/80a61ef3a14c3c22f0b8b1885d1a75d4", + "html_url": "https://src.opensuse.org/autogits", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2024-06-20T09:46:37+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "autogits" + }, + "name": "nodejs21", + "full_name": "autogits/nodejs21", + "description": "", + "empty": false, + "private": false, + "fork": true, + "template": false, + "parent": null, + "mirror": false, + "size": 1471, + "language": "", + "languages_url": "https://src.opensuse.org/api/v1/repos/autogits/nodejs21/languages", + "html_url": "https://src.opensuse.org/autogits/nodejs21", + "url": "https://src.opensuse.org/api/v1/repos/autogits/nodejs21", + "link": "", + "ssh_url": "gitea@src.opensuse.org:autogits/nodejs21.git", + "clone_url": "https://src.opensuse.org/autogits/nodejs21.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 1, + "watchers_count": 2, + "open_issues_count": 0, + "open_pr_counter": 1, + "release_counter": 0, + "default_branch": "factory", + "archived": false, + "created_at": "2024-07-09T23:40:35+02:00", + "updated_at": "2024-07-09T23:40:35+02:00", + "archived_at": "1970-01-01T01:00:00+01:00", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "has_issues": false, + "has_wiki": false, + "has_pull_requests": true, + "has_projects": false, + "projects_mode": "all", + "has_releases": false, + "has_packages": false, + "has_actions": false, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "allow_fast_forward_only_merge": true, + "allow_rebase_update": true, + "default_delete_branch_after_merge": false, + "default_merge_style": "merge", + "default_allow_maintainer_edit": false, + "avatar_url": "https://src.opensuse.org/", + "internal": false, + "mirror_interval": "", + "object_format_name": "sha256", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null + }, + "mirror": false, + "size": 1473, + "language": "", + "languages_url": "https://src.opensuse.org/api/v1/repos/adamm/nodejs21/languages", + "html_url": "https://src.opensuse.org/adamm/nodejs21", + "url": "https://src.opensuse.org/api/v1/repos/adamm/nodejs21", + "link": "", + "ssh_url": "gitea@src.opensuse.org:adamm/nodejs21.git", + "clone_url": "https://src.opensuse.org/adamm/nodejs21.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 0, + "watchers_count": 1, + "open_issues_count": 0, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "factory", + "archived": false, + "created_at": "2024-07-09T23:41:10+02:00", + "updated_at": "2024-07-09T23:41:50+02:00", + "archived_at": "1970-01-01T01:00:00+01:00", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "has_issues": false, + "has_wiki": false, + "has_pull_requests": true, + "has_projects": false, + "projects_mode": "all", + "has_releases": false, + "has_packages": false, + "has_actions": false, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "allow_fast_forward_only_merge": true, + "allow_rebase_update": true, + "default_delete_branch_after_merge": false, + "default_merge_style": "merge", + "default_allow_maintainer_edit": false, + "avatar_url": "https://src.opensuse.org/", + "internal": false, + "mirror_interval": "", + "object_format_name": "sha256", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null + } + }, + "merge_base": "24ee6bee7459a386dadabf8a9f6ae4fa15c3f810bfa01cee5238138aa214d180", + "due_date": null, + "created_at": "2024-07-09T23:42:38+02:00", + "updated_at": "2024-07-09T23:42:38+02:00", + "closed_at": null, + "pin_order": 0 + }, + "requested_reviewer": null, + "repository": { + "id": 90578, + "owner": { + "id": 983, + "login": "autogits", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "", + "avatar_url": "https://src.opensuse.org/avatars/80a61ef3a14c3c22f0b8b1885d1a75d4", + "html_url": "https://src.opensuse.org/autogits", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2024-06-20T09:46:37+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "autogits" + }, + "name": "nodejs21", + "full_name": "autogits/nodejs21", + "description": "", + "empty": false, + "private": false, + "fork": true, + "template": false, + "parent": { + "id": 71125, + "owner": { + "id": 64, + "login": "pool", + "login_name": "", + "source_id": 0, + "full_name": "", + "email": "", + "avatar_url": "https://src.opensuse.org/avatars/b10a8c0bede9eb4ea771b04db3149f28", + "html_url": "https://src.opensuse.org/pool", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-03-01T14:41:17+01:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "pool" + }, + "name": "nodejs21", + "full_name": "pool/nodejs21", + "description": "", + "empty": false, + "private": false, + "fork": false, + "template": false, + "parent": null, + "mirror": false, + "size": 1471, + "language": "", + "languages_url": "https://src.opensuse.org/api/v1/repos/pool/nodejs21/languages", + "html_url": "https://src.opensuse.org/pool/nodejs21", + "url": "https://src.opensuse.org/api/v1/repos/pool/nodejs21", + "link": "", + "ssh_url": "gitea@src.opensuse.org:pool/nodejs21.git", + "clone_url": "https://src.opensuse.org/pool/nodejs21.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 2, + "watchers_count": 2, + "open_issues_count": 0, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "factory", + "archived": false, + "created_at": "2024-06-17T17:17:37+02:00", + "updated_at": "2024-06-18T09:03:36+02:00", + "archived_at": "1970-01-01T01:00:00+01:00", + "permissions": { + "admin": true, + "push": true, + "pull": true + }, + "has_issues": true, + "internal_tracker": { + "enable_time_tracker": false, + "allow_only_contributors_to_track_time": true, + "enable_issue_dependencies": true + }, + "has_wiki": true, + "has_pull_requests": true, + "has_projects": true, + "projects_mode": "all", + "has_releases": true, + "has_packages": true, + "has_actions": true, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "allow_fast_forward_only_merge": true, + "allow_rebase_update": true, + "default_delete_branch_after_merge": false, + "default_merge_style": "merge", + "default_allow_maintainer_edit": false, + "avatar_url": "https://src.opensuse.org/", + "internal": false, + "mirror_interval": "", + "object_format_name": "sha256", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null + }, + "mirror": false, + "size": 1471, + "language": "", + "languages_url": "https://src.opensuse.org/api/v1/repos/autogits/nodejs21/languages", + "html_url": "https://src.opensuse.org/autogits/nodejs21", + "url": "https://src.opensuse.org/api/v1/repos/autogits/nodejs21", + "link": "", + "ssh_url": "gitea@src.opensuse.org:autogits/nodejs21.git", + "clone_url": "https://src.opensuse.org/autogits/nodejs21.git", + "original_url": "", + "website": "", + "stars_count": 0, + "forks_count": 1, + "watchers_count": 2, + "open_issues_count": 0, + "open_pr_counter": 0, + "release_counter": 0, + "default_branch": "factory", + "archived": false, + "created_at": "2024-07-09T23:40:35+02:00", + "updated_at": "2024-07-09T23:40:35+02:00", + "archived_at": "1970-01-01T01:00:00+01:00", + "permissions": { + "admin": true, + "push": true, + "pull": true + }, + "has_issues": false, + "has_wiki": false, + "has_pull_requests": true, + "has_projects": false, + "projects_mode": "all", + "has_releases": false, + "has_packages": false, + "has_actions": false, + "ignore_whitespace_conflicts": false, + "allow_merge_commits": true, + "allow_rebase": true, + "allow_rebase_explicit": true, + "allow_squash_merge": true, + "allow_fast_forward_only_merge": true, + "allow_rebase_update": true, + "default_delete_branch_after_merge": false, + "default_merge_style": "merge", + "default_allow_maintainer_edit": false, + "avatar_url": "https://src.opensuse.org/", + "internal": false, + "mirror_interval": "", + "object_format_name": "sha256", + "mirror_updated": "0001-01-01T00:00:00Z", + "repo_transfer": null + }, + "sender": { + "id": 129, + "login": "adamm", + "login_name": "", + "source_id": 0, + "full_name": "Adam Majer", + "email": "adamm@noreply@src.opensuse.org", + "avatar_url": "https://src.opensuse.org/avatar/babb6ae054a709c238a4e9b0f2b53d53", + "html_url": "https://src.opensuse.org/adamm", + "language": "", + "is_admin": false, + "last_login": "0001-01-01T00:00:00Z", + "created": "2023-07-21T16:43:48+02:00", + "restricted": false, + "active": false, + "prohibit_login": false, + "location": "", + "website": "", + "description": "", + "visibility": "public", + "followers_count": 0, + "following_count": 0, + "starred_repos_count": 0, + "username": "adamm" + }, + "commit_id": "", + "review": null +} +` diff --git a/bots-common/request_push_test.go b/bots-common/request_push_test.go index 5752f93..ad576e2 100644 --- a/bots-common/request_push_test.go +++ b/bots-common/request_push_test.go @@ -37,6 +37,9 @@ func TestPushRequestParsing(t *testing.T) { h.Logger = CreateTestLogger(t) json := h.parsePushRequest(strings.NewReader(examplePushJSON)) + if json == nil { + t.Fatalf("failed to parser push request: %v", h.Error) + } if h.HasError() { t.Fatalf("Can't parse struct: %s", h.Error.Error()) } diff --git a/pr-review/main_test.go b/pr-review/main_test.go index e26bdc1..0a9f7f2 100644 --- a/pr-review/main_test.go +++ b/pr-review/main_test.go @@ -1,2 +1,3 @@ package main +// TODO