Skip to content
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

adding token support for bitbucket, adding create/mod date for bitbucket #50

Merged
merged 2 commits into from
Mar 3, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ The basic structure is:
"url": "https://bitbucket.internal", // Base URL for a Bitbucket Server instance
"username": "", // Username to authenticate with
"password": "", // Password to authenticate with
"token": "", // Token to authenticate with, if supplied username and password are ignored

"exclude": [ ... ] // List of projects / repositories to exclude from inventory
}
Expand Down
39 changes: 33 additions & 6 deletions scraper/bitbucket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
import logging
import stashy

import datetime

from stashy.client import Stash

logger = logging.getLogger(__name__)


def connect(url, username, password):
def connect(url, username=None, password=None, token=None):
"""
Return a connected Bitbucket session
"""

bb_session = stashy.connect(url, username, password)

logger.info("Connected to: %s as %s", url, username)
if token is not None:
bb_session = Stash(url, token=token)
logger.info("Connected to: %s with token", url)
else:
bb_session = stashy.connect(url, username, password)
logger.info("Connected to: %s as username %s", url, username)

return bb_session


def all_repos(bb_session):
"""
Yields Stashy repo objects for all projects in Bitbucket
Yields Stashy repo dictionary objects for all repos in Bitbucket
"""

for repo in bb_session.repos.all():
all_commits = sorted(
bb_session.projects[repo["project"]["key"]]
.repos[repo["name"]]
.commits(None),
key=lambda x: x["authorTimestamp"],
)
if all_commits:
repo["created"] = (
datetime.datetime.fromtimestamp(
all_commits[0]["authorTimestamp"] / 1000
)
.date()
.isoformat()
)
repo["lastModified"] = (
datetime.datetime.fromtimestamp(
all_commits[-1]["authorTimestamp"] / 1000
)
.date()
.isoformat()
)
yield repo
8 changes: 4 additions & 4 deletions scraper/code_gov/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ def process_config(config):
url = instance.get("url")
# orgs = instance.get('orgs', None)
# public_only = instance.get('public_only', True)
# token = instance.get('token', None)
username = instance.get("username")
password = instance.get("password")
username = instance.get("username", None)
password = instance.get("password", None)
token = instance.get("token", None)
excluded = instance.get("exclude", [])

bb_session = bitbucket.connect(url, username, password)
bb_session = bitbucket.connect(url, username, password, token)

for repo in bitbucket.all_repos(bb_session):
project = repo["project"]["key"]
Expand Down
13 changes: 6 additions & 7 deletions scraper/code_gov/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,14 @@ def from_stashy(klass, repository, labor_hours=True):

# project['reusedCode'] = []

# date: [object] A date object describing the release.
# date: [object] A date object describing the release. Empty if repo has no commits.
# created: [string] The date the release was originally created, in YYYY-MM-DD or ISO 8601 format.
# lastModified: [string] The date the release was modified, in YYYY-MM-DD or ISO 8601 format.
# metadataLastUpdated: [string] The date the metadata of the release was last updated, in YYYY-MM-DD or ISO 8601 format.
# project['date'] = {
# 'created': repository.pushed_at.isoformat(),
# 'lastModified': repository.updated_at.isoformat(),
# 'metadataLastUpdated': '',
# }
if repository.get("created", None):
project["date"] = {
"created": repository["created"],
"lastModified": repository["lastModified"],
}

_prune_dict_null_str(project)

Expand Down