aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-06-28 10:09:33 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-06-28 10:09:33 -0400
commit12fded0aa09d094799a901b72dd8adccac890696 (patch)
tree57f93228ff33365503f9392f948991ca6c7aad0f
parentce10a7cdbe237fa689fb07670691c91fd081e705 (diff)
downloadstasis-12fded0aa09d094799a901b72dd8adccac890696.tar.gz
Implement get_github_release_notes_auto function that bases release note data off test contexts
-rw-r--r--include/template.h5
-rw-r--r--include/template_func_proto.h3
-rw-r--r--src/stasis_main.c3
-rw-r--r--src/template.c3
-rw-r--r--src/template_func_proto.c51
5 files changed, 58 insertions, 7 deletions
diff --git a/include/template.h b/include/template.h
index 9a34c6a..55d7494 100644
--- a/include/template.h
+++ b/include/template.h
@@ -40,11 +40,12 @@ char *tpl_render(char *str);
*/
int tpl_render_to_file(char *str, const char *filename);
-typedef int tplfunc(void *frame, void *result);
+typedef int tplfunc(void *frame, void *data_out);
struct tplfunc_frame {
char *key;
tplfunc *func;
+ void *data_in;
int argc;
union {
char **t_char_refptr;
@@ -68,7 +69,7 @@ struct tplfunc_frame {
* @param tplfunc_ptr pointer to function of type tplfunc
* @param argc number of function arguments to accept
*/
-void tpl_register_func(char *key, void *tplfunc_ptr, int argc);
+void tpl_register_func(char *key, void *tplfunc_ptr, int argc, void *data_in);
/**
* Get the function frame associated with a template function
diff --git a/include/template_func_proto.h b/include/template_func_proto.h
index 5516421..212759c 100644
--- a/include/template_func_proto.h
+++ b/include/template_func_proto.h
@@ -3,6 +3,7 @@
#include "template.h"
-int get_github_release_notes_tplfunc_entrypoint(void *frame, void *ptr);
+int get_github_release_notes_tplfunc_entrypoint(void *frame, void *data_out);
+int get_github_release_notes_auto_tplfunc_entrypoint(void *frame, void *data_out);
#endif //TEMPLATE_FUNC_PROTO_H \ No newline at end of file
diff --git a/src/stasis_main.c b/src/stasis_main.c
index 13576a1..2abcbc4 100644
--- a/src/stasis_main.c
+++ b/src/stasis_main.c
@@ -262,7 +262,8 @@ int main(int argc, char *argv[]) {
// Expose function(s) to the template engine
// Prototypes can be found in template_func_proto.h
- tpl_register_func("get_github_release_notes", &get_github_release_notes_tplfunc_entrypoint, 3);
+ tpl_register_func("get_github_release_notes", &get_github_release_notes_tplfunc_entrypoint, 3, NULL);
+ tpl_register_func("get_github_release_notes_auto", &get_github_release_notes_auto_tplfunc_entrypoint, 1, &ctx);
// Set up PREFIX/etc directory information
// The user may manipulate the base directory path with STASIS_SYSCONFDIR
diff --git a/src/template.c b/src/template.c
index 9fce8a2..8e1357c 100644
--- a/src/template.c
+++ b/src/template.c
@@ -25,11 +25,12 @@ extern void tpl_reset() {
tpl_pool_func_used = 0;
}
-void tpl_register_func(char *key, void *tplfunc_ptr, int argc) {
+void tpl_register_func(char *key, void *tplfunc_ptr, int argc, void *data_in) {
struct tplfunc_frame *frame = calloc(1, sizeof(*frame));
frame->key = strdup(key);
frame->argc = argc;
frame->func = tplfunc_ptr;
+ frame->data_in = data_in;
tpl_pool_func[tpl_pool_func_used] = frame;
tpl_pool_func_used++;
diff --git a/src/template_func_proto.c b/src/template_func_proto.c
index 459a3a6..8618a2e 100644
--- a/src/template_func_proto.c
+++ b/src/template_func_proto.c
@@ -1,8 +1,8 @@
#include "template_func_proto.h"
-int get_github_release_notes_tplfunc_entrypoint(void *frame, void *ptr) {
+int get_github_release_notes_tplfunc_entrypoint(void *frame, void *data_out) {
int result;
- char **output = (char **) ptr;
+ char **output = (char **) data_out;
struct tplfunc_frame *f = (struct tplfunc_frame *) frame;
char *api_token = getenv("STASIS_GITHUB_TOKEN");
if (!api_token) {
@@ -16,3 +16,50 @@ int get_github_release_notes_tplfunc_entrypoint(void *frame, void *ptr) {
return result;
}
+int get_github_release_notes_auto_tplfunc_entrypoint(void *frame, void *data_out) {
+ int result = 0;
+ char **output = (char **) data_out;
+ struct tplfunc_frame *f = (struct tplfunc_frame *) frame;
+ char *api_token = getenv("STASIS_GITHUB_TOKEN");
+ if (!api_token) {
+ api_token = getenv("GITHUB_TOKEN");
+ }
+
+ const struct Delivery *ctx = (struct Delivery *) f->data_in;
+ struct StrList *notes_list = strlist_init();
+ for (size_t i = 0; i < sizeof(ctx->tests) / sizeof(*ctx->tests); i++) {
+ // Get test context
+ const struct Test *test = &ctx->tests[i];
+ if (test->name && test->version && test->repository) {
+ char *repository = strdup(test->repository);
+ char *match = strstr(repository, "spacetelescope/");
+ // Cull repository URL
+ if (match) {
+ replace_text(repository, "https://github.com/", "", 0);
+ if (endswith(repository, ".git")) {
+ replace_text(repository, ".git", "", 0);
+ }
+ // Record release notes for version relative to HEAD
+ // Using HEAD, GitHub returns the previous tag
+ char *note = NULL;
+ char h1_title[NAME_MAX] = {0};
+ sprintf(h1_title, "# %s", test->name);
+ strlist_append(&notes_list, h1_title);
+ result += get_github_release_notes(api_token ? api_token : "anonymous",
+ repository,
+ test->version,
+ "HEAD",
+ &note);
+ if (note) {
+ strlist_append(&notes_list, note);
+ guard_free(note);
+ }
+ }
+ }
+ }
+ // Return all notes as a single string
+ if (strlist_count(notes_list)) {
+ *output = join(notes_list->data, "\n\n");
+ }
+ return result;
+}