def handle(self, *args, **options):
     api = Api(GIT_REAL_BOT_USERNAME)
     for project in RecruitProject.objects.filter(
         repository__owner=ORGANISATION, recruit_users__active__in=[True]
     ):
         print(project)
         recruit_project_invite_github_collaborators_to_repo(project_id=project.id)
예제 #2
0
def add_reviewer(cohort, content_item, reviewer, add_as_project_reviewer):
    api = Api(PERSONAL_GITHUB_NAME)

    projects = get_projects(cohort, content_item)
    # for o in projects: print(f"{o.id} {o}\n\t{o.content_item}\n\t{o.repository.full_name}\n")
    # cohort_users = cohort.get_member_users()
    # assert len(projects) == len(cohort_users), f"{projects}\n{cohort_users}"
    # if add_as_project_reviewer:
    if "@" in reviewer:
        user = User.objects.get(email=reviewer)
    else:
        user = User.objects.get(social_profile__github_name=reviewer)

    github_name = user.social_profile.github_name
    # else:
    #     github_name = reviewer

    for project in projects:
        print(project)
        if project.repository:
            add_collaborator(api, project.repository.full_name, github_name)
        project.save()
        if add_as_project_reviewer:
            project.reviewer_users.add(user)
        project.save()
예제 #3
0
def assign_random_reviewers(projects, users):
    api = Api(PERSONAL_GITHUB_NAME)
    shuffled_reviewers = list(
        shuffle_project_reviewers(projects, [o for o in users if o.active])
    )
    broken = [
        project
        for project, _ in shuffled_reviewers
        if not project.repository.full_name.startswith(ORGANISATION)
    ]
    # assert broken == [], "\n".join([f"{project.id} {project}" for project in broken])

    for project, user in shuffled_reviewers:
        print(user)
        if project.repository and project.repository.full_name.startswith(ORGANISATION):
            add_collaborator(
                api, project.repository.full_name, user.social_profile.github_name
            )
        project.reviewer_users.add(user)
        project.save()
        try:
            card = project.agile_card
        except AgileCard.DoesNotExist:
            pass
        else:
            assert card is not None
            card.reviewers.set(project.reviewer_users.all())
            card.save()
예제 #4
0
파일: helpers.py 프로젝트: Umuzi-org/Tilde
def add_collaborator(api,
                     repo_full_name,
                     github_user_name,
                     github_auth_login=None):
    api = api or Api(github_auth_login)

    print(f"adding {github_user_name}")

    response = api.put(
        f"repos/{repo_full_name}/collaborators/{github_user_name}",
        # {"permission": "push"},
        headers={"accept": "application/vnd.github.v3+json"},
        json=False,
        data={},
    )

    if response.status_code == 404:
        raise Exception(
            f"user or repo not found: {repo_full_name} {github_user_name}")

    if response.status_code == 422:
        # user blocked us
        return

    if response.status_code not in [201, 204]:
        raise Exception(
            f"bad response code {response.status_code} \n\tcontent: '{response.content}'"
        )
예제 #5
0
파일: helpers.py 프로젝트: ry-oc/Tilde
def create_repo(github_auth_login, repo_full_name, github_user_name,
                readme_text, api):
    api = api or Api(github_auth_login)

    create_org_repo(api, repo_full_name, exists_ok=True, private=True)
    upload_readme(api, repo_full_name, readme_text)
    protect_master(api, repo_full_name)
예제 #6
0
파일: views.py 프로젝트: Umuzi-org/Tilde
def authorize_github_callback(request):

    code = request.GET["code"]
    state = request.GET["state"]

    assert state == request.session[OAUTH_STATE]
    data = {
        "client_id": os.environ["GITHUB_CLIENT_ID"],
        "client_secret": os.environ["GITHUB_CLIENT_SECRET"],
        "code": code,
        "state": state,
    }

    response = requests.post(
        "https://github.com/login/oauth/access_token",
        json=data,
        headers={"Accept": "application/json"},
    )
    assert (response.status_code == 200
            ), "Unexpected response {response.status_code}\n{response.content}"

    response_data = response.json()

    if "error" in response_data:
        return JsonResponse(response_data)

    # save the credentials in the db
    token, created = models.GithubOAuthToken.objects.get_or_create(
        user=request.user, defaults=response_data)
    if not created:
        token.update(**response_data)
        token.save()

    # get the username
    api = Api(github_token=token.access_token)
    github_name = api.who_am_i()["github_name"]

    profile, created = models.SocialProfile.objects.get_or_create(
        user=request.user, github_name=github_name)
    if not created:
        profile.github_name = github_name
        profile.save()

    return JsonResponse({
        "status": "OK",
        "message": f"Github username set to {github_name}"
    })
예제 #7
0
    def handle(self, *args, **options):
        team_name = options["team"]
        team = Team.objects.get(name=team_name)
        assert team.active, f"{team} is not active. cannot continue"
        api = Api(PERSONAL_GITHUB_NAME)

        create_github_team(team_name, api)
        for user in team.user_set.filter(active=True):
            add_user_to_team(team_name, user, api)
예제 #8
0
def scrape_pull_requests_from_github():
    api = Api(PERSONAL_GITHUB_NAME)
    for repo in (models.Repository.objects.filter(
            recruit_projects__recruit_users__active__in=[True],
    ).filter(
            Q(recruit_projects__agile_card__status=AgileCard.IN_PROGRESS)
            | Q(recruit_projects__agile_card__status=AgileCard.IN_REVIEW)
            | Q(recruit_projects__agile_card__status=AgileCard.REVIEW_FEEDBACK)
    ).order_by("-recruit_projects__start_time")):
        scrape_repo_prs(api, repo)
예제 #9
0
    def invite_github_collaborators_to_repo(self):
        from social_auth.github_api import Api
        from git_real.helpers import add_collaborator

        users = list(self.recruit_users.all()) + list(
            self.reviewer_users.all())
        assert len(users) >= 1, "This repo has no collaborators to add"
        api = Api(GITHUB_BOT_USERNAME)
        for user in users:
            add_collaborator(api, self.repository.full_name,
                             user.social_profile.github_name)
예제 #10
0
def remove_collaborator(api,
                        repo_full_name,
                        github_user_name,
                        github_auth_login=None):
    api = api or Api(github_auth_login)
    response = api.delete(
        f"repos/{repo_full_name}/collaborators/{github_user_name}",
        headers={"Accept": "application/vnd.github.v3+json"},
        json=False,
    )
    assert (response.status_code == 204
            ), f"bad response code {response.status_code} {response.text}"
예제 #11
0
def scrape_repos_from_github():
    api = Api(PERSONAL_GITHUB_NAME)

    scrape_and_save_organisation_repos(api, ORGANISATION)

    for user in core_models.User.objects.filter(active=True,):
        try:
            profile = social_models.SocialProfile.objects.get(user=user)
        except social_models.SocialProfile.DoesNotExist:
            continue
        if profile.github_name:
            github_name = profile.github_name
            scrape_and_save_user_repos(api, github_name, user)
    def handle(self, *args, **options):
        api = Api(PERSONAL_GITHUB_NAME)
        existing_users = list(list_org_outside_collaborators(api))
        inactive_users = User.objects.filter(active=False)
        for user in inactive_users:
            try:
                social_profile = user.social_profile
            except SocialProfile.DoesNotExist:
                continue
            if social_profile.github_name in existing_users:
                remove_collaborator(api, user.social_profile.github_name)

        active_users = User.objects.filter(active=True)
예제 #13
0
    def handle(self, *args, **options):
        api = Api(PERSONAL_GITHUB_NAME)

        for proj in RecruitProject.objects.filter(repository__owner=ORGANISATION):
            repo = proj.repository
            print(repo)

            protect_master(api, repo.full_name)
            for user in proj.reviewer_users.all():
                print(user.social_profile.github_name)
                add_collaborator(api, repo.full_name, user.social_profile.github_name)
            for user in proj.recruit_users.all():
                print(user.social_profile.github_name)
                add_collaborator(api, repo.full_name, user.social_profile.github_name)
        print()
예제 #14
0
def add_collaborator(api,
                     repo_full_name,
                     github_user_name,
                     github_auth_login=None):
    api = api or Api(github_auth_login)

    response = api.put(
        f"repos/{repo_full_name}/collaborators/{github_user_name}",
        {"permission": "push"},
        json=False,
    )

    if response.status_code == 404:
        return  # TODO
        raise Exception(
            f"user or repo not found: {repo_full_name} {github_user_name}")

    if response.status_code not in [201, 204]:
        raise Exception(response.content)
예제 #15
0
def assign_random_reviewers(cards, users):
    api = Api(PERSONAL_GITHUB_NAME)
    shuffled_reviewers = list(
        shuffle_project_reviewers(cards, [o for o in users if o.active]))

    for card, user in shuffled_reviewers:
        print(user)

        if card.recruit_project:
            project = card.recruit_project
            if project.repository and project.repository.full_name.startswith(
                    ORGANISATION):
                add_collaborator(api, project.repository.full_name,
                                 user.social_profile.github_name)
            project.reviewer_users.add(user)
            project.save()

        if user not in card.reviewers.all():
            card.reviewers.add(user)
            card.save()
예제 #16
0
def add_reviewer(team, content_item, reviewer, add_as_project_reviewer):
    api = Api(PERSONAL_GITHUB_NAME)

    cards = get_team_cards(team, content_item)

    if "@" in reviewer:
        user = User.objects.get(email=reviewer)
    else:
        user = User.objects.get(social_profile__github_name=reviewer)

    github_name = user.social_profile.github_name

    for card in cards:

        if card.recruit_project and card.repository:
            add_collaborator(api, card.repository.full_name, github_name)
        card.save()
        if add_as_project_reviewer:
            card.reviewers.add(user)
            if card.recruit_project:
                card.recruit_project.reviewer_users.add(user)
        card.save()
예제 #17
0
def get_repo(github_auth_login, repo_full_name, api=None, response404=None):
    api = api or Api(github_auth_login)
    return api.request(f"repos/{repo_full_name}", response404=response404)
예제 #18
0
from core.models import User, Cohort, RecruitCohort
from curriculum_tracking.models import Cohort, RecruitProject
from social_auth.models import SocialProfile
from git_real.helpers import add_collaborator
from social_auth.github_api import Api
from git_real.constants import PERSONAL_GITHUB_NAME, ORGANISATION

staff_github_names = [
    o.github_name for o in SocialProfile.objects.filter(user__is_staff=True,
                                                        user__active=True)
]

api = Api(PERSONAL_GITHUB_NAME)

cohort_ds = Cohort.objects.get(pk=50)
cohort_web = Cohort.objects.get(pk=51)
cohorts = [cohort_ds, cohort_web]
cohort_users = []

for cohort in cohorts:
    cohort_users.extend(
        [o.user for o in RecruitCohort.objects.filter(cohort=cohort)])

projects = []
for user in cohort_users:
    projects.extend(RecruitProject.objects.filter(recruit_users__in=[user]))

repos = [project.repository for project in projects if project.repository]
repo_names = [
    repo.full_name for repo in repos if repo.full_name.startswith(ORGANISATION)
]
예제 #19
0
 def handle(self, *args, **options):
     api = Api(PERSONAL_GITHUB_NAME)
     who = api.who_am_i()
     pprint(who)
예제 #20
0
def scrape_pull_requests_from_github():
    api = Api(PERSONAL_GITHUB_NAME)
    for repo in models.Repository.objects.filter(archived=False):
        scrape_repo_prs(api, repo)