Skip to content

Instantly share code, notes, and snippets.

@jherrlin
Created September 6, 2015 19:21
Show Gist options
  • Save jherrlin/c0cab02dc5758b104448 to your computer and use it in GitHub Desktop.
Save jherrlin/c0cab02dc5758b104448 to your computer and use it in GitHub Desktop.
What i wanna do:
- Get all repos from a github organization.
- Save them to the database.
- Get all contributors from the repos that we just stored in the database.
- Create a two way relationship Contributor <-> Project
# Models.py
class Project(TimeStampedModel):
name = models.CharField(max_length=254, unique=True)
slug = models.SlugField(blank=True)
about = models.TextField(blank=True)
# Github specific
gh_name = models.CharField(max_length=254)
gh_id = models.IntegerField()
gh_url = models.CharField(max_length=254, blank=True)
def save(self, *args, **kwargs):
"""
Set slug
"""
if not self.name:
self.name = self.gh_name
if not self.slug:
self.slug = slugify(self.name)
super(Project, self).save(*args, **kwargs)
def __unicode__(self):
return self.gh_name
class Contributor(TimeStampedModel):
name = models.CharField(max_length=254, blank=True)
email = models.EmailField(max_length=254, blank=True)
website = models.CharField(max_length=254, blank=True)
about = HTMLField(blank=True)
# Github specific
gh_login = models.CharField(max_length=254)
gh_url = models.CharField(max_length=254)
gh_id = models.IntegerField()
def save(self, *args, **kwargs):
if not self.name:
self.name = self.gh_login
super(Contributor, self).save(*args, **kwargs)
def __unicode__(self):
return self.name
class ProjectContributor(TimeStampedModel):
project = models.ForeignKey(Project, primary_key=True)
contributor = models.ForeignKey(Contributor)
------------------------------------------------------------------
# Views.py
def get_repos():
"""
This function collects all the repos from
GitHub and store them in the database
"""
data = requests.get('https://api.github.com/orgs/videumcodeup/repos').json()
for obj in data:
form = ProjectForm({
'gh_name': obj['name'],
'gh_id': obj['id'],
'gh_url': obj['html_url']
})
if form.is_valid():
try:
project = Project.objects.get(gh_id=obj['id'])
except Project.DoesNotExist:
project = Project(**form.data)
project.save()
def get_contribs():
"""
This function get all the project objects from the database.
Ask the GitHub API for the contributors in the project.
Save them to the database and create a relation.
Project <-> Contributor
"""
for project in Project.objects.all():
data = requests.get('https://api.github.com/repos/videumcodeup/'+project.gh_name+'/contributors').json()
for i in data:
form = ContributorForm({
'gh_login': i['login'],
'gh_url': i['html_url'],
'gh_id': i['id']
})
if form.is_valid():
try:
contrib = Contributor.objects.get(gh_id = i['id'])
try:
ProCon(project=project,contributor=contrib).save()
except:
pass
except Contributor.DoesNotExist:
contrib = Contributor(**form.data)
contrib.save()
try:
ProCon(project=project,contributor=contrib).save()
except:
pass
class GithubHook(APIView):
permission_classes = (AllowAny,)
def post(self, request):
get_repos()
get_contribs()
return Response(status=status.HTTP_200_OK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment