@@ -12,17 +12,14 @@ async function run() {
12
12
const githubToken = core . getInput ( 'github-token' )
13
13
const octokit = github . getOctokit ( githubToken )
14
14
15
- // Get data
16
- const { data : pullRequest } = await octokit . rest . pulls . get ( {
17
- owner : github . context . payload . repository . owner . login ,
18
- repo : github . context . payload . repository . name ,
19
- pull_number : github . context . payload . pull_request . number
20
- } )
21
- const { data : commits } = await octokit . rest . pulls . listCommits ( {
22
- owner : github . context . payload . repository . owner . login ,
23
- repo : github . context . payload . repository . name ,
24
- pull_number : github . context . payload . pull_request . number
25
- } )
15
+ // Get context
16
+ const eventName = github . context . eventName
17
+ const payload = github . context . payload
18
+ const owner = payload . repository . owner . login
19
+ const repo = payload . repository . name
20
+
21
+ const ref = payload . ref
22
+ const pullRequest = payload . pull_request
26
23
27
24
// -----
28
25
// --------------- GET INPUT
@@ -47,21 +44,91 @@ async function run() {
47
44
const regexCommitMessageSplit = RegExp ( core . getInput ( 're-commit-message-split' ) , 's' )
48
45
const regexCommitMessageSubject = RegExp ( core . getInput ( 're-commit-message-subject' ) )
49
46
const regexPullRequestTitle = RegExp ( core . getInput ( 're-pull-request-title' ) )
47
+ const regexTagName = RegExp ( core . getInput ( 're-tag-name' ) )
48
+
49
+ // -----
50
+ // --------------- CHECK PULL REQUEST
51
+ // ----------
52
+
53
+ if ( eventName === "pull_request" ) {
54
+ // Print pull request title
55
+ core . info ( `Pull request title: ${ pullRequest . title } ` )
56
+
57
+ // Check pull request title
58
+ if ( ! regexPullRequestTitle . test ( pullRequest . title ) )
59
+ core . setFailed ( "Pull Request title does not match regex" )
60
+ }
50
61
51
62
// -----
52
- // --------------- CHECK DATA
63
+ // --------------- CHECK BRANCH
53
64
// ----------
54
65
55
- core . info ( `Pull request title: ${ pullRequest . title } ` )
56
- core . info ( `Branch name: ${ pullRequest . head . ref } ` )
66
+ // Get branch name
67
+ let branchName = ""
68
+ if ( eventName === "pull_request" ) {
69
+ branchName = pullRequest . head . ref
70
+ } else if ( eventName === "push" && ref . startsWith ( "refs/heads" ) ) {
71
+ branchName = ref . replace ( "refs/heads/" , "" )
72
+ }
57
73
58
- // Check pull request title
59
- if ( ! regexPullRequestTitle . test ( pullRequest . title ) )
60
- core . setFailed ( "Pull Request title does not match regex" )
74
+ if ( branchName !== "" ) {
75
+ // Print branch name
76
+ core . info ( `Branch name: ${ branchName } ` )
61
77
62
- // Check branch name
63
- if ( ! regexBranchName . test ( pullRequest . head . ref ) )
64
- core . setFailed ( "Branch name does not match regex" )
78
+ // Check branch name
79
+ if ( ! regexBranchName . test ( branchName ) )
80
+ core . setFailed ( "Branch name does not match regex" )
81
+ }
82
+
83
+ // -----
84
+ // --------------- CHECK TAG
85
+ // ----------
86
+
87
+ if ( eventName === "push" && ref . startsWith ( "refs/tags" ) ) {
88
+ // Get tag name
89
+ const tagName = ref . replace ( "refs/tags/" , "" )
90
+
91
+ // Print tag name
92
+ core . info ( `Tag name: ${ tagName } ` )
93
+
94
+ // Check tag name
95
+ if ( ! regexTagName . test ( tagName ) )
96
+ core . setFailed ( "Tag name does not match regex" )
97
+ }
98
+
99
+ // -----
100
+ // --------------- CHECK COMMITS
101
+ // ----------
102
+
103
+ // Get commits
104
+ let commits = [ ]
105
+ if ( eventName === "pull_request" ) {
106
+ // Get all commits from pull request
107
+ const { data } = await octokit . rest . pulls . listCommits ( {
108
+ owner : owner ,
109
+ repo : repo ,
110
+ pull_number : pullRequest . number
111
+ } )
112
+
113
+ // Set commits
114
+ commits = data
115
+ } else if ( eventName === "push" ) {
116
+ // Get pushed commits
117
+ let commitPromises = [ ]
118
+
119
+ payload . commits . forEach ( commit => {
120
+ commitPromises . push ( octokit . rest . repos . getCommit ( {
121
+ owner : owner ,
122
+ repo : repo ,
123
+ ref : commit . id
124
+ } ) )
125
+ } )
126
+
127
+ // Append commits
128
+ for await ( const { data : commit } of commitPromises ) {
129
+ commits . push ( commit )
130
+ }
131
+ }
65
132
66
133
// Check all commits
67
134
commits . forEach ( commit => {
0 commit comments