It's all about the answers!

Ask a question

Adding comments using OSLC


Sudipto Sarkar (631343) | asked May 05 '15, 6:08 a.m.
Hi,

 I am creating a workitem using OSLC 2.0. I want to add comment to the workitem,how to do it?

Thanks in Advance!
Sudipto

Accepted answer


permanent link
Tiago Moura (8387) | answered May 05 '15, 9:30 a.m.
edited May 05 '15, 9:32 a.m.
 This method adds comments on the Work Item represented by the changerequest :

private void updateComments(List<String> comments, final ChangeRequest changeRequest) throws IOException {
for (final String comment : comments) {
ContentProducer cp = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer out = new OutputStreamWriter(outstream, HTTP.UTF_8);
out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
out.append("<rdf:RDF\n");
out.append("\t\txmlns:rtc_ext=\"http://jazz.net/xmlns/prod/jazz/rtc/ext/1.0/\"\n");
out.append("\t\txmlns:rtc_cm=\"http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/\"\n");
out.append("\t\txmlns:oslc_cm=\"http://open-services.net/ns/cm#\"\n");
out.append("\t\txmlns:dcterms=\"http://purl.org/dc/terms/\"\n");
out.append("\t\txmlns:oslc_cmx=\"http://open-services.net/ns/cm-x#\"\n");
out.append("\t\txmlns:oslc=\"http://open-services.net/ns/core#\"\n");
out.append("\t\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n");
out.append("\t<rdf:Description \n");
out.append("\n\t\trdf:about=\"" + changeRequest.getAbout() + "\">\n");
out.append("\t<dcterms:description><![CDATA[" + comment + "]]> </dcterms:description>");
out.append("\t</rdf:Description>\n");
out.append("</rdf:RDF>\n");
out.flush();
}
};
HttpEntity entity = new EntityTemplate(cp);
HttpPost post = new HttpPost(changeRequest.getDiscussedBy() + "/oslc:comment");
post.addHeader("Accept", "application/rdf+xml");
post.addHeader("Content-type", "application/rdf+xml");
post.addHeader("OSLC-Core-Version", "2.0");
post.setEntity(entity);
// Call the PUT method against the Change Request URI
HttpResponse response = client.getHttpClient().execute(post);
response.getEntity().consumeContent();
}
}
Ralph Schoon selected this answer as the correct answer

Comments
Sudipto Sarkar commented May 08 '15, 6:51 a.m.

Hi,

 I was looking to your code, but what is ChangeRequest.getDsicussedBy(), and and why are are we putting in HttpPost. I am using the workitem url in HttpPost and trying to add comments. Can you please explain?


Tiago Moura commented May 08 '15, 12:31 p.m.

The method ChangeRequest.getDiscussedBy() returns the workitem URI. The URI follow this pattern:


https://localhost:9443/ccm/oslc/workitems/<uuid>

It return exactly the same that:

https://localhost:9443/ccm/oslc/workitems/<workitem id>

So post to the WorkItem URL has the same effect to the getDiscussedBy.

7 other answers



permanent link
Donald Nong (14.5k414) | answered May 05 '15, 8:22 p.m.
There is a dedicated section in the RTC OSLC API Wiki. Have a read.
https://jazz.net/wiki/bin/view/Main/WorkItemAPIsForOSLCCM20#Adding_comments

Comments
Sudipto Sarkar commented May 06 '15, 1:44 a.m.

Hi Donanld,

 I had a look in the above wiki page, For adding comments, we need to use the comment url but at the time of creating the url , how can I get this comment url

 </rdf:Description>
  <rdf:Description rdf:about="https://localhost:9443/jazz/resource/itemOid/
com.ibm.team.workitem.WorkItem/_xoNikpHMEeC_rYSykUtXXA/rtc_cm:comments/oslc:comment/1">
    <rdf:type rdf:resource="http://open-services.net/ns/core#Comment"/>;
    <dcterms:created>2011-07-06T15:09:47.574Z</dcterms:created>
    <dcterms:creator rdf:resource="https://localhost:9443/jazz/oslc/users/_KGRY4CFWEdq-WY5y7lROQw"/>;
    <dcterms:description rdf:parseType="Literal">Another Comment</dcterms:description>
    <dcterms:identifier>__Qo2YKfhEeCBC4EUCjDhXA</dcterms:identifier>
    <oslc:discussion rdf:resource="https://localhost:9443/jazz/resource/itemOid/
