1
1
import { z } from "zod" ;
2
2
import { githubRequest , buildUrl } from "../common/utils.js" ;
3
- import { GitHubCommitSchema , GitHubListCommitsSchema } from "../common/types.js" ;
4
3
5
- // Schema definitions
6
4
export const ListCommitsSchema = z . object ( {
7
- owner : z . string ( ) . describe ( "Repository owner (username or organization)" ) ,
8
- repo : z . string ( ) . describe ( "Repository name" ) ,
9
- page : z . number ( ) . optional ( ) . describe ( "Page number for pagination (default: 1)" ) ,
10
- perPage : z . number ( ) . optional ( ) . describe ( "Number of results per page (default: 30, max: 100)" ) ,
11
- sha : z . string ( ) . optional ( ) . describe ( "SHA of the commit to start listing from" ) ,
5
+ owner : z . string ( ) ,
6
+ repo : z . string ( ) ,
7
+ sha : z . string ( ) . optional ( ) ,
8
+ page : z . number ( ) . optional ( ) ,
9
+ perPage : z . number ( ) . optional ( )
12
10
} ) ;
13
11
14
- // Type exports
15
- export type ListCommitsParams = z . infer < typeof ListCommitsSchema > ;
16
-
17
- // Function implementations
18
12
export async function listCommits (
19
13
owner : string ,
20
14
repo : string ,
21
- page : number = 1 ,
22
- perPage : number = 30 ,
15
+ page ? : number ,
16
+ perPage ? : number ,
23
17
sha ?: string
24
18
) {
25
- const params = {
26
- page,
27
- per_page : perPage ,
28
- ...( sha ? { sha } : { } )
29
- } ;
30
-
31
- const url = buildUrl ( `https://api.github.com/repos/${ owner } /${ repo } /commits` , params ) ;
32
-
33
- const response = await githubRequest ( url ) ;
34
- return GitHubListCommitsSchema . parse ( response ) ;
35
- }
36
-
37
- export async function getCommit (
38
- owner : string ,
39
- repo : string ,
40
- sha : string
41
- ) {
42
- const response = await githubRequest (
43
- `https://api.github.com/repos/${ owner } /${ repo } /git/commits/${ sha } `
44
- ) ;
45
-
46
- return GitHubCommitSchema . parse ( response ) ;
47
- }
48
-
49
- export async function createCommit (
50
- owner : string ,
51
- repo : string ,
52
- message : string ,
53
- tree : string ,
54
- parents : string [ ]
55
- ) {
56
- const response = await githubRequest (
57
- `https://api.github.com/repos/${ owner } /${ repo } /git/commits` ,
58
- {
59
- method : "POST" ,
60
- body : {
61
- message,
62
- tree,
63
- parents,
64
- } ,
65
- }
66
- ) ;
67
-
68
- return GitHubCommitSchema . parse ( response ) ;
69
- }
70
-
71
- export async function compareCommits (
72
- owner : string ,
73
- repo : string ,
74
- base : string ,
75
- head : string
76
- ) {
77
- const response = await githubRequest (
78
- `https://api.github.com/repos/${ owner } /${ repo } /compare/${ base } ...${ head } `
19
+ return githubRequest (
20
+ buildUrl ( `https://api.github.com/repos/${ owner } /${ repo } /commits` , {
21
+ page : page ?. toString ( ) ,
22
+ per_page : perPage ?. toString ( ) ,
23
+ sha
24
+ } )
79
25
) ;
80
-
81
- return z . object ( {
82
- url : z . string ( ) ,
83
- html_url : z . string ( ) ,
84
- permalink_url : z . string ( ) ,
85
- diff_url : z . string ( ) ,
86
- patch_url : z . string ( ) ,
87
- base_commit : GitHubCommitSchema ,
88
- merge_base_commit : GitHubCommitSchema ,
89
- commits : z . array ( GitHubCommitSchema ) ,
90
- total_commits : z . number ( ) ,
91
- status : z . string ( ) ,
92
- ahead_by : z . number ( ) ,
93
- behind_by : z . number ( ) ,
94
- } ) . parse ( response ) ;
95
- }
26
+ }
0 commit comments