summaryrefslogtreecommitdiff
path: root/git.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2021-06-27 11:03:32 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2021-06-27 11:03:32 -0400
commit03004e38a4feb7298306d7d5368e907b98a9a742 (patch)
tree17d0c521da4064cfcb488646f75c6d6389183444 /git.c
parentc0913d90437752b9ad027c3c8c2fae66e6db72d0 (diff)
Added `add_comment()`.
This function adds the supplied comment to the `comments` file located in the PR's root directory.
Diffstat (limited to 'git.c')
-rw-r--r--git.c60
1 files changed, 60 insertions, 0 deletions
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;