com.ibm.team.workitem.WorkItem/_xoNikpHMEeC_rYSykUtXXA/rtc_cm:comments"/>
  </rdf:Description>

This above code will work when we need to fetch the comments for a particular workitem, but while creating a workitem, how to add comments?



1
Donald Nong commented May 06 '15, 3:00 a.m.

If you mean that you want to add the comment at the same time as creating the work item, I don't think you can. I believe you will need to create the work item first, get the reference, and then add the comment(s).


Lukas Steiger commented Feb 04 '16, 10:54 a.m. | edited Feb 04 '16, 10:55 a.m.

I'm currently facing the same issue. I would like to add a comment while creating a workitem over OSLC.
The web interface allows me to write a comment when creating a new work item, so the OSLC interface should be capable of doing this as well.
@dnong and @sudipto: Has one of you ever found a solution on this?


2
Donald Nong commented Feb 04 '16, 8:26 p.m.

The web interface does not use OSLC API, so you cannot claim that it "should be capable of" doing what you want.

The web interface uses internal API, and sends the comment content with the attribute "internalComment" which is not exposed in the OSLC API.

As I said before, you need at least two steps - this should not be a big problem when you use codes.


Lukas Steiger commented Feb 05 '16, 1:47 a.m.

In my eyes, it is unnecessary overhead to send two HTTP requests where one would suffice. It's not obvious why comments cannot be created in the same request.
But anyway, thank you for confirming that it's not possible to achieve this in a single request. Is there already an enhancement request regarding this (couldn't find one on jazz.net)?


Donald Nong commented Feb 05 '16, 2:13 a.m.

You can raise an enhancement request here:
https://www.ibm.com/developerworks/rfe/

But I doubt such request will ever be implemented. Currently we have the "one request" solution - using the internal API. For OSLC API, the specification does not dictate such operations must be carried out in one single request, so it will very likely remain as is.

showing 5 of 6 show 1 more comments

permanent link
Bing Zhu (1112) | answered Apr 19 '18, 4:06 p.m.
edited Apr 19 '18, 4:31 p.m.

 I've been following the link that Donald Nong provide: https://jazz.net/wiki/bin/view/Main/WorkItemAPIsForOSLCCM20#Adding_comments


But I still get the "404 Bad Request" in following 2 attempts:

1. Use the url from work item I created: 

   I just appended "/oslc:comment" at the end according to the documentation.

2. Use the url from the documentation: 

I'm using C# and create work item POST call worked.

What is the right url to do a POST request to create comments?

Thanks,
Bing


Comments
Lukas Steiger commented Apr 20 '18, 2:31 a.m.

Refer to my answer below, I have been successfully using that code for years


Bing Zhu commented Apr 20 '18, 8:11 a.m. | edited Apr 20 '18, 8:34 a.m.

Hi Lukas,


Thank you! I think I basically using the same way but in C# and got 404. See my answer below. 


permanent link
Lukas Steiger (3131626) | answered Apr 20 '18, 2:28 a.m.
edited Apr 20 '18, 2:30 a.m.

 Adding comments through the OSLC interface can be done like this:


/
  post one single comment to one work item
  @params {string} discussedByUri: a URI pointing to a work item discussion, 
                   can be taken e.g. from a 'oslc:discussedBy' field. 
                   sample URI: https://localhost:7443/jazz/oslc/workitems/15/rtc_cm:comments 
  @params {string} comment: a string that will be added to the work item comments section
  @returns {promise} a promise that indicates whether the creation was successful or not
 */
