@@ -21,6 +21,12 @@ module ProjectPullMover
2121 @gh_cli = GhCli . new ( options : @options , logger : @logger )
2222 end
2323
24+ def options_with_assignee ( login )
25+ argv = [ "-p" , @project_number . to_s , "-o" , @project_owner , "-t" , "user" , "-s" , @status_field , "-i" ,
26+ "MyInProgressID" , "-h" , "gh" , "-A" , login ]
27+ Options . parse ( file : "project_pull_mover.rb" , argv : argv , logger : @logger )
28+ end
29+
2430 describe "#set_graphql_data" do
2531 it "initializes repo and data from GraphQL response" do
2632 initial_data = { }
@@ -99,6 +105,90 @@ module ProjectPullMover
99105 end
100106 end
101107
108+ describe "#assignees" do
109+ it "returns assignee logins from GraphQL data" do
110+ pull = PullRequest . new ( { } , options : @options , project : @project , gh_cli : @gh_cli )
111+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ { "login" => "userA" } ,
112+ { "login" => "userB" } ] } } } )
113+
114+ assert_equal %w( userA userB ) , pull . assignees
115+ end
116+
117+ it "returns an empty array when there is no assignee data" do
118+ pull = PullRequest . new ( { } , options : @options , project : @project , gh_cli : @gh_cli )
119+ pull . set_graphql_data ( { "pullRequest" => { } } )
120+
121+ assert_empty pull . assignees
122+ end
123+ end
124+
125+ describe "#has_assignee?" do
126+ it "returns true when the login is already an assignee, case-insensitively" do
127+ pull = PullRequest . new ( { } , options : @options , project : @project , gh_cli : @gh_cli )
128+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ { "login" => "userA" } ] } } } )
129+
130+ assert pull . has_assignee? ( "USERA" )
131+ refute pull . has_assignee? ( "userB" )
132+ end
133+ end
134+
135+ describe "#should_add_assignee?" do
136+ it "returns false when no assignee option is set" do
137+ pull = PullRequest . new ( { } , options : @options , project : @project , gh_cli : @gh_cli )
138+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ ] } } } )
139+
140+ refute_predicate pull , :should_add_assignee?
141+ end
142+
143+ it "returns true when assignee option is set and the PR lacks that assignee" do
144+ options = options_with_assignee ( "userB" )
145+ pull = PullRequest . new ( { } , options : options , project : @project , gh_cli : @gh_cli )
146+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ { "login" => "userA" } ] } } } )
147+
148+ assert_predicate pull , :should_add_assignee?
149+ end
150+
151+ it "returns false when the PR already has that assignee" do
152+ options = options_with_assignee ( "userA" )
153+ pull = PullRequest . new ( { } , options : options , project : @project , gh_cli : @gh_cli )
154+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ { "login" => "userA" } ] } } } )
155+
156+ refute_predicate pull , :should_add_assignee?
157+ end
158+ end
159+
160+ describe "#add_assignee_if_necessary" do
161+ it "adds the assignee and returns the login when the PR lacks that assignee" do
162+ options = options_with_assignee ( "userB" )
163+ initial_data = { "content" => { "repository" => "foo/bar" , "number" => 5 } }
164+ pull = PullRequest . new ( initial_data , options : options , project : @project , gh_cli : @gh_cli )
165+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ { "login" => "userA" } ] } } } )
166+ @gh_cli . expects ( :add_pull_request_assignee ) . once . with ( login : "userB" , number : 5 , repo_nwo : "foo/bar" ,
167+ pull_name : "foo/bar#5" )
168+
169+ assert_equal "userB" , pull . add_assignee_if_necessary
170+ end
171+
172+ it "does nothing and returns nil when the PR already has that assignee" do
173+ options = options_with_assignee ( "userA" )
174+ initial_data = { "content" => { "repository" => "foo/bar" , "number" => 5 } }
175+ pull = PullRequest . new ( initial_data , options : options , project : @project , gh_cli : @gh_cli )
176+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ { "login" => "userA" } ] } } } )
177+ @gh_cli . expects ( :add_pull_request_assignee ) . never
178+
179+ assert_nil pull . add_assignee_if_necessary
180+ end
181+
182+ it "does nothing and returns nil when no assignee option is set" do
183+ initial_data = { "content" => { "repository" => "foo/bar" , "number" => 5 } }
184+ pull = PullRequest . new ( initial_data , options : @options , project : @project , gh_cli : @gh_cli )
185+ pull . set_graphql_data ( { "pullRequest" => { "assignees" => { "nodes" => [ ] } } } )
186+ @gh_cli . expects ( :add_pull_request_assignee ) . never
187+
188+ assert_nil pull . add_assignee_if_necessary
189+ end
190+ end
191+
102192 describe "#repo_name_with_owner" do
103193 it "returns full repository name and owner from initial data" do
104194 initial_data = { "content" => { "repository" => "someone/somerepo" } }
@@ -211,6 +301,7 @@ module ProjectPullMover
211301 assert_includes result , "pullRequest(number: 123) {"
212302 assert_includes result , "isRequired(pullRequestNumber: 123)"
213303 assert_includes result , "fieldValueByName(name: \" #{ @status_field } \" ) {"
304+ assert_includes result , "assignees(first: 100) {"
214305 end
215306
216307 it "raises error when repo details or number are missing" do
0 commit comments