1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2019, Linaro Limited
5#
6
7from subprocess import Popen, PIPE
8import argparse
9
10
11def get_args():
12    parser = argparse.ArgumentParser(description='Helper script that updates '
13                                     'the CHANGELOG.md file.\n'
14                                     'Usage example:\n'
15                                     '  ./update_changelog.py '
16                                     ' --changelog-file CHANGELOG.md'
17                                     ' --release-version 3.7.0'
18                                     ' --previous-release-version 3.6.0'
19                                     ' --release-date 2019-10-11')
20
21    parser.add_argument('--changelog-file', action='store', required=False,
22                        default='CHANGELOG.md',
23                        help='Changelog file to be updated.')
24
25    parser.add_argument('--release-date', action='store', required=True,
26                        help='The release date (yyyy-mm-dd).')
27
28    parser.add_argument('--release-version', action='store', required=True,
29                        help='Release version (MAJOR.MINOR.PATCH).')
30
31    parser.add_argument('--previous-release-version', action='store',
32                        required=True,
33                        help='Previous release version (MAJOR.MINOR.PATCH).')
34
35    return parser.parse_args()
36
37
38def prepend_write(filename, text):
39    with open(filename, 'r+') as f:
40        current_content = f.read()
41        f.seek(0, 0)
42        f.write(text + '\n' + current_content)
43        f.flush()
44
45
46def get_previous_release_date(tag):
47    cmd = "git log -1 --date=format:%Y-%m-%d --format=format:%cd " \
48          "{}".format(tag)
49    process = Popen(cmd.split(), stdout=PIPE)
50    (output, err) = process.communicate()
51    return output.decode("utf-8")
52
53
54def main():
55    global args
56
57    args = get_args()
58
59    gits = ["OP-TEE/optee_os", "OP-TEE/optee_client", "OP-TEE/optee_test",
60            "OP-TEE/build", "linaro-swg/optee_examples"]
61
62    # Shorten name
63    clf = args.changelog_file
64    rv = args.release_version
65    prv = args.previous_release_version
66    rd = args.release_date
67    prd = get_previous_release_date(prv)
68
69    # In some cases we need underscore in string
70    rvu = rv.replace('.', '_')
71
72    text = "# OP-TEE - version {} ({})\n".format(rv, rd)
73    text += "\n"
74    text += "- Links to the release pages, commits and pull requests merged " \
75            "into this release for:\n"
76
77    for g in gits:
78        gu = g.replace('/', '_')
79        gu = gu.replace('-', '_')
80        text += "  - {}: [release page][{}_release_{}], " \
81                "[commits][{}_commits_{}] and [pull requests]" \
82                "[{}_pr_{}]\n".format(g, gu, rvu, gu, rvu, gu, rvu)
83
84    text += "\n"
85
86    for g in gits:
87        gu = g.replace('/', '_')
88        gu = gu.replace('-', '_')
89        text += "\n[{}_release_{}]: https://github.com/{}/releases/tag/" \
90                "{}\n".format(gu, rvu, g, rv)
91        text += "[{}_commits_{}]: https://github.com/{}/compare/" \
92                "{}...{}\n".format(gu, rvu, g, prv, rv)
93        text += "[{}_pr_{}]: https://github.com/{}/pulls?q=is%3Apr+is%3A" \
94                "merged+base%3Amaster+merged%3A{}..{}\n".format(
95                        gu, rvu, g, prd, rd)
96
97    prepend_write(args.changelog_file, text)
98
99
100if __name__ == "__main__":
101    main()
102