postComment: function(discussedByUri, comment) {
if(typeof discussedByUri === "string" && discussedByUri.endsWith("/rtc_cm:comments") &&
typeof comment === "string") {
var commentObject = {
"dcterms:description": comment
};
return XHR.oslcJsonPostRequest(discussedByUri + "/oslc:comment", commentObject);
},

oslcJsonPostRequest: function(url, data) {
return this.xhr("POST", {
url: url,            
headers: {
"OSLC-Core-Version": "2.0",
"Accept": "application/json",
"Content-Type": "application/json"
},
postData: JSON.stringify(data),
handleAs: "json"
}, false);
}


permanent link
Bing Zhu (1112) | answered Apr 20 '18, 8:11 a.m.
edited Apr 20 '18, 8:33 a.m.

 Hi Lucas,

My code sample seems too long for comments, so I had to put it as an answer. Sorry if I'm not using this correctly.
This is the url that I got from oslc:discussedBy: "https://[ServerName]/oslc/workitems/_q8RqoJoQEee2N-g-o1qd6A/rtc_cm:comments" 
I also appended "/oslc:comment" at the end. This is my code. It  works to create a workitem (json is the properties for the new wokitem), I'm using it for creating comments too but got 404.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://[ServerName]/oslc/workitems/_q8RqoJoQEee2N-g-o1qd6A/rtc_cm:comments/oslc:comment" );
            request.Method = "POST";
            request.AllowAutoRedirect = true;
            request.KeepAlive = true;
            request.Timeout = 480000;
            request.Credentials = context.Credential;
            request.Headers.Add(context.AuthHeader);
            request.Headers.Add("OSLC-Core-Version", "2.0");
            request.Accept = "application/json";
            request.ContentType = "application/json";
            request.CookieContainer = context.Cookie;
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


permanent link
Samuel Pares (11) | answered Feb 24 '21, 1:42 p.m.

Is possible to set the creation date? I'm setting it, but its not having effect. Comment on work item is being created, but not setting the date I sent:


POST 
BODY
{
"dcterms:description": "comment",
"dcterms:created": "2021-02-24T15:36:33.371Z"
}


permanent link
Stefano Antoniazzi (1701711) | answered Aug 18 '21, 4:37 p.m.

 For those reading here in 2021...I've still 6.0.2 and it works in this way:


for workitem number 2613 it's quite easy

POST

Headers (KEY VALUE):
Content-Type application/json
OSLC-Core-Version 2.0
Accept application/json

BODY of the request:
{"dcterms:description""a silly comment here"}

RESPONSE: 201 created

Response headers conains Location:
ServerApache-Coyote/1.1
Strict-Transport-Securitymax-age=31536000
Last-ModifiedWed, 18 Aug 2021 20:35:25 GMT
Cache-Controlprivate, max-age=31536000, must-revalidate
ExpiresSat, 04 Sep 2021 21:15:54 GMT
VaryAccept, OSLC-Core-Version
Content-Typeapplication/json;charset=UTF-8
Transfer-Encodingchunked
DateWed, 18 Aug 2021 20:35:25 GMT
Accept-Rangesnone
ConnectionKeep-Alive


Comments
Dave Evans commented Sep 13 '21, 4:45 p.m.

This method is not working in 7.0.2. Going to try getting the UUID and try as per the documentation.


Dave Evans commented Sep 14 '21, 12:19 p.m.

 Woops, accidentally posted my reply on how I got it done in 7.0.2 below as a new answer...


permanent link
Dave Evans (14812846) | answered Sep 14 '21, 12:08 p.m.

After many hours, I finally looked at the ccm.log file. The response was saying my account did not have the required group memberships. The ccm.log clarified that my account did have the necessary group memberships, but that the request was blocked because it might have been malicious and said I had to use the JSESSION ID in a CRSF Prevent header. My tool was not retrieving any such JSESSIONID in the header, so I found you could get around the check by specifying User-Agent. HERE is how I got it to work in 7.0.2:


POST:
https://EWMHost:port/ccm/oslc/workitems/12345/rtc_cm:comments/oslc:comment

Headers:
Accept: application/json
Content-Type: application/json
OSLC-Core-Version: 2.0
User-Agent: CustomInterface

Request Body:
{"dcterms:description":"My Comment"}

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.