<commit_before>package com.flowdock.plugins.confluence;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.jrcs.diff.DifferentiationFailedException;

import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.diff.ChangeChunk;
import com.atlassian.confluence.diff.ConfluenceDiff;
import com.atlassian.confluence.diff.renderer.StaticHtmlChangeChunkRenderer;
import com.atlassian.confluence.event.events.content.page.PageCreateEvent;
import com.atlassian.confluence.event.events.content.page.PageEvent;
import com.atlassian.confluence.event.events.content.page.PageTrashedEvent;
import com.atlassian.confluence.event.events.content.page.PageUpdateEvent;
import com.atlassian.user.User;

/**
 * This class tries to figure out all relevant information from an event.
 * 
 * It's inspired by Atlassian's own PageNotificationsListener and
 * AbstractNotificationsListener. However, overriding those classes
 * turned out to be a mess, so everything's re-implemented in this class.
 * 
 * @author mutru
 *
 */
public class FlowdockEventRenderer {
	public Map<String, String> renderEvent(PageEvent event) {
		HashMap<String, String> result = new HashMap<String, String>();
		
		// Space
		result.put("space", event.getPage().getSpace().getName());
		
		// Page
		result.put("title", event.getPage().getTitle());
		result.put("url", event.getPage().getUrlPath());
		result.put("version_comment", event.getPage().getRenderedVersionComment());
		
		if (event instanceof PageCreateEvent) {
			result.put("event", "create");
		} else if (event instanceof PageTrashedEvent) {
			result.put("event", "trashed");
			
			User user = ((PageTrashedEvent)event).getOriginatingUser();
			result.put("email", user.getEmail());
			result.put("user", user.getFullName());
		} else if (event instanceof PageUpdateEvent) {
			result.put("event", "update");
			result.put("diff", getDiff((PageUpdateEvent)event));
		} else {
			throw new RuntimeException("Unknown page event type.");
		}
		
		return result;
	}
	
	private String getDiff(PageUpdateEvent event) {
		StaticHtmlChangeChunkRenderer renderer = StaticHtmlChangeChunkRenderer.INSTANCE;
		ContentEntityObject originalContent = event.getOriginalPage();
		ContentEntityObject content = event.getPage();
		StringBuffer output = new StringBuffer();
		
		try {
			ConfluenceDiff diff = new ConfluenceDiff(originalContent, content, true);
			
			for (ChangeChunk chunk : diff.getChunks()) {
				// example chunk:
				// <tr><td class="diff-added-lines" style="background-color: #dfd;"> <br>THIS IS SO AWESOME <br></td></tr>
				String chunkText = renderer.getFormattedText(chunk);
				output.append(chunkText);
			}
			
		} catch (DifferentiationFailedException e) {
			// There's nothing we can do about it - diff failed.
		}
		
		return output.toString();
	}
}<commit_msg>Added more data to POSTs.<commit_after>package com.flowdock.plugins.confluence;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.jrcs.diff.DifferentiationFailedException;

import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.diff.ChangeChunk;
import com.atlassian.confluence.diff.ConfluenceDiff;
import com.atlassian.confluence.diff.renderer.StaticHtmlChangeChunkRenderer;
import com.atlassian.confluence.event.events.content.page.PageCreateEvent;
import com.atlassian.confluence.event.events.content.page.PageEvent;
import com.atlassian.confluence.event.events.content.page.PageTrashedEvent;
import com.atlassian.confluence.event.events.content.page.PageUpdateEvent;
import com.atlassian.confluence.util.GeneralUtil;
import com.atlassian.user.User;

/**
 * This class tries to figure out all relevant information from an event.
 * 
 * It's inspired by Atlassian's own PageNotificationsListener and
 * AbstractNotificationsListener. However, overriding those classes
 * turned out to be a mess, so everything's re-implemented in this class.
 * 
 * @author mutru
 *
 */
public class FlowdockEventRenderer {
	public Map<String, String> renderEvent(PageEvent event) {
		if (skipEvent(event)) {
			return null;
		}
		
		HashMap<String, String> result = new HashMap<String, String>();
		
		// Space
		result.put("space_name", event.getPage().getSpace().getName());
		result.put("space_path", event.getPage().getSpace().getUrlPath());
		
		// Page
		result.put("page_title", event.getPage().getTitle());
		result.put("page_path", event.getPage().getUrlPath());
		result.put("page_url", GeneralUtil.getPageUrl(event.getPage()));
		
		result.put("version_comment", event.getPage().getRenderedVersionComment());
		
		if (event instanceof PageCreateEvent) {
			result.put("event", "create");
			
			User user = findEventUser((PageCreateEvent)event);
			result.put("user_email", user.getEmail());
			result.put("user_name", user.getFullName());
			
			String content = event.getPage().getContent();
			result.put("content", content);
			result.put("content_summary", GeneralUtil.makeSummary(content).toString());
		} else if (event instanceof PageTrashedEvent) {
			result.put("event", "trashed");
			
			User user = ((PageTrashedEvent)event).getOriginatingUser();
			result.put("user_email", user.getEmail());
			result.put("user_name", user.getFullName());
		} else if (event instanceof PageUpdateEvent) {
			result.put("event", "update");
			result.put("diff", getDiff((PageUpdateEvent)event));
			
			User user = findEventUser((PageUpdateEvent)event);
			result.put("user_email", user.getEmail());
			result.put("user_name", user.getFullName());
		} else {
			throw new RuntimeException("Unknown page event type.");
		}
		
		return result;
	}
	
	private String getDiff(PageUpdateEvent event) {
		StaticHtmlChangeChunkRenderer renderer = StaticHtmlChangeChunkRenderer.INSTANCE;
		ContentEntityObject originalContent = event.getOriginalPage();
		ContentEntityObject content = event.getPage();
		StringBuffer output = new StringBuffer();
		
		try {
			ConfluenceDiff diff = new ConfluenceDiff(originalContent, content, true);
			
			for (ChangeChunk chunk : diff.getChunks()) {
				// example chunk:
				// <tr><td class="diff-added-lines" style="background-color: #dfd;"> <br>THIS IS SO AWESOME <br></td></tr>
				String chunkText = renderer.getFormattedText(chunk);
				output.append(chunkText);
			}
			
		} catch (DifferentiationFailedException e) {
			// There's nothing we can do about it - diff failed.
		}
		
		return output.toString();
	}
	
	private boolean skipEvent(PageEvent event) {
		if (event instanceof PageUpdateEvent && ((PageUpdateEvent)event).isMinorEdit())
			return true;
		
		return false;
	}
	
	private User findEventUser(PageCreateEvent event) {
		return event.getPage().getUserAccessor().getUser(event.getPage().getCreatorName());
	}
	
	private User findEventUser(PageUpdateEvent event) {
		return event.getPage().getUserAccessor().getUser(event.getNew().getLastModifierName());
	}
}
{'fraction_non_alphanumeric': 0.12850534836657992, 'fraction_numerical': 0.0, 'mean_word_length': 4.954388984509467, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3958256', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2014, 'n_words': 426}