From 03004e38a4feb7298306d7d5368e907b98a9a742 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Sun, 27 Jun 2021 11:03:32 -0400 Subject: Added `add_comment()`. This function adds the supplied comment to the `comments` file located in the PR's root directory. --- git.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/git.c b/git.c index 96fffa0..1303621 100644 --- a/git.c +++ b/git.c @@ -180,6 +180,66 @@ int create_info_file(pull_request *pr, const char *pr_root) { return 1; } +int add_comment(comment *comment, const char *pr_root) { + int len; + int buf_len; + int j = 0; + char *filename; + char *file_buf; + FILE *fp; + struct tm tm; + + /* Is the comment NULL? */ + if (pr == NULL) { + log(LOG_ERR, "Comment is NULL."); + return 0; + } + /* Is the PR directory NULL? */ + if (pr_root == NULL) { + log(LOG_ERR,"Pull request root is NULL."); + return 0; + } + + localtime_r(&comment->date, &tm); + + len = strlen(pr_root) + strlen("/comments"); + buf_len = get_comment_len(comment); + filename = calloc(len+1, sizeof(char)); + file_buf = calloc(buf_len+1, sizeof(char)); + + j = sprintf(file_buf, "id: %i\n", comment->id); + j += sprintf(file_buf+j, "author: %s\n", comment->author); + j += sprintf(file_buf+j, "date: "); + j += strftime(file_buf+j, -1, "%c %z\n", &tm); + + /* Is this comment a reply? */ + if (comment->reply != NULL) { + localtime_r(&comment->reply->date, &tm); + j += sprintf(file_buf+j, "reply-to: %i\n", comment->reply->id); + j += sprintf(file_buf+j, "reply-author: %s\n", comment->reply->author); + j += sprintf(file_buf+j, "reply-date: "); + j += strftime(file_buf+j, -1, "%c %z\n", &tm); + } + j += sprintf(file_buf+j, "description: %s", comment->desc); + + /* Append /comments to the PR root. */ + sprintf(filename, "%s/comments", pr_root); + + /* Open the comments file. */ + fp = fopen(filename, "a"); + + /* Did we fail to open the file? */ + if (fp == NULL) { + log(LOG_ERR, "Failed to open comments file."); + return 0; + } + + fwrite(file_buf, sizeof(char), buf_len, fp); + fclose(fp); + + return 1; +} + int create_pull_request_dir(pull_request *pr, int id, const char *root) { int ret = id; struct stat st; -- cgit v1.2.3-13-gbd6f