summaryrefslogtreecommitdiff
path: root/src/main/paisa/JenkinsReBuild.c
blob: 07adafdb5834218d72120149ab1727f06f7f9ec6 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#if 0

  && HOST_=x86_64-w64-mingw32- \
  && CC=${HOST_}gcc \
  && LD=${HOST_}gcc \
  && OBJDUMP=${HOST_}objdump \
  && BINEXT=.exe \
  && CFLAGS="-Wall -Werror -Wextra -pedantic -fmax-errors=3 -Iimport/include" \
  && LDFLAGS="-Wl,--gc-sections,--as-needed,-dn,-lgarbage,-lcJSON,-lexpat,-lmbedtls,-lmbedx509,-lmbedcrypto,-l:libwinpthread.a,-dy,-lws2_32,-Limport/lib" \

  && PROJECT_VERSION="$(date -u +0.0.0-%Y%m%d.%H%M%S)" \
  && mkdir -p build/bin \
  && ${CC:?} -c -o /tmp/rBcCAJFNAgDObAIA src/main/paisa/JenkinsReBuild.c ${CFLAGS:?} -DPROJECT_VERSION=${PROJECT_VERSION:?} \
  && ${LD:?} -o build/bin/JenkinsReBuild$BINEXT /tmp/rBcCAJFNAgDObAIA ${LDFLAGS:?} \

  && bullshit=$(${OBJDUMP?} -p build/bin/JenkinsReBuild$BINEXT|grep DLL\ Name|egrep -v ' (KERNEL32.dll|SHELL32.dll|WS2_32.dll|ADVAPI32.dll|msvcrt.dll)$'||true) \
  && if test -n "$bullshit"; then printf '\n  ERROR: Bullshit has sneaked in:\n\n%s\n\n' "$bullshit"; rm build/bin/JenkinsReBuild$BINEXT; false; fi \

#endif

#include <assert.h>
#include <stdio.h>
#include <string.h>
#if _WIN32
#   include <windows.h>
#endif

#include <Garbage.h>


#define STR_QUOT_(s) #s
#define STR_QUOT(s) STR_QUOT_(s)
#define LOGDBG(...) fprintf(stderr, __VA_ARGS__)
#define LOGERR(...) fprintf(stderr, __VA_ARGS__)
#define REGISTER /*no-op*/

#define FLG_isHelp (1<<0)
#define FLG_printRspBodyAnyway (1<<1)


typedef  struct App  App;


#define App_mAGIC (signed)0x88180200
struct App {
    int mAGIC;
    int flg;
    int exitCode;
    int httpRspCode;
    char *serviceName;
    char *cookie;
    char *rspBody;
    int rspBody_cap, rspBody_end;
    struct GarbageEnv **env;
    struct Garbage_TlsClient **tlsClient;
    void *envMem[SIZEOF_struct_GarbageEnv/sizeof(void*)];
};


static inline struct App* assert_is_App( void*p, char const*f, int l ){
#if !NDEBUG
    if( p == NULL ){ LOGDBG("assert(app != NULL)  %s:%d\n", f, l); assert(0); }
    App *a = p;
    if( a->mAGIC != App_mAGIC ){
        LOGDBG("assert(app.mAGIC != %d)  %s:%d\n", a->mAGIC, f, l); assert(0); }
#endif
    return p;
}
#define assert_is_App(p) assert_is_App(p, __FILE__, __LINE__)


static void printHelp( void ){
    fprintf(stdout, "%s%s%s",
        "  \n"
        "  JenkinsReBuild (v", STR_QUOT(PROJECT_VERSION),").\n"
        "  \n"
        "  Fights annoying just-trigger-another-build workaround.\n"
        "  \n"
        "  Options:\n"
        "  \n"
        "      --service <str>\n"
        "      Name of service to ask for (eg 'slarti').\n"
        "  \n"
        "      --cookie <str>\n"
        "      This tool does not support any auth mechanism. A cookie header can\n"
        "      be provided here. For example one copied from an established\n"
        "      browser session or similar. This already works for some auth\n"
        "      mechanisms.\n"
        "  \n"
    );
}


static int parseArgs( App*app, char**argv ){
    assert(app->serviceName == NULL);
    register int iA = 0;
nextArg:;
    char *arg = argv[++iA];
    if( arg == NULL ){ goto verify; }
    if( !strcmp(arg, "--help") ){
        app->flg |= FLG_isHelp; return 0;
    }else if( !strcmp(arg, "--service") ){
        app->serviceName = argv[++iA];
        if( app->serviceName == NULL ){ LOGERR("EINVAL: %s needs value\n", arg); return-1; }
    }else if( !strcmp(arg, "--cookie") ){
        app->cookie = argv[++iA];
        if( app->cookie == NULL ){ LOGERR("EINVAL: %s needs value\n", arg); return-1; }
    }else{
        LOGERR("EINVAL: %s\n", arg); return-1;
    }
    goto nextArg;
verify:
    if( app->serviceName == NULL ){ LOGERR("EINVAL: --service missing\n"); return -1; }
    return 0;
}


static void HttpReq_pushIoTask( void(*task)(void*arg), void*arg, void*app_ ){
    App*const app = assert_is_App(app_);
    (*app->env)->enqueBlocking(app->env, task, arg);
}


static void HttpReq_onError( int retval, void*mentorCls ){
    LOGDBG("[DEBUG] %s(eno=%d)\n", __func__, retval);
}


