Skip to content

Add tag support for GitHub URLs - #12

Merged
pombredanne merged 1 commit into
aboutcode-org:masterfrom
TG1999:tags
Aug 20, 2020
Merged

Add tag support for GitHub URLs#12
pombredanne merged 1 commit into
aboutcode-org:masterfrom
TG1999:tags

Conversation

@TG1999

@TG1999 TG1999 commented Mar 9, 2020

Copy link
Copy Markdown
Collaborator

Solves issue #6 for GitHub URLs
Signed-off-by: TG1999 tushar.goel.dav@gmail.com

@TG1999

TG1999 commented Mar 9, 2020

Copy link
Copy Markdown
Collaborator Author

@pombredanne @MaJuRG, please check this PR :)

@TG1999

TG1999 commented Mar 9, 2020

Copy link
Copy Markdown
Collaborator Author

I ran it for https://github.com/nexb/scancode-toolkit and got this
['v3.1.1', 'v3.1.0', 'v3.0.2', 'v3.0.1', 'v3.0.0', 'v2.9.9', 'v2.9.8', 'v2.9.7', 'v2.9.6', 'v2.9.5', 'v2.9.4', 'v2.9.3', 'v2.9.2', 'v2.9.1', 'v2.9.0b1', 'v2.2.1', 'v2.2.0', 'v2.1.0', 'v2.0.1', 'v2.0.0', 'v2.0.0.rc3', 'v2.0.0.rc2', 'v2.0.0.rc1', 'v1.6.3', 'v1.6.2', 'v1.6.1', 'v1.6.0', 'v1.5.0', 'v1.4.3', 'v1.4.2']
so I thik it's good to go

@steven-esser steven-esser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add more tests here. Convert the test you did above locally into an actual test case as a first step.

@TG1999

TG1999 commented Mar 9, 2020

Copy link
Copy Markdown
Collaborator Author

Okay should I match both the arrays @MaJuRG the one that I shown in comments with the test case?

@steven-esser

Copy link
Copy Markdown
Contributor

@TG1999 Assuming the list does not change over time, this is fine. Otherwise you can simply check for the presence of a few tag values that we know will continue to exists.

@TG1999

TG1999 commented Mar 9, 2020

Copy link
Copy Markdown
Collaborator Author

Okay I think I can match the last element of the array, since it won't change over time

@TG1999

TG1999 commented Mar 9, 2020

Copy link
Copy Markdown
Collaborator Author

@MaJuRG done the changes as per your comments :)

Comment thread tests/test_github_tags.py Outdated
def test_github_tags_for_not_empty_list():
url = 'https://github.com/nexb/scancode-toolkit'
response = github_tags.get_tags(url=url)
assert (response[-1] == 'v1.4.2')

@steven-esser steven-esser Mar 9, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should change this to assert 'v1.4.2' in response. This is easier to read.

Additionally, we should test for two additional items:

  1. Another version number that is a valid tag in response
  2. A version number that IS NOT in response

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done @MaJuRG :)

@steven-esser steven-esser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you accessing the internet in your test cases? To me, it seems you are (which is something we do not want to do). Just want to make sure

@TG1999

TG1999 commented Mar 10, 2020

Copy link
Copy Markdown
Collaborator Author

Yes, I am since using it since I have to acess GitHub api

@steven-esser steven-esser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to mock these API requests then. We do not want tests that rely on accessing potentially changing data on the web.

@TG1999

TG1999 commented Mar 10, 2020

Copy link
Copy Markdown
Collaborator Author

Cool, pushing changes in a while

@TG1999

TG1999 commented Mar 10, 2020

Copy link
Copy Markdown
Collaborator Author

I have mocked the responses now you can merge it :)

Comment thread tests/test_github_tags.py Outdated
@mock.patch('fetchcode.github_tags.requests.get')
def test_github_tags_for_not_empty_list(mock_get):
url = 'https://github.com/nexb/scancode-toolkit'
mock_get.return_value.json.return_value =[

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this big thing to a separate json file?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, will do so

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done @MaJuRG :D

@steven-esser steven-esser left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are going to (probably) have multiple test files in the future, please:

  1. Create a new directory tests/data/
  2. Rename this JSON file to gh-scancode-tags-response.json or similar.

@TG1999

TG1999 commented Mar 10, 2020

Copy link
Copy Markdown
Collaborator Author

Okay making changes in 5 minutes

@TG1999

TG1999 commented Mar 10, 2020

Copy link
Copy Markdown
Collaborator Author

Done @MaJuRG :)

@pombredanne pombredanne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
See some of my comments inline

Comment thread fetchcode/github_tags.py Outdated
"""
Builds URL according to GitHub API
"""
return base_url + path + '/tags'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe what base_url and path are ? what would examples of values? Also using + sounds rather britlle...
What about using format or a f string instead? and does base_url ever changes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also is this function really warranted since you are not even testing it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested the get_tags() function that is using this function so should I also write unit test for this function 😅

