-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpull_request_commit.rb
More file actions
81 lines (63 loc) · 1.88 KB
/
pull_request_commit.rb
File metadata and controls
81 lines (63 loc) · 1.88 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
# SPDX-License-Identifier: BSD-2-Clause
#
# pull_request_commit.rb
# Part of NetDEF CI System
#
# Copyright (c) 2023 by
# Network Device Education Foundation, Inc. ("NetDEF")
#
# frozen_string_literal: true
module Github
module Parsers
# Class responsible for parsing pull request commits.
class PullRequestCommit
# Initializes a new PullRequestCommit parser.
#
# @param repo [String] the repository name.
# @param pr_id [Integer] the pull request ID.
def initialize(repo, pr_id)
@repo = repo
@pr_id = pr_id
@pull_request = PullRequest.find_by(github_pr_id: pr_id)
created_github_check unless invalid?
end
def invalid?
@pull_request.nil?
end
# Finds a commit by its SHA.
#
# @param sha256 [String] the SHA256 hash of the commit.
# @return [Hash, nil] the commit data if found, otherwise nil.
def find_by_sha(sha256)
return nil if sha256.nil?
page = 1
loop do
output = @github_check.fetch_pull_request_commits(@pr_id, @repo, page)
break if output.empty?
found = output.find { |entry| entry[:sha][0..7].include? sha256 }
return found unless found.nil? or found.empty?
page += 1
end
nil
end
# Retrieves the last commit in the pull request.
#
# @return [Hash, nil] the last commit data if found, otherwise nil.
def last_commit_in_pr
page = 1
last_commit = nil
loop do
output = @github_check.fetch_pull_request_commits(@pr_id, @repo, page)
break if output.last.nil?
last_commit = output.last
page += 1
end
last_commit
end
private
def created_github_check
@github_check = Github::Check.new(@pull_request.check_suites.last)
end
end
end
end