1
1
#!/usr/bin/env python3
2
- # Mock GitHub API server for testing an opened PR
2
+ # Mock GitHub API server for testing an opened PR or an issue for an image-refresh
3
3
# You can run this manually in `tasks/run-local.sh -i` with `podman cp` and running
4
4
# cd bots
5
5
# PYTHONPATH=. ./mock-github cockpit-project/bots $(git rev-parse HEAD) &
@@ -27,6 +27,12 @@ class Handler(MockHandler):
27
27
self .replyJson (self .server .data [self .path ])
28
28
elif self .path .startswith (f'/repos/{ repo } /pulls?' ):
29
29
self .replyJson ([self .server .data [f'/repos/{ repo } /pulls/1' ]])
30
+ elif self .path == f'/repos/{ repo } /pulls/2' :
31
+ # image-refresh issue converted into PR
32
+ self .replyJson ({
33
+ ** self .server .data [f'/repos/{ repo } /issues/2' ],
34
+ "head" : {"sha" : "a1b2c3" },
35
+ })
30
36
elif self .path == f'/{ repo } /{ sha } /.cockpit-ci/container' :
31
37
self .replyData ('quay.io/cockpit/tasks' )
32
38
else :
@@ -35,6 +41,18 @@ class Handler(MockHandler):
35
41
def do_POST (self ):
36
42
if self .path .startswith (f'/repos/{ repo } /statuses/{ sha } ' ):
37
43
self .replyJson ({})
44
+ # new SHA from mock-pushed PR #2 for image-refresh
45
+ elif self .path .startswith (f'/repos/{ repo } /statuses/a1b2c3' ):
46
+ self .replyJson ({})
47
+ elif self .path .startswith (f'/repos/{ repo } /issues/2' ):
48
+ # updates the issue to "in progress", sets label, adds comment etc.; maybe keep state and assert?
49
+ self .replyJson ({})
50
+ elif self .path == f'/repos/{ repo } /pulls' :
51
+ # image-refresh creates a PR for a refresh isssue
52
+ self .replyJson ({
53
+ ** GITHUB_DATA [f'/repos/{ repo } /issues/2' ],
54
+ "head" : {"sha" : "987654" },
55
+ })
38
56
else :
39
57
self .send_error (405 , 'Method not allowed: ' + self .path )
40
58
@@ -43,6 +61,8 @@ argparser = argparse.ArgumentParser()
43
61
argparser .add_argument ('--port' , type = int , default = 8443 , help = "Port to listen on (default: %(default)s)" )
44
62
argparser .add_argument ('--print-pr-event' , action = 'store_true' ,
45
63
help = "Print GitHub webhook pull_request event and exit" )
64
+ argparser .add_argument ('--print-image-refresh-event' , action = 'store_true' ,
65
+ help = "Print GitHub webhook issue event for an image-refresh and exit" )
46
66
argparser .add_argument ('repo' , metavar = 'USER/PROJECT' , help = "GitHub user/org and project name" )
47
67
argparser .add_argument ('sha' , help = "SHA to test in repo for the mock PR" )
48
68
args = argparser .parse_args ()
@@ -52,6 +72,9 @@ sha = args.sha
52
72
ADDRESS = ('127.0.0.7' , args .port )
53
73
54
74
GITHUB_DATA = {
75
+ f'/repos/{ repo } ' : {
76
+ "default_branch" : "main" ,
77
+ },
55
78
f'/repos/{ repo } /pulls/1' : {
56
79
'title' : 'mock PR' ,
57
80
'number' : 1 ,
@@ -67,6 +90,18 @@ GITHUB_DATA = {
67
90
'statuses' : [],
68
91
'sha' : sha ,
69
92
},
93
+ f'/repos/{ repo } /issues/2' : {
94
+ 'title' : 'Refresh foonux image' ,
95
+ 'number' : 2 ,
96
+ 'body' : "blabla\n - [ ] image-refresh foonux\n " ,
97
+ # is in our allowlist
98
+ 'user' : {"login" : "cockpit-project" },
99
+ 'labels' : [{"name" : "bot" }],
100
+ 'url' : f'http://{ ADDRESS [0 ]} /{ repo } /issues/2' ,
101
+ },
102
+ f'/repos/{ repo } /git/ref/heads/main' : {
103
+ 'object' : {'sha' : sha },
104
+ },
70
105
}
71
106
72
107
if args .print_pr_event :
@@ -79,6 +114,17 @@ if args.print_pr_event:
79
114
}, indent = 4 ))
80
115
exit (0 )
81
116
117
+ if args .print_image_refresh_event :
118
+ print (json .dumps ({
119
+ 'event' : 'issues' ,
120
+ 'request' : {
121
+ 'action' : 'opened' ,
122
+ 'issue' : GITHUB_DATA [f'/repos/{ repo } /issues/2' ],
123
+ 'repository' : {'full_name' : repo },
124
+ }
125
+ }, indent = 4 ))
126
+ exit (0 )
127
+
82
128
temp = tempfile .TemporaryDirectory ()
83
129
cache_dir = os .path .join (temp .name , 'cache' )
84
130
os .environ ['XDG_CACHE_HOME' ] = cache_dir
0 commit comments