1

I have a multiple project in my gitlab repository wherein I do perform multiple commits when it requires. I have develop a code in python through which I can get report of all the commits done by me in a csv format for all the projects available in gitlab repository as I have hard coded the the project ids in my python code as a LIST. The Header of the csv file is : Date, submitted, gitlab_url, project, username, subject.

Now I want to run the pipeline manually by setting up an environment variable as 'Project_Ids' and want to pass some of the project ids as value (More than one project id as a value) so that csv report should get generated for only these projects which has been passed as a value in environment variable.

so My question is , How can I pass multiple project ids as a value in 'Project_Ids' key while running the pipeline manually.

enter image description here

import gitlab
import os
import datetime
import csv
import re

Project_id_list = ['9427','8401','17937','26813','24899','23729','34779','27638','28600']

headerList = ['Date', 'Submitted', 'Gitlab_url', 'Project', 'Branch', 'Status', 'Username', 'Ticket', 'Subject']

filename = 'mydemo_{}'.format(datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S'))

# private token authentication
gl = gitlab.Gitlab('https://main.gitlab.in.com/', private_token="MLyWwLyEhU2zZjjjhZXog")

gl.auth()

# list all projects


for m in Project_id_list:
    i=0
    if (i<len(Project_id_list)):
        i=+1
    print(m)
    projects = gl.projects.get(m)
    commits = projects.commits.list(all=True, query_parameters={'ref_name': 'master'})

    with open(f"{filename}_{m}.csv", 'w', newline="") as file:
        dw = csv.DictWriter(file, delimiter=',',
              fieldnames=headerList)
        dw.writeheader()

        for commit in commits:
            print(commit)
            msg = commit.message

            if 'master' in msg or 'LCS-' in msg:
                projectName = projects.path_with_namespace
                branch = 'master'
                status = 'merged'
                date = commit.committed_date.split('T')[0]
                submitted1 = commit.created_at.split('T')[1]
                submitted = submitted1.split('.000')[0]
                Gitlab_url = commit.web_url.split('-')[0]
                username = commit.author_name
                subject = commit.title
                subject1 = commit.message.splitlines()
                print(subject1)
                subject2 = subject1[0:3]
                print(subject2)
                subject3 = '    '.join(subject2)
                print(subject3)
                match = re.search('S-\d+', subject3)
                

                if match:
                    ticket = match.group(0)
                    ticket_url = 'https://.in.com/browse/' + str(ticket)
                    ticket1 = ticket_url
                    dw.writerow({'Date': date, 'Submitted': submitted, 'Gitlab_url': Gitlab_url, 'Project': projectName,
                                 'Branch': branch, 'Status': status, 'Username': username, 'Ticket': ticket1,
                                 'Subject': subject3})
                else:
                    ticket1 = 'Not Found'
                    dw.writerow({'Date': date, 'Submitted': submitted, 'Gitlab_url': Gitlab_url, 'Project': projectName,
                                'Branch': branch, 'Status': status, 'Username': username, 'Ticket': ticket1,
                                'Subject': subject3})
4
  • 1
    Please post your code as text, images of code can not be accepted on Stack Overflow. Commented Aug 3, 2022 at 8:22
  • @KlausD. yes , I am editing my post. Commented Aug 3, 2022 at 8:33
  • Nit an image. Just copy and paste it. Commented Aug 3, 2022 at 8:35
  • @toyotaSupra...done Commented Aug 3, 2022 at 8:50

1 Answer 1

1

Just use a space or some other delimiter in the variable value. For example, a string like 123 456 789

Then in Python, simply parse the variable. For example, using the string .split method to split on whitespace.

import os
...
project_ids_variable = os.environ.get('PROJECT_IDS', '') # '123 456 789'
project_ids = project_ids_variable.split() # ['123', '456', '789']
for project_id in project_ids:
    project = gl.projects.get(project_id)
    print(project)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks , It is working One more doubt regarding the same, Is it possible to create a dropdown in VALUE field so that I will save all project id over there and then directly select the required project id while running pipeline manually.
@TheTester You can't create a dropdown, but you can pre-fill a static value (the default) and a helpful text description that will show up in the UI by using the value: and description: keys in your YAML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.