@@ -63,41 +63,64 @@ def post(url, payload:)
63
63
end
64
64
end
65
65
66
- begin
67
- # Quickly finish collecting facts to avoid a race condition as much as possible.
68
- # TODO: Retry this operation several times if the race happens often.
69
- ls_remote = Git . ls_remote ( 'github' )
70
- show_ref = Git . show_ref
71
-
72
- # Start digesting the data after the collection.
73
- remote_refs = Hash [ ls_remote . lines . map { |l | rev , ref = l . chomp . split ( "\t " ) ; [ ref , rev ] } ]
74
- local_refs = Hash [ show_ref . lines . map { |l | rev , ref = l . chomp . split ( ' ' ) ; [ ref , rev ] } ]
75
-
76
- # Remove refs which are not to be checked here.
77
- remote_refs . delete ( 'HEAD' ) # show-ref does not show it
78
- remote_refs . keys . each { |ref | remote_refs . delete ( ref ) if ref . match ( %r[\A refs/pull/\d +/\w +\z ] ) } # pull requests
79
-
80
- # Check consistency
81
- errors = [ ]
82
- ( remote_refs . keys | local_refs . keys ) . each do |ref |
83
- remote_rev = remote_refs [ ref ]
84
- local_rev = local_refs [ ref ]
85
-
86
- if remote_rev != local_rev
87
- errors << [ remote_rev , local_rev ]
66
+ module GitSyncCheck
67
+ class Errors < StandardError
68
+ attr_reader :errors
69
+
70
+ def initialize ( errors )
71
+ @errors = errors
72
+ super ( 'git-sync-check failed' )
88
73
end
89
74
end
90
75
91
- if errors . empty?
92
- puts 'SUCCUESS: Everything is consistent.'
93
- else
94
- message = "FAILURE: Following inconsistencies are found.\n "
95
- errors . each do |remote_rev , local_rev |
96
- message << "remote:#{ remote_rev . inspect } local:#{ local_rev . inspect } \n "
76
+ def self . check_consistency
77
+ # Quickly finish collecting facts to avoid a race condition as much as possible.
78
+ # TODO: Retry this operation several times if the race happens often.
79
+ ls_remote = Git . ls_remote ( 'github' )
80
+ show_ref = Git . show_ref
81
+
82
+ # Start digesting the data after the collection.
83
+ remote_refs = Hash [ ls_remote . lines . map { |l | rev , ref = l . chomp . split ( "\t " ) ; [ ref , rev ] } ]
84
+ local_refs = Hash [ show_ref . lines . map { |l | rev , ref = l . chomp . split ( ' ' ) ; [ ref , rev ] } ]
85
+
86
+ # Remove refs which are not to be checked here.
87
+ remote_refs . delete ( 'HEAD' ) # show-ref does not show it
88
+ remote_refs . keys . each { |ref | remote_refs . delete ( ref ) if ref . match ( %r[\A refs/pull/\d +/\w +\z ] ) } # pull requests
89
+
90
+ # Check consistency
91
+ errors = { }
92
+ ( remote_refs . keys | local_refs . keys ) . each do |ref |
93
+ remote_rev = remote_refs [ ref ]
94
+ local_rev = local_refs [ ref ]
95
+
96
+ if remote_rev != local_rev
97
+ errors [ ref ] = [ remote_rev , local_rev ]
98
+ end
97
99
end
98
- Slack . notify ( message )
99
- puts message
100
+
101
+ unless errors . empty?
102
+ raise Errors . new ( errors )
103
+ end
104
+ end
105
+ end
106
+
107
+ attempts = 3
108
+ begin
109
+ GitSyncCheck . check_consistency
110
+ puts 'SUCCUESS: Everything is consistent.'
111
+ rescue GitSyncCheck ::Errors => e
112
+ attempts -= 1
113
+ if attempts > 0
114
+ sleep 5
115
+ retry
116
+ end
117
+
118
+ message = "FAILURE: Following inconsistencies are found.\n "
119
+ e . errors . each do |ref , ( remote_rev , local_rev ) |
120
+ message << "ref:#{ ref . inspect } remote:#{ remote_rev . inspect } local:#{ local_rev . inspect } \n "
100
121
end
122
+ Slack . notify ( message )
123
+ puts message
101
124
rescue => e
102
125
Slack . notify ( "#{ e . class } : #{ e . message } \n #{ e . backtrace . join ( "\n " ) } " )
103
126
end
0 commit comments