Comment thread fetchcode/github_tags.py Outdated

def build_url(base_url, path):
"""
Builds URL according to GitHub API

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind using imperative style like in commit message?
e.g. Build URL according to GitHub API

or rather:
Return a URL to access the list of tags using the GitHub API

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got it

Comment thread fetchcode/github_tags.py Outdated

def get_tags(url):
"""
Takes Github VCS URL as input and provides

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above: use imperative instead.

Return a list of git tags  given the `url` to a Github repository

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got this

Comment thread fetchcode/github_tags.py Outdated
print('Not a GitHub URL')

else:
final_url = build_url(base_url=base_url, path=url_parts.path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments on build_url

Comment thread fetchcode/github_tags.py Outdated
else:
final_url = build_url(base_url=base_url, path=url_parts.path)
resp = requests.get(final_url)
print(resp)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you print things?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got this

Comment thread fetchcode/github_tags.py Outdated
print(resp)
tags = []
for res in resp.json():
tags.append(res['name'])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure 'name' is always there?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this

@pombredanne pombredanne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the updates! see my comments

Comment thread fetchcode/github_tags.py Outdated
url_parts = urlparse(url)

if not(url_parts.netloc == 'github.com'):
print('Not a GitHub URL')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why print anything? You do not print things in a library: you return values or you raise Exceptions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this :)

Comment thread tests/test_github_tags.py Outdated
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
from unittest import mock

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please organize your imports:

import xxx <-std lib imports, one per line, sorted
empty line
import zzz <-third-party imports, one per line, sorted
empty line
import foo <-own modules imports, one per line, sorted
empty line
empty line

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this :), will take care for future too

Comment thread tests/test_github_tags.py Outdated
@mock.patch('fetchcode.github_tags.requests.get')
def test_github_tags_for_not_empty_list(mock_get):
url = 'https://github.com/nexb/scancode-toolkit'
with open('tests/data/gh_tags_response.json','r') as file:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why r mode? this is the default.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this :)

Comment thread tests/test_github_tags.py Outdated
data = file.read()
mock_get.return_value.json.return_value = json.loads(data)
response = github_tags.get_tags(url=url)
assert 'v1.4.2' in response

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why assert only three values and not everything?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this :)

Comment thread tests/test_github_tags.py Outdated

def test_build_url_with_conventional_URL():
url = 'https://github.com/nexb/fetchcode'
supposed_build_url = 'https://api.github.com/repos/nexb/fetchcode/tags'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supposed would likely be best as "expected" ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got this :)

Comment thread tests/test_github_tags.py Outdated
url = 'https://github.com/nexb/fetchcode'
supposed_build_url = 'https://api.github.com/repos/nexb/fetchcode/tags'
final_url = github_tags.build_url(url)
assert final_url == supposed_build_url

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We typically write asserts the other ways as assert "expected value" == "obtained value"...
This ensures that in diffs the expected values are before or on the left and the actual obtained values are on the right or below... which is helpful as a convention when there are failure traces.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remember this for future ref. too

Comment thread tests/test_github_tags.py Outdated
Comment on lines +30 to +33
assert 'v3.1.1' in response
assert 'v3.1.0' in response
assert 'v3.0.2' in response
assert 'v3.0.1' in response
assert 'v3.0.0' in response
assert 'v2.9.9' in response
assert 'v2.9.8' in response
assert 'v2.9.7' in response
assert 'v2.9.6' in response
assert 'v2.9.5' in response
assert 'v2.9.4' in response
assert 'v2.9.3' in response
assert 'v2.9.2' in response
assert 'v2.9.1' in response
assert 'v2.9.0b1' in response
assert 'v2.2.1' in response
assert 'v2.2.0' in response
assert 'v2.1.0' in response
assert 'v2.0.1' in response
assert 'v2.0.0' in response
assert 'v2.0.0.rc3' in response
assert 'v2.0.0.rc2' in response
assert 'v2.0.0.rc1' in response
assert 'v1.6.3' in response
assert 'v1.6.2' in response
assert 'v1.6.1' in response
assert 'v1.6.0' in response
assert 'v1.5.0' in response
assert 'v1.4.3' in response
assert 'v1.4.2' in response
assert 'v5.0.1' not in response

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add these values to a single list and iterate thru them? There is no need to take up many lines here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay cool, doing changes in 10 minutes

@TG1999

TG1999 commented Mar 13, 2020

Copy link
Copy Markdown
Collaborator Author

Done @MaJuRG 😄

@TG1999

TG1999 commented Mar 19, 2020

Copy link
Copy Markdown
Collaborator Author

Thanks @MaJuRG now waiting for @pombredanne

@TG1999
TG1999 requested a review from pombredanne March 27, 2020 15:33
Signed-off-by: TG1999 <tushar.goel.dav@gmail.com>
@pombredanne

Copy link
Copy Markdown
Member

Thanks! all merged.

@pombredanne
pombredanne merged commit 451f648 into aboutcode-org:master Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants