def restore_snippets(project, entry):
    """
    Restore a single snippet
    """
    if not entry.get('visibility_level'):
        entry['visibility_level'] = VISIBILITY_PRIVATE

    if not entry.get('file_name'):
        entry['file_name'] = quote(entry['title']).replace(" ", "_").replace("'", "") + ".txt"

        snippet_content = os.path.join(args.backup_dir, "snippet_" + str(entry.get('id')) + "_content.dump")
        entry['code'] = gitlab_lib.parse_json(snippet_content)

        if entry['code']:
            gitlab_lib.debug("RESTORE ENTRY\n\tcomponent %s\n\turl %s\n\tproject %s\n\tentry %s\n" % (entry['component'],
                                                                                                      gitlab_lib.PROJECT_COMPONENTS[entry['component']],
                                                                                                      project['id'],
                                                                                                      gitlab_lib.prepare_restore_data(project, entry)))

            gitlab_lib.rest_api_call(gitlab_lib.PROJECT_COMPONENTS["snippets"] % (gitlab_lib.API_URL, project['id']),
                                     gitlab_lib.prepare_restore_data(project, entry))

            restore_notes(gitlab_lib.NOTES_FOR_SNIPPET % (gitlab_lib.API_URL, project['id'], str(entry.get('id'))),
                          project,
                          entry)
        else:
            gitlab_lib.error("Content file of snippet " + str(entry.get('id')) + " cannot be found. Won't restore snippet!")
def restore_notes(api_url, project, entry):
    """
    Restore the notes to a component like snippets or issues
    """
    notes_file = os.path.join(args.backup_dir, entry['component'] + "_" + str(entry.get('id')) + "_notes.dump")

    if os.path.exists(notes_file):
        notes = gitlab_lib.parse_json(notes_file)

        for note in notes:
            note[entry['component'] + '_id'] = str(entry.get('id'))

            gitlab_lib.rest_api_call(api_url,
                                     gitlab_lib.prepare_restore_data(project, note))
def restore_entry(project, queue):
    """
    Restore a single entry of a project component
    """
    while not queue.empty():
        entry = queue.get()

        gitlab_lib.log("Restoring %s [%s]" % (entry['component'], entry.get('name') or "ID " + str(entry.get('id'))))

        # for snippets we must additionally restore the content file
        if entry['component'] == "snippets":
            restore_snippets(project, entry)
        else:
            result = gitlab_lib.rest_api_call(gitlab_lib.PROJECT_COMPONENTS[entry['component']] % (gitlab_lib.API_URL, project['id']),
                                              gitlab_lib.prepare_restore_data(project, entry))

            if entry['component'] == "issues":
                result = result.json()
                gitlab_lib.rest_api_call(gitlab_lib.ISSUE_EDIT % (gitlab_lib.API_URL, project['id'], result.get('id')),
                                         gitlab_lib.prepare_restore_data(project, update_issue_metadata(entry)),
                                         "PUT")
                restore_notes(gitlab_lib.NOTES_FOR_ISSUE % (gitlab_lib.API_URL, project['id'], str(entry.get('id'))),
                              project,
                              entry)
def backup_snippets(api_url, project, backup_dir):
    """
    Backup snippets and their contents
    snippet contents must be backuped by additional api call
    """
    gitlab_lib.log(u"Backing up snippets from project %s [ID %s]" % (project['name'], project['id']))

    snippets = gitlab_lib.fetch(api_url % (gitlab_lib.API_URL, project['id']))

    dump(backup_dir, "snippets.json", snippets)

    for snippet in snippets:
        dump(backup_dir,
             "snippet_%d_content.dump" % (snippet['id'],),
             gitlab_lib.rest_api_call(gitlab_lib.GET_SNIPPET_CONTENT % (gitlab_lib.API_URL, project['id'], snippet['id']), method="GET").text)

        notes = gitlab_lib.fetch(gitlab_lib.NOTES_FOR_SNIPPET % (gitlab_lib.API_URL, project['id'], snippet['id']))

        if notes:
            dump(backup_dir, "snippet_%d_notes.dump" % (snippet['id'],), notes)