0

I have created a text file using file operations in python. I want the file to be pushed to my existed GITLAB repository.

I have tried the below code where i get the created file in my local folders.

file_path = 'E:\My material\output.txt'
k= 'Fail/Pass'
with open (file_path, 'w+') as text:
    text.write('Test case :' +k)
    text.close() 

What is the process or steps or any modifications in file_path to move the created text file to the GITLAB repository through python code.

Mike
  • 25
  • 6

3 Answers3

0

Do you mean executing Shell Commands with Python? Suppose this newly created file and this python script are both under the specific local git repository which connected with the remote repository you want to commit. our plan is packing all the bash command in os.system.

import os 
os.system('git add E:\My material\output.txt; git commit -m "anything"; git push -u origin master')

update

import os 
os.system('cd /local/repo; mv E:\My material\output.txt .; git add output.txt; git commit -m "anything"; git push -u origin master')
ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45
0

Using python gitlab module :

We can push the file to the gitlab,but you need to follow the below steps:

Step 1) Clone the repository to your local

Step 2) Add the file to the clonned repository

Step 3) Push the code to the gitlab

code:

import gitlab  
import base64
from gitlab import Gitlab
import shutil

callbacks = pygit2.RemoteCallbacks(pygit2.UserPass("Your_private_token", 'x-oauth-basic'))
repoClone = pygit2.clone_repository("https://gitlab.com/group/"+api_name+".git", local_clone_path,checkout_branch=target_branch,callbacks=callbacks)  # clonning the repo to local
shutil.copy(src,dst_clone_repo_path)   #copy your file to the cloned repo
repoClone.remotes.set_url("origin", "https://gitlab.com/group/"+api_name+".git")
index = repoClone.index
index.add_all()
index.write()
tree = index.write_tree()
oid = repoClone.create_commit('refs/heads/'+target_branch, author, commiter, "init commit",tree,[repoClone.head.peel().hex])
remote = repoClone.remotes["origin"]
credentials = pygit2.UserPass("your_private_token", 'x-oauth-basic')  # passing credentials
remote.credentials = credentials
remote.push(['refs/heads/'+target_branch],callbacks=callbacks) # push the code to the gitlab repo
mohit sehrawat
  • 171
  • 1
  • 12
0

A bit late to the party but:

Using the gitlab python library you can do this:


def commit_file(project_id: int, file_path: str, gitlab_url: str, private_token: str, branch: str = "main") -> bool:
    """Commit a file to the repository
    
    Parameters
    ----------
    project_id: int
        the project id to commit into. E.g. 1582
    file_path: str
        the file path to commit. NOTE: expecting a path relative to the
        repo path. This will also be used as the path to commit into.
        If you want to use absolute local path you will also have to 
        pass a parameter for the file relative repo path
    gitlab_url: str
        The gitlab url. E.g. https://gitlab.example.com
    private_token: str
        Private access token. See doc for more details
    branch: str
        The branch you are working in. See note below for more about this
    """
    gl = gitlab.Gitlab(gitlab_url, private_token=private_token)

    try:
        # get the project by the project id
        project = gl.projects.get(project_id)
        
        # read the file contents
        with open(file_path, 'r') as fin:
            content = fin.read()

        file_data = {'file_path': file_path,
                 'branch': branch,
                 'content': content,
                 'author_email': "your@email.com", # don't think this is required
                 'commit_message': 'Created a file'}

        resp = project.files.create(file_data)
        # do something with resp
    except gitlab.exceptions.GitlabGetError as get_error:
        # project does not exists
        print(f"could not find no project with id {project_id}: {get_error}")
        return False
    except gitlab.exceptions.GitlabCreateError as create_error:
        # project does not exists
        print(f"could not create file: {create_error}")
        return False

    return True

Example is based on gitlab project files documentation python-gitlab package docs

Note that if you want to create the file in a new branch, you will have to also provide a start_branch attribute:

file_data = {'file_path': file_path,
                 'branch': your_new_branch,
                 'start_branch': base_branch,
                 'content': content,
                 'author_email': "your@email.com",
                 'commit_message': 'Created a file in new branch'}

Also, if you don't care for using the python-gitlab package, you can use the rest api directly (using request or something like that. The relevant documentation is here

Tomer Cagan
  • 1,078
  • 17
  • 31