#5206 Fix PR close in API for user token
Merged 2 years ago by pingou. Opened 2 years ago by zlopez.
zlopez/pagure close_pull_request  into  master

file modified
+5 -2
@@ -770,10 +770,13 @@ 

  

      repo = _get_repo(repo, username, namespace)

      _check_pull_request(repo)

-     _check_token(repo)

+     _check_token(repo, project_token=False)

      request = _get_request(repo, requestid)

  

-     if not is_repo_committer(repo):

+     if (

+         not is_repo_committer(repo)

+         and not flask.g.fas_user.username == request.user.username

+     ):

          raise pagure.exceptions.APIError(403, error_code=APIERROR.ENOPRCLOSE)

  

      try:

@@ -1115,6 +1115,66 @@ 

          )

  

      @patch("pagure.lib.notify.send_email")

+     def test_api_pull_request_close_cross_project_token(self, send_email):

+         """ Test the api_pull_request_close method of the flask api for cross-project API token. """

+         send_email.return_value = True

+ 

+         tests.create_projects(self.session)

+         tests.create_tokens(self.session)

+         tests.create_tokens_acl(self.session)

+ 

+         # Create the pull-request to close

+         repo = pagure.lib.query.get_authorized_project(self.session, "test")

+         forked_repo = pagure.lib.query.get_authorized_project(

+             self.session, "test"

+         )

+         req = pagure.lib.query.new_pull_request(

+             session=self.session,

+             repo_from=forked_repo,

+             branch_from="master",

+             repo_to=repo,

+             branch_to="master",

+             title="test pull-request",

+             user="foo",

+         )

+         self.session.commit()

+         self.assertEqual(req.id, 1)

+         self.assertEqual(req.title, "test pull-request")

+         self.assertEqual(req.user.id, 2)

+ 

+         # Create a token for foo

+         item = pagure.lib.model.Token(

+             id="foobar_token",

+             user_id=2,

+             project_id=None,

+             expiration=datetime.datetime.utcnow()

+             + datetime.timedelta(days=30),

+         )

+         self.session.add(item)

+         self.session.commit()

+ 

+         # Allow the token to close PR

+         acls = pagure.lib.query.get_acls(self.session)

+         for acl in acls:

+             if acl.name == "pull_request_close":

+                 break

+         item = pagure.lib.model.TokenAcl(

+             token_id="foobar_token", acl_id=acl.id

+         )

+         self.session.add(item)

+         self.session.commit()

+ 

+         headers = {"Authorization": "token foobar_token"}

+ 

+         # User is the same that created this PR

+         output = self.app.post(

+             "/api/0/test/pull-request/1/close", headers=headers

+         )

+         self.assertEqual(output.status_code, 200)

+         data = json.loads(output.get_data(as_text=True))

+         self.assertDictEqual(data, {"message": "Pull-request closed!"})

+ 

+     @patch("pagure.lib.notify.send_email")

      def test_api_pull_request_close(self, send_email):

          """ Test the api_pull_request_close method of the flask api. """

          send_email.return_value = True

Currently the close PR API call has some issues when called by using cross project token instead of project only token, this PR fix those issues.

This allows for cross-project API tokens with this ACL

:thumbsup: for me

Tests are passing on pagure-fedora-rpms-py3

1 new commit added

  • Allow PR author to close PR using API
2 years ago

2 new commits added

  • Allow PR author to close PR using API
  • Don't assume project token for PR close
2 years ago

:thumbsup: on the new commit

We should look at expanding the test suite for this though

I can look at the tests, but I have difficulties to run them locally, even in vagrant machine.
I tried the container test run, but I'm not sure what I did wrong. Should ./dev/run-tests-container.py --repo . --branch close_pull_request work for running the tests locally?

1 new commit added

  • Add test for cross-project API token
2 years ago

1 new commit added

  • Use the correct status_code in test
2 years ago

pretty please pagure-ci rebuild

2 years ago

rebased onto c3e5610f42fbbfa55c32cc3fb8e86b58479efea4

2 years ago

Could we merge this, it's only failing on CentOS Stream 8

I'm working to fix the CS8 target today, then I'll go through and re-run and merge things.

rebased onto 7c817feffb07b526df4f1f61cdb7984056a63541

2 years ago

rebased onto bbfd98c556ac6ed0f49a76dfafac004f217a220e

2 years ago

rebased onto 661557f

2 years ago

Tests pass on fedora-rpms-py3 so I'm going to merge it :)

Pull-Request has been merged by pingou

2 years ago

Was this change deployed on staging yet? I tried to test it on dist-git today and it looks like it isn't.