static void HttpReq_onRspHdr(
    const char*proto, int proto_len, int rspCode, const char*phrase, int phrase_len,
    const struct Garbage_HttpMsg_Hdr*hdrs, int hdrs_cnt,
    struct Garbage_HttpClientReq**req, void*app_
){
    App*const app = assert_is_App(app_);
    app->httpRspCode = rspCode;
    if( rspCode != 200 ){
        LOGDBG("%.*s %d %.*s\n", proto_len, proto, rspCode, phrase_len, phrase);
        for( int i = 0 ; i < hdrs_cnt ; ++i ){
            LOGDBG("%.*s: %.*s\n", hdrs[i].key_len, hdrs[i].key, hdrs[i].val_len, hdrs[i].val);
        }
        LOGDBG("\n");
    }
}


static void HttpReq_onRspBody(
    const char*buf, int buf_len, struct Garbage_HttpClientReq**req, void*app_
){
    App*const app = assert_is_App(app_);
    if( app->httpRspCode != 200 ){
        LOGDBG("%.*s", buf_len, buf);
        return;
    }
    if( app->flg & FLG_printRspBodyAnyway ){
        LOGDBG("%.*s", buf_len, buf);
    }
    if( app->rspBody_cap - app->rspBody_end < buf_len ){
        app->rspBody_cap += buf_len;
        void *tmp = realloc(app->rspBody, app->rspBody_cap*sizeof*app->rspBody);
        if( tmp == NULL ){ assert(!"TODO_0GgAAAF8AACMaAAA ENOMEM"); }
        app->rspBody = tmp;
    }
    memcpy(app->rspBody + app->rspBody_end, buf, buf_len);
    app->rspBody_end += buf_len;
}


static void HttpReq_onRspDone( struct Garbage_HttpClientReq**req, void*app_ ){
    App*const app = assert_is_App(app_);
    if( app->flg & FLG_printRspBodyAnyway ){
        LOGDBG("\n");/*fix broken server which deliver no LF at TEXT body end*/
    }
    assert(app->rspBody != NULL);
    int const isFail = strstr(app->rspBody, "\"state\":\"failure\"") != NULL;
    if( isFail ){
        LOGDBG("[DEBUG] Build has FAILED\n");
    }else{
        LOGDBG("[DEBUG] Build has SUCCEEDED (likely)\n");
    }
}


static void TlsClientMentor_pushIoTask( void(*task)(void*arg), void*arg, void*app_ ){ assert(!"TODO_4WgAAI8UAACIdQAA"); }
static void TlsClientMentor_onError( int eno, void*app_ ){ assert(!"TODO_gxsAAMspAABkYgAA"); }


static void run( void*app_ ){
    REGISTER int err;
    App*const app = assert_is_App(app_);
    static char const*const peerHostname = "jenkinspaisa-temp.tools.pnet.ch";
    assert(app->tlsClient == NULL);
    static struct Garbage_TlsClient_Mentor tlsMentor = {
        .pushIoTask = TlsClientMentor_pushIoTask,
        .onError = TlsClientMentor_onError,
    };
    app->tlsClient = (*app->env)->newTlsClient(app->env,
        &tlsMentor, app, &(struct Garbage_TlsClient_Opts){
            .peerHostname = peerHostname,
            //.mallocator = NULL,
            //.socketMgr = NULL,
            //.ioWorker = NULL,
        }
    );
    struct Garbage_HttpClientReq **req = NULL;
    static struct Garbage_HttpClientReq_Mentor httpMentor = {
        .pushIoTask = HttpReq_pushIoTask,
        .onError = HttpReq_onError,
        .onRspHdr = HttpReq_onRspHdr,
        .onRspBody = HttpReq_onRspBody,
        .onRspDone = HttpReq_onRspDone,
    };
    assert(5*sizeof(void*) == sizeof httpMentor);
    #define SPACE (url_cap - (it - url))
    int const url_cap = 127;
    char url[url_cap];
    char *it = url;
    err = snprintf(it, SPACE, "/job/"); it += err; assert(err == 5);
    err = snprintf(it, SPACE, "%s", app->serviceName); it += err; assert(err == (signed)strlen(app->serviceName));
    err = snprintf(it, SPACE, "/job/SDCISA-15648-RemoveSlimPackaging-n2/lastBuild/pipeline-graph/tree"); it += err; assert(err == 70);
    #undef SPACE
    //LOGDBG("[DEBUG] GET %s\n", url);
    req = (*app->env)->newHttpClientReq(app->env, &httpMentor, app,
        &(struct Garbage_HttpClientReq_Opts){
            //.mallocator = NULL,
            .socketMgr = (*app->tlsClient)->asSocketMgr(app->tlsClient),
            .mthd = "GET",
            .host = peerHostname,
            .url = url,
            .port = 443,
            .hdrs_cnt = (app->cookie == NULL) ? 0 : 1,
            .hdrs = ((struct Garbage_HttpMsg_Hdr[]){{
                .key = "Cookie", .key_len = 6,
                .val = app->cookie, .val_len = (app->cookie == NULL ? 0 : strlen(app->cookie)),
            }}),
        });
    (*req)->resume(req);
}


int main( int argc, char**argv ){
#if _WIN32
    WSAStartup(1, &(WSADATA){0});
#endif
    (void)argc;
    App app = {0}; assert((void*)0 == NULL);
    #define app (&app)
    app->mAGIC = App_mAGIC;
    app->exitCode = -1;
    if( parseArgs(app, argv) ){ goto endFn; }
    if( app->flg & FLG_isHelp ){ printHelp(); goto endFn; }
    app->exitCode = 0;
    //app->flg |= FLG_printRspBodyAnyway;
    app->env = GarbageEnv_ctor(app->envMem, sizeof app->envMem);
    (*app->env)->enqueBlocking(app->env, run, app);
    (*app->env)->runUntilDone(app->env);
endFn:
    return !!app->exitCode;
    #undef app
}