Commit ad8e9587 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Add auth-token parameters

parent 9b2d859c
Loading
Loading
Loading
Loading
Loading
+28 −7
Changes for generateCR/changemarks.py: 28 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -168,14 +168,28 @@ class MR:
            # Build optional authentication headers for Git over HTTPS
            extra:list[str] = []
            if isinstance(self.headers, dict):
                if 'PRIVATE-TOKEN' in self.headers and self.headers['PRIVATE-TOKEN']:
                    extra += ['-c', f"http.extraheader=PRIVATE-TOKEN: {self.headers['PRIVATE-TOKEN']}"]
                if 'Authorization' in self.headers and self.headers['Authorization']:
                    # e.g., 'Bearer <token>'
                    extra += ['-c', f"http.extraheader={self.headers['Authorization']}"]
            
                # GitLab's web rate limit attributes a user from PRIVATE-TOKEN.
                # Authorization: Bearer is enough for /api/v4, and it replaces git's Basic header.
                private_token = self.headers.get('PRIVATE-TOKEN')
                if not private_token and self.headers.get('Authorization'):
                    authorization = self.headers['Authorization']
                    private_token = authorization[7:].strip() if authorization.lower().startswith('bearer ') else authorization
                if private_token:
                    extra += ['-c', f"http.extraheader=PRIVATE-TOKEN: {private_token}"]
            
            git_user = None
            git_token = None
            if isinstance(self.headers, dict):
                if self.headers.get('Authorization'):
                    git_user = 'oauth2'
                    authorization = self.headers['Authorization']
                    git_token = authorization[7:].strip() if authorization.lower().startswith('bearer ') else authorization
                elif self.headers.get('PRIVATE-TOKEN'):
                    git_user = 'gitlab-ci-token'
                    git_token = self.headers['PRIVATE-TOKEN']
            origin_url = add_basic_auth_to_url(self.web_url, git_user, git_token) if git_token else self.web_url
            set_url_cmd = [
                'git', 'remote', 'set-url', 'origin',add_basic_auth_to_url(self.web_url, 'gitlab-ci-token', self.headers['PRIVATE-TOKEN'])
                'git', 'remote', 'set-url', 'origin', origin_url
            ]
            logging.debug(f"Running: {' '.join(str(x) for x in set_url_cmd)}")
            set_url_proc = subprocess.run(set_url_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, text=True)
@@ -661,6 +675,7 @@ def main(args=None):
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--outdir', '-o', action='store', dest='outDirectory', default = 'out', metavar = '<output directory>',  help = 'specify output directory')
    parser.add_argument('-t', '--token', default = None, help="Token required for internal/private repositories")
    parser.add_argument('-at', '--auth-token', default = None, help="Authentication token required for internal/private repositories")
    parser.add_argument('rootURL',      help="Forge root URL")
    parser.add_argument('projectID',    help="Forge project ID")
    parser.add_argument('mergeID',      help="Merge IID")
@@ -675,6 +690,12 @@ def main(args=None):

    if pargs.token:
        headers = {'PRIVATE-TOKEN' : pargs.token}
    elif pargs.auth_token:
        # Bearer authenticates /api/v4. PRIVATE-TOKEN makes git and web requests count as authenticated.
        headers = {
            'Authorization' : f'Bearer {pargs.auth_token}',
            'PRIVATE-TOKEN' : pargs.auth_token,
        }
    else:
        logging.debug(f"No token provided, repository MUST BE PUBLIC")
        headers = None