-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcommon_functions.pm
More file actions
168 lines (130 loc) · 5.33 KB
/
common_functions.pm
File metadata and controls
168 lines (130 loc) · 5.33 KB
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
#!/usr/bin/perl
package common_functions;
use Exporter;
use JSON;
# Export subroutines
our @ISA = qw(Exporter);
our @EXPORT = qw(get_and_verify_token get_microsoft_email get_git_name get_sorted_prs create_release_changelog has_backport_label);
# untaint environment
local $ENV{'PATH'} = '/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin';
sub get_microsoft_email {
unless (exists $ENV{MICROSOFT_EMAIL}) {
die "You must have a MICROSOFT_EMAIL set";
}
my $microsoft_email = $ENV{MICROSOFT_EMAIL};
return $microsoft_email;
}
sub get_git_name {
my $git_name = `git config user.name`;
# Strip trailing newline
chomp $git_name;
if ($git_name eq "") {
die "You must set your git name using 'git config user.name \"Your name Here\"'";
}
return $git_name;
}
sub get_and_verify_token {
unless (exists $ENV{GITHUB_TOKEN}) {
die "You must have a GITHUB_TOKEN set";
}
my $github_token = $ENV{GITHUB_TOKEN};
if ($ENV{GITHUB_TOKEN} =~ /^(\w+)$/x) {
$github_token = $1;
}
else {
die "Malformed GITHUB_TOKEN: $github_token";
}
my $cmd = "curl -sf -H 'Accept: application/vnd.github.v3.full+json' -X GET --user '$github_token:x-oauth-basic' " . 'https://api.github.com/';
my $result = `$cmd > /dev/null 2>&1`;
my $exit_code = $? >> 8;
if ($exit_code == 22) {
die "Your token was rejected by GitHub.";
}
return $github_token;
}
# Get sorted PRs according to merged-at by collecting PRs up to given data (using created info)
sub get_sorted_prs {
my $earliest_pr_date = @_[0];
my $repo_name = @_[1];
my %sorted_pr_hash = ();
my $github_token = get_and_verify_token();
my $page_number = 1;
$merged_date = "2100-12-12";
do {
my $prs_text = `curl -H "Accept: application/vnd.github.v3.full+json" -X GET --user "$github_token:x-oauth-basic" 'https://api.github.com/repos/citusdata/$repo_name/pulls?base=master&state=all&page=$page_number' 2> /dev/null`;
my @prs = @{decode_json($prs_text)};
foreach my $pr (@prs) {
my %pr_hash = %$pr;
if (defined($pr_hash{'merged_at'})) {
$merged_date = substr($pr_hash{'merged_at'}, 0, 10);
$created_date = substr($pr_hash{'created_at'}, 0, 10);
if ($created_date lt $earliest_pr_date) {
last;
}
if ($merged_date lt $earliest_pr_date) {
next;
}
$sorted_pr_hash{$pr_hash{'merged_at'}} = $pr_hash{'url'};
}
}
$page_number += 1;
} while ($created_date gt $earliest_pr_date);
my @keys = reverse sort keys(%sorted_pr_hash);
my @vals = @sorted_pr_hash{@keys};
print( "PRs has been read in the merge order ..." . "\n" );
return @vals;
}
# Search for the backport label given a label array
sub has_backport_label {
my @labels = @{$_[0]};
foreach my $label (@labels) {
%label_hash = %{$label};
if ($label_hash{'name'} eq 'backport') {
return 1;
}
}
return 0;
}
# Creates changelog entries up to the given last date. It extracts lines starting with DESCRIPTION: from
# PR messages to generate these entries. You can use different repo names to generate changelog entries for
# different repos.
sub create_release_changelog {
my $earliest_pr_date = @_[0];
my $repo_name = @_[1];
my $is_point_release = @_[2];
my @comment_lines = ();
my $github_token = get_and_verify_token();
my @sorted_pr_urls = get_sorted_prs($earliest_pr_date, $repo_name);
foreach my $pr_url (@sorted_pr_urls) {
print("Getting information for " . $pr_url . "\n");
my $pr_text = `curl -H "Accept: application/vnd.github.v3.full+json" -X GET --user "$github_token:x-oauth-basic" '$pr_url' 2> /dev/null`;
my %pr_hash = %{decode_json($pr_text)};
my $add_to_changelog = 1;
if ($is_point_release) {
my $issue_url = $pr_hash{'issue_url'};
my $issue_text = `curl -H "Accept: application/vnd.github.v3.full+json" -X GET --user "$github_token:x-oauth-basic" '$issue_url' 2> /dev/null`;
my %issue_hash = %{decode_json($issue_text)};
$add_to_changelog = 0;
@labels = @{$issue_hash{'labels'}};
if (has_backport_label(\@labels)) {
$add_to_changelog = 1;
}
}
if ($add_to_changelog) {
@log_output = split("\n", $pr_hash{'body'});
foreach $line (@log_output) {
if ($line =~ /^DESCRIPTION: */) {
$description_part = substr($line, length($&), -1);
if (length($description_part) > 78) {
print("You have to shorten PR message $description_part of $pr_url\n");
print("Description should not be longer than 78 charachters, please manually shorten this description\n");
push(@comment_lines, "TODO: " . "PLEASE SHORTEN THE NEXT LINE MANUALLY, IT SHOULD BE NO LONGER THAN 78 CHARS\n");
}
print("Description $description_part has been added ... \n");
push(@comment_lines, "* " . $description_part . "\n\n");
}
}
}
}
return @comment_lines;
}