File: replace-relative-links.yml

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (217 lines) | stat: -rw-r--r-- 8,675 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
parameters: 
  TargetFolder: ''
  RootFolder: ''
  BuildSHA: ''
  RepoId: $(Build.Repository.Name)

steps:
  - task: PythonScript@0
    displayName: Replace Relative Readme Links with Absolute References
    inputs:
      scriptSource: inline
      script: |
        import argparse
        import sys
        import os
        import logging
        import glob
        import re
        import fnmatch
        from io import open
        try:
            from pathlib import Path
        except:
            from pathlib2 import Path

        # This script is intended to be run against a single folder. All readme.md files (regardless of casing) will have the relative links
        # updated with appropriate full reference links. This is a recursive update..

        logging.getLogger().setLevel(logging.INFO)

        RELATIVE_LINK_REPLACEMENT_SYNTAX = (
            "https://github.com/{repo_id}/tree/{build_sha}/{target_resource_path}"
        )

        LINK_DISCOVERY_REGEX = r"\[([^\]]*)\]\(([^)]+)\)"
        PREDEFINED_LINK_DISCOVERY_REGEX = r"(\[[^\]]+]\:)\s*([^\s]+)"

        IMAGE_FILE_EXTENSIONS = ['.jpeg', '.jpg', '.png', '.gif', '.tiff']
        RELATIVE_LINK_REPLACEMENT_SYNTAX_FOR_IMAGE = (
            "https://github.com/{repo_id}/raw/{build_sha}/{target_resource_path}"
        )

        def locate_readmes(directory):
            readme_set = []

            for root, dirs, files in os.walk(directory):
                for file in files:
                    if file.lower() == "readme.md":
                        readme_set.append(os.path.join(root, file))
            return readme_set


        def is_relative_link(link_value, readme_location):
            link_without_location = link_value
            if link_without_location.find('#') > 0:
                link_without_location = link_without_location[0:link_without_location.find('#')]

            try:
                return os.path.exists(
                    os.path.abspath(os.path.join(os.path.dirname(readme_location), link_without_location))
                )
            except:
                return False


        def replace_relative_link(match, readme_location, root_folder, build_sha, repo_id):
            link_path = match.group(2).strip()

            if is_relative_link(link_path, readme_location):
                # if it is a relative reference, we need to find the path from the root of the repository
                resource_absolute_path = os.path.abspath(
                    os.path.join(os.path.dirname(readme_location), link_path)
                )
                placement_from_root = os.path.relpath(resource_absolute_path, root_folder)

                suffix = Path(placement_from_root).suffix
                if (suffix in IMAGE_FILE_EXTENSIONS):
                    updated_link = RELATIVE_LINK_REPLACEMENT_SYNTAX_FOR_IMAGE.format(
                        repo_id=repo_id,
                        build_sha=build_sha,
                        target_resource_path=placement_from_root,
                    ).replace("\\", "/")
                else:
                    updated_link = RELATIVE_LINK_REPLACEMENT_SYNTAX.format(
                        repo_id=repo_id,
                        build_sha=build_sha,
                        target_resource_path=placement_from_root,
                    ).replace("\\", "/")

                return "[{}]({})".format(match.group(1), updated_link)
            else:
                return match.group(0)

        def replace_predefined_relative_links(match, readme_location, root_folder, build_sha, repo_id):
            link_path = match.group(2).strip()

            if is_relative_link(link_path, readme_location):
                # if it is a relative reference, we need to find the path from the root of the repository
                resource_absolute_path = os.path.abspath(
                    os.path.join(os.path.dirname(readme_location), link_path)
                )
                placement_from_root = os.path.relpath(resource_absolute_path, root_folder)

                suffix = Path(placement_from_root).suffix
                if (suffix in IMAGE_FILE_EXTENSIONS):
                    updated_link = RELATIVE_LINK_REPLACEMENT_SYNTAX_FOR_IMAGE.format(
                        repo_id=repo_id,
                        build_sha=build_sha,
                        target_resource_path=placement_from_root,
                    ).replace("\\", "/")
                else:
                    updated_link = RELATIVE_LINK_REPLACEMENT_SYNTAX.format(
                        repo_id=repo_id,
                        build_sha=build_sha,
                        target_resource_path=placement_from_root,
                    ).replace("\\", "/")

                return "{} {}".format(match.group(1), updated_link)
            else:
                return match.group(0)


        def transfer_content_to_absolute_references(
            root_folder, build_sha, repo_id, readme_location, content
        ):
            content = re.sub(
                LINK_DISCOVERY_REGEX,
                lambda match, readme_location=readme_location, root_folder=root_folder, build_sha=build_sha, repo_id=repo_id: replace_relative_link(
                    match, readme_location, root_folder, build_sha, repo_id
                ),
                content,
            )

            content = re.sub(
                PREDEFINED_LINK_DISCOVERY_REGEX,
                lambda match, readme_location=readme_location, root_folder=root_folder, build_sha=build_sha, repo_id=repo_id: replace_predefined_relative_links(
                    match, readme_location, root_folder, build_sha, repo_id
                ),
                content,
            )

            return content


        if __name__ == "__main__":
            parser = argparse.ArgumentParser(
                description="Replaces relative links for any README.md under the target folder. Given any discovered relative link, will replace with the provided repoId and SHA. Case insensitive"
            )

            parser.add_argument(
                "-t",
                "--target",
                dest="target_folder",
                help="The target folder that contains a README ",
                default="${{ parameters.TargetFolder }}",
            )

            parser.add_argument(
                "-i",
                "--repoid",
                dest="repo_id",
                help='The target repository used as the base for the path replacement. Full Id, example: "Azure/azure-sdk-for-net"',
                default="${{ parameters.RepoId }}",
            )

            parser.add_argument(
                "-r",
                "--root",
                dest="root_folder",
                help="The root directory of the repository. This gives us the ability to rationalize links in situations where a relative link traverses UPWARDS from the readme.",
                default="${{ parameters.RootFolder }}",
            )

            parser.add_argument(
                "-s",
                "--sha",
                dest="build_sha",
                help="The commit hash associated with this change. Using this will mean that links will never be broken.",
                default="${{ parameters.BuildSHA }}",
            )

            args = parser.parse_args()

            logging.info("Root Folder: {}".format(args.root_folder))
            logging.info("Target Folder: {}".format(args.target_folder))
            logging.info("Repository Id: {}".format(args.repo_id))
            logging.info("Build SHA: {}".format(args.build_sha))

            readme_files = locate_readmes(args.target_folder)

            for readme_location in readme_files:
                try:
                    logging.info(
                        "Running Relative Link Replacement on {}.".format(readme_location)
                    )

                    with open(readme_location, "r", encoding="utf-8") as readme_stream:
                        readme_content = readme_stream.read()

                    new_content = transfer_content_to_absolute_references(
                        args.root_folder,
                        args.build_sha,
                        args.repo_id,
                        readme_location,
                        readme_content,
                    )

                    with open(readme_location, "w", encoding="utf-8") as readme_stream:
                        readme_stream.write(new_content)
                        
                except Exception as e:
                    logging.error(e)
                    exit(1)

  - script: |
        git diff -U0
    displayName: Highlight Readme Updates