1
- const { existsSync, readFileSync } = require ( 'fs' )
1
+ const { existsSync } = require ( 'fs' )
2
2
const { resolve } = require ( 'path' )
3
3
const core = require ( '@actions/core' )
4
4
const github = require ( '@actions/github' )
5
- const read = require ( '@commitlint/read ' )
5
+ const exec = require ( '@actions/exec ' )
6
6
const lint = require ( '@commitlint/lint' )
7
7
const { format } = require ( '@commitlint/format' )
8
8
const load = require ( '@commitlint/load' )
9
+ const gitRawCommits = require ( 'git-raw-commits' )
9
10
10
- const githubToken = process . env . GITHUB_TOKEN
11
+ const pullRequestEvent = 'pull_request'
12
+
13
+ const { GITHUB_TOKEN , GITHUB_EVENT_NAME , GITHUB_SHA } = process . env
11
14
12
15
const configPath = resolve (
13
16
process . env . GITHUB_WORKSPACE ,
14
17
core . getInput ( 'configFile' ) ,
15
18
)
16
19
17
20
const getRangeFromPullRequest = async ( ) => {
18
- const octokit = new github . GitHub ( githubToken )
21
+ if ( GITHUB_EVENT_NAME !== pullRequestEvent ) return [ null , GITHUB_SHA ]
22
+
23
+ const octokit = new github . GitHub ( GITHUB_TOKEN )
19
24
const { owner, repo, number } = github . context . issue
20
25
const { data : commits } = await octokit . pulls . listCommits ( {
21
26
owner,
@@ -29,8 +34,30 @@ const getRangeFromPullRequest = async () => {
29
34
return [ from , to ]
30
35
}
31
36
37
+ function getHistoryCommits ( from , to ) {
38
+ const options = {
39
+ from,
40
+ to,
41
+ }
42
+
43
+ if ( ! from ) {
44
+ options . maxCount = 1
45
+ }
46
+
47
+ return new Promise ( ( resolve , reject ) => {
48
+ const data = [ ]
49
+
50
+ gitRawCommits ( options )
51
+ . on ( 'data' , chunk => data . push ( chunk . toString ( 'utf-8' ) ) )
52
+ . on ( 'error' , reject )
53
+ . on ( 'end' , ( ) => {
54
+ resolve ( data )
55
+ } )
56
+ } )
57
+ }
58
+
32
59
const showLintResults = async ( [ from , to ] ) => {
33
- const commits = await read ( { from, to } )
60
+ const commits = await getHistoryCommits ( from , to )
34
61
const config = existsSync ( configPath )
35
62
? await load ( { } , { file : configPath } )
36
63
: { }
@@ -46,18 +73,17 @@ const showLintResults = async ([from, to]) => {
46
73
} ,
47
74
)
48
75
49
- if ( formattedResults . length ) {
50
- process . stderr . write ( formattedResults )
51
- process . exit ( 1 )
76
+ if ( formattedResults ) {
77
+ core . setFailed (
78
+ `You have commit messages with errors\n\n${ formattedResults } ` ,
79
+ )
52
80
} else {
53
81
console . log ( 'Lint free! 🎉' )
54
82
}
55
83
}
56
84
57
85
const exitWithMessage = message => error => {
58
- console . log ( message )
59
- console . error ( error )
60
- process . exit ( 1 )
86
+ core . setFailed ( `${ message } \n${ error } ` )
61
87
}
62
88
63
89
const main = ( ) =>
0 commit comments